turnkey_tk 0.4.1

A CLI for machines to use Turnkey for git, ssh, and credential management
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Asserts on the classified error code.
#![allow(clippy::disallowed_types)]
use super::*;
use crate::errors::{Classification, ErrorCode, classify};
use clap::Parser;
use clap::error::ErrorKind;
use reqwest::Client;
use std::net::TcpListener;
use std::sync::OnceLock;
use turnkey_api_key_stamper::TurnkeyP256ApiKey;
use wiremock::{
    Mock, MockServer, ResponseTemplate,
    matchers::{body_partial_json, path},
};
#[derive(Debug, Parser)]
struct RequestCli {
    #[command(flatten)]
    request: RequestArgs,
}

#[derive(Debug, Parser)]
struct ActivityCli {
    #[command(subcommand)]
    activity: ActivityCommand,
}

const ORG: &str = "00000000-0000-4000-8000-000000000001";
fn activity(id: &str, status: &str) -> Value {
    json!({"activity":{"id":id,"status":status,"fingerprint":"sha256:example"}})
}

fn auth(server: &MockServer, key: TurnkeyP256ApiKey) -> ResolvedAuth {
    ResolvedAuth::for_tests(ORG, &server.uri(), key)
}

fn json(route: &str, body: Value) -> Mock {
    Mock::given(path(format!("/public/v1/{route}")))
        .respond_with(ResponseTemplate::new(200).set_body_json(body))
}

fn get_activity(id: &str, status: &str) -> Mock {
    json("query/get_activity", activity(id, status))
}

fn activity_error(error: &Error) -> &ActivityError {
    error.downcast_ref::<ActivityError>().unwrap()
}

async fn consensus_target(queries: u64) -> (MockServer, ResolvedAuth) {
    let server = MockServer::start().await;
    let auth = auth(&server, TurnkeyP256ApiKey::generate());
    get_activity("target", "ACTIVITY_STATUS_CONSENSUS_NEEDED")
        .expect(queries)
        .mount(&server)
        .await;
    (server, auth)
}

#[test]
fn result_serialization_preserves_the_machine_contract() {
    let data = json!({"activity":{"id":"a","status":"ACTIVITY_STATUS_COMPLETED"}});
    let output = OperationOutput::result("test", data);
    assert_eq!(
        serde_json::to_value(output).unwrap(),
        json!({
            "schemaVersion":1,"reason":"command_result","command":"test","status":"completed",
            "data":{"activity":{"id":"a","status":"ACTIVITY_STATUS_COMPLETED"}},
            "activity":{"id":"a","status":"ACTIVITY_STATUS_COMPLETED"}
        })
    );
}

#[test]
fn submission_requires_recoverable_activity_identity() {
    let error = submission_result(
        "test",
        json!({"activity":{"status":"ACTIVITY_STATUS_COMPLETED"}}),
    )
    .unwrap_err();
    assert_eq!(
        activity_error(&error).kind(),
        ActivityErrorKind::SubmissionUnknown
    );
}

#[tokio::test]
async fn query_names_the_endpoint_when_the_response_shape_mismatches() {
    let server = MockServer::start().await;
    json("query/get_activity", json!({"organizationId": 1}))
        .expect(1)
        .mount(&server)
        .await;
    let auth = auth(&server, TurnkeyP256ApiKey::generate());
    let error =
        query::<Value, GetActivityRequest>("/public/v1/query/get_activity", &json!({}), &auth)
            .await
            .unwrap_err();
    let error = activity_error(&error);
    assert_eq!(error.kind(), ActivityErrorKind::MalformedResponse);
    assert_eq!(error.to_string(), "get_activity response was malformed");
    server.verify().await;
}

#[test]
fn url_keeps_a_base_path_prefix() {
    assert_eq!(
        url("http://host/prefix/", "/public/v1/query/whoami")
            .unwrap()
            .as_str(),
        "http://host/prefix/public/v1/query/whoami"
    );
}

#[test]
fn parser_enforces_body_source_and_safe_path() {
    assert_eq!(
        RequestCli::try_parse_from(["tk", "--path", "/public/v1/query/whoami"])
            .unwrap_err()
            .kind(),
        ErrorKind::MissingRequiredArgument
    );
    assert_eq!(
        RequestCli::try_parse_from([
            "tk",
            "--path",
            "/public/v1/query/whoami",
            "--body",
            "{}",
            "--body-file",
            "-"
        ])
        .unwrap_err()
        .kind(),
        ErrorKind::ArgumentConflict
    );
    assert_eq!(
        RequestCli::try_parse_from(["tk", "--path", "https://other.test", "--body", "{}"])
            .unwrap_err()
            .kind(),
        ErrorKind::ValueValidation
    );
    assert_eq!(
        ActivityCli::try_parse_from(["tk", "wait", "a", "--timeout", "0"])
            .unwrap_err()
            .kind(),
        ErrorKind::ValueValidation
    );
}

#[test]
fn list_parser_accepts_repeated_filters_and_rejects_unknown_types() {
    let ActivityCommand::List(args) = ActivityCli::try_parse_from([
        "tk",
        "list",
        "--status",
        "pending",
        "--status",
        "failed",
        "--type",
        "ACTIVITY_TYPE_CREATE_USER_TAG",
        "--type",
        "ACTIVITY_TYPE_CREATE_POLICY_V3",
        "--since",
        "36h",
    ])
    .unwrap()
    .activity
    else {
        panic!("expected list");
    };
    assert_eq!(args.limit, 50);
    assert_eq!(args.cursor, None);
    assert_eq!(args.status, [StatusFilter::Pending, StatusFilter::Failed]);
    assert_eq!(
        args.types,
        [ActivityType::CreateUserTag, ActivityType::CreatePolicyV3]
    );
    assert_eq!(args.since.map(ExpiresIn::seconds), Some(36 * 3_600));
    for bad in [
        ["tk", "list", "--type", "ACTIVITY_TYPE_UNSPECIFIED"],
        ["tk", "list", "--type", "CREATE_USER_TAG"],
        ["tk", "list", "--since", "1w"],
        ["tk", "list", "--status", "created"],
    ] {
        let error = ActivityCli::try_parse_from(bad).unwrap_err();
        assert!(
            matches!(
                error.kind(),
                ErrorKind::ValueValidation | ErrorKind::InvalidValue
            ),
            "{bad:?}: {error}"
        );
    }
}

fn listed(seconds: u64, ids: impl IntoIterator<Item = u32>) -> Value {
    let items: Vec<Value> = ids
        .into_iter()
        .map(|n| {
            json!({
                "id": format!("id-{n}"),
                "status": "ACTIVITY_STATUS_COMPLETED",
                "createdAt": {"seconds": seconds.to_string(), "nanos": "0"},
                "votes": [],
            })
        })
        .collect();
    json!({"activities": items})
}

async fn list_since(auth: &ResolvedAuth, limit: u32, cursor: Option<&str>) -> Value {
    list(
        auth,
        ListArgs {
            limit,
            cursor: cursor.map(str::to_owned),
            status: vec![StatusFilter::Completed],
            types: vec![],
            since: Some("1h".parse().unwrap()),
        },
    )
    .await
    .unwrap()
    .into_data()
}

#[tokio::test]
async fn since_walks_full_pages_and_stops_at_the_window_or_the_cap() {
    let server = MockServer::start().await;
    let now = unix_now().unwrap().as_secs();
    let page = |limit: &str, after: &str, body: Value| {
        Mock::given(path("/public/v1/query/list_activities"))
            .and(body_partial_json(json!({
                "filterByStatus": ["ACTIVITY_STATUS_COMPLETED"],
                "paginationOptions": {"limit": limit, "after": after},
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(body))
    };
    page("100", "", listed(now, 1..=100)).mount(&server).await;
    page("99", "", listed(now, 1..=99)).mount(&server).await;
    let mut tail = listed(now - 60, 101..=102);
    let mut old = listed(now - 7_200, 103..=103);
    tail["activities"]
        .as_array_mut()
        .unwrap()
        .append(old["activities"].as_array_mut().unwrap());
    page("100", "id-100", tail).mount(&server).await;
    page("100", "id-99", listed(now - 60, 100..=101))
        .mount(&server)
        .await;
    let auth = auth(&server, TurnkeyP256ApiKey::generate());

    let all = list_since(&auth, 500, None).await;
    let ids: Vec<String> = all["items"]
        .as_array()
        .unwrap()
        .iter()
        .map(|item| item["id"].as_str().unwrap().to_owned())
        .collect();
    assert_eq!(
        ids,
        (1..=102).map(|n| format!("id-{n}")).collect::<Vec<_>>()
    );
    assert_eq!(all["nextCursor"], Value::Null);
    assert_eq!(all["items"][0]["votes"], json!([]));

    let capped = list_since(&auth, 99, None).await;
    assert_eq!(capped["items"].as_array().unwrap().len(), 99);
    assert_eq!(capped["nextCursor"], "id-99");

    let resumed = list_since(&auth, 500, Some("id-99")).await;
    assert_eq!(
        resumed["items"]
            .as_array()
            .unwrap()
            .iter()
            .map(|item| item["id"].as_str().unwrap())
            .collect::<Vec<_>>(),
        ["id-100", "id-101"]
    );
    assert_eq!(resumed["nextCursor"], Value::Null);
}

#[tokio::test]
async fn reject_success_and_malformed_submission_are_distinct() {
    let server = MockServer::start().await;
    let auth = auth(&server, TurnkeyP256ApiKey::generate());
    get_activity("a", "ACTIVITY_STATUS_CONSENSUS_NEEDED")
        .mount(&server)
        .await;
    json(
        "submit/reject_activity",
        activity("a", "ACTIVITY_STATUS_REJECTED"),
    )
    .expect(1)
    .mount(&server)
    .await;
    let output = run_activity(ActivityCommand::Reject { id: "a".into() }, &auth)
        .await
        .unwrap();
    assert_eq!(output.status(), Status::Rejected);
    json("submit/test", json!({}))
        .expect(1)
        .mount(&server)
        .await;
    let error = submit_activity(&auth, "test", "test", "ACTIVITY_TYPE_TEST", &json!({}))
        .await
        .unwrap_err();
    assert_eq!(
        activity_error(&error).kind(),
        ActivityErrorKind::SubmissionUnknown
    );
    server.verify().await;
}

#[tokio::test]
async fn rejected_reject_proposal_is_not_a_rejected_target() {
    let (server, auth) = consensus_target(1).await;
    json(
        "submit/reject_activity",
        activity("decision", "ACTIVITY_STATUS_REJECTED"),
    )
    .expect(1)
    .mount(&server)
    .await;
    let error = run_activity(
        ActivityCommand::Reject {
            id: "target".into(),
        },
        &auth,
    )
    .await
    .unwrap_err();
    let error = activity_error(&error);
    assert_eq!(error.kind(), ActivityErrorKind::NotCompleted);
    assert_eq!(
        error.activity(),
        Some(&json!({"id": "decision", "status": "ACTIVITY_STATUS_REJECTED"}))
    );
    server.verify().await;
}

#[tokio::test]
async fn completed_vote_proposal_reports_the_target_status() {
    let (server, auth) = consensus_target(2).await;
    json(
        "submit/approve_activity",
        activity("decision", "ACTIVITY_STATUS_COMPLETED"),
    )
    .expect(1)
    .mount(&server)
    .await;
    let output = run_activity(
        ActivityCommand::Approve {
            id: "target".into(),
        },
        &auth,
    )
    .await
    .unwrap();
    assert_eq!(output.status(), Status::Pending);
    assert_eq!(
        output.activity,
        Some(json!({"id": "target", "status": "ACTIVITY_STATUS_CONSENSUS_NEEDED"}))
    );
    server.verify().await;
}

#[tokio::test]
async fn mutation_timeout_is_unknown_and_does_not_leak_body() {
    let server = MockServer::start().await;
    Mock::given(path("/public/v1/submit/test"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_delay(Duration::from_millis(100))
                .set_body_json(json!({})),
        )
        .expect(1)
        .mount(&server)
        .await;
    let mut auth = auth(&server, TurnkeyP256ApiKey::generate());
    auth.http = OnceLock::from(
        Client::builder()
            .timeout(Duration::from_millis(10))
            .build()
            .unwrap(),
    );
    let error = post::<Value>(
        &auth,
        url(&server.uri(), "/public/v1/submit/test").unwrap(),
        "secret-marker".into(),
        true,
    )
    .await
    .unwrap_err();
    assert_eq!(
        activity_error(&error).kind(),
        ActivityErrorKind::SubmissionUnknown
    );
    assert!(!format!("{error:#}").contains("secret-marker"));
    assert!(
        error
            .chain()
            .any(<dyn std::error::Error>::is::<reqwest::Error>)
    );
    server.verify().await;
}

#[tokio::test]
async fn mutation_connection_failure_is_safe_to_retry() {
    let base = {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        format!("http://{}", listener.local_addr().unwrap())
    };
    let auth = ResolvedAuth::for_tests(ORG, &base, TurnkeyP256ApiKey::generate());
    let error = post::<Value>(
        &auth,
        url(&base, "/public/v1/submit/test").unwrap(),
        "{}".into(),
        true,
    )
    .await
    .unwrap_err();

    assert!(error.downcast_ref::<ActivityError>().is_none());
    assert_eq!(
        classify(&error),
        Classification {
            code: ErrorCode::NetworkError,
            http_status: None,
        }
    );
}

#[tokio::test]
async fn vote_submission_failures_retain_last_observed_target() {
    for approve in [true, false] {
        for timeout in [true, false] {
            let (server, mut auth) = consensus_target(1).await;
            let vote_path = if approve {
                "/public/v1/submit/approve_activity"
            } else {
                "/public/v1/submit/reject_activity"
            };
            let response = ResponseTemplate::new(200).set_body_json(json!({}));
            Mock::given(path(vote_path))
                .respond_with(if timeout {
                    response.set_delay(Duration::from_millis(500))
                } else {
                    response
                })
                .expect(1)
                .mount(&server)
                .await;
            auth.http = OnceLock::from(
                Client::builder()
                    .timeout(Duration::from_millis(100))
                    .build()
                    .unwrap(),
            );
            let args = if approve {
                ActivityCommand::Approve {
                    id: "target".into(),
                }
            } else {
                ActivityCommand::Reject {
                    id: "target".into(),
                }
            };
            let error = run_activity(args, &auth).await.unwrap_err();
            let failure = activity_error(&error);
            assert_eq!(failure.kind(), ActivityErrorKind::SubmissionUnknown);
            assert_eq!(
                failure.activity(),
                Some(&json!({"id":"target", "status":"ACTIVITY_STATUS_CONSENSUS_NEEDED"}))
            );
            server.verify().await;
        }
    }
}

#[tokio::test]
async fn wait_survives_transient_failures_and_fails_fast_on_client_errors() {
    let server = MockServer::start().await;
    let auth = auth(&server, TurnkeyP256ApiKey::generate());
    Mock::given(path("/public/v1/query/get_activity"))
        .respond_with(ResponseTemplate::new(503).set_body_string("unavailable"))
        .up_to_n_times(1)
        .expect(1)
        .mount(&server)
        .await;
    get_activity("a", "ACTIVITY_STATUS_COMPLETED")
        .expect(1)
        .mount(&server)
        .await;
    let output = run_activity(
        ActivityCommand::Wait {
            id: "a".into(),
            timeout: 5,
        },
        &auth,
    )
    .await
    .unwrap();
    assert_eq!(output.status(), Status::Completed);
    server.verify().await;

    server.reset().await;
    Mock::given(path("/public/v1/query/get_activity"))
        .respond_with(ResponseTemplate::new(400).set_body_string("bad request"))
        .expect(1)
        .mount(&server)
        .await;
    let error = run_activity(
        ActivityCommand::Wait {
            id: "a".into(),
            timeout: 5,
        },
        &auth,
    )
    .await
    .unwrap_err();
    assert_eq!(
        error.downcast_ref::<UnexpectedHttpStatus>().unwrap().status,
        400
    );
    server.verify().await;
}