tsafe-cli 1.2.1

Local-first secrets runtime for developers — inject credentials via exec, never shell history or .env files
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
//! Mobile enrollment command handlers.

use std::io::Read;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
use chrono::{Duration, Utc};
use rand::RngCore;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use tsafe_core::{age_crypto, team};
use tsafe_mobile_core::envelopes::{
    parse_enrollment_response_envelope, parse_enrollment_start_envelope,
    validate_enrollment_response_matches_start, EnrollmentResponseEnvelope,
    EnrollmentStartEnvelope, MobileRepoRef, ENROLLMENT_START_SCHEMA,
};

use crate::cli::{MobileAction, MobileEnrollAction};

pub(crate) fn cmd_mobile(profile: &str, action: MobileAction) -> Result<()> {
    match action {
        MobileAction::Enroll { action } => cmd_mobile_enroll(profile, action),
    }
}

fn cmd_mobile_enroll(profile: &str, action: MobileEnrollAction) -> Result<()> {
    match action {
        MobileEnrollAction::Start {
            repo,
            branch,
            team_keys_path,
            vault_path,
            ttl_minutes,
            display_name,
            json,
            qr,
        } => enroll_start(
            profile,
            &repo,
            &branch,
            &team_keys_path,
            vault_path.as_deref(),
            ttl_minutes,
            display_name,
            json,
            qr,
        ),
        MobileEnrollAction::Accept {
            start,
            response,
            team_keys,
            identity,
            json,
        } => enroll_accept(
            &start,
            &response,
            team_keys.as_deref(),
            identity.as_deref(),
            json,
        ),
    }
}

#[allow(clippy::too_many_arguments)]
fn enroll_start(
    profile: &str,
    repo: &str,
    branch: &str,
    team_keys_path: &str,
    vault_path: Option<&str>,
    ttl_minutes: i64,
    display_name: Option<String>,
    emit_json: bool,
    emit_qr: bool,
) -> Result<()> {
    if !emit_json && !emit_qr {
        anyhow::bail!("pass --json or --qr so the enrollment payload is explicit");
    }
    if ttl_minutes <= 0 {
        anyhow::bail!("--ttl-minutes must be greater than zero");
    }

    let repo = parse_repo_ref(repo)?;
    let created_at = Utc::now();
    let expires_at = created_at + Duration::minutes(ttl_minutes);
    let nonce = random_b64url(32);
    let session_id = format!("mobenr_{}", uuid::Uuid::new_v4());
    let (_desktop_private_identity, desktop_public_key) = age_crypto::generate_identity();
    let vault_path = vault_path
        .map(str::to_string)
        .unwrap_or_else(|| format!(".tsafe/vaults/{profile}.vault"));

    let envelope = EnrollmentStartEnvelope {
        schema: ENROLLMENT_START_SCHEMA.to_string(),
        version: "1.0".to_string(),
        session_id,
        created_at,
        expires_at,
        desktop_public_key,
        nonce,
        repo,
        branch: Some(branch.to_string()),
        profile: profile.to_string(),
        team_keys_path: team_keys_path.to_string(),
        vault_path,
        vault_identity: None,
        requested_capabilities: vec![
            "mobile_age_recipient".to_string(),
            "team_keys_membership_read".to_string(),
            "vault_ciphertext_pull".to_string(),
            "encrypted_qr_share".to_string(),
        ],
        display_name,
    };

    let bytes = serde_json::to_vec(&envelope)?;
    parse_enrollment_start_envelope(&bytes, created_at)
        .context("generated enrollment start envelope did not validate")?;

    if emit_qr {
        let qr_payload = json!({
            "schema": "tsafe/mobile/enrollment-qr-frame/v1",
            "version": "1.0",
            "encoding": "json",
            "frame_count": 1,
            "frame_index": 0,
            "payload": envelope,
        });
        println!("{}", serde_json::to_string_pretty(&qr_payload)?);
    } else {
        println!("{}", serde_json::to_string_pretty(&envelope)?);
    }

    Ok(())
}

fn enroll_accept(
    start_path: &Path,
    response_path: &str,
    team_keys_override: Option<&Path>,
    identity_path: Option<&Path>,
    emit_json: bool,
) -> Result<()> {
    let now = Utc::now();
    let start_bytes = std::fs::read(start_path)
        .with_context(|| format!("read enrollment start envelope {}", start_path.display()))?;
    let response_bytes = if response_path == "-" {
        let mut bytes = Vec::new();
        std::io::stdin()
            .read_to_end(&mut bytes)
            .context("read enrollment response from stdin")?;
        bytes
    } else {
        std::fs::read(response_path)
            .with_context(|| format!("read enrollment response envelope {response_path}"))?
    };

    let start = parse_enrollment_start_envelope(&start_bytes, now)
        .context("invalid enrollment start envelope")?;
    let response = parse_enrollment_response_envelope(&response_bytes, now)
        .context("invalid enrollment response envelope")?;
    validate_enrollment_response_matches_start(&start, &response)
        .context("enrollment response does not match start envelope")?;

    let team_keys_path = team_keys_override
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from(&start.team_keys_path));
    let record = record_mobile_recipient(&team_keys_path, &start, &response)?;
    let team_rewrap = rewrap_team_vault_for_mobile(&start, &response, identity_path)?;

    let receipt = json!({
        "schema": "tsafe/mobile/enrollment-accept-receipt/v1",
        "status": "accepted",
        "profile": start.profile,
        "session_id": start.session_id,
        "nonce": start.nonce,
        "repo": start.repo,
        "team_keys_path": team_keys_path,
        "mobile_recipient": response.mobile_recipient,
        "device_label": response.device_label,
        "already_recorded": record.already_recorded,
        "team_rewrap": team_rewrap,
    });

    if emit_json {
        println!("{}", serde_json::to_string_pretty(&receipt)?);
    } else if record.already_recorded {
        println!(
            "Mobile recipient already recorded: {}",
            response.mobile_recipient
        );
    } else {
        println!("Recorded mobile recipient: {}", response.mobile_recipient);
    }
    if !emit_json {
        match team_rewrap.status.as_str() {
            "rewrapped" => println!(
                "Rewrapped team vault for mobile recipient: {}",
                response.mobile_recipient
            ),
            "already_rewrapped" => println!(
                "Team vault already includes mobile recipient: {}",
                response.mobile_recipient
            ),
            "handoff_required" => println!(
                "Team rewrap handoff required: rerun accept with --identity to update {}",
                team_rewrap.vault_path
            ),
            "blocked" => {
                if let Some(reason) = &team_rewrap.reason {
                    println!("Team rewrap blocked: {reason}");
                }
            }
            _ => {}
        }
    }

    Ok(())
}

fn parse_repo_ref(repo: &str) -> Result<MobileRepoRef> {
    let (owner, name) = repo
        .split_once('/')
        .ok_or_else(|| anyhow::anyhow!("--repo must be OWNER/NAME"))?;
    if owner.trim().is_empty() || name.trim().is_empty() || name.contains('/') {
        anyhow::bail!("--repo must be OWNER/NAME");
    }
    Ok(MobileRepoRef {
        owner: owner.to_string(),
        name: name.to_string(),
    })
}

fn random_b64url(bytes_len: usize) -> String {
    let mut bytes = vec![0_u8; bytes_len];
    rand::thread_rng().fill_bytes(&mut bytes);
    URL_SAFE_NO_PAD.encode(bytes)
}

#[derive(Debug, Default, Deserialize, Serialize)]
struct TeamKeysFile {
    #[serde(default)]
    members: Vec<TeamKeyEntry>,
    #[serde(flatten)]
    extra: serde_json::Map<String, Value>,
}

#[derive(Debug, Deserialize, Serialize)]
struct TeamKeyEntry {
    name: String,
    email: String,
    public_key: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    source: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    enrolled_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    enrollment_session_id: Option<String>,
    #[serde(flatten)]
    extra: serde_json::Map<String, Value>,
}

struct RecordResult {
    already_recorded: bool,
}

#[derive(Debug, Serialize)]
struct TeamRewrapResult {
    status: String,
    vault_path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    already_rewrapped: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    recipient_count: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reason: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    next_action: Option<String>,
}

fn record_mobile_recipient(
    path: &Path,
    start: &EnrollmentStartEnvelope,
    response: &EnrollmentResponseEnvelope,
) -> Result<RecordResult> {
    let mut file = if path.exists() {
        let bytes = std::fs::read(path)
            .with_context(|| format!("read team keys file {}", path.display()))?;
        serde_json::from_slice::<TeamKeysFile>(&bytes)
            .with_context(|| format!("parse team keys file {}", path.display()))?
    } else {
        TeamKeysFile::default()
    };

    let already_recorded = file
        .members
        .iter()
        .any(|member| member.public_key == response.mobile_recipient);

    if !already_recorded {
        file.members.push(TeamKeyEntry {
            name: response
                .device_label
                .clone()
                .unwrap_or_else(|| "Mobile device".to_string()),
            email: "mobile-enrollment@local.invalid".to_string(),
            public_key: response.mobile_recipient.clone(),
            source: Some("tsafe mobile enroll accept".to_string()),
            enrolled_at: Some(Utc::now().to_rfc3339()),
            enrollment_session_id: Some(start.session_id.clone()),
            extra: serde_json::Map::new(),
        });

        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let tmp = path.with_extension("json.tmp");
        std::fs::write(&tmp, serde_json::to_string_pretty(&file)?)
            .with_context(|| format!("write temporary team keys file {}", tmp.display()))?;
        std::fs::rename(&tmp, path)
            .with_context(|| format!("replace team keys file {}", path.display()))?;
    }

    Ok(RecordResult { already_recorded })
}

fn rewrap_team_vault_for_mobile(
    start: &EnrollmentStartEnvelope,
    response: &EnrollmentResponseEnvelope,
    identity_path: Option<&Path>,
) -> Result<TeamRewrapResult> {
    let vault_path = PathBuf::from(&start.vault_path);
    let vault_path_display = vault_path.display().to_string();
    let Some(identity_path) = identity_path else {
        return Ok(TeamRewrapResult {
            status: "handoff_required".to_string(),
            vault_path: vault_path_display.clone(),
            already_rewrapped: None,
            recipient_count: None,
            reason: Some(
                "source-local team vault rewrap requires an operator age identity".to_string(),
            ),
            next_action: Some(format!(
                "rerun accept with --identity <age-identity> to rewrap {vault_path_display}"
            )),
        });
    };

    if !vault_path.exists() {
        return Ok(TeamRewrapResult {
            status: "blocked".to_string(),
            vault_path: vault_path_display,
            already_rewrapped: None,
            recipient_count: None,
            reason: Some("vault ciphertext path from enrollment start envelope was not found".to_string()),
            next_action: Some(
                "make the source-local team vault ciphertext available, then rerun accept with --identity"
                    .to_string(),
            ),
        });
    }

    let vault_json = std::fs::read_to_string(&vault_path)
        .with_context(|| format!("read team vault {}", vault_path.display()))?;
    let mut file: tsafe_core::vault::VaultFile = serde_json::from_str(&vault_json)
        .with_context(|| format!("parse team vault {}", vault_path.display()))?;

    if !team::is_team_vault(&file) {
        return Ok(TeamRewrapResult {
            status: "blocked".to_string(),
            vault_path: vault_path_display,
            already_rewrapped: None,
            recipient_count: None,
            reason: Some("vault ciphertext is not an age team vault".to_string()),
            next_action: Some(
                "initialize or select a team vault before accepting mobile enrollment".to_string(),
            ),
        });
    }

    if file.age_recipients.contains(&response.mobile_recipient) {
        return Ok(TeamRewrapResult {
            status: "already_rewrapped".to_string(),
            vault_path: vault_path_display,
            already_rewrapped: Some(true),
            recipient_count: Some(file.age_recipients.len()),
            reason: None,
            next_action: None,
        });
    }

    let identities = age_crypto::load_identities(identity_path)
        .with_context(|| format!("load age identity {}", identity_path.display()))?;
    team::add_member(&mut file, &response.mobile_recipient, &identities)
        .context("rewrap team vault for mobile recipient")?;
    write_team_vault(&vault_path, &file)?;

    Ok(TeamRewrapResult {
        status: "rewrapped".to_string(),
        vault_path: vault_path_display,
        already_rewrapped: Some(false),
        recipient_count: Some(file.age_recipients.len()),
        reason: None,
        next_action: None,
    })
}

fn write_team_vault(path: &Path, file: &tsafe_core::vault::VaultFile) -> Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let tmp = path.with_extension("vault.tmp");
    std::fs::write(&tmp, serde_json::to_string_pretty(file)?)
        .with_context(|| format!("write temporary team vault {}", tmp.display()))?;
    std::fs::rename(&tmp, path)
        .with_context(|| format!("replace team vault {}", path.display()))?;
    Ok(())
}