Skip to content

fix(storage-mongodb): look up an IAM user by its ARN index - #1570

Open
hitesh96db wants to merge 3 commits into
mainfrom
fix/841-iam-users-index
Open

hitesh96db wants to merge 3 commits into
mainfrom
fix/841-iam-users-index

Conversation

@hitesh96db

@hitesh96db hitesh96db commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What

On MongoDB, the lookups of one IAM user by account and name now filter on user_arn as well as account_id and user_name. user_arn carries the unique index of iam_users, so the server answers each lookup with an index scan. The new helper user_filter in crates/storage-mongodb/src/management_store.rs builds the filter, and create_user builds the ARN with the same function. The seven methods that read, update or delete one user use it: get_user_detail, verify_iam_user_password, create_access_key, change_user_password, get_user_password_hash, fetch_user_created_at and delete_user (also the undo of a failed create_user, through principal_filter).

This takes the first of the two options in the issue, so there is no catalog migration. The check the issue asks for: create_user is the only code that inserts into iam_users (grep over crates/), and it has built arn:aws:iam::{account_id}:user/{user_name} since the backend was added (c1f3de3). account_id and user_name stay in the filter, so a document whose ARN differed from its fields would not be matched by another user's lookup.

Why

Fixes #841. The only index on iam_users is the unique one on user_arn, so a filter on account_id and user_name alone was a collection scan, and a lookup of a user that does not exist read every document. #847 made the login run one bcrypt check whether or not the name exists but did not touch the query.

Not done: the issue's timing measurement at 1,000, 10,000 and 100,000 users, and the { account_id, user_name } index. The index would need a catalog migration across four backends for the same effect.

Testing done

  • New test crates/storage-mongodb/tests/iam_user_lookup_plan.rs. It turns the profiler on, calls the seven methods for a user created by create_user, one stored in the shape every release since 0.1.4 stored it, and one that does not exist. After each call it asserts that the call sent at least one command to iam_users, that every such command has a planSummary starting with IXSCAN, and that the existing users are found.
    • Without the change it fails on a COLLSCAN entry for find with filter { account_id, user_name }. With only the first commit, it fails on COLLSCAN for delete_user.
    • With the change it passes.
  • cargo test -p extenddb-storage-mongodb --test iam_leftovers --test session_data --test secret_rows --test iam_user_lookup_plan against a local single-node replica set: 7 passed.
  • cargo test -p extenddb-storage-mongodb --lib: 197 passed, 86 ignored.
  • cargo fmt --all -- --check is clean.
  • cargo +1.97.0 clippy -p extenddb-storage-mongodb --all-targets -- -D warnings is clean.
  • Not run: cargo test --workspace, the --ignored live tests of the crate, and the Python suite against MongoDB.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, or public CLI surface, an RFC has been accepted or is linked
    below. Otherwise, an ADR captures the decision (link below).

ADR / RFC: n/a


By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

The MongoDB catalog's only index on iam_users is the unique one on
user_arn. get_user_detail, verify_iam_user_password, create_access_key,
change_user_password, fetch_user_created_at and get_user_password_hash
filtered on account_id and user_name, so each was a collection scan,
and a lookup of a user that does not exist read every document.

The filter now also carries user_arn, which create_user builds from the
same two fields, so the unique index answers it. account_id and
user_name stay in the filter.

Signed-off-by: Hitesh Sharma <hts@amazon.com>
delete_user and the undo of a failed create_user filtered iam_users by
account_id and user_name, which is a collection scan, as the lookups
fixed for #841 were. Build their filter with user_filter.

The plan test now covers every single-user operation, including
delete_user and a user stored as 0.1.4 and later releases stored it. It
checks each call on its own: the call must reach iam_users, read an
index, and find a user that exists. The design doc names the ARN form
the lookups depend on.

Signed-off-by: Hitesh Sharma <hts@amazon.com>
…ert message

CodeQL flagged the Debug output of change_user_password's result as cleartext logging of sensitive data. The message now names the call only.

Signed-off-by: Hitesh Sharma <hts@amazon.com>
@navareamol navareamol self-assigned this Dec 4, 2026

@navareamol navareamol left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found nothing that blocks this. I built it and ran iam_user_lookup_plan against a single node replica set: it passes. I then took the user_arn line out of user_filter on a clean tree and ran the same test: it fails with a COLLSCAN entry for get_user_detail on the filter { account_id, user_name }, so the test does detect the change. The other three test files named in the description pass too, 7 tests in all. I also checked the claim that create_user is the only insert into iam_users and that the ARN form has not changed since the backend was added, and both hold in the code and history.

One question, not a condition for this PR. fetch_user_tags, fetch_user_group_policies and the tag and member reads in get_user_detail still filter on { account_id, user_name } on iam_user_tags, iam_group_members and iam_policies, and I see no index on those fields for those three collections in m0001_baseline.rs. I did not run an explain on them. Do you want to file that as a separate issue?

I will approve once you have answered that question.

@hitesh96db

Copy link
Copy Markdown
Contributor Author

Thanks, @navareamol. Yes, filed as #1580. From reading the code: iam_user_tags and iam_group_members have no index at all, iam_policies none on account_id, and the same holds for list_users, the group and role name lookups, and account_exists on accounts (access_keys already has {account_id, user_name}). Those need a catalog migration with compound indexes, so I kept them out of this PR, which stays the no-migration fix for the iam_users single-user lookups. I have not run an explain on them yet; the issue says so.

Also, the CodeQL cleartext-logging alert on iam_user_lookup_plan.rs:100 is fixed in 8f3aa0c: the assert message no longer prints the result of change_user_password.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MongoDB: IAM user lookups by account and user name scan the iam_users collection

3 participants