tusktsk 2.1.3

🦀 TuskTsk Enhanced - Ultra-fast Rust configuration parser with maximum syntax flexibility
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
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use k8s_openapi::api::core::v1::Secret;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
use kube::{Api, Client, ResourceExt};
use anyhow::{Result, Context};
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;

use crate::k8s::crd::{TuskLangApp, SecretConfig, SecretGenerationPolicy};

/// Secret manager for TuskLang applications
pub struct SecretManager {
    client: Client,
    namespace: String,
    secrets: Arc<RwLock<HashMap<String, SecretInfo>>>,
}

/// Information about a managed secret
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SecretInfo {
    /// Secret name
    pub name: String,
    /// Namespace
    pub namespace: String,
    /// Secret type
    pub secret_type: String,
    /// Last rotation time
    pub last_rotation: DateTime<Utc>,
    /// Rotation interval in seconds
    pub rotation_interval: u64,
    /// Whether the secret is healthy
    pub healthy: bool,
    /// Error message if unhealthy
    pub error_message: Option<String>,
    /// Number of keys in the secret
    pub key_count: usize,
}

/// Secret rotation result
#[derive(Debug)]
pub struct SecretRotationResult {
    /// Whether the secret was created
    pub created: bool,
    /// Whether the secret was rotated
    pub rotated: bool,
    /// Secret name
    pub name: String,
    /// Number of keys rotated
    pub keys_rotated: usize,
    /// Error if any
    pub error: Option<String>,
}

impl SecretManager {
    /// Create a new secret manager
    pub async fn new(client: Client, namespace: String) -> Result<Self> {
        Ok(Self {
            client,
            namespace,
            secrets: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Create or rotate secrets for a TuskLang application
    pub async fn reconcile_secrets(&self, app: &TuskLangApp) -> Result<Vec<SecretRotationResult>> {
        let mut results = Vec::new();
        let api: Api<Secret> = Api::namespaced(self.client.clone(), &self.namespace);

        if !app.spec.secrets.enable_rotation {
            return Ok(results);
        }

        for secret_name in &app.spec.secrets.secrets {
            let result = self.create_or_rotate_secret(&api, app, secret_name).await;
            results.push(result);
        }

        Ok(results)
    }

    /// Create or rotate a single secret
    async fn create_or_rotate_secret(
        &self,
        api: &Api<Secret>,
        app: &TuskLangApp,
        secret_name: &str,
    ) -> SecretRotationResult {
        // Check if secret exists
        match api.get(secret_name).await {
            Ok(existing_secret) => {
                // Check if rotation is needed
                if self.needs_rotation(&existing_secret, &app.spec.secrets).await {
                    match self.rotate_secret(api, &existing_secret, app, secret_name).await {
                        Ok(keys_rotated) => {
                            self.update_secret_info(secret_name, app.spec.secrets.rotation_interval).await;
                            SecretRotationResult {
                                created: false,
                                rotated: true,
                                name: secret_name.to_string(),
                                keys_rotated,
                                error: None,
                            }
                        }
                        Err(e) => SecretRotationResult {
                            created: false,
                            rotated: false,
                            name: secret_name.to_string(),
                            keys_rotated: 0,
                            error: Some(e.to_string()),
                        },
                    }
                } else {
                    SecretRotationResult {
                        created: false,
                        rotated: false,
                        name: secret_name.to_string(),
                        keys_rotated: 0,
                        error: None,
                    }
                }
            }
            Err(_) => {
                // Create new secret
                match self.create_secret(api, app, secret_name).await {
                    Ok(keys_created) => {
                        self.update_secret_info(secret_name, app.spec.secrets.rotation_interval).await;
                        SecretRotationResult {
                            created: true,
                            rotated: false,
                            name: secret_name.to_string(),
                            keys_rotated: keys_created,
                            error: None,
                        }
                    }
                    Err(e) => SecretRotationResult {
                        created: false,
                        rotated: false,
                        name: secret_name.to_string(),
                        keys_rotated: 0,
                        error: Some(e.to_string()),
                    },
                }
            }
        }
    }

    /// Create a new secret
    async fn create_secret(
        &self,
        api: &Api<Secret>,
        app: &TuskLangApp,
        secret_name: &str,
    ) -> Result<usize> {
        let mut data = HashMap::new();
        let policy = &app.spec.secrets.generation_policy;

        // Generate default keys
        let default_keys = vec!["api_key", "database_password", "jwt_secret", "encryption_key"];
        let mut keys_created = 0;

        for key in default_keys {
            let value = self.generate_secret_value(policy);
            data.insert(key.to_string(), value.into_bytes());
            keys_created += 1;
        }

        let secret = Secret {
            metadata: ObjectMeta {
                name: Some(secret_name.to_string()),
                namespace: Some(self.namespace.clone()),
                labels: Some(HashMap::from([
                    ("app".to_string(), app.metadata.name.as_ref().unwrap().clone()),
                    ("managed-by".to_string(), "tusklang-operator".to_string()),
                ])),
                annotations: Some(HashMap::from([
                    ("tusklang.io/rotation-interval".to_string(), app.spec.secrets.rotation_interval.to_string()),
                    ("tusklang.io/created-at".to_string(), Utc::now().to_rfc3339()),
                    ("tusklang.io/last-rotation".to_string(), Utc::now().to_rfc3339()),
                ])),
                ..Default::default()
            },
            data: Some(data),
            type_: Some("Opaque".to_string()),
            ..Default::default()
        };

        api.create(&Default::default(), &secret).await
            .context("Failed to create secret")?;

        Ok(keys_created)
    }

    /// Rotate an existing secret
    async fn rotate_secret(
        &self,
        api: &Api<Secret>,
        existing_secret: &Secret,
        app: &TuskLangApp,
        secret_name: &str,
    ) -> Result<usize> {
        let mut updated_secret = existing_secret.clone();
        let policy = &app.spec.secrets.generation_policy;
        let mut keys_rotated = 0;

        if let Some(ref mut data) = updated_secret.data {
            for (key, _) in data.iter_mut() {
                let new_value = self.generate_secret_value(policy);
                *data.get_mut(key).unwrap() = new_value.into_bytes();
                keys_rotated += 1;
            }
        }

        // Update annotations
        if let Some(ref mut annotations) = updated_secret.metadata.annotations {
            annotations.insert("tusklang.io/last-rotation".to_string(), Utc::now().to_rfc3339());
            annotations.insert("tusklang.io/rotated-at".to_string(), Utc::now().to_rfc3339());
        }

        api.replace(secret_name, &Default::default(), &updated_secret).await
            .context("Failed to rotate secret")?;

        Ok(keys_rotated)
    }

    /// Check if secret needs rotation
    async fn needs_rotation(&self, secret: &Secret, config: &SecretConfig) -> bool {
        if let Some(annotations) = &secret.metadata.annotations {
            if let Some(last_rotation_str) = annotations.get("tusklang.io/last-rotation") {
                if let Ok(last_rotation) = DateTime::parse_from_rfc3339(last_rotation_str) {
                    let now = Utc::now();
                    let time_since_rotation = now.signed_duration_since(last_rotation.with_timezone(&Utc));
                    return time_since_rotation.num_seconds() as u64 >= config.rotation_interval;
                }
            }
        }
        true // Default to rotation if we can't determine last rotation time
    }

    /// Generate a secret value based on policy
    fn generate_secret_value(&self, policy: &SecretGenerationPolicy) -> String {
        let mut rng = thread_rng();
        let mut charset = policy.charset.clone();

        if policy.include_special {
            charset.push_str("!@#$%^&*()_+-=[]{}|;:,.<>?");
        }

        let value: String = (0..policy.length)
            .map(|_| {
                let idx = rng.gen_range(0..charset.len());
                charset.chars().nth(idx).unwrap()
            })
            .collect();

        value
    }

    /// Update secret information in memory
    async fn update_secret_info(&self, name: &str, rotation_interval: u64) {
        let mut secrets = self.secrets.write().await;
        secrets.insert(name.to_string(), SecretInfo {
            name: name.to_string(),
            namespace: self.namespace.clone(),
            secret_type: "Opaque".to_string(),
            last_rotation: Utc::now(),
            rotation_interval,
            healthy: true,
            error_message: None,
            key_count: 0, // Will be updated when we fetch the secret
        });
    }

    /// Get secret information
    pub async fn get_secret_info(&self, name: &str) -> Option<SecretInfo> {
        let secrets = self.secrets.read().await;
        secrets.get(name).cloned()
    }

    /// List all managed secrets
    pub async fn list_secrets(&self) -> Vec<SecretInfo> {
        let secrets = self.secrets.read().await;
        secrets.values().cloned().collect()
    }

    /// Delete a secret
    pub async fn delete_secret(&self, name: &str) -> Result<()> {
        let api: Api<Secret> = Api::namespaced(self.client.clone(), &self.namespace);
        
        api.delete(name, &Default::default()).await
            .context("Failed to delete secret")?;

        // Remove from memory
        let mut secrets = self.secrets.write().await;
        secrets.remove(name);

        Ok(())
    }

    /// Clean up secrets for a deleted application
    pub async fn cleanup_application_secrets(&self, app_name: &str) -> Result<()> {
        let api: Api<Secret> = Api::namespaced(self.client.clone(), &self.namespace);
        
        // List secrets with app label
        let secrets = api.list(&Default::default()).await
            .context("Failed to list secrets")?;

        for secret in secrets {
            if let Some(labels) = &secret.metadata.labels {
                if labels.get("app") == Some(app_name) {
                    if let Some(name) = &secret.metadata.name {
                        self.delete_secret(name).await?;
                    }
                }
            }
        }

        Ok(())
    }

    /// Validate secret health
    pub async fn validate_secret_health(&self, name: &str) -> Result<bool> {
        let api: Api<Secret> = Api::namespaced(self.client.clone(), &self.namespace);
        
        match api.get(name).await {
            Ok(secret) => {
                // Check if secret has data
                if let Some(data) = &secret.data {
                    if !data.is_empty() {
                        // Update health status and key count
                        let mut secrets = self.secrets.write().await;
                        if let Some(info) = secrets.get_mut(name) {
                            info.healthy = true;
                            info.error_message = None;
                            info.key_count = data.len();
                        }
                        Ok(true)
                    } else {
                        self.mark_secret_unhealthy(name, "No data found").await;
                        Ok(false)
                    }
                } else {
                    self.mark_secret_unhealthy(name, "No data found").await;
                    Ok(false)
                }
            }
            Err(e) => {
                self.mark_secret_unhealthy(name, &e.to_string()).await;
                Ok(false)
            }
        }
    }

    /// Mark secret as unhealthy
    async fn mark_secret_unhealthy(&self, name: &str, error: &str) {
        let mut secrets = self.secrets.write().await;
        if let Some(info) = secrets.get_mut(name) {
            info.healthy = false;
            info.error_message = Some(error.to_string());
        }
    }

    /// Get secret statistics
    pub async fn get_statistics(&self) -> SecretStatistics {
        let secrets = self.secrets.read().await;
        let total = secrets.len();
        let healthy = secrets.values().filter(|info| info.healthy).count();
        let unhealthy = total - healthy;
        let total_keys: usize = secrets.values().map(|info| info.key_count).sum();

        SecretStatistics {
            total,
            healthy,
            unhealthy,
            total_keys,
            last_update: Utc::now(),
        }
    }

    /// Force rotation of a secret
    pub async fn force_rotate_secret(&self, name: &str, app: &TuskLangApp) -> Result<SecretRotationResult> {
        let api: Api<Secret> = Api::namespaced(self.client.clone(), &self.namespace);
        
        match api.get(name).await {
            Ok(existing_secret) => {
                match self.rotate_secret(&api, &existing_secret, app, name).await {
                    Ok(keys_rotated) => {
                        self.update_secret_info(name, app.spec.secrets.rotation_interval).await;
                        Ok(SecretRotationResult {
                            created: false,
                            rotated: true,
                            name: name.to_string(),
                            keys_rotated,
                            error: None,
                        })
                    }
                    Err(e) => Ok(SecretRotationResult {
                        created: false,
                        rotated: false,
                        name: name.to_string(),
                        keys_rotated: 0,
                        error: Some(e.to_string()),
                    }),
                }
            }
            Err(e) => Ok(SecretRotationResult {
                created: false,
                rotated: false,
                name: name.to_string(),
                keys_rotated: 0,
                error: Some(e.to_string()),
            }),
        }
    }
}

/// Secret statistics
#[derive(Debug, Serialize, Deserialize)]
pub struct SecretStatistics {
    /// Total number of secrets
    pub total: usize,
    /// Number of healthy secrets
    pub healthy: usize,
    /// Number of unhealthy secrets
    pub unhealthy: usize,
    /// Total number of keys across all secrets
    pub total_keys: usize,
    /// Last update time
    pub last_update: DateTime<Utc>,
}

impl SecretInfo {
    /// Check if secret needs rotation based on interval
    pub fn needs_rotation(&self) -> bool {
        let now = Utc::now();
        let time_since_rotation = now.signed_duration_since(self.last_rotation);
        time_since_rotation.num_seconds() as u64 >= self.rotation_interval
    }

    /// Get time until next rotation
    pub fn time_until_rotation(&self) -> std::time::Duration {
        let now = Utc::now();
        let time_since_rotation = now.signed_duration_since(self.last_rotation);
        let seconds_until_rotation = self.rotation_interval.saturating_sub(time_since_rotation.num_seconds() as u64);
        std::time::Duration::from_secs(seconds_until_rotation)
    }
}