Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
feat(iam): GenerateCredentialReport, GetCredentialReport (#860)
Shai Lahmishfa29d1ac088e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 12777975..c77c0949 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -21,6 +21,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).- **SQS — out-of-range numeric attributes rejected with `InvalidAttributeValue`** — `CreateQueue` and `SetQueueAttributes` accepted any value and stored it verbatim, so `VisibilityTimeout=99999` (and every other numeric attribute) was silently kept. Now `VisibilityTimeout` (0..43200), `MaximumMessageSize` (1024..262144), `MessageRetentionPeriod` (60..1209600), `DelaySeconds` (0..900), `ReceiveMessageWaitTimeSeconds` (0..20), and `KmsDataKeyReusePeriodSeconds` (60..86400) are validated against the AWS ranges and rejected with `InvalidAttributeValue` (400) when outside the documented bounds or non-numeric. Reported by @dcabib.- **EventBridge — `anything-but` honors nested `prefix` / `suffix` / `wildcard` content filters** — `{"anything-but": {"prefix": "TEST-"}}` (and `suffix` / `wildcard` variants) was silently ignored at dispatch and every event matched regardless of the field value, because the handler only recognized literal and list-of-literal forms. The nested-matcher form per AWS docs is now negated correctly: an event whose field matches the nested filter is excluded. Reported by @aldirrix.- **ElastiCache — Redis container respawned after restart** — with `PERSIST_STATE=1`, restored cluster metadata reported `CacheClusterStatus=available` but the persisted Docker container id no longer existed, so the endpoint was unreachable even though `DescribeCacheClusters` looked healthy. Restored clusters and replication groups are now marked pending respawn at `restore_state` time and lazily spawned (under a lock to prevent concurrent first-requests from double-spawning) on the first dispatcher call — endpoint metadata is rewritten to the freshly-spawned container before any caller can read it. Failures are logged once and cleared from the pending set (no retry storm). Reported by @ItsSmiffy.+- **IAM — `GenerateCredentialReport`, `GetCredentialReport`** — generates and returns the account credential report as a CSV (exact AWS column header). One row per user including `password_enabled` (from login profiles), `mfa_active` (from MFA device assignments), and `access_key_1/2_active` (from access-key status). Root account synthetic row included. `GetCredentialReport` returns `ReportNotPresent` (410) when no report has been generated. `Content` is base64-encoded per the AWS blob encoding contract.- **IAM — `GetAccountSummary`, `GetAccountPasswordPolicy`, `UpdateAccountPasswordPolicy`, `DeleteAccountPasswordPolicy`, `ListAccountAliases`, `CreateAccountAlias`, `DeleteAccountAlias`** — account-level hygiene reads. `GetAccountSummary` returns computed counts (`Users`, `Groups`, `Roles`, `Policies`, `MFADevices`, `MFADevicesInUse`, `AccountMFAEnabled`) plus static quotas. `GetAccountPasswordPolicy` returns `NoSuchEntity` (404) before any policy is set, matching real AWS. Account aliases stored per-account (replace-on-create semantics).---diff --git a/README.md b/README.mdindex 6a112dfa..7f32f362 100644--- a/README.md+++ b/README.md@@ -356,6 +356,7 @@ subnet = ec2.create_subnet(| **DynamoDB** | CreateTable, UpdateTable, DeleteTable, DescribeTable, ListTables, PutItem, GetItem, DeleteItem, UpdateItem, Query, Scan, BatchWriteItem, BatchGetItem, TransactWriteItems, TransactGetItems, DescribeTimeToLive, UpdateTimeToLive, DescribeContinuousBackups, UpdateContinuousBackups, DescribeEndpoints, TagResource, UntagResource, ListTagsOfResource, EnableKinesisStreamingDestination, DisableKinesisStreamingDestination, DescribeKinesisStreamingDestination, UpdateKinesisStreamingDestination | TTL enforced via thread-safe background reaper (60s cadence); DynamoDB Streams — `StreamSpecification` emits INSERT/MODIFY/REMOVE records on all write operations, respects `StreamViewType`; Kinesis streaming destinations (`aws_dynamodb_kinesis_streaming_destination`) fan item mutations out into any Kinesis stream by ARN while the destination is ACTIVE || **DynamoDB Streams** | ListStreams, DescribeStream, GetShardIterator, GetRecords | Reads records emitted by the main DynamoDB service via `boto3.client("dynamodbstreams")` — single synthetic shard per stream; `TRIM_HORIZON`/`LATEST`/`AT_SEQUENCE_NUMBER`/`AFTER_SEQUENCE_NUMBER` iterator types; `NEW_AND_OLD_IMAGES`, `NEW_IMAGE`, `OLD_IMAGE`, `KEYS_ONLY` view types; opaque base64 iterator tokens || **Lambda** | CreateFunction, DeleteFunction, GetFunction, GetFunctionConfiguration, ListFunctions, Invoke, UpdateFunctionCode, UpdateFunctionConfiguration, AddPermission, RemovePermission, GetPolicy, ListVersionsByFunction, PublishVersion, CreateAlias, GetAlias, UpdateAlias, DeleteAlias, ListAliases, TagResource, UntagResource, ListTags, CreateEventSourceMapping, DeleteEventSourceMapping, GetEventSourceMapping, ListEventSourceMappings, UpdateEventSourceMapping, CreateFunctionUrlConfig, GetFunctionUrlConfig, UpdateFunctionUrlConfig, DeleteFunctionUrlConfig, ListFunctionUrlConfigs, PutFunctionConcurrency, GetFunctionConcurrency, DeleteFunctionConcurrency, PutFunctionEventInvokeConfig, GetFunctionEventInvokeConfig, DeleteFunctionEventInvokeConfig, PublishLayerVersion, GetLayerVersion, GetLayerVersionByArn, ListLayerVersions, DeleteLayerVersion, ListLayers, AddLayerVersionPermission, RemoveLayerVersionPermission, GetLayerVersionPolicy, CheckpointDurableExecution, GetDurableExecution, GetDurableExecutionState, GetDurableExecutionHistory, ListDurableExecutionsByFunction, StopDurableExecution, SendDurableExecutionCallbackSuccess, SendDurableExecutionCallbackFailure, SendDurableExecutionCallbackHeartbeat | Python and Node.js runtimes execute with warm worker pool; `provided.al2023`/`provided.al2` runtimes execute via Docker RIE (Go, Rust, C++ support); `Publish=True` creates immutable numbered versions; Code via `ZipFile`, `S3Bucket`/`S3Key` (with optional `S3ObjectVersion`), or `ImageUri` (Docker image); `PackageType: Image` pulls and invokes user-provided Docker images via Lambda RIE; SQS, Kinesis, and DynamoDB Streams event source mappings; Function URL CRUD; Lambda Layers CRUD; Aliases; Concurrency; EventInvokeConfig; **Durable Functions** — `CreateFunction` accepts `DurableConfig`; checkpoint/state/history/list/stop ops at the preview API (`2025-12-01`); external `SendCallback{Success,Failure,Heartbeat}` resume the SDK across invocations; resume scheduler fires WAIT expiries, callback timeouts, and step-retry backoffs; verified against the official `aws-durable-execution-sdk-python` and `aws-durable-execution-sdk-java`; **X-Ray active tracing** — `TracingConfig.Mode=Active` injects `_X_AMZN_TRACE_ID` (`Root=1-<hex>-<hex>;Parent=<hex>;Sampled=1`) into the runtime per invocation so the AWS X-Ray SDK runs without `Missing AWS Lambda trace data`; supported on the warm Python / Node executor, provided runtimes, and the local subprocess fallback (docker RIE upstream does not implement X-Ray and is logged but unsupported) |+| **IAM** | CreateUser, GetUser, ListUsers, DeleteUser, CreateRole, GetRole, ListRoles, DeleteRole, CreatePolicy, GetPolicy, DeletePolicy, AttachRolePolicy, DetachRolePolicy, PutRolePolicy, GetRolePolicy, DeleteRolePolicy, ListRolePolicies, ListAttachedRolePolicies, CreateAccessKey, ListAccessKeys, DeleteAccessKey, UpdateAccessKey, GetAccessKeyLastUsed, CreateInstanceProfile, GetInstanceProfile, DeleteInstanceProfile, AddRoleToInstanceProfile, RemoveRoleFromInstanceProfile, ListInstanceProfiles, CreateGroup, GetGroup, AddUserToGroup, RemoveUserFromGroup, CreateServiceLinkedRole, DeleteServiceLinkedRole, GetServiceLinkedRoleDeletionStatus, CreateOpenIDConnectProvider, ListOpenIDConnectProviders, CreateSAMLProvider, GetSAMLProvider, ListSAMLProviders, UpdateSAMLProvider, DeleteSAMLProvider, CreateLoginProfile, GetLoginProfile, UpdateLoginProfile, DeleteLoginProfile, CreateVirtualMFADevice, EnableMFADevice, DeactivateMFADevice, ResyncMFADevice, ListMFADevices, ListVirtualMFADevices, DeleteVirtualMFADevice, GetAccountAuthorizationDetails, GenerateServiceLastAccessedDetails, GetServiceLastAccessedDetails, TagRole, UntagRole, TagUser, UntagUser, TagPolicy, UntagPolicy , GenerateCredentialReport, GetCredentialReport | || **IAM** | CreateUser, GetUser, ListUsers, DeleteUser, CreateRole, GetRole, ListRoles, DeleteRole, CreatePolicy, GetPolicy, DeletePolicy, AttachRolePolicy, DetachRolePolicy, PutRolePolicy, GetRolePolicy, DeleteRolePolicy, ListRolePolicies, ListAttachedRolePolicies, CreateAccessKey, ListAccessKeys, DeleteAccessKey, UpdateAccessKey, GetAccessKeyLastUsed, CreateInstanceProfile, GetInstanceProfile, DeleteInstanceProfile, AddRoleToInstanceProfile, RemoveRoleFromInstanceProfile, ListInstanceProfiles, CreateGroup, GetGroup, AddUserToGroup, RemoveUserFromGroup, CreateServiceLinkedRole, DeleteServiceLinkedRole, GetServiceLinkedRoleDeletionStatus, CreateOpenIDConnectProvider, ListOpenIDConnectProviders, CreateSAMLProvider, GetSAMLProvider, ListSAMLProviders, UpdateSAMLProvider, DeleteSAMLProvider, CreateLoginProfile, GetLoginProfile, UpdateLoginProfile, DeleteLoginProfile, CreateVirtualMFADevice, EnableMFADevice, DeactivateMFADevice, ResyncMFADevice, ListMFADevices, ListVirtualMFADevices, DeleteVirtualMFADevice, GetAccountAuthorizationDetails, GenerateServiceLastAccessedDetails, GetServiceLastAccessedDetails, TagRole, UntagRole, TagUser, UntagUser, TagPolicy, UntagPolicy , GetAccountSummary, GetAccountPasswordPolicy, UpdateAccountPasswordPolicy, DeleteAccountPasswordPolicy, ListAccountAliases, CreateAccountAlias, DeleteAccountAlias | || **STS** | GetCallerIdentity, AssumeRole, GetSessionToken, AssumeRoleWithWebIdentity | || **IMDS** (EC2 Instance Metadata) | `PUT /latest/api/token`, `GET /latest/meta-data/instance-id`, `GET /latest/meta-data/iam/security-credentials/`, `GET /latest/meta-data/iam/security-credentials/<role>`, `GET /latest/meta-data/iam/info`, `GET /latest/meta-data/placement/{region,availability-zone,...}`, `GET /latest/dynamic/instance-identity/document` | IMDSv1 + IMDSv2; default credential chain falls through to a `ministack-instance-role` document with `ASIA*` session creds. Point SDKs at ministack via `AWS_EC2_METADATA_SERVICE_ENDPOINT=http://localhost:4566` (or `ec2_metadata_service_endpoint` in `~/.aws/config`); set `MINISTACK_IMDS_V2_REQUIRED=1` to require the token PUT |diff --git a/ministack/services/iam.py b/ministack/services/iam.pyindex 0d7613fe..b88da81f 100644--- a/ministack/services/iam.py+++ b/ministack/services/iam.py@@ -61,6 +61,7 @@ _sla_jobs = AccountScopedDict()_saml_providers = AccountScopedDict()_mfa_devices = AccountScopedDict()_login_profiles = AccountScopedDict()+_credential_reports = AccountScopedDict()_account_password_policy = AccountScopedDict()_account_aliases = AccountScopedDict()@@ -293,6 +294,7 @@ def get_state():"saml_providers": copy.deepcopy(_saml_providers),"mfa_devices": copy.deepcopy(_mfa_devices),"login_profiles": copy.deepcopy(_login_profiles),+ "credential_reports": copy.deepcopy(_credential_reports),"account_password_policy": copy.deepcopy(_account_password_policy),"account_aliases": copy.deepcopy(_account_aliases),}@@ -314,6 +316,7 @@ def restore_state(data):_saml_providers.update(data.get("saml_providers", {}))_mfa_devices.update(data.get("mfa_devices", {}))_login_profiles.update(data.get("login_profiles", {}))+ _credential_reports.update(data.get("credential_reports", {}))_account_password_policy.update(data.get("account_password_policy", {}))_account_aliases.update(data.get("account_aliases", {}))@@ -1910,6 +1913,96 @@ def _delete_login_profile(p):return _xml(200, "DeleteLoginProfileResponse", "", ns="iam")+# -------------------- Credential report --------------------++_CRED_REPORT_HEADER = (+ "user,arn,user_creation_time,password_enabled,password_last_used,"+ "password_last_changed,password_next_rotation,mfa_active,"+ "access_key_1_active,access_key_1_last_rotated,access_key_1_last_used_date,"+ "access_key_1_last_used_region,access_key_1_last_used_service,"+ "access_key_2_active,access_key_2_last_rotated,access_key_2_last_used_date,"+ "access_key_2_last_used_region,access_key_2_last_used_service,"+ "cert_1_active,cert_1_last_rotated,cert_2_active,cert_2_last_rotated"+)+++def _build_credential_report():+ acct = get_account_id()+ rows = [_CRED_REPORT_HEADER]++ # Synthetic root account row+ rows.append(+ f"<root_account>,arn:aws:iam::{acct}:root,{_now()},"+ "not_supported,not_supported,not_supported,not_supported,false,"+ "false,N/A,N/A,N/A,N/A,"+ "false,N/A,N/A,N/A,N/A,"+ "false,N/A,false,N/A"+ )++ # One row per user+ for name, user in _users.items():+ has_password = name in _login_profiles+ has_mfa = any(+ dev.get("User") == name for dev in _mfa_devices.values()+ )++ # Collect up to 2 active access keys (sorted by creation for stable output)+ user_keys = sorted(+ [v for v in _access_keys.values() if v["UserName"] == name],+ key=lambda k: k.get("CreateDate", ""),+ )+ key1 = user_keys[0] if len(user_keys) > 0 else None+ key2 = user_keys[1] if len(user_keys) > 1 else None++ def _key_cols(k):+ if k is None:+ return "false,N/A,N/A,N/A,N/A"+ active = "true" if k.get("Status") == "Active" else "false"+ rotated = k.get("CreateDate", "N/A")+ return f"{active},{rotated},N/A,N/A,N/A"++ row = (+ f"{name},{user['Arn']},{user['CreateDate']},"+ f"{'true' if has_password else 'false'},"+ f"N/A,N/A,N/A,"+ f"{'true' if has_mfa else 'false'},"+ f"{_key_cols(key1)},"+ f"{_key_cols(key2)},"+ "false,N/A,false,N/A"+ )+ rows.append(row)++ return "\n".join(rows)+++def _generate_credential_report(p):+ _credential_reports["report"] = {+ "content": _build_credential_report(),+ "generated_time": _now(),+ }+ return _xml(200, "GenerateCredentialReportResponse",+ "<GenerateCredentialReportResult>"+ "<State>COMPLETE</State>"+ "<Description>No report exists. Starting a new report generation task</Description>"+ "</GenerateCredentialReportResult>",+ ns="iam")+++def _get_credential_report(p):+ report = _credential_reports.get("report")+ if not report:+ return _error(410, "ReportNotPresent",+ "No credential report exists. Generate one with GenerateCredentialReport.", ns="iam")+ content_b64 = base64.b64encode(report["content"].encode("utf-8")).decode()+ return _xml(200, "GetCredentialReportResponse",+ f"<GetCredentialReportResult>"+ f"<Content>{content_b64}</Content>"+ f"<ReportFormat>text/csv</ReportFormat>"+ f"<GeneratedTime>{report['generated_time']}</GeneratedTime>"+ f"</GetCredentialReportResult>",+ ns="iam")++# -------------------- OIDC providers --------------------def _create_oidc_provider(p):@@ -2573,6 +2666,8 @@ _IAM_HANDLERS = {"GetLoginProfile": _get_login_profile,"UpdateLoginProfile": _update_login_profile,"DeleteLoginProfile": _delete_login_profile,+ "GenerateCredentialReport": _generate_credential_report,+ "GetCredentialReport": _get_credential_report,"GetAccountSummary": _get_account_summary,"GetAccountPasswordPolicy": _get_account_password_policy,"UpdateAccountPasswordPolicy": _update_account_password_policy,@@ -2600,6 +2695,7 @@ def reset():_saml_providers.clear()_mfa_devices.clear()_login_profiles.clear()+ _credential_reports.clear()_account_password_policy.clear()_account_aliases.clear()# Re-seed AWS-managed policies. They're not customer state, so adiff --git a/tests/conftest.py b/tests/conftest.pyindex af4b9abf..38b3e855 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -96,6 +96,10 @@ _SERIAL_TESTS = {"tests/test_appsync.py::test_appsync_lambda_authorizer_rejection_returns_unauthorized","tests/test_appsync.py::test_appsync_lambda_missing_authorizer_returns_unauthorized","tests/test_appsync.py::test_appsync_lambda_failing_authorizer_returns_unauthorized",+ # Credential report reflects all users in the account; run serially to avoid+ # parallel-test interference on the account-global CSV snapshot.+ "tests/test_iam.py::test_iam_credential_report_mfa_and_password",+ "tests/test_iam.py::test_iam_credential_report_header",# Account-global mutations (password policy, alias); must run serially."tests/test_iam.py::test_iam_password_policy_absent_then_set","tests/test_iam.py::test_iam_account_alias_crud",diff --git a/tests/test_elasticache.py b/tests/test_elasticache.pyindex 067a9631..e17f0e96 100644--- a/tests/test_elasticache.py+++ b/tests/test_elasticache.py@@ -899,7 +899,9 @@ def test_elasticache_restore_state_marks_clusters_for_respawn(monkeypatch):def test_elasticache_restore_state_wipes_stale_replication_group_container_ids(monkeypatch):+ from ministack.core.responses import set_request_account_idfrom ministack.services import elasticache as _ec+ set_request_account_id("000000000000")_ec.reset()monkeypatch.setattr(_ec, "_spawn_redis_container",lambda name, engine, engine_version, labels: ("rg-host", 6379, f"cid-{name}"))diff --git a/tests/test_iam.py b/tests/test_iam.pyindex 85884dac..ecdb6ad7 100644--- a/tests/test_iam.py+++ b/tests/test_iam.py@@ -963,6 +963,7 @@ def test_iam_delete_assigned_mfa_conflict(iam):except Exception:passiam.delete_user(UserName=user_name)+# ── Login profiles ────────────────────────────────────────────────────@@ -1018,6 +1019,76 @@ def test_iam_delete_login_profile(iam):iam.delete_user(UserName=name)+# ── Credential report ─────────────────────────────────────────────────++_CRED_REPORT_COLUMNS = (+ "user,arn,user_creation_time,password_enabled,password_last_used,"+ "password_last_changed,password_next_rotation,mfa_active,"+ "access_key_1_active,access_key_1_last_rotated,access_key_1_last_used_date,"+ "access_key_1_last_used_region,access_key_1_last_used_service,"+ "access_key_2_active,access_key_2_last_rotated,access_key_2_last_used_date,"+ "access_key_2_last_used_region,access_key_2_last_used_service,"+ "cert_1_active,cert_1_last_rotated,cert_2_active,cert_2_last_rotated"+)+++def test_iam_credential_report_get_before_generate(iam):+ with pytest.raises(Exception) as exc_info:+ iam.get_credential_report()+ assert exc_info.value.response["Error"]["Code"] == "ReportNotPresent"+++def test_iam_credential_report_mfa_and_password(iam):+ user_a = "cr-user-a-mfapw"+ user_b = "cr-user-b-neither"+ device_name = "cr-mfa-device"++ iam.create_user(UserName=user_a)+ iam.create_user(UserName=user_b)+ iam.create_login_profile(UserName=user_a, Password="Test1234!")+ serial = iam.create_virtual_mfa_device(VirtualMFADeviceName=device_name)["VirtualMFADevice"]["SerialNumber"]+ iam.enable_mfa_device(UserName=user_a, SerialNumber=serial,+ AuthenticationCode1="111111", AuthenticationCode2="222222")+ try:+ iam.generate_credential_report()+ resp = iam.get_credential_report()+ csv_bytes = resp["Content"]+ csv_text = csv_bytes.decode("utf-8") if isinstance(csv_bytes, (bytes, bytearray)) else csv_bytes+ rows = {r.split(",")[0]: r.split(",") for r in csv_text.strip().splitlines()[1:]}++ # user A: password_enabled=true, mfa_active=true+ assert rows[user_a][3] == "true", f"Expected password_enabled=true for {user_a}"+ assert rows[user_a][7] == "true", f"Expected mfa_active=true for {user_a}"++ # user B: password_enabled=false, mfa_active=false+ assert rows[user_b][3] == "false", f"Expected password_enabled=false for {user_b}"+ assert rows[user_b][7] == "false", f"Expected mfa_active=false for {user_b}"+ finally:+ try:+ iam.deactivate_mfa_device(UserName=user_a, SerialNumber=serial)+ except Exception:+ pass+ try:+ iam.delete_virtual_mfa_device(SerialNumber=serial)+ except Exception:+ pass+ try:+ iam.delete_login_profile(UserName=user_a)+ except Exception:+ pass+ iam.delete_user(UserName=user_a)+ iam.delete_user(UserName=user_b)+++def test_iam_credential_report_header(iam):+ iam.generate_credential_report()+ resp = iam.get_credential_report()+ csv_bytes = resp["Content"]+ csv_text = csv_bytes.decode("utf-8") if isinstance(csv_bytes, (bytes, bytearray)) else csv_bytes+ lines = csv_text.strip().splitlines()+ assert lines[0] == _CRED_REPORT_COLUMNS+ user_col = [r.split(",")[0] for r in lines]+ assert "<root_account>" in user_col# ── Account posture (summary / password policy / aliases) ─────────────