agentknock 0.4.1

Developer secrets on your phone, provided only to approved commands.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use std::{future::Future, path::Path, pin::Pin};

use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD};
use serde::{Deserialize, Serialize};
use ulid::Ulid;

use crate::{
    Client, DenialReason, RequestError,
    config::{Pairing, clear_rotation_key, read_pairing_from},
    crypto::{self, Session},
    pairing::RotationError,
    protocol::{self, Method, Response},
    websocket::{self, RelayExchange},
};

const SSH_SIGNATURE_BEGIN: &str = "-----BEGIN SSH SIGNATURE-----\n";
const SSH_SIGNATURE_END: &str = "-----END SSH SIGNATURE-----";

/// Describes one Git signature requested by a command invocation.
pub struct GitSignRequest<'a> {
    /// The request identifier of the initial invocation.
    pub invocation_id: &'a str,

    /// The authorization token created for the invocation.
    pub invocation_token: &'a [u8; 32],

    /// The name of the signing secret selected for the invocation.
    pub secret: &'a str,

    /// The exact message bytes to sign.
    pub message: &'a [u8],

    /// Advisory information about the Git repository, if available.
    pub repository: Option<&'a GitSignRepository>,
}

/// Advisory repository information for a Git signature request.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GitSignRepository {
    /// The repository's sanitized remote identity, without credentials.
    pub remote: Option<String>,

    /// The absolute path to the current worktree.
    pub worktree: Option<String>,

    /// The current branch or detached-HEAD state.
    pub head: Option<GitSignHead>,

    /// The number of paths changed by the object being signed.
    pub changed_path_count: Option<usize>,

    /// The complete changed-path list, or [`None`] if it was not collected.
    pub changed_paths: Option<Vec<GitSignChangedPath>>,
}

/// Describes the repository HEAD associated with a Git signature request.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum GitSignHead {
    /// HEAD names a local branch.
    Branch {
        /// The short local branch name.
        name: String,

        /// The configured upstream branch, if one is available.
        upstream: Option<String>,
    },

    /// HEAD is detached.
    Detached,
}

/// Describes one path changed by the object being signed.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GitSignChangedPath {
    /// The kind of change.
    pub status: GitSignChangeStatus,

    /// The repository-relative path.
    pub path: String,
}

/// Describes how a path changed in a signed Git object.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum GitSignChangeStatus {
    /// The path was added.
    Added,

    /// The path was deleted.
    Deleted,

    /// The path's contents or file mode changed.
    Modified,

    /// The path's object type changed.
    TypeChanged,
}

/// A stage reported while a Git signature request is running.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum GitSignProgress {
    /// Agentknock is reading local state and preparing the protected request.
    Preparing,

    /// The request is waiting to be delivered to the device.
    WaitingForDelivery,

    /// The device has received the request but hasn't returned a decision.
    WaitingForResponse,

    /// Agentknock is validating the response and handing off the completion.
    Completing,

    /// The exchange and completion handoff finished successfully.
    Completed,
}

impl Client {
    /// Requests a Git signature from a secret selected for an invocation.
    ///
    /// The device makes a separate decision for each signature. The request
    /// includes the exact bytes that Git asks the signing program to sign.
    /// SSH-backed secrets use the SSHSIG `git` namespace.
    ///
    /// # Errors
    ///
    /// Returns [`RequestError`] if local pairing state isn't active, the relay
    /// exchange fails, the device denies the signature, the response is
    /// invalid, or the operation is canceled.
    pub async fn request_git_signature<P>(
        &self,
        request: GitSignRequest<'_>,
        cancellation: impl Future<Output = ()>,
        mut progress: P,
    ) -> Result<String, RequestError>
    where
        P: FnMut(GitSignProgress),
    {
        tokio::pin!(cancellation);
        progress(GitSignProgress::Preparing);
        request
            .invocation_id
            .parse::<Ulid>()
            .map_err(RequestError::other)?;
        if request.secret.is_empty() {
            return Err(RequestError::other("signing secret name is empty"));
        }
        self.maybe_rotate_psk().map_err(|error| match error {
            RotationError::Configuration(error) => RequestError::Configuration(error),
            RotationError::Other(error) => RequestError::Other(error),
        })?;
        let pairing_path = self.pairing_path()?;
        let pairing = read_pairing_from(&pairing_path)?;
        let request_id = Ulid::generate();
        let payload = GitSignRequestPayload {
            method: Method::GitSign,
            invocation_id: request.invocation_id,
            invocation_token: BASE64_STANDARD.encode(request.invocation_token),
            secret: request.secret,
            message: BASE64_STANDARD.encode(request.message),
            repository: request.repository.map(GitSignRepositoryPayload::from),
        };

        git_sign_exchange(
            self,
            &pairing_path,
            &pairing,
            request_id,
            &payload,
            cancellation.as_mut(),
            &mut progress,
        )
        .await
    }
}

async fn git_sign_exchange<C, P>(
    client: &Client,
    pairing_path: &Path,
    pairing: &Pairing,
    request_id: Ulid,
    request_payload: &GitSignRequestPayload<'_>,
    mut cancellation: Pin<&mut C>,
    progress: &mut P,
) -> Result<String, RequestError>
where
    C: Future<Output = ()> + ?Sized,
    P: FnMut(GitSignProgress),
{
    let plaintext = client
        .encode(request_payload)
        .map_err(RequestError::other)?;
    let mut session = Session::new(pairing, &request_id).map_err(RequestError::other)?;
    let request = session
        .seal_request(&plaintext)
        .map_err(RequestError::other)?;
    let mut relay = RelayExchange::authenticated(client, pairing, &request_id.to_string())?;

    progress(GitSignProgress::WaitingForDelivery);
    let response = match tokio::select! {
        biased;
        _ = cancellation.as_mut() => {
            if relay.request_was_sent() {
                complete_cancelled(client, &mut session, &mut relay).await;
            }
            return Err(RequestError::Interrupted);
        }
        response = relay.request(&request, || {
            progress(GitSignProgress::WaitingForResponse);
        }) => response,
    } {
        Ok(response) => response,
        Err(error) => {
            let reason = abort_reason(&error);
            let error = RequestError::from(error);
            if let Some(completion) = seal_aborted(client, &mut session, reason, error.to_string())
            {
                tokio::select! {
                    biased;
                    _ = cancellation.as_mut() => {
                        let _ = relay.complete_briefly(&completion).await;
                        return Err(RequestError::Interrupted);
                    }
                    _ = relay.complete(&completion) => {}
                }
            }
            return Err(error);
        }
    };

    progress(GitSignProgress::Completing);
    let plaintext = session
        .open_response(response)
        .map_err(RequestError::other)?;
    if let Some(rotation_key) = pairing.rotation_key() {
        clear_rotation_key(pairing_path, rotation_key)?;
    }
    let result: GitSignResult = match protocol::decode_response(&plaintext)
        .map_err(RequestError::other)?
    {
        Response::Message(result) => result,
        Response::Error(error) => {
            if let Some(completion) = protocol::seal_error_completion(client, &mut session, &error)
            {
                let _ = relay.complete_briefly(&completion).await;
            }
            return Err(RequestError::DeviceRejected {
                code: error.code,
                message: error.message,
            });
        }
    };

    let (completion_result, exchange_result) = match result {
        GitSignResult::Approved {
            signature: Some(signature),
        } if valid_ssh_signature_envelope(&signature) => {
            (GitSignResult::Approved { signature: None }, Ok(signature))
        }
        GitSignResult::Approved { .. } => (
            GitSignResult::Aborted {
                reason: GitSignAbortReason::InvalidResponse,
                message: "approved response doesn't contain a valid SSH signature envelope".into(),
            },
            Err(RequestError::other(
                "approved response doesn't contain a valid SSH signature envelope",
            )),
        ),
        GitSignResult::Denied { reason, message } => (
            GitSignResult::Denied {
                reason,
                message: message.clone(),
            },
            Err(RequestError::Denied { reason, message }),
        ),
        GitSignResult::Aborted { .. } => (
            GitSignResult::Aborted {
                reason: GitSignAbortReason::InvalidResponse,
                message: "received an ABORTED result in a response".into(),
            },
            Err(RequestError::other(
                "received an ABORTED result in a response",
            )),
        ),
    };

    let plaintext = client
        .encode(&completion_result)
        .map_err(RequestError::other)?;
    let completion = session
        .seal_completion(&plaintext)
        .map_err(RequestError::other)?;
    let interrupted = tokio::select! {
        biased;
        _ = cancellation.as_mut() => true,
        result = relay.complete(&completion) => {
            result?;
            progress(GitSignProgress::Completed);
            false
        }
    };
    if interrupted {
        let _ = relay.complete_briefly(&completion).await;
        return Err(RequestError::Interrupted);
    }

    exchange_result
}

fn valid_ssh_signature_envelope(signature: &str) -> bool {
    signature.starts_with(SSH_SIGNATURE_BEGIN) && signature.trim_end().ends_with(SSH_SIGNATURE_END)
}

fn abort_reason(error: &websocket::Error) -> GitSignAbortReason {
    match error {
        websocket::Error::RetriesExhausted { .. } => GitSignAbortReason::TimedOut,
        websocket::Error::UnexpectedStatus(status) if (400..500).contains(status) => {
            GitSignAbortReason::ClientError
        }
        _ => GitSignAbortReason::InvalidResponse,
    }
}

fn seal_aborted(
    client: &Client,
    session: &mut Session,
    reason: GitSignAbortReason,
    message: String,
) -> Option<crypto::Completion> {
    let plaintext = client
        .encode(&GitSignResult::Aborted { reason, message })
        .ok()?;
    session.seal_completion(&plaintext).ok()
}

async fn complete_cancelled(client: &Client, session: &mut Session, relay: &mut RelayExchange) {
    let Some(completion) = seal_aborted(
        client,
        session,
        GitSignAbortReason::Cancelled,
        RequestError::Interrupted.to_string(),
    ) else {
        return;
    };
    let _ = relay.complete_briefly(&completion).await;
}

#[derive(Serialize)]
struct GitSignRequestPayload<'a> {
    method: Method,
    invocation_id: &'a str,
    invocation_token: String,
    secret: &'a str,
    message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    repository: Option<GitSignRepositoryPayload<'a>>,
}

#[derive(Serialize)]
struct GitSignRepositoryPayload<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    remote: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    worktree: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    head: Option<GitSignHeadPayload<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    changed_path_count: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    changed_paths: Option<Vec<GitSignChangedPathPayload<'a>>>,
}

impl<'a> From<&'a GitSignRepository> for GitSignRepositoryPayload<'a> {
    fn from(repository: &'a GitSignRepository) -> Self {
        Self {
            remote: repository.remote.as_deref(),
            worktree: repository.worktree.as_deref(),
            head: repository.head.as_ref().map(GitSignHeadPayload::from),
            changed_path_count: repository.changed_path_count,
            changed_paths: repository
                .changed_paths
                .as_ref()
                .map(|paths| paths.iter().map(GitSignChangedPathPayload::from).collect()),
        }
    }
}

#[derive(Serialize)]
#[serde(tag = "type", rename_all = "SCREAMING_SNAKE_CASE")]
enum GitSignHeadPayload<'a> {
    Branch {
        name: &'a str,
        #[serde(skip_serializing_if = "Option::is_none")]
        upstream: Option<&'a str>,
    },
    Detached,
}

impl<'a> From<&'a GitSignHead> for GitSignHeadPayload<'a> {
    fn from(head: &'a GitSignHead) -> Self {
        match head {
            GitSignHead::Branch { name, upstream } => Self::Branch {
                name,
                upstream: upstream.as_deref(),
            },
            GitSignHead::Detached => Self::Detached,
        }
    }
}

#[derive(Serialize)]
struct GitSignChangedPathPayload<'a> {
    status: GitSignChangeStatusPayload,
    path: &'a str,
}

impl<'a> From<&'a GitSignChangedPath> for GitSignChangedPathPayload<'a> {
    fn from(path: &'a GitSignChangedPath) -> Self {
        Self {
            status: path.status.into(),
            path: &path.path,
        }
    }
}

#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum GitSignChangeStatusPayload {
    Added,
    Deleted,
    Modified,
    TypeChanged,
}

impl From<GitSignChangeStatus> for GitSignChangeStatusPayload {
    fn from(status: GitSignChangeStatus) -> Self {
        match status {
            GitSignChangeStatus::Added => Self::Added,
            GitSignChangeStatus::Deleted => Self::Deleted,
            GitSignChangeStatus::Modified => Self::Modified,
            GitSignChangeStatus::TypeChanged => Self::TypeChanged,
        }
    }
}

#[derive(Deserialize, Serialize)]
#[serde(tag = "result", rename_all = "SCREAMING_SNAKE_CASE")]
enum GitSignResult {
    Approved {
        #[serde(skip_serializing_if = "Option::is_none")]
        signature: Option<String>,
    },
    Denied {
        reason: DenialReason,
        message: String,
    },
    Aborted {
        reason: GitSignAbortReason,
        message: String,
    },
}

#[derive(Deserialize, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum GitSignAbortReason {
    Cancelled,
    TimedOut,
    InvalidResponse,
    ClientError,
    Other,
}