nebulous 0.1.86

A globally distributed container orchestrator
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
use anyhow::Result;
use aws_config::{self, BehaviorVersion, Region, SdkConfig};
use aws_sdk_iam::Client as IamClient;
use aws_sdk_s3::config::{Credentials, Region as S3Region};
use aws_sdk_s3::Client as S3Client;
use aws_sdk_sts::primitives::DateTime;
use aws_sdk_sts::Client as StsClient;
use serde_json::json;
use tracing::{debug, error, info, warn};

pub struct S3ClientInternal {
    client: S3Client,
    bucket: String,
    base_path: String,
}

impl S3ClientInternal {
    pub fn new(
        access_key: &str,
        secret_key: &str,
        bucket: &str,
        namespace: &str,
        name: &str,
    ) -> Result<Self> {
        let credentials = Credentials::new(
            access_key,
            secret_key,
            None, // No session token needed for permanent credentials
            None, // No expiration
            "permanent-credentials",
        );

        let config = aws_sdk_s3::Config::builder()
            .region(Region::new("us-east-1")) // adjust as needed
            .credentials_provider(credentials)
            .build();

        let client = S3Client::from_conf(config);
        let base_path = format!("data/{}/{}", namespace, name);

        Ok(Self {
            client,
            bucket: bucket.to_string(),
            base_path,
        })
    }

    // Helper method to construct full path
    fn full_path(&self, key: &str) -> String {
        format!("{}/{}", self.base_path, key.trim_matches('/'))
    }

    // Example methods for S3 operations
    pub async fn put_object(&self, key: &str, data: Vec<u8>) -> Result<()> {
        self.client
            .put_object()
            .bucket(&self.bucket)
            .key(self.full_path(key))
            .body(data.into())
            .send()
            .await?;
        Ok(())
    }

    pub async fn get_object(&self, key: &str) -> Result<Vec<u8>> {
        let response = self
            .client
            .get_object()
            .bucket(&self.bucket)
            .key(self.full_path(key))
            .send()
            .await?;

        Ok(response.body.collect().await?.into_bytes().to_vec())
    }

    pub async fn list_objects(&self, prefix: Option<&str>) -> Result<Vec<String>> {
        let prefix = match prefix {
            Some(p) => format!("{}/{}", self.base_path, p.trim_matches('/')),
            None => self.base_path.clone(),
        };

        let response = self
            .client
            .list_objects_v2()
            .bucket(&self.bucket)
            .prefix(prefix)
            .send()
            .await?;

        let keys: Vec<String> = response
            .contents()
            .iter()
            .filter_map(|obj| obj.key.clone())
            .collect();

        Ok(keys)
    }
}

pub struct IamCredentials {
    pub access_key_id: String,
    pub secret_access_key: String,
    pub username: String,
}

pub struct StsCredentials {
    pub access_key_id: String,
    pub secret_access_key: String,
    pub session_token: String,
    pub expiration: Option<DateTime>,
}

pub async fn create_s3_scoped_user(
    bucket_name: &str,
    namespace: &str,
    name: &str,
) -> Result<IamCredentials> {
    let config = aws_config::defaults(BehaviorVersion::latest())
        .region(Region::new("us-east-1"))
        .load()
        .await;
    let client = IamClient::new(&config);

    // Create a unique username
    let username = format!("s3-scoped-{}-{}", namespace, name);

    // Create the IAM user
    client.create_user().user_name(&username).send().await?;

    let policy_document = json!({
      "Version": "2012-10-17",
      "Statement": [
        // -- 1) Allow listing objects only under data/<namespace> prefix
        {
          "Effect": "Allow",
          "Action": "s3:ListBucket",
          "Resource": [
            format!("arn:aws:s3:::{}", bucket_name)
          ],
          "Condition": {
            "StringLike": {
              "s3:prefix": [
                format!("data/{}/", namespace),
                format!("data/{}/*", namespace)
              ]
            }
          }
        },
        // -- 2) Allow working with objects under data/<namespace> prefix
        {
          "Effect": "Allow",
          "Action": [
            "s3:*"
          ],
          "Resource": [
            format!("arn:aws:s3:::{}/data/{}", bucket_name, namespace),
            format!("arn:aws:s3:::{}/data/{}/*", bucket_name, namespace)
          ]
        }
      ]
    });

    debug!(">>> Policy document: {}", policy_document);

    // Create the policy
    let policy_name = format!("s3-scope-{}-{}", namespace, name);
    let policy_response = client
        .create_policy()
        .policy_name(&policy_name)
        .policy_document(policy_document.to_string())
        .send()
        .await?;

    // Attach the policy to the user
    client
        .attach_user_policy()
        .user_name(&username)
        .policy_arn(policy_response.policy().unwrap().arn().unwrap())
        .send()
        .await?;

    // Create access key for the user
    let key_response = client
        .create_access_key()
        .user_name(&username)
        .send()
        .await?;

    let access_key = key_response.access_key().unwrap();

    Ok(IamCredentials {
        access_key_id: access_key.access_key_id().to_string(),
        secret_access_key: access_key.secret_access_key().to_string(),
        username,
    })
}

pub async fn delete_s3_scoped_user(namespace: &str, name: &str) -> Result<()> {
    let config = aws_config::defaults(BehaviorVersion::latest())
        .region(Region::new("us-east-1"))
        .load()
        .await;
    let client = IamClient::new(&config);

    let username = format!("s3-scoped-{}-{}", namespace, name);
    // let policy_name = format!("s3-scope-{}-{}", namespace, name); // Keep for reference, maybe needed if ARN lookup fails

    // --- 1. Delete Access Keys ---
    debug!("Attempting to delete access keys for user: {}", username);
    match client.list_access_keys().user_name(&username).send().await {
        Ok(keys_output) => {
            for key_metadata in keys_output.access_key_metadata() {
                if let Some(key_id) = key_metadata.access_key_id() {
                    debug!("Deleting access key {} for user {}", key_id, username);
                    match client
                        .delete_access_key()
                        .user_name(&username)
                        .access_key_id(key_id)
                        .send()
                        .await
                    {
                        Ok(_) => debug!("Successfully deleted access key {}", key_id),
                        Err(e) => {
                            // Check if it's a NoSuchEntity error (key already gone)
                            if let Some(aws_err) = e.as_service_error() {
                                if aws_err.is_no_such_entity_exception() {
                                    warn!("Access key {} not found for user {}, likely already deleted.", key_id, username);
                                } else {
                                    error!("Failed to delete access key {}: {}", key_id, e);
                                    // Decide if this should be fatal. Continuing for now.
                                }
                            } else {
                                error!("Failed to delete access key {}: {}", key_id, e);
                            }
                        }
                    }
                } else {
                    warn!(
                        "Found access key metadata without an ID for user {}",
                        username
                    );
                }
            }
        }
        Err(e) => {
            // Check if the user doesn't exist
            if let Some(aws_err) = e.as_service_error() {
                if aws_err.is_no_such_entity_exception() {
                    warn!(
                        "User {} not found when listing access keys, assuming already deleted.",
                        username
                    );
                    // If user doesn't exist, we can potentially stop here or attempt policy cleanup if ARN is known/constructible
                    return Ok(()); // Assuming successful deletion if user gone
                } else {
                    error!("Failed to list access keys for user {}: {}", username, e);
                    return Err(e.into());
                }
            } else {
                error!("Failed to list access keys for user {}: {}", username, e);
                return Err(e.into());
            }
        }
    }

    // --- 2. Detach Managed Policy ---
    debug!(
        "Attempting to detach managed policies for user: {}",
        username
    );
    let mut policy_arn_to_delete: Option<String> = None;
    match client
        .list_attached_user_policies()
        .user_name(&username)
        .send()
        .await
    {
        Ok(attached_policies_output) => {
            if let Some(policies) = attached_policies_output.attached_policies {
                // Assuming our function attaches only one specific policy
                if let Some(policy) = policies.first() {
                    // Take the first, assuming it's ours
                    if let Some(arn) = policy.policy_arn() {
                        debug!("Found attached policy {} for user {}", arn, username);
                        policy_arn_to_delete = Some(arn.to_string());
                        debug!("Detaching policy {} from user {}", arn, username);
                        match client
                            .detach_user_policy()
                            .user_name(&username)
                            .policy_arn(arn)
                            .send()
                            .await
                        {
                            Ok(_) => debug!("Successfully detached policy {}", arn),
                            Err(e) => {
                                if let Some(aws_err) = e.as_service_error() {
                                    if aws_err.is_no_such_entity_exception() {
                                        warn!("Policy {} or User {} not found during detachment, proceeding.", arn, username);
                                    } else {
                                        error!(
                                            "Failed to detach policy {} from user {}: {}",
                                            arn, username, e
                                        );
                                        // Decide if fatal. Continuing for now.
                                    }
                                } else {
                                    error!(
                                        "Failed to detach policy {} from user {}: {}",
                                        arn, username, e
                                    );
                                }
                            }
                        }
                    } else {
                        warn!(
                            "Attached policy found for user {} but ARN is missing.",
                            username
                        );
                    }
                } else {
                    debug!("No attached policies found for user {}", username);
                }
            } else {
                debug!("No attached policies found for user {}", username);
            }
        }
        Err(e) => {
            if let Some(aws_err) = e.as_service_error() {
                if aws_err.is_no_such_entity_exception() {
                    warn!(
                        "User {} not found when listing attached policies.",
                        username
                    );
                    // If user doesn't exist, policy can't be attached.
                } else {
                    error!(
                        "Failed to list attached policies for user {}: {}",
                        username, e
                    );
                    return Err(e.into());
                }
            } else {
                error!(
                    "Failed to list attached policies for user {}: {}",
                    username, e
                );
                return Err(e.into());
            }
        }
    }

    // --- 3. Delete the Managed Policy ---
    if let Some(ref arn) = policy_arn_to_delete {
        debug!("Attempting to delete policy: {}", arn);
        match client.delete_policy().policy_arn(arn).send().await {
            Ok(_) => debug!("Successfully deleted policy {}", arn),
            Err(e) => {
                if let Some(aws_err) = e.as_service_error() {
                    if aws_err.is_no_such_entity_exception() {
                        warn!(
                            "Policy {} not found during deletion, likely already deleted.",
                            arn
                        );
                    } else if aws_err.is_delete_conflict_exception() {
                        error!("Failed to delete policy {} due to conflict (maybe still attached?): {}", arn, e);
                        // This might be fatal if the policy should have been detached.
                        // Consider returning an error here. For now, just logging.
                    } else {
                        error!("Failed to delete policy {}: {}", arn, e);
                        // Decide if fatal. Continuing for now.
                    }
                } else {
                    error!("Failed to delete policy {}: {}", arn, e);
                }
            }
        }
    } else {
        debug!(
            "Skipping policy deletion as no attached policy ARN was found for user {}.",
            username
        );
    }

    // --- 4. Delete the User ---
    debug!("Attempting to delete user: {}", username);
    match client.delete_user().user_name(&username).send().await {
        Ok(_) => debug!("Successfully deleted user {}", username),
        Err(e) => {
            if let Some(aws_err) = e.as_service_error() {
                if aws_err.is_no_such_entity_exception() {
                    warn!(
                        "User {} not found during deletion, assuming already deleted.",
                        username
                    );
                } else if aws_err.is_delete_conflict_exception() {
                    error!("Failed to delete user {} due to conflict (resources might still be attached): {}", username, e);
                    // This indicates a problem with the previous cleanup steps. Return error.
                    return Err(e.into());
                } else {
                    error!("Failed to delete user {}: {}", username, e);
                    return Err(e.into());
                }
            } else {
                error!("Failed to delete user {}: {}", username, e);
                return Err(e.into());
            }
        }
    }

    info!(
        "Successfully deleted S3 scoped user and associated resources for {}",
        username
    );
    Ok(())
}

/// Generate temporary AWS credentials with a specific S3 path restriction using federation tokens.
/// This approach uses STS GetFederationToken with an inline policy for proper restrictions.
pub async fn generate_temporary_s3_credentials(
    bucket_name: &str,
    namespace: &str,
    duration_seconds: i32,
) -> Result<StsCredentials> {
    let config = aws_config::defaults(BehaviorVersion::latest())
        .region(Region::new("us-east-1"))
        .load()
        .await;
    let sts_client = StsClient::new(&config);

    // Create a friendly name for the federated session
    let federated_user_name = format!("nebulous-{}-session", namespace);
    // Ensure name meets requirements (alphanumeric + [,_.=@-], length <=32)
    let federated_user_name: String = federated_user_name
        .chars()
        .filter(|c| c.is_alphanumeric() || [',', '.', '_', '=', '@', '-'].contains(c))
        .take(32)
        .collect();

    // Define the inline policy document restricting access to namespace path
    let policy_document = json!({
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "s3:ListBucket"
                ],
                "Resource": [
                    format!("arn:aws:s3:::{}", bucket_name)
                ],
                "Condition": {
                    "StringLike": {
                        "s3:prefix": [
                            format!("data/{}/", namespace),
                            format!("data/{}/*", namespace)
                        ]
                    }
                }
            },
            {
                "Effect": "Allow",
                "Action": [
                    "s3:GetObject",
                    "s3:PutObject",
                    "s3:DeleteObject"
                ],
                "Resource": [
                    format!("arn:aws:s3:::{}/data/{}/*", bucket_name, namespace)
                ]
            }
        ]
    });

    let policy_string = policy_document.to_string();
    debug!("Federation Token Policy: {}", policy_string);

    // Request federation token with policy restrictions
    debug!("Requesting federation token for: {}", federated_user_name);

    let federation_token_output = sts_client
        .get_federation_token()
        .name(&federated_user_name)
        .policy(policy_string)
        .duration_seconds(duration_seconds)
        .send()
        .await?;

    match federation_token_output.credentials() {
        Some(creds) => {
            info!("Successfully obtained policy-restricted federation token credentials for namespace {}", namespace);

            if let Some(federated_user) = federation_token_output.federated_user() {
                debug!("Federated User ARN: {}", federated_user.arn());
            }

            Ok(StsCredentials {
                access_key_id: creds.access_key_id().to_string(),
                secret_access_key: creds.secret_access_key().to_string(),
                session_token: creds.session_token().to_string(),
                expiration: Some(creds.expiration().clone()),
            })
        }
        None => {
            error!("GetFederationToken succeeded but returned no credentials");
            Err(anyhow::anyhow!(
                "GetFederationToken returned no credentials"
            ))
        }
    }
}