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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
use std::fmt::{self, Display, Formatter};
use std::fs;
use std::io::{self, Read};
use std::mem::take;
use std::path::PathBuf;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{Context, Error, Result};
use clap::{Args, Subcommand, ValueEnum};
use reqwest::Url;
use serde::{Serialize, Serializer, de::DeserializeOwned, ser::SerializeStruct};
use serde_json::{Value, error::Category, from_slice, json};
use tokio::time::{sleep, timeout};
use turnkey_api_key_stamper::Stamp;
use turnkey_client::generated::{
    ActivityStatus, ActivityType, GetActivitiesRequest, GetActivityRequest,
    external::options::v1::Pagination,
};
use uuid::Uuid;

use crate::auth::ResolvedAuth;
use crate::errors::{
    ActivityError, ActivityErrorKind, InvalidInput, Malformed, UnexpectedHttpStatus,
    transient_status,
};
use crate::sessions::duration::ExpiresIn;

const WALK_PAGE_SIZE: usize = 100;

const POLL_INTERVAL: Duration = Duration::from_millis(500);

const MAX_ERROR_BODY_BYTES: usize = 4 * 1024;

#[derive(Debug, Args)]
pub struct RequestArgs {
    /// Absolute API path under /public/v1/, such as /public/v1/query/whoami.
    #[arg(long, value_parser = RequestPath::parse)]
    path: RequestPath,
    /// Exact request body.
    #[arg(
        long,
        required_unless_present = "body_file",
        conflicts_with = "body_file"
    )]
    body: Option<String>,
    /// Read exact UTF-8 request bytes from a file, or - for stdin.
    #[arg(long, required_unless_present = "body", conflicts_with = "body")]
    body_file: Option<PathBuf>,
    /// Produce a stamp without submitting the request.
    #[arg(long)]
    stamp_only: bool,
}

#[derive(Debug, Subcommand)]
pub enum ActivityCommand {
    /// List activities, one page at a time.
    List(ListArgs),
    /// Fetch one activity by ID.
    Get {
        /// Activity ID.
        id: String,
    },
    /// Approve a `pending` activity by ID.
    Approve {
        /// Activity ID.
        id: String,
    },
    /// Reject a `pending` activity by ID.
    Reject {
        /// Activity ID.
        id: String,
    },
    /// Poll one activity until it reaches a terminal status.
    Wait {
        /// Activity ID.
        id: String,
        /// Seconds to poll before failing with `wait_timeout`.
        #[arg(long, default_value_t = 60, value_parser = clap::value_parser!(u64).range(1..))]
        timeout: u64,
    },
}

#[derive(Debug, Args)]
pub struct ListArgs {
    /// Page size.
    #[arg(long, default_value_t = 50, value_parser = clap::value_parser!(u32).range(1..))]
    limit: u32,
    /// Activity ID to continue after.
    #[arg(long)]
    cursor: Option<String>,
    /// Keep only these statuses, filtered by the server; repeatable. pending matches created, pending, consensus-needed, and authenticators-needed activities.
    #[arg(long, value_enum)]
    status: Vec<StatusFilter>,
    /// Keep only these activity types, filtered by the server, such as `ACTIVITY_TYPE_CREATE_USER_TAG`; repeatable.
    #[arg(long = "type", value_name = "ACTIVITY_TYPE", value_parser = parse_activity_type)]
    types: Vec<ActivityType>,
    /// Keep only activities created within this window, such as 24h, filtered here: walks pages newest first from --cursor until one is older; --limit caps the matches and sets nextCursor so the same command with --cursor resumes.
    #[arg(long, value_name = "DURATION")]
    since: Option<ExpiresIn>,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
#[cfg_attr(test, derive(PartialEq))]
enum StatusFilter {
    Pending,
    Completed,
    Rejected,
    Failed,
}

impl StatusFilter {
    fn statuses(self) -> &'static [ActivityStatus] {
        match self {
            Self::Pending => &[
                ActivityStatus::Created,
                ActivityStatus::Pending,
                ActivityStatus::ConsensusNeeded,
                ActivityStatus::AuthenticatorsNeeded,
            ],
            Self::Completed => &[ActivityStatus::Completed],
            Self::Rejected => &[ActivityStatus::Rejected],
            Self::Failed => &[ActivityStatus::Failed],
        }
    }
}

fn parse_activity_type(value: &str) -> Result<ActivityType, String> {
    ActivityType::from_str_name(value)
        .filter(|kind| *kind != ActivityType::Unspecified)
        .ok_or_else(|| format!("unknown activity type {value:?}; expected an ACTIVITY_TYPE_ name"))
}

#[cfg_attr(test, derive(Debug))]
pub struct OperationOutput {
    command: &'static str,
    data: Value,
    activity: Option<Value>,
}

/// Record status. A record without an activity is complete; every other
/// status is read from the activity the record carries.
#[derive(Serialize)]
#[cfg_attr(test, derive(Debug, PartialEq))]
#[serde(rename_all = "lowercase")]
enum Status {
    Completed,
    Rejected,
    Failed,
    Pending,
    Unknown,
}

impl OperationOutput {
    pub fn result(command: &'static str, data: Value) -> Self {
        let activity = data
            .get("activity")
            .filter(|v| v.is_object())
            .map(|v| json!({"id":v.get("id"),"status":v.get("status")}));
        Self {
            command,
            data,
            activity,
        }
    }

    pub(crate) fn data(&self) -> &Value {
        &self.data
    }

    pub(crate) fn into_data(self) -> Value {
        self.data
    }

    fn status(&self) -> Status {
        self.activity.as_ref().map_or(Status::Completed, Status::of)
    }

    pub(crate) fn is_pending(&self) -> bool {
        matches!(self.status(), Status::Pending)
    }

    pub(crate) fn pending_activity_id(&self) -> Option<&str> {
        if !self.is_pending() {
            return None;
        }
        self.activity.as_ref()?["id"].as_str()
    }
}

impl Status {
    fn of(activity: &Value) -> Self {
        match activity
            .get("status")
            .and_then(Value::as_str)
            .and_then(ActivityStatus::from_str_name)
        {
            Some(ActivityStatus::Completed) => Self::Completed,
            Some(ActivityStatus::Rejected) => Self::Rejected,
            Some(ActivityStatus::Failed) => Self::Failed,
            Some(
                ActivityStatus::Created
                | ActivityStatus::Pending
                | ActivityStatus::ConsensusNeeded
                | ActivityStatus::AuthenticatorsNeeded,
            ) => Self::Pending,
            Some(ActivityStatus::Unspecified) | None => Self::Unknown,
        }
    }
}

impl Serialize for OperationOutput {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut record = serializer
            .serialize_struct("OperationOutput", 5 + usize::from(self.activity.is_some()))?;
        record.serialize_field("schemaVersion", &1u32)?;
        record.serialize_field("reason", "command_result")?;
        record.serialize_field("command", self.command)?;
        record.serialize_field("status", &self.status())?;
        record.serialize_field("data", &self.data)?;
        if let Some(activity) = &self.activity {
            record.serialize_field("activity", activity)?;
        }
        record.end()
    }
}

impl Display for OperationOutput {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        // fmt::Error carries no payload.
        #[allow(clippy::map_err_ignore)]
        let rendered = serde_json::to_string_pretty(self).map_err(|_| fmt::Error)?;
        f.write_str(&rendered)
    }
}

/// An absolute `/public/v1/` API path with no query, fragment, or traversal,
/// classified as a read-only query or an activity submission.
#[derive(Clone, Debug)]
struct RequestPath {
    raw: String,
    kind: RequestKind,
}

#[derive(Clone, Debug)]
enum RequestKind {
    Query,
    Submit,
}

impl RequestPath {
    fn parse(value: &str) -> Result<Self, String> {
        if !value.starts_with("/public/v1/")
            || value.contains(['?', '#', '\\', '%'])
            || value.split('/').any(|s| s == ".." || s == ".")
        {
            return Err(
                "path must be an absolute /public/v1/ API path without query, fragment, or traversal"
                    .into(),
            );
        }
        let kind = if value.starts_with("/public/v1/query/") {
            RequestKind::Query
        } else {
            RequestKind::Submit
        };
        Ok(Self {
            raw: value.into(),
            kind,
        })
    }
}

fn url(base: &str, path: &str) -> Result<Url> {
    Url::parse(&format!("{}{path}", base.trim_end_matches('/')))
        .map_err(|error| Malformed::new("invalid request URL", error).into())
}

async fn post<R: DeserializeOwned>(
    auth: &ResolvedAuth,
    endpoint: Url,
    body: String,
    mutation: bool,
) -> Result<R> {
    let stamp = auth
        .stamper
        .stamp(body.as_bytes())
        .context("could not stamp request")?;
    let name = endpoint
        .path()
        .rsplit_once('/')
        .map_or(endpoint.path(), |(_, end)| end)
        .to_owned();
    let response = auth
        .http()?
        .post(endpoint)
        .header(stamp.name, stamp.value)
        .header("Content-Type", "application/json")
        .body(body)
        .send()
        .await
        .map_err(|error| {
            if mutation && !error.is_connect() {
                ActivityError::new(
                    ActivityErrorKind::SubmissionUnknown,
                    "request outcome is unknown; inspect activities before resubmitting",
                )
                .with_source(error)
                .into()
            } else {
                Error::new(error).context("API request failed")
            }
        })?;
    let status = response.status();
    if !status.is_success() {
        let mut body = response
            .text()
            .await
            .unwrap_or_else(|_| "unreadable response body".into());
        if body.len() > MAX_ERROR_BODY_BYTES {
            let mut cut = MAX_ERROR_BODY_BYTES;
            while !body.is_char_boundary(cut) {
                cut -= 1;
            }
            body.truncate(cut);
            body.push('…');
        }
        return Err(UnexpectedHttpStatus {
            status: status.as_u16(),
            body,
        }
        .into());
    }
    let kind = if mutation {
        ActivityErrorKind::SubmissionUnknown
    } else {
        ActivityErrorKind::MalformedResponse
    };
    let bytes = response.bytes().await.map_err(|error| {
        ActivityError::new(kind, "API returned an unreadable response").with_source(error)
    })?;
    from_slice(&bytes).map_err(|error| match error.classify() {
        Category::Data => ActivityError::new(
            ActivityErrorKind::MalformedResponse,
            format!("{name} response was malformed"),
        )
        .with_source(error)
        .into(),
        Category::Io | Category::Syntax | Category::Eof => {
            ActivityError::new(kind, "API returned an unreadable response")
                .with_source(error)
                .into()
        }
    })
}

fn encode<T: Serialize>(value: &T) -> Result<String> {
    serde_json::to_string(value).context("could not encode request")
}

pub(crate) fn unix_now() -> Result<Duration> {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .context("system clock precedes Unix epoch")
}

fn envelope<T: Serialize>(kind: &str, organization_id: Uuid, parameters: &T) -> Result<Value> {
    let timestamp_ms = unix_now()?.as_millis().to_string();
    Ok(json!({
        "type": kind,
        "timestampMs": timestamp_ms,
        "organizationId": organization_id,
        "parameters": parameters,
        "generateAppProofs": null,
    }))
}

pub struct PreparedRequest {
    path: RequestPath,
    body: String,
    organization_id: Option<Uuid>,
    stamp_only: bool,
}

impl RequestArgs {
    pub fn prepare(self) -> Result<PreparedRequest> {
        let body = match (self.body, self.body_file) {
            (Some(body), _) => body,
            (None, Some(path)) if path.as_os_str() == "-" => {
                let mut body = String::new();
                io::stdin()
                    .read_to_string(&mut body)
                    .map_err(|error| Malformed::new("could not read UTF-8 request body", error))?;
                body
            }
            (None, Some(path)) => fs::read_to_string(path)
                .map_err(|error| Malformed::new("could not read UTF-8 request body", error))?,
            (None, None) => unreachable!("clap requires exactly one body source"),
        };
        let value: Value = serde_json::from_str(&body)
            .map_err(|error| Malformed::new("body must be valid JSON", error))?;
        if !value.is_object() {
            return Err(InvalidInput("body must be a JSON object".into()).into());
        }
        let organization_id = value
            .get("organizationId")
            .and_then(Value::as_str)
            .and_then(|id| Uuid::parse_str(id).ok());
        Ok(PreparedRequest {
            path: self.path,
            body,
            organization_id,
            stamp_only: self.stamp_only,
        })
    }
}

impl PreparedRequest {
    pub async fn run(self, auth: &ResolvedAuth) -> Result<OperationOutput> {
        let command = "request";
        let body = self.body;
        if self.organization_id != Some(auth.org_id) {
            return Err(InvalidInput(
                "body organizationId must match selected organization".into(),
            )
            .into());
        }
        let RequestPath { raw, kind } = self.path;
        let endpoint = url(auth.api_base_url.as_str(), &raw)?;
        if self.stamp_only {
            let stamp = auth
                .stamper
                .stamp(body.as_bytes())
                .context("could not stamp request")?;
            return Ok(OperationOutput::result(
                command,
                json!({"url":endpoint.as_str(),"method":"POST","header":{"name":stamp.name,"value":stamp.value},"body":body}),
            ));
        }
        let mutation = matches!(kind, RequestKind::Submit);
        let value = post::<Value>(auth, endpoint, body, mutation).await?;
        match kind {
            RequestKind::Query => Ok(OperationOutput::result(command, value)),
            RequestKind::Submit => submission_result(command, value),
        }
    }
}

fn submission_result(command: &'static str, data: Value) -> Result<OperationOutput> {
    if data
        .pointer("/activity/id")
        .and_then(Value::as_str)
        .is_none_or(str::is_empty)
    {
        return Err(ActivityError::new(
            ActivityErrorKind::SubmissionUnknown,
            "response omitted activity identity; inspect activities before resubmitting",
        )
        .into());
    }
    terminal(OperationOutput::result(command, data), true)
}

fn terminal(output: OperationOutput, submitted: bool) -> Result<OperationOutput> {
    let Some(activity) = output.activity else {
        return Ok(output);
    };
    let (kind, message) = match Status::of(&activity) {
        Status::Completed | Status::Pending => {
            return Ok(OperationOutput {
                activity: Some(activity),
                ..output
            });
        }
        Status::Rejected => (ActivityErrorKind::NotCompleted, "activity rejected"),
        Status::Failed => (ActivityErrorKind::NotCompleted, "activity failed"),
        Status::Unknown if submitted => (
            ActivityErrorKind::SubmissionUnknown,
            "unknown activity status; inspect activity before resubmitting",
        ),
        Status::Unknown => (
            ActivityErrorKind::MalformedResponse,
            "unknown activity status",
        ),
    };
    Err(ActivityError::new(kind, message)
        .with_activity(activity)
        .into())
}

pub(crate) fn observed(command: &'static str, data: Value) -> Result<OperationOutput> {
    terminal(OperationOutput::result(command, data), false)
}

fn with_target(error: Error, target: Value) -> Error {
    match error.downcast::<ActivityError>() {
        Ok(activity) if activity.activity().is_none() => activity.with_activity(target).into(),
        Ok(activity) => activity.into(),
        Err(error) => error,
    }
}

pub(crate) async fn query_activity(auth: &ResolvedAuth, id: &str) -> Result<Value> {
    let body = encode(&GetActivityRequest {
        organization_id: auth.org_id.to_string(),
        activity_id: id.into(),
    })?;
    let endpoint = url(auth.api_base_url.as_str(), "/public/v1/query/get_activity")?;
    let value = post::<Value>(auth, endpoint, body, false).await?;
    let matches = value
        .pointer("/activity/id")
        .and_then(Value::as_str)
        .is_some_and(|returned| returned.eq_ignore_ascii_case(id));
    if !matches {
        return Err(ActivityError::new(
            ActivityErrorKind::MalformedResponse,
            "response omitted or mismatched requested activity",
        )
        .into());
    }
    Ok(value)
}

pub async fn run_activity(args: ActivityCommand, auth: &ResolvedAuth) -> Result<OperationOutput> {
    match args {
        ActivityCommand::List(args) => list(auth, args).await,
        ActivityCommand::Get { id } => {
            let value = query_activity(auth, &id).await?;
            Ok(OperationOutput::result("activity.get", value))
        }
        ActivityCommand::Wait { id, timeout } => wait(auth, &id, timeout).await,
        ActivityCommand::Approve { id } => vote(auth, &id, true).await,
        ActivityCommand::Reject { id } => vote(auth, &id, false).await,
    }
}

async fn list(auth: &ResolvedAuth, args: ListArgs) -> Result<OperationOutput> {
    let ListArgs {
        limit,
        cursor,
        status,
        types,
        since,
    } = args;
    let limit = limit as usize;
    let cutoff = since
        .map(|since| Ok::<_, Error>(unix_now()?.as_secs().saturating_sub(since.seconds())))
        .transpose()?;
    let filter_by_status: Vec<ActivityStatus> = status
        .iter()
        .flat_map(|filter| filter.statuses().iter().copied())
        .collect();
    let endpoint = url(
        auth.api_base_url.as_str(),
        "/public/v1/query/list_activities",
    )?;
    let page_size = if cutoff.is_some() {
        limit.min(WALK_PAGE_SIZE)
    } else {
        limit
    };
    let mut request = GetActivitiesRequest {
        organization_id: auth.org_id.to_string(),
        filter_by_status,
        filter_by_type: types,
        pagination_options: Some(Pagination {
            limit: page_size.to_string(),
            before: String::new(),
            after: cursor.unwrap_or_default(),
        }),
    };
    let mut items = Vec::new();
    let mut capped = false;
    'pages: loop {
        let mut response = post::<Value>(auth, endpoint.clone(), encode(&request)?, false).await?;
        let page = response
            .get_mut("activities")
            .and_then(Value::as_array_mut)
            .map(take)
            .ok_or_else(|| {
                ActivityError::new(
                    ActivityErrorKind::MalformedResponse,
                    "response omitted activities",
                )
            })?;
        let full = page.len() == page_size;
        let Some(cutoff) = cutoff else {
            capped = full;
            items = page;
            break;
        };
        for item in page {
            if created_at_seconds(&item)? < cutoff {
                break 'pages;
            }
            items.push(item);
            if items.len() == limit {
                capped = true;
                break 'pages;
            }
        }
        if !full {
            break;
        }
        if let Some(pagination) = &mut request.pagination_options
            && let Some(last) = items.last()
        {
            pagination.after = activity_id(last)?.to_owned();
        }
    }
    let next = if capped {
        items.last().map(activity_id).transpose()?.map(Value::from)
    } else {
        None
    };
    Ok(OperationOutput::result(
        "activity.list",
        json!({"items":items,"nextCursor":next}),
    ))
}

fn activity_id(item: &Value) -> Result<&str> {
    item.get("id").and_then(Value::as_str).ok_or_else(|| {
        ActivityError::new(ActivityErrorKind::MalformedResponse, "activity omitted id").into()
    })
}

fn created_at_seconds(item: &Value) -> Result<u64> {
    item.pointer("/createdAt/seconds")
        .and_then(Value::as_str)
        .and_then(|seconds| seconds.parse().ok())
        .ok_or_else(|| {
            ActivityError::new(
                ActivityErrorKind::MalformedResponse,
                format!(
                    "activity {} omitted or malformed createdAt.seconds",
                    activity_id(item).unwrap_or("?")
                ),
            )
            .into()
        })
}

async fn wait(auth: &ResolvedAuth, id: &str, seconds: u64) -> Result<OperationOutput> {
    let command = "activity.wait";
    let mut last: Option<Value> = None;
    let result = timeout(Duration::from_secs(seconds), async {
        loop {
            match query_activity(auth, id).await {
                Ok(value) => {
                    let output = OperationOutput::result(command, value);
                    if !output.is_pending() {
                        return terminal(output, false);
                    }
                    last = output.activity;
                }
                Err(error) if transient(&error) => {}
                Err(error) => {
                    let target = last
                        .take()
                        .unwrap_or_else(|| json!({"id": id, "status": null}));
                    return Err(with_target(error, target));
                }
            }
            sleep(POLL_INTERVAL).await;
        }
    })
    .await;
    match result {
        Ok(result) => result,
        Err(_) => Err(ActivityError::new(
            ActivityErrorKind::WaitTimeout,
            "wait timed out; resume with activity wait and the same ID",
        )
        .with_activity(last.unwrap_or_else(|| json!({"id": id, "status": null})))
        .into()),
    }
}

fn transient(error: &Error) -> bool {
    error.chain().any(|cause| {
        cause
            .downcast_ref::<UnexpectedHttpStatus>()
            .is_some_and(|http| transient_status(http.status))
            || cause.downcast_ref::<reqwest::Error>().is_some()
    })
}

async fn vote(auth: &ResolvedAuth, id: &str, approve: bool) -> Result<OperationOutput> {
    let command = if approve {
        "activity.approve"
    } else {
        "activity.reject"
    };
    let value = query_activity(auth, id).await?;
    let target = json!({"id": id, "status": value.pointer("/activity/status")});
    let submitted = async {
        let fingerprint = value
            .pointer("/activity/fingerprint")
            .and_then(Value::as_str)
            .filter(|s| !s.is_empty())
            .ok_or_else(|| {
                ActivityError::new(
                    ActivityErrorKind::MalformedResponse,
                    "activity omitted fingerprint",
                )
            })?
            .to_owned();
        let (path, kind) = if approve {
            (
                "/public/v1/submit/approve_activity",
                "ACTIVITY_TYPE_APPROVE_ACTIVITY",
            )
        } else {
            (
                "/public/v1/submit/reject_activity",
                "ACTIVITY_TYPE_REJECT_ACTIVITY",
            )
        };
        let body = encode(&envelope(
            kind,
            auth.org_id,
            &json!({ "fingerprint": fingerprint }),
        )?)?;
        let response =
            post::<Value>(auth, url(auth.api_base_url.as_str(), path)?, body, true).await?;
        let returned = response
            .pointer("/activity/id")
            .and_then(Value::as_str)
            .filter(|s| !s.is_empty())
            .ok_or_else(|| {
                ActivityError::new(
                    ActivityErrorKind::SubmissionUnknown,
                    "response omitted activity; inspect activity before resubmitting",
                )
            })?;
        let data = if returned.eq_ignore_ascii_case(id) {
            response
        } else {
            terminal(OperationOutput::result(command, response), true)?;
            query_activity(auth, id).await?
        };
        let output = OperationOutput::result(command, data);
        if !approve && matches!(output.status(), Status::Rejected) {
            Ok(output)
        } else {
            terminal(output, true)
        }
    }
    .await;
    submitted.map_err(|error| with_target(error, target))
}

pub async fn submit_activity<T: Serialize>(
    auth: &ResolvedAuth,
    command: &'static str,
    endpoint: &str,
    kind: &str,
    parameters: &T,
) -> Result<OperationOutput> {
    let body = encode(&envelope(kind, auth.org_id, parameters)?)?;
    let endpoint = url(
        auth.api_base_url.as_str(),
        &format!("/public/v1/submit/{endpoint}"),
    )?;
    let value = post::<Value>(auth, endpoint, body, true).await?;
    submission_result(command, value)
}

pub(crate) async fn query<T: Serialize, R: DeserializeOwned>(
    path: &str,
    request: &T,
    auth: &ResolvedAuth,
) -> Result<R> {
    post::<R>(
        auth,
        url(auth.api_base_url.as_str(), path)?,
        encode(request)?,
        false,
    )
    .await
}

#[cfg(test)]
mod tests;