mise 2026.8.3

Dev tools, env vars, and tasks in one CLI
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
use std::collections::BTreeSet;
use std::path::Path;
use std::sync::{Arc, Mutex};

use demand::Input;
use eyre::{Result, bail};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use tera::{Kwargs, State, TeraResult, Value};

use crate::config::Config;
use crate::env_diff::EnvMap;
use crate::tera::{BASE_CONTEXT, get_tera_v2, render_str_v2};

#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum SecretTomlConfig {
    Env(String),
    Options(SecretOptionsTomlConfig),
}

#[derive(Clone, Debug, Deserialize)]
pub struct SecretOptionsTomlConfig {
    pub env: String,
    pub description: Option<String>,
    #[serde(default)]
    pub allow_empty: bool,
}

#[derive(Clone, Debug)]
pub struct SecretDeclaration {
    pub name: String,
    pub env: String,
    pub description: Option<String>,
    pub allow_empty: bool,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SecretState {
    Available,
    Missing,
    Empty,
    InvalidUnicode,
}

impl std::fmt::Display for SecretState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Available => write!(f, "available"),
            Self::Missing => write!(f, "missing"),
            Self::Empty => write!(f, "empty"),
            Self::InvalidUnicode => write!(f, "invalid_unicode"),
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct SecretStatus {
    pub name: String,
    pub env: String,
    pub state: SecretState,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

#[derive(Debug, Default)]
struct SecretResolution {
    declarations: IndexMap<String, SecretDeclaration>,
    values: IndexMap<String, String>,
    redaction_env: EnvMap,
    unavailable: IndexMap<String, String>,
}

#[derive(Clone, Debug, Default)]
pub struct SecretValues {
    resolution: Arc<Mutex<SecretResolution>>,
    prompt: bool,
}

#[derive(Debug, thiserror::Error)]
#[error(
    "required bootstrap secrets are unavailable: {details}. Supply them in the environment (for example, `fnox exec -- mise bootstrap ...`) or pass --prompt-secrets"
)]
struct SecretUnavailable {
    details: String,
}

pub fn declarations_from_config(config: &Config) -> Result<Vec<SecretDeclaration>> {
    let mut merged = IndexMap::new();
    for cf in config.config_files.values() {
        if let Some(bootstrap) = cf.bootstrap_config() {
            for (name, declaration) in bootstrap.secrets {
                merged.entry(name).or_insert(declaration);
            }
        }
    }
    merged
        .into_iter()
        .map(|(name, declaration)| declaration_from_toml(name, declaration))
        .collect()
}

pub fn statuses(config: &Config) -> Result<Vec<SecretStatus>> {
    Ok(declarations_from_config(config)?
        .into_iter()
        .map(|declaration| SecretStatus {
            state: env_state(&declaration),
            name: declaration.name,
            env: declaration.env,
            description: declaration.description,
        })
        .collect())
}

pub fn resolve(config: &Config, prompt: bool) -> Result<SecretValues> {
    let declarations = declarations_from_config(config)?
        .into_iter()
        .map(|declaration| (declaration.name.clone(), declaration))
        .collect();
    Ok(SecretValues {
        resolution: Arc::new(Mutex::new(SecretResolution {
            declarations,
            ..Default::default()
        })),
        prompt,
    })
}

impl SecretValues {
    pub fn used_statuses(&self) -> Result<Vec<SecretStatus>> {
        let resolution = self
            .resolution
            .lock()
            .map_err(|_| eyre::eyre!("bootstrap secret resolver is unavailable"))?;
        Ok(resolution
            .declarations
            .values()
            .filter(|declaration| {
                resolution.values.contains_key(&declaration.name)
                    || resolution.unavailable.contains_key(&declaration.name)
            })
            .map(|declaration| SecretStatus {
                state: if resolution.values.contains_key(&declaration.name) {
                    SecretState::Available
                } else {
                    env_state(declaration)
                },
                name: declaration.name.clone(),
                env: declaration.env.clone(),
                description: declaration.description.clone(),
            })
            .collect())
    }

    pub fn render(
        &self,
        config: &Config,
        input: &str,
        base: &Path,
        target: &Path,
    ) -> Result<String> {
        self.render_inner(Some(config), input, base, target)
    }

    fn render_inner(
        &self,
        config: Option<&Config>,
        input: &str,
        base: &Path,
        target: &Path,
    ) -> Result<String> {
        let resolution = self.resolution.clone();
        let used = Arc::new(Mutex::new(BTreeSet::new()));
        let used_by_function = used.clone();
        let prompt = self.prompt;
        let mut tera = get_tera_v2(Some(base));
        tera.register_function(
            "secret",
            move |args: Kwargs, _: &State| -> TeraResult<Value> {
                let name = args.must_get::<&str>("name")?;
                used_by_function
                    .lock()
                    .map_err(|_| tera::Error::message("bootstrap secret resolver is unavailable"))?
                    .insert(name.to_string());
                let mut resolution = resolution.lock().map_err(|_| {
                    tera::Error::message("bootstrap secret resolver is unavailable")
                })?;
                if let Some(value) = resolution.values.get(name) {
                    return Ok(Value::from(value.as_str()));
                }
                if resolution.unavailable.contains_key(name) {
                    return Ok(Value::from(""));
                }
                let declaration = resolution.declarations.get(name).cloned().ok_or_else(|| {
                    tera::Error::message(format!(
                        "bootstrap secret '{name}' is not declared in [bootstrap.secrets]"
                    ))
                })?;
                match resolve_declaration(&declaration, prompt) {
                    Ok(value) => {
                        resolution
                            .redaction_env
                            .insert(declaration.env, value.clone());
                        resolution.values.insert(name.to_string(), value.clone());
                        Ok(Value::from(value))
                    }
                    Err(detail) => {
                        resolution.unavailable.insert(name.to_string(), detail);
                        Ok(Value::from(""))
                    }
                }
            },
        );
        let mut context = BASE_CONTEXT.clone();
        context.insert("config_root", base);
        context.insert("target", target);
        let rendered = render_str_v2(&mut tera, input, &context);
        let resolution = self
            .resolution
            .lock()
            .map_err(|_| eyre::eyre!("bootstrap secret resolver is unavailable"))?;
        if let Some(config) = config {
            config.add_redactions_excluding(
                resolution.redaction_env.keys().cloned(),
                &resolution.redaction_env,
                &BTreeSet::new(),
            );
        }
        let used = used
            .lock()
            .map_err(|_| eyre::eyre!("bootstrap secret resolver is unavailable"))?;
        let unavailable = used
            .iter()
            .filter_map(|name| resolution.unavailable.get(name))
            .cloned()
            .collect::<Vec<_>>();
        if !unavailable.is_empty() {
            return Err(SecretUnavailable {
                details: unavailable.join(", "),
            }
            .into());
        }
        rendered.map_err(Into::into)
    }

    #[cfg(test)]
    fn from_values(values: impl IntoIterator<Item = (String, String)>) -> Self {
        Self {
            resolution: Arc::new(Mutex::new(SecretResolution {
                values: values.into_iter().collect(),
                ..Default::default()
            })),
            prompt: false,
        }
    }
}

pub fn is_unavailable(error: &eyre::Report) -> bool {
    error
        .chain()
        .any(|cause| cause.downcast_ref::<SecretUnavailable>().is_some())
}

fn resolve_declaration(declaration: &SecretDeclaration, prompt: bool) -> Result<String, String> {
    let value = match std::env::var_os(&declaration.env) {
        Some(value) => match value.into_string() {
            Ok(value) if declaration.allow_empty || !value.is_empty() => Some(value),
            Ok(_) => None,
            Err(_) => {
                return Err(format!(
                    "{} ({}) contains non-Unicode data",
                    declaration.name, declaration.env
                ));
            }
        },
        None => None,
    };
    let value = match value {
        Some(value) => value,
        None if prompt => prompt_value(declaration).map_err(|error| error.to_string())?,
        None => return Err(format!("{} ({})", declaration.name, declaration.env)),
    };
    if value.is_empty() && !declaration.allow_empty {
        return Err(format!(
            "{} ({}) must not be empty",
            declaration.name, declaration.env
        ));
    }
    Ok(value)
}

fn declaration_from_toml(name: String, declaration: SecretTomlConfig) -> Result<SecretDeclaration> {
    if name.is_empty()
        || !name
            .chars()
            .all(|character| character.is_ascii_alphanumeric() || "_.-".contains(character))
    {
        bail!("invalid bootstrap secret name '{name}': use ASCII letters, digits, '.', '_' or '-'");
    }
    let (env, description, allow_empty) = match declaration {
        SecretTomlConfig::Env(env) => (env, None, false),
        SecretTomlConfig::Options(options) => {
            (options.env, options.description, options.allow_empty)
        }
    };
    if !valid_env_name(&env) {
        bail!("bootstrap secret '{name}' has invalid environment variable name '{env}'");
    }
    Ok(SecretDeclaration {
        name,
        env,
        description,
        allow_empty,
    })
}

fn valid_env_name(name: &str) -> bool {
    let mut characters = name.chars();
    characters
        .next()
        .is_some_and(|first| first == '_' || first.is_ascii_alphabetic())
        && characters.all(|character| character == '_' || character.is_ascii_alphanumeric())
}

fn env_state(declaration: &SecretDeclaration) -> SecretState {
    match std::env::var_os(&declaration.env) {
        None => SecretState::Missing,
        Some(value) => match value.into_string() {
            Err(_) => SecretState::InvalidUnicode,
            Ok(value) if value.is_empty() && !declaration.allow_empty => SecretState::Empty,
            Ok(_) => SecretState::Available,
        },
    }
}

fn prompt_value(declaration: &SecretDeclaration) -> Result<String> {
    if !console::user_attended_stderr() {
        bail!(
            "cannot prompt for bootstrap secret '{}' without an interactive terminal",
            declaration.name
        );
    }
    let prompt = declaration
        .description
        .clone()
        .unwrap_or_else(|| format!("Enter bootstrap secret {}", declaration.name));
    Ok(Input::new(&prompt)
        .password(true)
        .theme(&crate::ui::theme::get_theme())
        .run()?)
}

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

    #[test]
    fn validates_names() {
        assert!(valid_env_name("CACHE_TOKEN"));
        assert!(!valid_env_name("CACHE-TOKEN"));
        assert!(
            declaration_from_toml(
                "cache.token".to_string(),
                SecretTomlConfig::Env("CACHE_TOKEN".to_string()),
            )
            .is_ok()
        );
        assert!(
            declaration_from_toml(
                "cache token".to_string(),
                SecretTomlConfig::Env("CACHE_TOKEN".to_string()),
            )
            .is_err()
        );
    }

    #[test]
    fn renders_only_declared_secret_values() {
        let values = SecretValues::from_values([("token".to_string(), "sensitive".to_string())]);
        assert_eq!(
            values
                .render_inner(
                    None,
                    "token={{ secret(name=\"token\") }}",
                    Path::new("/tmp"),
                    Path::new("/etc/example"),
                )
                .unwrap(),
            "token=sensitive"
        );
        assert!(
            values
                .render_inner(
                    None,
                    "{{ secret(name=\"missing\") }}",
                    Path::new("/tmp"),
                    Path::new("/etc/example"),
                )
                .is_err()
        );
    }

    #[test]
    fn unavailable_secret_is_scoped_to_templates_that_use_it() {
        let name = "token".to_string();
        let values = SecretValues {
            resolution: Arc::new(Mutex::new(SecretResolution {
                declarations: [(
                    name.clone(),
                    SecretDeclaration {
                        name: name.clone(),
                        env: "MISE_TEST_UNAVAILABLE_SECRET".to_string(),
                        description: None,
                        allow_empty: false,
                    },
                )]
                .into_iter()
                .collect(),
                unavailable: [(name, "token is unavailable".to_string())]
                    .into_iter()
                    .collect(),
                ..Default::default()
            })),
            prompt: false,
        };
        assert_eq!(
            values
                .render_inner(None, "literal", Path::new("/tmp"), Path::new("/etc/static"))
                .unwrap(),
            "literal"
        );
        assert!(
            values
                .render_inner(
                    None,
                    "{{ secret(name=\"token\") }}",
                    Path::new("/tmp"),
                    Path::new("/etc/secret"),
                )
                .is_err()
        );
    }
}