tvc 0.13.1

CLI for Turnkey Verifiable Cloud
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
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
//! Turnkey CLI configuration management.
//!
//! Config files are stored at `~/.config/turnkey/`:
//! - `tvc.config.toml` - Main config with org registry, active org, and key paths
//! - `orgs/<alias>/api_key.json` - Default location for API keys
//! - `orgs/<alias>/operator.json` - Default location for operator keys
//!
//! Key paths are stored in the config so users can customize storage locations.

mod api_key;
mod qos_operator_key;

pub use api_key::{KeyCurve, StoredApiKey};
pub use qos_operator_key::StoredQosOperatorKey;

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};
use std::path::Path;
use std::path::PathBuf;
use tracing::debug;
use uuid::Uuid;

const CONFIG_DIR: &str = ".config/turnkey";
const CONFIG_FILE: &str = "tvc.config.toml";
const ORGS_DIR: &str = "orgs";
const API_KEY_FILE: &str = "api_key.json";
const OPERATOR_KEY_FILE: &str = "operator.json";
const CONFIG_VERSION: u16 = 1;
const DEFAULT_OPERATOR_NAME: &str = "default";

/// Current in-memory TVC configuration.
///
/// Disk schemas are versioned separately below. Loading a legacy v0 config
/// converts it to this model without writing it back; the next existing save
/// point persists it as v1.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct Config {
    /// The currently active organization alias
    #[serde(default)]
    pub active_org: Option<String>,
    /// Map of org alias -> org config
    #[serde(default)]
    pub orgs: HashMap<String, OrgConfig>,
    /// Map of org alias -> last created app ID (for convenience)
    #[serde(default)]
    pub last_created_app_id: HashMap<String, String>,
    /// Map of org alias -> last manifest set operator IDs (for convenience)
    #[serde(default)]
    pub last_operator_ids: HashMap<String, Vec<String>>,
    /// Unrecognized top-level fields retained across supported config rewrites.
    #[serde(default, flatten)]
    pub extra: toml::Table,
}

/// Versioned on-disk schemas and migrations into the current runtime model.
mod disk {
    use super::{CONFIG_VERSION, Config, OperatorKind, OperatorRecord, OrgConfig};
    use anyhow::{Context, Result, bail};
    use serde::Serialize;
    use std::collections::HashMap;
    use std::path::PathBuf;

    /// Every supported shape of `tvc.config.toml`.
    enum DiskConfig {
        /// The legacy, unversioned config schema.
        V0(v0::Config),
        /// The current config schema, identified by `version = 1` on disk.
        V1(Config),
    }

    impl DiskConfig {
        /// Parse the version header once, then deserialize the remaining table
        /// into the matching supported schema.
        fn from_toml(content: &str) -> Result<Self> {
            let mut table: toml::Table =
                toml::from_str(content).context("failed to parse config TOML")?;
            let header = ConfigVersionHeader::take_from(&mut table)?;
            let config = toml::Value::Table(table);

            match header.version {
                None => config
                    .try_into()
                    .map(Self::V0)
                    .context("failed to parse v0 config"),
                Some(CONFIG_VERSION) => {
                    let config = config.try_into().context("failed to parse v1 config")?;
                    Ok(Self::V1(config))
                }
                Some(version) if version > CONFIG_VERSION => bail!(
                    "config written by a newer tvc (version {version}); this tvc supports through version {CONFIG_VERSION}"
                ),
                Some(version) => bail!("unsupported tvc config version {version}"),
            }
        }

        /// Convert any supported disk schema into the current runtime model.
        fn into_current(self) -> Result<Config> {
            match self {
                Self::V0(config) => migrate_v0(config),
                Self::V1(config) => Ok(config),
            }
        }
    }

    pub(super) fn from_toml(content: &str) -> Result<Config> {
        DiskConfig::from_toml(content)?.into_current()
    }

    /// The version marker is removed before deserializing a schema payload so
    /// it cannot be captured as an unknown field in `Config::extra`.
    struct ConfigVersionHeader {
        version: Option<u16>,
    }

    impl ConfigVersionHeader {
        fn take_from(table: &mut toml::Table) -> Result<Self> {
            let Some(value) = table.remove("version") else {
                return Ok(Self { version: None });
            };
            let version = value
                .try_into()
                .context("config version must be an unsigned 16-bit integer")?;
            Ok(Self {
                version: Some(version),
            })
        }
    }

    /// Serialization-only v1 envelope. Its private construction guarantees
    /// that every saved current config is labeled with the current version.
    #[derive(Serialize)]
    struct V1Envelope<'a> {
        version: u16,
        #[serde(flatten)]
        config: &'a Config,
    }

    pub(super) fn to_toml(config: &Config) -> Result<String> {
        toml::to_string_pretty(&V1Envelope {
            version: CONFIG_VERSION,
            config,
        })
        .context("failed to serialize config")
    }

    fn migrate_v0(config: v0::Config) -> Result<Config> {
        let orgs = config
            .orgs
            .into_iter()
            .map(|(alias, org)| migrate_v0_org(&alias, org).map(|org| (alias, org)))
            .collect::<Result<HashMap<_, _>>>()?;

        Ok(Config {
            active_org: config.active_org,
            orgs,
            last_created_app_id: config.last_created_app_id,
            last_operator_ids: config.last_operator_ids,
            extra: config.extra,
        })
    }

    fn migrate_v0_org(alias: &str, mut table: toml::Table) -> Result<OrgConfig> {
        let operator_key_path = table.remove("operator_key_path").with_context(|| {
            format!("v0 config for organization '{alias}' is missing operator_key_path")
        })?;
        let operator_key_path: PathBuf = operator_key_path.try_into().with_context(|| {
            format!("invalid operator_key_path in v0 config for organization '{alias}'")
        })?;
        let mut org: OrgConfig = toml::Value::Table(table)
            .try_into()
            .with_context(|| format!("failed to migrate v0 config for organization '{alias}'"))?;

        org.default_operator_kind = OperatorKind::Local;
        org.operators = vec![OperatorRecord::local(operator_key_path)];
        Ok(org)
    }

    pub(super) mod v0 {
        use serde::Deserialize;
        use std::collections::HashMap;

        /// Legacy top-level schema. Organization tables remain untyped until
        /// migration extracts `operator_key_path` and parses the current shape.
        #[derive(Deserialize)]
        pub(super) struct Config {
            #[serde(default)]
            pub(super) active_org: Option<String>,
            #[serde(default)]
            pub(super) orgs: HashMap<String, toml::Table>,
            #[serde(default)]
            pub(super) last_created_app_id: HashMap<String, String>,
            #[serde(default)]
            pub(super) last_operator_ids: HashMap<String, Vec<String>>,
            /// Unknown root values are carried into the current config so a
            /// migration does not discard data owned by another writer.
            #[serde(default, flatten)]
            pub(super) extra: toml::Table,
        }
    }
}

/// Known API base URLs for different environments.
pub const API_BASE_URL_PROD: &str = "https://api.turnkey.com";
pub const API_BASE_URL_PREPROD: &str = "https://api.preprod.turnkey.engineering";
pub const API_BASE_URL_DEV: &str = "https://api.dev.turnkey.engineering";
pub const API_BASE_URL_LOCAL: &str = "http://localhost:8081";

/// Dashboard base URLs corresponding to each environment.
pub const DASHBOARD_URL_PROD: &str = "https://app.turnkey.com";
pub const DASHBOARD_URL_PREPROD: &str = "https://app.preprod.turnkey.engineering";
pub const DASHBOARD_URL_DEV: &str = "https://app.dev.turnkey.engineering";

/// Maps an API base URL to the dashboard base URL for the same environment.
///
/// Any URL that isn't a known Turnkey environment (e.g. a local API) falls back
/// to the production dashboard.
pub fn dashboard_base_url(api_base_url: &str) -> &'static str {
    match api_base_url {
        API_BASE_URL_PROD => DASHBOARD_URL_PROD,
        API_BASE_URL_PREPROD => DASHBOARD_URL_PREPROD,
        API_BASE_URL_DEV => DASHBOARD_URL_DEV,
        _ => DASHBOARD_URL_PROD,
    }
}

/// The active operator backend for an organization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum OperatorKind {
    #[default]
    Local,
    Hosted,
}

impl Display for OperatorKind {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Local => "local".fmt(f),
            Self::Hosted => "hosted".fmt(f),
        }
    }
}

/// One durable operator entry in an organization's registry.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct OperatorRecord {
    /// Human-readable name within the organization.
    pub name: String,
    #[serde(flatten)]
    pub kind: OperatorRecordKind,
}

impl OperatorRecord {
    pub fn local(key_path: PathBuf) -> Self {
        Self {
            name: DEFAULT_OPERATOR_NAME.to_string(),
            kind: OperatorRecordKind::Local(LocalOperatorRecord {
                key_path,
                operator_id: None,
                extra: toml::Table::new(),
            }),
        }
    }

    pub fn operator_kind(&self) -> OperatorKind {
        match self.kind {
            OperatorRecordKind::Local(_) => OperatorKind::Local,
            OperatorRecordKind::Hosted(_) => OperatorKind::Hosted,
        }
    }
}

/// Kind-specific durable operator metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum OperatorRecordKind {
    Local(LocalOperatorRecord),
    Hosted(HostedOperatorRecord),
}

/// Locator and optional Turnkey identity for a local operator key.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct LocalOperatorRecord {
    /// Path to a `StoredQosOperatorKey` JSON document.
    pub key_path: PathBuf,
    #[serde(default)]
    pub operator_id: Option<String>,
    #[serde(default, flatten)]
    pub extra: toml::Table,
}

/// Public metadata for an operator whose keys are held by Turnkey.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct HostedOperatorRecord {
    pub operator_id: Uuid,
    pub wallet_id: Uuid,
    pub path: String,
    pub encrypt_public_key: String,
    pub sign_public_key: String,
    #[serde(default, flatten)]
    pub extra: toml::Table,
}

/// Configuration for a single organization.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct OrgConfig {
    /// The Turnkey organization ID
    pub id: String,
    /// Path to the API key file
    pub api_key_path: PathBuf,
    /// API base URL for this organization
    #[serde(default = "default_api_base_url")]
    pub api_base_url: String,
    /// Operator backend selected for default command resolution.
    #[serde(default)]
    pub default_operator_kind: OperatorKind,
    /// Durable local and hosted operator metadata.
    #[serde(default)]
    pub operators: Vec<OperatorRecord>,
    /// Unrecognized organization fields retained across supported config rewrites.
    #[serde(default, flatten)]
    pub extra: toml::Table,
}

impl OrgConfig {
    /// Return the sole active local operator registry entry.
    pub(crate) fn select_local_operator(&self, org_alias: &str) -> Result<&OperatorRecord> {
        if self.default_operator_kind != OperatorKind::Local {
            bail!(
                "the active operator kind for org '{org_alias}' is {}",
                self.default_operator_kind
            )
        }

        let candidates: Vec<_> = self
            .operators
            .iter()
            .filter(|operator| matches!(operator.kind, OperatorRecordKind::Local(_)))
            .collect();

        // TODO: Decouple this function from its org_alias callsite so it is more
        // flexible to be used anywhere else.
        match candidates.as_slice() {
            [] => bail!("No local operator configured for org '{org_alias}'"),
            [operator] => Ok(*operator),
            _ => bail!("Multiple local operators are configured for org '{org_alias}'"),
        }
    }

    /// Return the kind-specific record for the sole active local operator.
    pub fn select_local_record(&self, org_alias: &str) -> Result<&LocalOperatorRecord> {
        let operator = self.select_local_operator(org_alias)?;
        let OperatorRecordKind::Local(local) = &operator.kind else {
            bail!("selected operator is not local");
        };
        Ok(local)
    }
}

fn default_api_base_url() -> String {
    API_BASE_URL_PROD.to_string()
}

/// Returns the base config directory: `~/.config/turnkey/`
pub fn config_dir() -> Result<PathBuf> {
    let home = std::env::var("HOME").context("HOME environment variable not set")?;
    Ok(PathBuf::from(home).join(CONFIG_DIR))
}

/// Returns the path to tvc.config.toml
pub fn config_file_path() -> Result<PathBuf> {
    Ok(config_dir()?.join(CONFIG_FILE))
}

/// Returns the default orgs directory: `~/.config/turnkey/orgs/`
pub fn orgs_dir() -> Result<PathBuf> {
    Ok(config_dir()?.join(ORGS_DIR))
}

/// Returns the default directory for a specific org: `~/.config/turnkey/orgs/<alias>/`
pub fn default_org_dir(alias: &str) -> Result<PathBuf> {
    Ok(orgs_dir()?.join(alias))
}

/// Returns the default API key path for an org
pub fn default_api_key_path(alias: &str) -> Result<PathBuf> {
    Ok(default_org_dir(alias)?.join(API_KEY_FILE))
}

/// Returns the default operator key path for an org
pub fn default_operator_key_path(alias: &str) -> Result<PathBuf> {
    Ok(default_org_dir(alias)?.join(OPERATOR_KEY_FILE))
}

impl Config {
    /// Load config from disk, or return default if it doesn't exist
    pub async fn load() -> Result<Self> {
        let path = config_file_path()?;
        debug!(config_path = %path.display(), "loading tvc config");
        if !path.exists() {
            debug!(config_path = %path.display(), "tvc config not found; using defaults");
            return Ok(Config::default());
        }

        let config = Self::load_from_path(&path).await?;

        debug!(
            config_path = %path.display(),
            active_org = ?config.active_org,
            org_count = config.orgs.len(),
            "loaded tvc config"
        );

        Ok(config)
    }

    /// Save config to disk
    pub async fn save(&self) -> Result<()> {
        let path = config_file_path()?;
        debug!(
            config_path = %path.display(),
            active_org = ?self.active_org,
            org_count = self.orgs.len(),
            "saving tvc config"
        );

        self.save_to_path(&path).await
    }

    async fn load_from_path(path: &Path) -> Result<Self> {
        let content = tokio::fs::read_to_string(path)
            .await
            .with_context(|| format!("failed to read config file: {}", path.display()))?;
        disk::from_toml(&content)
            .with_context(|| format!("failed to parse config file: {}", path.display()))
    }

    async fn save_to_path(&self, path: &Path) -> Result<()> {
        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            tokio::fs::create_dir_all(parent).await.with_context(|| {
                format!("failed to create config directory: {}", parent.display())
            })?;
        }

        let content = disk::to_toml(self)?;

        tokio::fs::write(&path, content)
            .await
            .with_context(|| format!("failed to write config file: {}", path.display()))?;

        debug!(config_path = %path.display(), "saved tvc config");

        Ok(())
    }

    /// Get the active organization config, if any
    pub fn active_org_config(&self) -> Option<(&String, &OrgConfig)> {
        let alias = self.active_org.as_ref()?;
        self.orgs.get(alias).map(|config| (alias, config))
    }

    /// Add or update an organization with default key paths
    pub fn add_org(&mut self, alias: &str, org_id: String, api_base_url: String) -> Result<()> {
        debug!(org_alias = alias, %api_base_url, "adding organization config");
        let org_config = OrgConfig {
            id: org_id,
            api_key_path: default_api_key_path(alias)?,
            api_base_url,
            default_operator_kind: OperatorKind::Local,
            operators: vec![OperatorRecord::local(default_operator_key_path(alias)?)],
            extra: toml::Table::new(),
        };
        self.orgs.insert(alias.to_string(), org_config);
        Ok(())
    }

    /// Remove an organization from the config, along with the convenience state
    /// (last app ID, last operator IDs) tracked for it. If the removed org was
    /// the active one, the active org is cleared.
    ///
    /// Returns the removed [`OrgConfig`], or `None` if no org with that alias
    /// was configured. This only touches the config registry; deleting the
    /// org's key files on disk is the caller's responsibility.
    pub fn remove_org(&mut self, alias: &str) -> Option<OrgConfig> {
        let removed = self.orgs.remove(alias)?;
        debug!(org_alias = alias, "removing organization config");
        self.last_created_app_id.remove(alias);
        self.last_operator_ids.remove(alias);
        if self.active_org.as_deref() == Some(alias) {
            self.active_org = None;
        }
        Some(removed)
    }

    /// Set the active organization
    pub fn set_active_org(&mut self, alias: &str) -> Result<()> {
        debug!(org_alias = alias, "setting active organization");
        if !self.orgs.contains_key(alias) {
            bail!("organization '{}' not found in config", alias);
        }
        self.active_org = Some(alias.to_string());
        Ok(())
    }

    /// Get list of configured org aliases
    pub fn org_aliases(&self) -> Vec<&String> {
        self.orgs.keys().collect()
    }

    /// Store the last created app ID for the active org
    pub fn set_last_app_id(&mut self, app_id: &str) -> Result<()> {
        let alias = self
            .active_org
            .as_ref()
            .context("no active organization set")?;
        self.last_created_app_id
            .insert(alias.clone(), app_id.to_string());
        Ok(())
    }

    /// Get the last created app ID for the active org, if any
    pub fn get_last_app_id(&self) -> Option<String> {
        let alias = self.active_org.as_ref()?;
        self.last_created_app_id.get(alias).cloned()
    }

    /// Store the last manifest set operator IDs for the active org
    pub fn set_last_operator_ids(&mut self, operator_ids: &[String]) -> Result<()> {
        let alias = self
            .active_org
            .as_ref()
            .context("no active organization set")?;
        self.last_operator_ids
            .insert(alias.clone(), operator_ids.to_vec());
        Ok(())
    }

    /// Get the last manifest set operator IDs for the active org
    pub fn get_last_operator_ids(&self) -> Option<Vec<String>> {
        let alias = self.active_org.as_ref()?;
        self.last_operator_ids.get(alias).cloned()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    const V0_CONFIG: &str = r#"
active_org = "default"
future_root = "keep-root"

[orgs.default]
id = "org-123"
api_key_path = "/keys/api.json"
operator_key_path = "/keys/operator.json"
future_org = 42

[last_created_app_id]
default = "app-123"

[last_operator_ids]
default = ["operator-123"]
"#;

    const MIGRATED_V1_CONFIG: &str = r#"
version = 1
active_org = "default"
future_root = "keep-root"

[orgs.default]
id = "org-123"
api_key_path = "/keys/api.json"
api_base_url = "https://api.turnkey.com"
default_operator_kind = "local"
future_org = 42

[[orgs.default.operators]]
name = "default"
kind = "local"
key_path = "/keys/operator.json"

[last_created_app_id]
default = "app-123"

[last_operator_ids]
default = ["operator-123"]
"#;

    #[tokio::test]
    async fn migrates_v0_in_memory_and_writes_v1_lazily() {
        let temp = TempDir::new().unwrap();
        let path = temp.path().join("tvc.config.toml");
        tokio::fs::write(&path, V0_CONFIG).await.unwrap();

        let config = Config::load_from_path(&path).await.unwrap();
        let original = tokio::fs::read_to_string(&path).await.unwrap();
        assert_eq!(original, V0_CONFIG);

        let org = &config.orgs["default"];
        assert_eq!(org.default_operator_kind, OperatorKind::Local);
        assert_eq!(org.operators.len(), 1);
        assert_eq!(org.operators[0].name, DEFAULT_OPERATOR_NAME);
        let OperatorRecordKind::Local(local) = &org.operators[0].kind else {
            panic!("migrated operator must be local");
        };
        assert_eq!(local.key_path, PathBuf::from("/keys/operator.json"));

        config.save_to_path(&path).await.unwrap();
        let saved = tokio::fs::read_to_string(&path).await.unwrap();
        let actual = disk::from_toml(&saved).unwrap();
        let expected = disk::from_toml(MIGRATED_V1_CONFIG).unwrap();
        assert_eq!(actual, expected);
    }

    #[test]
    fn rejects_malformed_version() {
        let error = disk::from_toml("version = \"one\"").expect_err("malformed version must fail");
        assert!(
            error
                .to_string()
                .contains("config version must be an unsigned 16-bit integer")
        );
    }
}