Migrate IAM access keys#13838
Conversation
LocalStack Community integration with Pro 2 files ±0 2 suites ±0 4m 24s ⏱️ + 1m 34s For more details on these failures, see this check. Results for commit 0ad2171. ± Comparison against base commit f8126b5. |
Test Results (amd64) - Integration, Bootstrap 5 files ±0 5 suites ±0 11m 37s ⏱️ -35s For more details on these failures, see this check. Results for commit 0ad2171. ± Comparison against base commit f8126b5. |
|
|
||
| def _generate_access_key_id(self, context: RequestContext) -> str: | ||
| """Generate an access key ID with the appropriate prefix based on config.""" | ||
| prefix = "AKIA" if config.PARITY_AWS_ACCESS_KEY_ID else "LKIA" | ||
| return generate_iam_identifier(context.account_id, prefix=prefix, total_length=20) | ||
|
|
||
| def _generate_secret_access_key(self) -> str: | ||
| """Generate a 40-character random secret access key.""" | ||
| charset = string.ascii_letters + string.digits + "+/" | ||
| return "".join(random.choices(charset, k=40)) |
There was a problem hiding this comment.
suggestion: these 2 methods could be put in the new utils.py
There was a problem hiding this comment.
Happy to, let's refactor those methods a together for all PRs - it's currently mostly in provider.py, we can bulk move them!
Motivation
We need to migrate IAM user access keys from moto to LocalStack.
Changes
Operations Implemented
create_access_keydelete_access_keylist_access_keysupdate_access_keyget_access_key_last_usedNew Entity:
AccessKeyEntityExtended
UserEntityAdded
access_keysfield as a dict for efficient lookup by access key ID:Extended
IamStoreAdded
ACCESS_KEY_INDEXfor efficient cross-user access key lookups:Design Decisions
1. Access Key ID Generation
generate_iam_identifier()fromutils.py(same as role/user IDs)config.PARITY_AWS_ACCESS_KEY_ID:AKIAfor AWS parity modeLKIAfor LocalStack default2. Secret Access Key Generation
string.ascii_letters + string.digits + "+/"3. User Name Derivation (Self-Referential Operations)
When
user_nameis not provided, the implementation:ACCESS_KEY_INDEXValidationError("Must specify userName when calling with non-User credentials")This approach allows users to manage their own access keys without specifying their username.
4. Access Key Limit
LIMIT_ACCESS_KEYS_PER_USER = 2constantLimitExceededExceptionwhen exceeded5. Delete User Validation
Updated
delete_userto check for access keys before deletion:DeleteConflictException("Cannot delete entity, must delete access keys first.")if user has access keys6. Error Handling
get_access_key_last_usedwith nonexistent key returnsAccessDenied(notNoSuchEntity) to prevent enumeration attacks - this matches AWS behaviorThread Safety
All access key operations use the existing
self._user_lockfor thread safety.Known Limitations
last_usedfield is not populated because tracking when access keys are actually used requires integration with the authentication/STS layer. This is marked for future work.Tests
test_access_key_lifecycletest_access_key_update_statustest_access_key_limittest_access_key_last_usedtest_access_key_errorstest_access_key_deletion_without_usernameRelated