skill-context 1.0.0

Execution context management for skill-engine
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
//! Secrets configuration types.
//!
//! This module defines secret management configuration and provider types
//! for execution contexts.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Secrets configuration.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct SecretsConfig {
    /// Individual secret definitions.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub secrets: HashMap<String, SecretDefinition>,

    /// Secret provider configuration.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub providers: Vec<SecretProviderConfig>,
}

impl SecretsConfig {
    /// Create a new empty secrets configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a secret definition.
    pub fn with_secret(mut self, key: impl Into<String>, definition: SecretDefinition) -> Self {
        self.secrets.insert(key.into(), definition);
        self
    }

    /// Add a required secret with environment variable injection.
    pub fn with_required_env_secret(
        mut self,
        key: impl Into<String>,
        env_var: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        let key = key.into();
        self.secrets.insert(
            key.clone(),
            SecretDefinition {
                key: key.clone(),
                description: Some(description.into()),
                required: true,
                provider: None,
                env_var: Some(env_var.into()),
                file_path: None,
                file_mode: None,
            },
        );
        self
    }

    /// Add a required secret that is written to a file.
    pub fn with_required_file_secret(
        mut self,
        key: impl Into<String>,
        file_path: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        let key = key.into();
        self.secrets.insert(
            key.clone(),
            SecretDefinition {
                key: key.clone(),
                description: Some(description.into()),
                required: true,
                provider: None,
                env_var: None,
                file_path: Some(file_path.into()),
                file_mode: Some("0600".to_string()),
            },
        );
        self
    }

    /// Add a secret provider configuration.
    pub fn with_provider(mut self, provider: SecretProviderConfig) -> Self {
        self.providers.push(provider);
        self
    }

    /// Get all secret keys.
    pub fn keys(&self) -> Vec<&str> {
        self.secrets.keys().map(|s| s.as_str()).collect()
    }

    /// Get all required secret keys.
    pub fn required_keys(&self) -> Vec<&str> {
        self.secrets
            .iter()
            .filter(|(_, def)| def.required)
            .map(|(k, _)| k.as_str())
            .collect()
    }

    /// Get all optional secret keys.
    pub fn optional_keys(&self) -> Vec<&str> {
        self.secrets
            .iter()
            .filter(|(_, def)| !def.required)
            .map(|(k, _)| k.as_str())
            .collect()
    }

    /// Get a secret definition by key.
    pub fn get(&self, key: &str) -> Option<&SecretDefinition> {
        self.secrets.get(key)
    }

    /// Check if a secret is defined.
    pub fn contains(&self, key: &str) -> bool {
        self.secrets.contains_key(key)
    }

    /// Get the number of secrets defined.
    pub fn len(&self) -> usize {
        self.secrets.len()
    }

    /// Check if there are no secrets defined.
    pub fn is_empty(&self) -> bool {
        self.secrets.is_empty()
    }
}

/// Definition of a single secret.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct SecretDefinition {
    /// Secret key name.
    pub key: String,

    /// Human-readable description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Whether this secret is required.
    #[serde(default)]
    pub required: bool,

    /// Provider to use (defaults to platform keychain).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,

    /// Environment variable name to inject as.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub env_var: Option<String>,

    /// File path to write secret to (for file-based secrets).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub file_path: Option<String>,

    /// File permissions (octal, e.g., "0600").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub file_mode: Option<String>,
}

impl SecretDefinition {
    /// Create a new required secret definition.
    pub fn required(key: impl Into<String>) -> Self {
        let key = key.into();
        Self {
            key: key.clone(),
            description: None,
            required: true,
            provider: None,
            env_var: None,
            file_path: None,
            file_mode: None,
        }
    }

    /// Create a new optional secret definition.
    pub fn optional(key: impl Into<String>) -> Self {
        let key = key.into();
        Self {
            key: key.clone(),
            description: None,
            required: false,
            provider: None,
            env_var: None,
            file_path: None,
            file_mode: None,
        }
    }

    /// Set the description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Set the provider.
    pub fn with_provider(mut self, provider: impl Into<String>) -> Self {
        self.provider = Some(provider.into());
        self
    }

    /// Set the environment variable to inject as.
    pub fn inject_as_env(mut self, env_var: impl Into<String>) -> Self {
        self.env_var = Some(env_var.into());
        self
    }

    /// Set the file path to write to.
    pub fn write_to_file(mut self, path: impl Into<String>) -> Self {
        self.file_path = Some(path.into());
        self
    }

    /// Set the file mode.
    pub fn with_file_mode(mut self, mode: impl Into<String>) -> Self {
        self.file_mode = Some(mode.into());
        self
    }

    /// Check if this secret should be injected as an environment variable.
    pub fn has_env_var(&self) -> bool {
        self.env_var.is_some()
    }

    /// Check if this secret should be written to a file.
    pub fn has_file_path(&self) -> bool {
        self.file_path.is_some()
    }

    /// Get the injection targets for this secret.
    pub fn injection_targets(&self) -> Vec<SecretInjectionTarget> {
        let mut targets = Vec::new();
        if let Some(ref env_var) = self.env_var {
            targets.push(SecretInjectionTarget::EnvVar(env_var.clone()));
        }
        if let Some(ref file_path) = self.file_path {
            targets.push(SecretInjectionTarget::File {
                path: file_path.clone(),
                mode: self.file_mode.clone(),
            });
        }
        targets
    }
}

/// Where a secret should be injected.
#[derive(Debug, Clone, PartialEq)]
pub enum SecretInjectionTarget {
    /// Inject as environment variable.
    EnvVar(String),
    /// Write to file.
    File {
        /// File path to write the secret to.
        path: String,
        /// File permissions (octal, e.g., "0600").
        mode: Option<String>,
    },
}

/// Configuration for a secret provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum SecretProviderConfig {
    /// Platform keychain (default).
    Keychain,

    /// Environment variable (for CI/CD).
    EnvironmentVariable {
        /// Prefix for environment variable names.
        prefix: String,
    },

    /// File-based secrets.
    File {
        /// Path to secrets file.
        path: String,
        /// File format.
        format: SecretFileFormat,
    },

    /// External secret manager.
    External {
        /// Provider type.
        provider_type: ExternalSecretProvider,
        /// Provider-specific configuration.
        #[serde(default)]
        config: HashMap<String, String>,
    },
}

impl SecretProviderConfig {
    /// Create a keychain provider config.
    pub fn keychain() -> Self {
        Self::Keychain
    }

    /// Create an environment variable provider config.
    pub fn environment_variable(prefix: impl Into<String>) -> Self {
        Self::EnvironmentVariable {
            prefix: prefix.into(),
        }
    }

    /// Create a file provider config.
    pub fn file(path: impl Into<String>, format: SecretFileFormat) -> Self {
        Self::File {
            path: path.into(),
            format,
        }
    }

    /// Create an external provider config.
    pub fn external(provider_type: ExternalSecretProvider) -> Self {
        Self::External {
            provider_type,
            config: HashMap::new(),
        }
    }

    /// Get the provider name.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Keychain => "keychain",
            Self::EnvironmentVariable { .. } => "environment",
            Self::File { .. } => "file",
            Self::External { provider_type, .. } => provider_type.name(),
        }
    }
}

/// External secret provider type.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ExternalSecretProvider {
    /// HashiCorp Vault.
    Vault,
    /// AWS Secrets Manager.
    AwsSecretsManager,
    /// Google Cloud Secret Manager.
    GcpSecretManager,
    /// Azure Key Vault.
    AzureKeyVault,
    /// 1Password CLI.
    OnePassword,
    /// Doppler.
    Doppler,
}

impl ExternalSecretProvider {
    /// Get the provider name.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Vault => "vault",
            Self::AwsSecretsManager => "aws-secrets-manager",
            Self::GcpSecretManager => "gcp-secret-manager",
            Self::AzureKeyVault => "azure-key-vault",
            Self::OnePassword => "1password",
            Self::Doppler => "doppler",
        }
    }

    /// Get a human-readable display name.
    pub fn display_name(&self) -> &'static str {
        match self {
            Self::Vault => "HashiCorp Vault",
            Self::AwsSecretsManager => "AWS Secrets Manager",
            Self::GcpSecretManager => "GCP Secret Manager",
            Self::AzureKeyVault => "Azure Key Vault",
            Self::OnePassword => "1Password",
            Self::Doppler => "Doppler",
        }
    }
}

/// Secret file format.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SecretFileFormat {
    /// KEY=value format.
    Env,
    /// JSON object.
    Json,
    /// YAML file.
    Yaml,
    /// Single secret per file (raw content).
    Raw,
}

impl SecretFileFormat {
    /// Get the file extension for this format.
    pub fn extension(&self) -> &'static str {
        match self {
            Self::Env => "env",
            Self::Json => "json",
            Self::Yaml => "yaml",
            Self::Raw => "txt",
        }
    }
}

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

    #[test]
    fn test_secrets_config_builder() {
        let config = SecretsConfig::new()
            .with_required_env_secret("api-key", "API_KEY", "API authentication key")
            .with_required_file_secret("db-password", "/run/secrets/db", "Database password");

        assert_eq!(config.secrets.len(), 2);
        assert!(config.secrets.get("api-key").unwrap().required);
        assert!(config.secrets.get("api-key").unwrap().env_var.is_some());
        assert!(config.secrets.get("db-password").unwrap().file_path.is_some());
    }

    #[test]
    fn test_secret_definition_builder() {
        let secret = SecretDefinition::required("api-key")
            .with_description("API key for authentication")
            .inject_as_env("API_KEY")
            .with_provider("keychain");

        assert!(secret.required);
        assert_eq!(secret.env_var, Some("API_KEY".to_string()));
        assert_eq!(secret.provider, Some("keychain".to_string()));
    }

    #[test]
    fn test_secret_injection_targets() {
        let secret = SecretDefinition::required("multi-target")
            .inject_as_env("SECRET_VAR")
            .write_to_file("/run/secrets/key")
            .with_file_mode("0400");

        let targets = secret.injection_targets();
        assert_eq!(targets.len(), 2);
        assert!(targets.contains(&SecretInjectionTarget::EnvVar("SECRET_VAR".to_string())));
        assert!(targets.contains(&SecretInjectionTarget::File {
            path: "/run/secrets/key".to_string(),
            mode: Some("0400".to_string()),
        }));
    }

    #[test]
    fn test_secret_provider_config() {
        let keychain = SecretProviderConfig::keychain();
        assert_eq!(keychain.name(), "keychain");

        let env = SecretProviderConfig::environment_variable("SECRET_");
        assert_eq!(env.name(), "environment");

        let file = SecretProviderConfig::file("/secrets.json", SecretFileFormat::Json);
        assert_eq!(file.name(), "file");

        let vault = SecretProviderConfig::external(ExternalSecretProvider::Vault);
        assert_eq!(vault.name(), "vault");
    }

    #[test]
    fn test_secrets_config_queries() {
        let config = SecretsConfig::new()
            .with_secret("required-key", SecretDefinition::required("required-key"))
            .with_secret("optional-key", SecretDefinition::optional("optional-key"));

        assert_eq!(config.required_keys(), vec!["required-key"]);
        assert_eq!(config.optional_keys(), vec!["optional-key"]);
        assert!(config.contains("required-key"));
        assert!(!config.contains("nonexistent"));
    }

    #[test]
    fn test_secrets_config_serialization() {
        let config = SecretsConfig::new()
            .with_required_env_secret("api-key", "API_KEY", "API key")
            .with_provider(SecretProviderConfig::keychain());

        let json = serde_json::to_string(&config).unwrap();
        let deserialized: SecretsConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(config.secrets.len(), deserialized.secrets.len());
        assert_eq!(config.providers.len(), deserialized.providers.len());
    }

    #[test]
    fn test_external_provider_names() {
        assert_eq!(ExternalSecretProvider::Vault.name(), "vault");
        assert_eq!(
            ExternalSecretProvider::AwsSecretsManager.display_name(),
            "AWS Secrets Manager"
        );
    }
}