secretspec 0.16.0

Declarative secrets, every environment, any provider
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
//! The shared codegen intermediate representation (IR).
//!
//! Every typed-accessor generator computes the *same* decisions from a manifest:
//! which secrets exist, whether a field is optional, whether it is a file path,
//! how profiles map to types. If each generator (the Rust derive macro and the
//! JSON Schema emitter that drives quicktype for other languages) recomputed
//! those decisions, they would drift. This module is the single brain: a
//! manifest is reduced to a language-neutral [`CodegenIr`] once, and each
//! emitter is a thin template over it.
//!
//! The IR deliberately mirrors the two shapes the derive crate exposes:
//! - a **union** field set (`SecretSpec`) safe to use without knowing the
//!   profile: a field is optional if it is optional in, or missing from, *any*
//!   profile, and a path if it is a path in *any* profile;
//! - **per-profile** field sets (`SecretSpecProfile`) matching the effective
//!   runtime profile, including fields inherited from `default`.
//!
//! Optionality describes successful output presence, not raw TOML spelling: an
//! optional secret is nullable, while required, defaulted, and generated secrets
//! are guaranteed to have a value when resolution succeeds.

use crate::config::Config;
use crate::manifest::{CompiledManifest, CompiledSecret};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// One field in a generated type. `name` is the canonical `UPPER_SNAKE` env key
/// and the source of truth; each emitter applies its own casing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IrField {
    /// The declared secret name (the `UPPER_SNAKE` manifest key).
    pub name: String,
    /// Whether the generated field is optional (nullable) rather than required.
    pub optional: bool,
    /// Whether the value is exposed as a file path rather than inline.
    pub as_path: bool,
    /// The secret's description, when one is declared.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// A profile and its effective field set.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IrProfile {
    /// The profile name as written in the manifest (e.g. `production`).
    pub name: String,
    /// The profile's fields, sorted by name for deterministic output.
    pub fields: Vec<IrField>,
}

/// The complete, language-neutral codegen description of a manifest.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CodegenIr {
    /// The project name.
    pub project: String,
    /// Profile names in sorted order; `["default"]` when the manifest declares
    /// none.
    pub profiles: Vec<String>,
    /// Union fields safe across every profile, sorted by name.
    pub union: Vec<IrField>,
    /// Per-profile exact field sets, in the same order as [`Self::profiles`].
    pub profile_fields: Vec<IrProfile>,
}

/// Build the union field set: every unique secret across all profiles, sorted.
///
/// Computed in a single pass over every `(profile, secret)` rather than
/// re-scanning all profiles per field. A union field is:
/// - optional if successful resolution may omit it in any profile (because it
///   is absent there or has the compiled `Omit` policy);
/// - a path if *any* profile declares it `as_path`;
/// - described by the first profile, in sorted name order, that declares a
///   description.
fn build_union(manifest: &CompiledManifest) -> Vec<IrField> {
    let total_profiles = manifest.profiles.len();
    struct Acc {
        /// Profiles where successful resolution guarantees the secret.
        guaranteed_count: usize,
        as_path: bool,
        description: Option<String>,
    }
    let mut acc: BTreeMap<String, Acc> = BTreeMap::new();

    for profile in manifest.profiles.values() {
        for (name, secret) in &profile.secrets {
            let entry = acc.entry(name.clone()).or_insert(Acc {
                guaranteed_count: 0,
                as_path: false,
                description: None,
            });
            if secret.missing.guaranteed_on_success() {
                entry.guaranteed_count += 1;
            }
            if secret.config.as_path == Some(true) {
                entry.as_path = true;
            }
            if entry.description.is_none() {
                entry.description = secret.config.description.clone();
            }
        }
    }

    acc.into_iter()
        .map(|(name, a)| IrField {
            name,
            optional: a.guaranteed_count != total_profiles,
            as_path: a.as_path,
            description: a.description,
        })
        .collect()
}

/// Capitalize the first character, leaving the rest unchanged. Shared by the
/// JSON Schema emitter (for `<Profile>Secrets` titles) and the derive macro (for
/// `SecretSpecProfile::<Variant>` names) so the two never disagree on casing.
pub fn capitalize(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
    }
}

/// Build one effective profile's field set, sorted by name.
fn build_profile_fields(secrets: &BTreeMap<String, CompiledSecret>) -> Vec<IrField> {
    secrets
        .iter()
        .map(|(name, secret)| IrField {
            name: name.clone(),
            optional: !secret.missing.guaranteed_on_success(),
            as_path: secret.config.as_path.unwrap_or(false),
            description: secret.config.description.clone(),
        })
        .collect()
}

/// Reduce a manifest to the language-neutral [`CodegenIr`] every emitter
/// consumes. This is the only place manifest typing decisions are made.
pub fn build_ir(config: &Config) -> CodegenIr {
    let manifest = CompiledManifest::compile(config);
    let union = build_union(&manifest);

    let profile_fields = if manifest.profiles.is_empty() {
        // No declared profiles: a single `default` profile carrying every field,
        // matching the derive macro's empty-profile case.
        vec![IrProfile {
            name: "default".to_string(),
            fields: union.clone(),
        }]
    } else {
        manifest
            .profiles
            .iter()
            .map(|(name, profile)| IrProfile {
                name: name.clone(),
                fields: build_profile_fields(&profile.secrets),
            })
            .collect()
    };

    let profiles = profile_fields.iter().map(|p| p.name.clone()).collect();

    CodegenIr {
        project: manifest.project,
        profiles,
        union,
        profile_fields,
    }
}

/// JSON Schema emitter.
///
/// Rather than hand-write typed accessors per language, we emit a JSON Schema
/// describing one manifest shape and let [quicktype](https://quicktype.io)
/// generate the idiomatic type and deserializer for any target language. We then
/// maintain only the small generic `fields()` helper in each runtime SDK, which
/// hands quicktype's deserializer a flat `{SECRET_NAME: value}` map.
///
/// The schema is a single-root object so quicktype emits a properly named type
/// with a converter in every language (a wrapper or `$ref` root makes quicktype
/// drop the converter or rename the type). By default it describes the union
/// `SecretSpec` (safe for any profile); with a profile it describes that
/// profile's exact fields. Pair it with `quicktype --top-level <Name>`.
pub mod schema {
    use super::{CodegenIr, IrField, capitalize};
    use serde_json::{Map, Value, json};

    fn property_type(field: &IrField) -> Value {
        // Every secret is a string; optional secrets are nullable. `as_path`
        // secrets are also strings (the file path), so they need no special type.
        if field.optional {
            json!({ "type": ["string", "null"] })
        } else {
            json!({ "type": "string" })
        }
    }

    fn object_schema(title: &str, fields: &[IrField], additional_properties: bool) -> Value {
        let mut properties = Map::new();
        let mut required = Vec::new();
        for field in fields {
            properties.insert(field.name.clone(), property_type(field));
            if !field.optional {
                required.push(Value::String(field.name.clone()));
            }
        }
        json!({
            "$schema": "http://json-schema.org/draft-06/schema#",
            "type": "object",
            "additionalProperties": additional_properties,
            "title": title,
            "properties": Value::Object(properties),
            "required": required,
        })
    }

    /// Emit the JSON Schema (draft-06, the dialect quicktype consumes) for the
    /// union (`profile = None`) or one profile's fields. Returns an error if the
    /// named profile does not exist.
    ///
    /// Both union and per-profile schemas are exhaustive. Per-profile IR already
    /// includes every effective field inherited from `default`.
    pub fn emit(ir: &CodegenIr, profile: Option<&str>) -> Result<String, String> {
        let schema = match profile {
            None => object_schema("SecretSpec", &ir.union, false),
            Some(name) => {
                let found = ir
                    .profile_fields
                    .iter()
                    .find(|p| p.name == name)
                    .ok_or_else(|| {
                        format!(
                            "unknown profile '{name}'; available: {}",
                            ir.profiles.join(", ")
                        )
                    })?;
                object_schema(
                    &format!("{}Secrets", capitalize(name)),
                    &found.fields,
                    false,
                )
            }
        };
        Ok(format!(
            "{}\n",
            serde_json::to_string_pretty(&schema).unwrap()
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{Profile, Project, Secret};
    use std::collections::HashMap;

    fn secret(required: Option<bool>, as_path: Option<bool>, desc: Option<&str>) -> Secret {
        Secret {
            description: desc.map(String::from),
            required,
            as_path,
            ..Default::default()
        }
    }

    fn config_with(profiles: Vec<(&str, Vec<(&str, Secret)>)>) -> Config {
        let mut map = HashMap::new();
        for (name, secrets) in profiles {
            let mut secret_map = HashMap::new();
            for (sname, s) in secrets {
                secret_map.insert(sname.to_string(), s);
            }
            map.insert(
                name.to_string(),
                Profile {
                    defaults: None,
                    secrets: secret_map,
                },
            );
        }
        Config {
            project: Project {
                name: "ir-test".to_string(),
                ..Default::default()
            },
            profiles: map,
            providers: None,
        }
    }

    fn union_field<'a>(ir: &'a CodegenIr, name: &str) -> &'a IrField {
        ir.union.iter().find(|f| f.name == name).unwrap()
    }

    #[test]
    fn union_optional_if_optional_or_missing_in_any_profile() {
        let ir = build_ir(&config_with(vec![
            (
                "development",
                vec![
                    ("DATABASE_URL", secret(Some(true), None, None)),
                    ("API_KEY", secret(Some(false), None, None)),
                ],
            ),
            (
                "production",
                vec![
                    ("DATABASE_URL", secret(Some(true), None, None)),
                    ("API_KEY", secret(Some(true), None, None)),
                    ("REDIS_URL", secret(Some(true), None, None)),
                ],
            ),
        ]));

        // Required in every profile it appears in -> required in the union.
        assert!(!union_field(&ir, "DATABASE_URL").optional);
        // Optional in development -> optional in the union.
        assert!(union_field(&ir, "API_KEY").optional);
        // Missing from development -> optional in the union.
        assert!(union_field(&ir, "REDIS_URL").optional);

        // Union is sorted and complete.
        let names: Vec<&str> = ir.union.iter().map(|f| f.name.as_str()).collect();
        assert_eq!(names, vec!["API_KEY", "DATABASE_URL", "REDIS_URL"]);
    }

    #[test]
    fn union_as_path_if_any_profile_marks_it() {
        let ir = build_ir(&config_with(vec![
            (
                "development",
                vec![("CERT", secret(Some(true), None, None))],
            ),
            (
                "production",
                vec![("CERT", secret(Some(true), Some(true), None))],
            ),
        ]));
        assert!(union_field(&ir, "CERT").as_path);
    }

    #[test]
    fn per_profile_fields_are_sorted_and_exact() {
        let ir = build_ir(&config_with(vec![
            (
                "development",
                vec![
                    ("DATABASE_URL", secret(Some(true), None, Some("dev db"))),
                    ("API_KEY", secret(Some(false), None, None)),
                ],
            ),
            (
                "production",
                vec![("DATABASE_URL", secret(Some(true), Some(true), None))],
            ),
        ]));

        assert_eq!(ir.profiles, vec!["development", "production"]);

        let dev = ir
            .profile_fields
            .iter()
            .find(|p| p.name == "development")
            .unwrap();
        let dev_names: Vec<&str> = dev.fields.iter().map(|f| f.name.as_str()).collect();
        assert_eq!(dev_names, vec!["API_KEY", "DATABASE_URL"]);
        // Description flows through per profile.
        assert_eq!(dev.fields[1].description.as_deref(), Some("dev db"));

        // production has only DATABASE_URL, here as a path.
        let prod = ir
            .profile_fields
            .iter()
            .find(|p| p.name == "production")
            .unwrap();
        assert_eq!(prod.fields.len(), 1);
        assert!(prod.fields[0].as_path);
        assert!(!prod.fields[0].optional);
    }

    #[test]
    fn unspecified_required_is_non_optional_matching_runtime() {
        let ir = build_ir(&config_with(vec![(
            "default",
            vec![("TOKEN", secret(None, None, None))],
        )]));
        assert!(!union_field(&ir, "TOKEN").optional);
    }

    #[test]
    fn defaulted_secret_is_non_optional_because_resolution_guarantees_a_value() {
        let mut token = secret(None, None, None);
        token.default = Some("fallback".to_string());

        let ir = build_ir(&config_with(vec![("default", vec![("TOKEN", token)])]));

        assert!(!union_field(&ir, "TOKEN").optional);
    }

    #[test]
    fn profile_fields_include_secrets_inherited_from_default() {
        let ir = build_ir(&config_with(vec![
            (
                "default",
                vec![("SHARED_TOKEN", secret(Some(true), None, None))],
            ),
            (
                "production",
                vec![("PRODUCTION_TOKEN", secret(Some(true), None, None))],
            ),
        ]));

        let production = ir
            .profile_fields
            .iter()
            .find(|profile| profile.name == "production")
            .unwrap();
        let names: Vec<&str> = production
            .fields
            .iter()
            .map(|field| field.name.as_str())
            .collect();
        assert_eq!(names, vec!["PRODUCTION_TOKEN", "SHARED_TOKEN"]);

        let schema: serde_json::Value =
            serde_json::from_str(&schema::emit(&ir, Some("production")).unwrap()).unwrap();
        assert!(schema["properties"]["SHARED_TOKEN"].is_object());
        assert_eq!(schema["additionalProperties"], false);
    }

    #[test]
    fn schema_emits_types_and_nullability_for_quicktype() {
        let ir = build_ir(&config_with(vec![
            (
                "development",
                vec![
                    ("DATABASE_URL", secret(Some(true), None, None)),
                    ("API_KEY", secret(Some(false), None, None)),
                ],
            ),
            (
                "production",
                vec![("DATABASE_URL", secret(Some(true), None, None))],
            ),
        ]));

        // Union schema: single-root object titled SecretSpec.
        let union: serde_json::Value =
            serde_json::from_str(&schema::emit(&ir, None).unwrap()).unwrap();
        assert_eq!(union["type"], "object");
        assert_eq!(union["title"], "SecretSpec");
        // The union is exhaustive across every profile, so it is strict.
        assert_eq!(union["additionalProperties"], false);

        // Required vs nullable: DATABASE_URL required everywhere; API_KEY optional
        // in development, so optional in the union and nullable in the schema.
        assert_eq!(union["properties"]["DATABASE_URL"]["type"], "string");
        assert_eq!(
            union["properties"]["API_KEY"]["type"],
            serde_json::json!(["string", "null"])
        );
        let required: Vec<&str> = union["required"]
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_str().unwrap())
            .collect();
        assert!(required.contains(&"DATABASE_URL"));
        assert!(!required.contains(&"API_KEY"));

        // A profile schema is titled <Profile>Secrets with that profile's fields.
        let prod: serde_json::Value =
            serde_json::from_str(&schema::emit(&ir, Some("production")).unwrap()).unwrap();
        assert_eq!(prod["title"], "ProductionSecrets");
        assert!(prod["properties"]["DATABASE_URL"].is_object());
        assert!(prod["properties"]["API_KEY"].is_null()); // not in production
        // Effective per-profile schemas are exhaustive too.
        assert_eq!(prod["additionalProperties"], false);

        // An unknown profile is an error.
        assert!(schema::emit(&ir, Some("nope")).is_err());
    }

    #[test]
    fn empty_profiles_yield_single_default_with_union_fields() {
        let mut config = config_with(vec![]);
        config.profiles.clear();
        let ir = build_ir(&config);
        assert_eq!(ir.profiles, vec!["default"]);
        assert_eq!(ir.profile_fields.len(), 1);
        assert_eq!(ir.profile_fields[0].name, "default");
        assert!(ir.union.is_empty());
    }
}