awsim-iam 0.2.0

AWS IAM emulator for AWSim
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/// Miscellaneous IAM operations that are stubs or return empty data.
use std::collections::HashMap;

use awsim_core::AwsError;
use awsim_iam_policy::{
    AuthzRequest, ContextValue, Decision, EvalContext, PolicyDocument, evaluate,
};
use serde_json::{Value, json};

use crate::{
    error::{malformed_policy_document, no_such_entity},
    ids::{new_access_key_id, new_secret_access_key, now_iso8601},
    state::{IamState, ServiceSpecificCredential, SigningCertificate},
};

use super::{opt_str, require_str};

// ── ListServiceSpecificCredentials ────────────────────────────────────────────

/// ListServiceSpecificCredentials — Return credentials owned by user.
pub fn list_service_specific_credentials(
    state: &IamState,
    input: &Value,
) -> Result<Value, AwsError> {
    let user_name = input.get("UserName").and_then(|v| v.as_str());
    if let Some(name) = user_name
        && !state.users.contains_key(name)
    {
        return Err(crate::error::no_such_entity("User", name));
    }
    let service_filter = opt_str(input, "ServiceName");

    let creds: Vec<Value> = state
        .service_specific_credentials
        .iter()
        .filter(|c| {
            user_name.map(|u| c.value().user_name == u).unwrap_or(true)
                && service_filter
                    .map(|s| c.value().service_name == s)
                    .unwrap_or(true)
        })
        .map(|c| {
            json!({
                "UserName": c.value().user_name,
                "Status": c.value().status,
                "ServiceUserName": c.value().service_user_name,
                "CreateDate": c.value().create_date,
                "ServiceSpecificCredentialId": c.value().service_specific_credential_id,
                "ServiceName": c.value().service_name,
            })
        })
        .collect();

    Ok(json!({
        "ServiceSpecificCredentials": { "member": creds }
    }))
}

// ── ListSigningCertificates ───────────────────────────────────────────────────

/// ListSigningCertificates — Return certificates owned by the user.
pub fn list_signing_certificates(state: &IamState, input: &Value) -> Result<Value, AwsError> {
    let user_name = input.get("UserName").and_then(|v| v.as_str());
    if let Some(name) = user_name
        && !state.users.contains_key(name)
    {
        return Err(crate::error::no_such_entity("User", name));
    }

    let certs: Vec<Value> = state
        .signing_certificates
        .iter()
        .filter(|c| user_name.map(|u| c.value().user_name == u).unwrap_or(true))
        .map(|c| {
            json!({
                "UserName": c.value().user_name,
                "CertificateId": c.value().certificate_id,
                "CertificateBody": c.value().certificate_body,
                "Status": c.value().status,
                "UploadDate": c.value().upload_date,
            })
        })
        .collect();

    Ok(json!({
        "Certificates": { "member": certs },
        "IsTruncated": false
    }))
}

pub fn upload_signing_certificate(state: &IamState, input: &Value) -> Result<Value, AwsError> {
    let user_name = require_str(input, "UserName")?;
    let body = require_str(input, "CertificateBody")?;

    if !state.users.contains_key(user_name) {
        return Err(no_such_entity("User", user_name));
    }

    let id = format!("ASCA{}", new_access_key_id().trim_start_matches("AKIA"));
    let cert = SigningCertificate {
        user_name: user_name.to_string(),
        certificate_id: id.clone(),
        certificate_body: body.to_string(),
        status: "Active".to_string(),
        upload_date: now_iso8601(),
    };

    let result = json!({
        "UserName": cert.user_name,
        "CertificateId": cert.certificate_id,
        "CertificateBody": cert.certificate_body,
        "Status": cert.status,
        "UploadDate": cert.upload_date,
    });
    state.signing_certificates.insert(id, cert);

    Ok(json!({ "Certificate": result }))
}

pub fn update_signing_certificate(state: &IamState, input: &Value) -> Result<Value, AwsError> {
    let cert_id = require_str(input, "CertificateId")?;
    let status = require_str(input, "Status")?;

    let mut cert = state
        .signing_certificates
        .get_mut(cert_id)
        .ok_or_else(|| no_such_entity("SigningCertificate", cert_id))?;
    cert.status = status.to_string();
    Ok(json!({}))
}

pub fn delete_signing_certificate(state: &IamState, input: &Value) -> Result<Value, AwsError> {
    let cert_id = require_str(input, "CertificateId")?;
    if state.signing_certificates.remove(cert_id).is_none() {
        return Err(no_such_entity("SigningCertificate", cert_id));
    }
    Ok(json!({}))
}

pub fn create_service_specific_credential(
    state: &IamState,
    input: &Value,
) -> Result<Value, AwsError> {
    let user_name = require_str(input, "UserName")?;
    let service_name = require_str(input, "ServiceName")?;

    if !state.users.contains_key(user_name) {
        return Err(no_such_entity("User", user_name));
    }

    let cred_id = new_access_key_id();
    let cred = ServiceSpecificCredential {
        user_name: user_name.to_string(),
        service_name: service_name.to_string(),
        service_user_name: format!("{user_name}-{}", &cred_id[4..8]),
        service_specific_credential_id: cred_id.clone(),
        service_password: new_secret_access_key(),
        status: "Active".to_string(),
        create_date: now_iso8601(),
    };

    let result = json!({
        "CreateDate": cred.create_date,
        "ServiceName": cred.service_name,
        "ServiceUserName": cred.service_user_name,
        "ServicePassword": cred.service_password,
        "ServiceSpecificCredentialId": cred.service_specific_credential_id,
        "UserName": cred.user_name,
        "Status": cred.status,
    });
    state.service_specific_credentials.insert(cred_id, cred);

    Ok(json!({ "ServiceSpecificCredential": result }))
}

pub fn delete_service_specific_credential(
    state: &IamState,
    input: &Value,
) -> Result<Value, AwsError> {
    let cred_id = require_str(input, "ServiceSpecificCredentialId")?;
    if state.service_specific_credentials.remove(cred_id).is_none() {
        return Err(no_such_entity("ServiceSpecificCredential", cred_id));
    }
    Ok(json!({}))
}

pub fn reset_service_specific_credential(
    state: &IamState,
    input: &Value,
) -> Result<Value, AwsError> {
    let cred_id = require_str(input, "ServiceSpecificCredentialId")?;

    let mut cred = state
        .service_specific_credentials
        .get_mut(cred_id)
        .ok_or_else(|| no_such_entity("ServiceSpecificCredential", cred_id))?;
    cred.service_password = new_secret_access_key();

    Ok(json!({
        "ServiceSpecificCredential": {
            "CreateDate": cred.create_date,
            "ServiceName": cred.service_name,
            "ServiceUserName": cred.service_user_name,
            "ServicePassword": cred.service_password,
            "ServiceSpecificCredentialId": cred.service_specific_credential_id,
            "UserName": cred.user_name,
            "Status": cred.status,
        }
    }))
}

pub fn update_service_specific_credential(
    state: &IamState,
    input: &Value,
) -> Result<Value, AwsError> {
    let cred_id = require_str(input, "ServiceSpecificCredentialId")?;
    let status = require_str(input, "Status")?;

    let mut cred = state
        .service_specific_credentials
        .get_mut(cred_id)
        .ok_or_else(|| no_such_entity("ServiceSpecificCredential", cred_id))?;
    cred.status = status.to_string();
    Ok(json!({}))
}

pub fn list_policies_granting_service_access(
    _state: &IamState,
    input: &Value,
) -> Result<Value, AwsError> {
    let _arn = require_str(input, "Arn")?;
    let services = input
        .get("ServiceNamespaces")
        .and_then(|v| v.get("member"))
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();
    let entries: Vec<Value> = services
        .iter()
        .filter_map(|s| s.as_str())
        .map(|svc| {
            json!({
                "ServiceNamespace": svc,
                "Policies": { "member": [] },
            })
        })
        .collect();
    Ok(json!({
        "PoliciesGrantingServiceAccess": { "member": entries },
        "IsTruncated": false,
    }))
}

pub fn get_service_last_accessed_details_with_entities(
    _state: &IamState,
    input: &Value,
) -> Result<Value, AwsError> {
    let _job_id = require_str(input, "JobId")?;
    let _service_namespace = opt_str(input, "ServiceNamespace");
    Ok(json!({
        "JobStatus": "COMPLETED",
        "JobCreationDate": now_iso8601(),
        "JobCompletionDate": now_iso8601(),
        "EntityDetailsList": { "member": [] },
        "IsTruncated": false,
    }))
}

pub fn set_security_token_service_preferences(
    _state: &IamState,
    _input: &Value,
) -> Result<Value, AwsError> {
    Ok(json!({}))
}

pub fn get_organizations_access_report(
    _state: &IamState,
    input: &Value,
) -> Result<Value, AwsError> {
    let _job_id = require_str(input, "JobId")?;
    Ok(json!({
        "JobStatus": "COMPLETED",
        "JobCreationDate": now_iso8601(),
        "JobCompletionDate": now_iso8601(),
        "NumberOfServicesAccessible": 0u64,
        "NumberOfServicesNotAccessed": 0u64,
        "AccessDetails": { "member": [] },
        "IsTruncated": false,
    }))
}

pub fn generate_organizations_access_report(
    _state: &IamState,
    input: &Value,
) -> Result<Value, AwsError> {
    let _entity_path = require_str(input, "EntityPath")?;
    Ok(json!({ "JobId": crate::ids::new_uuid() }))
}

// ── Policy Simulator ─────────────────────────────────────────────────────────

fn extract_string_list(input: &Value, key: &str) -> Vec<String> {
    let v = match input.get(key) {
        Some(v) => v,
        None => return Vec::new(),
    };
    if let Some(arr) = v.as_array() {
        return arr
            .iter()
            .filter_map(|x| x.as_str().map(str::to_string))
            .collect();
    }
    if let Some(members) = v.get("member").and_then(|m| m.as_array()) {
        return members
            .iter()
            .filter_map(|x| x.as_str().map(str::to_string))
            .collect();
    }
    if let Some(s) = v.as_str() {
        return vec![s.to_string()];
    }
    Vec::new()
}

fn parse_policy_input_list(input: &Value, key: &str) -> Result<Vec<PolicyDocument>, AwsError> {
    extract_string_list(input, key)
        .iter()
        .map(|raw| {
            awsim_iam_policy::parse(raw)
                .map_err(|e| malformed_policy_document(format!("Syntax errors in policy. {e}")))
        })
        .collect()
}

fn extract_context_entries(input: &Value) -> HashMap<String, ContextValue> {
    let mut ctx = HashMap::new();
    let raw = match input.get("ContextEntries") {
        Some(v) => v,
        None => return ctx,
    };
    let entries = raw
        .as_array()
        .cloned()
        .or_else(|| raw.get("member").and_then(|m| m.as_array()).cloned())
        .unwrap_or_default();
    for entry in entries {
        let key = match entry.get("ContextKeyName").and_then(|v| v.as_str()) {
            Some(k) => k.to_string(),
            None => continue,
        };
        let typ = entry
            .get("ContextKeyType")
            .and_then(|v| v.as_str())
            .unwrap_or("string");
        let values = extract_string_list(&entry, "ContextKeyValues");
        let value = match typ {
            "string" => ContextValue::String(values.into_iter().next().unwrap_or_default()),
            "stringList" => ContextValue::StringList(values),
            "numeric" => values
                .into_iter()
                .next()
                .and_then(|v| v.parse::<f64>().ok())
                .map(ContextValue::Number)
                .unwrap_or(ContextValue::Number(0.0)),
            "boolean" => values
                .into_iter()
                .next()
                .map(|v| ContextValue::Bool(v == "true"))
                .unwrap_or(ContextValue::Bool(false)),
            "ip" => ContextValue::Ip(values.into_iter().next().unwrap_or_default()),
            _ => ContextValue::StringList(values),
        };
        ctx.insert(key, value);
    }
    ctx
}

fn decision_to_str(d: Decision) -> &'static str {
    match d {
        Decision::Allow => "allowed",
        Decision::ExplicitDeny => "explicitDeny",
        Decision::ImplicitDeny => "implicitDeny",
    }
}

fn build_evaluation_results(
    identity_policies: &[PolicyDocument],
    actions: &[String],
    resources: &[String],
    principal_arn: &str,
    principal_account: &str,
    context: &HashMap<String, ContextValue>,
) -> Vec<Value> {
    let mut out = Vec::new();
    for action in actions {
        for resource in resources {
            let req = AuthzRequest {
                principal_arn,
                principal_account,
                action,
                resource_arn: resource,
                context,
            };
            let ctx = EvalContext {
                identity_policies,
                ..Default::default()
            };
            let decision = evaluate(&req, &ctx);
            out.push(json!({
                "EvalActionName": action,
                "EvalResourceName": resource,
                "EvalDecision": decision_to_str(decision),
                "MatchedStatements": { "member": [] },
                "MissingContextValues": { "member": [] }
            }));
        }
    }
    out
}

pub fn simulate_custom_policy(_state: &IamState, input: &Value) -> Result<Value, AwsError> {
    let actions = extract_string_list(input, "ActionNames");
    let mut resources = extract_string_list(input, "ResourceArns");
    if resources.is_empty() {
        resources.push("*".to_string());
    }
    let identity_policies = parse_policy_input_list(input, "PolicyInputList")?;
    let context = extract_context_entries(input);

    let results = build_evaluation_results(
        &identity_policies,
        &actions,
        &resources,
        "arn:aws:iam::000000000000:user/simulated",
        "000000000000",
        &context,
    );

    Ok(json!({
        "EvaluationResults": { "member": results },
        "IsTruncated": false
    }))
}

pub fn simulate_principal_policy(state: &IamState, input: &Value) -> Result<Value, AwsError> {
    let principal_arn = require_str(input, "PolicySourceArn")?.to_string();
    let actions = extract_string_list(input, "ActionNames");
    let mut resources = extract_string_list(input, "ResourceArns");
    if resources.is_empty() {
        resources.push("*".to_string());
    }
    let mut identity_policies = parse_policy_input_list(input, "PolicyInputList")?;
    let context = extract_context_entries(input);

    let (principal_policies, principal_account) =
        collect_principal_policies(state, &principal_arn)?;
    identity_policies.extend(principal_policies);

    let results = build_evaluation_results(
        &identity_policies,
        &actions,
        &resources,
        &principal_arn,
        &principal_account,
        &context,
    );

    Ok(json!({
        "EvaluationResults": { "member": results },
        "IsTruncated": false
    }))
}

fn collect_principal_policies(
    state: &IamState,
    arn: &str,
) -> Result<(Vec<PolicyDocument>, String), AwsError> {
    let account = arn
        .split(':')
        .nth(4)
        .map(|s| s.to_string())
        .unwrap_or_else(|| "000000000000".to_string());

    if let Some(user_entry) = state.users.iter().find(|e| e.value().arn == arn) {
        let user = user_entry.value().clone();
        let mut docs = Vec::new();
        for raw in user.inline_policies.values() {
            docs.push(parse_required(raw)?);
        }
        for arn in &user.attached_policies {
            if let Some(p) = state.policies.get(arn) {
                docs.push(parse_required(&p.value().policy_document)?);
            }
        }
        for group_name in &user.groups {
            if let Some(group) = state.groups.get(group_name) {
                let group = group.value();
                for raw in group.inline_policies.values() {
                    docs.push(parse_required(raw)?);
                }
                for arn in &group.attached_policies {
                    if let Some(p) = state.policies.get(arn) {
                        docs.push(parse_required(&p.value().policy_document)?);
                    }
                }
            }
        }
        return Ok((docs, account));
    }

    if let Some(role_entry) = state.roles.iter().find(|e| e.value().arn == arn) {
        let role = role_entry.value().clone();
        let mut docs = Vec::new();
        for raw in role.inline_policies.values() {
            docs.push(parse_required(raw)?);
        }
        for arn in &role.attached_policies {
            if let Some(p) = state.policies.get(arn) {
                docs.push(parse_required(&p.value().policy_document)?);
            }
        }
        return Ok((docs, account));
    }

    Err(no_such_entity("Principal", arn))
}

fn parse_required(raw: &str) -> Result<PolicyDocument, AwsError> {
    awsim_iam_policy::parse(raw)
        .map_err(|e| malformed_policy_document(format!("Syntax errors in policy. {e}")))
}

// ── GetContextKeys stubs ──────────────────────────────────────────────────────

/// GetContextKeysForCustomPolicy — Return empty context key list.
pub fn get_context_keys_for_custom_policy(
    _state: &IamState,
    _input: &Value,
) -> Result<Value, AwsError> {
    Ok(json!({
        "ContextKeyNames": { "member": [] }
    }))
}

/// GetContextKeysForPrincipalPolicy — Return empty context key list.
pub fn get_context_keys_for_principal_policy(
    _state: &IamState,
    _input: &Value,
) -> Result<Value, AwsError> {
    Ok(json!({
        "ContextKeyNames": { "member": [] }
    }))
}