Skip to content

Fix | SSO token expiry compared in UTC, MFA recognizes STS errors + auth.py tests with Stubber - #354

Merged
diego-ojeda-binbash merged 2 commits into
masterfrom
feature/auth-stubber-tests
Nov 14, 2026
Merged

diego-ojeda-binbash merged 2 commits into
masterfrom
feature/auth-stubber-tests

Conversation

@diego-ojeda-binbash

@diego-ojeda-binbash diego-ojeda-binbash commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What?

Two bugs in auth.py, which refreshes the SSO/MFA credentials before every tofu/terraform command, plus a rewrite of its tests.

Case Before After
Expired SSO token, local zone UTC-3 Taken as valid for 3 more hours; the refresh then fails with a confusing error Rejected at once: "SSO session has expired"
Valid SSO token, local zone UTC+ Rejected as expired Accepted
MFA, user without permission to assume the role (STS AccessDenied) Asked for the code 3 times with no message, then "Failed after 3 attempts" Exits 40 after one code: "User does not have permission to assume role ..."
MFA, any other STS error (e.g. ExpiredTokenException on the source profile) Same 3 silent re-prompts Exits 50 at once with the error

Wrong codes (invalid MFA one time pass code) and a wrong code length still re-prompt, as before.

Two commits:

  1. The fixes:
    • check_sso_token compares expiresAt (UTC) with the current UTC time instead of the local time.
    • The MFA handler recognizes the codes STS actually returns. The ExpiredToken retry branch could never match and is gone.
  2. The tests: tests/test_modules/test_auth.py is rewritten.
    • Uses a real PathsHandler over a project in tmp_path, with real config, credentials, token and cache files and an isolated HOME.
    • Real sso/sts clients run under botocore.stub.Stubber. No more mocks of open, Path.read_text or update_config_section.
    • Covers:
      • the token, including a timezone case
      • the per-layer refresh: several accounts, still-valid credentials, the 30-minute renewal margin, a missing SSO profile, SSO errors
      • refresh_all_accounts
      • MFA, which had no tests before
    • Drops: the tests that duplicated aws.py functions (get_account_roles, add_sso_profile, configure_sso_profiles, aws sso refresh). test_aws.py already covers them end to end.

Why?

H6 in #346: auth.py runs before every command, and its tests mocked the file system so heavily that both bugs went unnoticed.

Results

  • 328 tests pass.
  • auth.py coverage: 60% → 93%. Total: 73% → 77%. aws.py stays at 97%.
  • Against the current code, 5 of the new tests fail: 3 timezone ones and 2 MFA ones.

Not touched here: with python-hcl2 7.x, profile = var.profile parses as "${var.profile}", so the "var.profile" branch in get_layer_profile is dead code. That belongs to #326.

Relates to #346

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • SSO token expiration is now evaluated consistently regardless of the system’s local time zone.
    • MFA role-assumption requests now report permission errors for both recognized access-denied responses.
    • Expired-token and other unmatched MFA errors now follow the standard authentication error path instead of retrying expired-token requests.

diego-ojeda-binbash and others added 2 commits November 14, 2026 19:19
- check_sso_token compared the token's UTC expiresAt with the local time:
  in UTC-3 an expired token was taken as valid for 3 more hours, and in
  UTC+ zones valid tokens were rejected.
- refresh_layer_credentials_mfa expected AccessDeniedException and
  ExpiredToken, but STS returns AccessDenied and ExpiredTokenException.
  A user without permission was asked for the MFA code 3 times without
  any message. Now AccessDenied exits with 40 and any other error exits
  with 50 at once, instead of asking for another code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…3 clients

Rewrites tests/test_modules/test_auth.py: a real PathsHandler over a
project in tmp_path, real config/credentials/token files, an isolated
HOME, and real sso/sts clients under botocore Stubber. Adds the first
MFA tests and a timezone case for check_sso_token. Drops the tests that
duplicated aws.py functions, already covered end to end in test_aws.py.

auth.py coverage goes from 60% to 93%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@diego-ojeda-binbash diego-ojeda-binbash added bug Something isn't working test labels Nov 14, 2026
@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change updates SSO token expiration checks to use UTC and adjusts MFA role-assumption error handling. The auth tests now use isolated project files and Stubber-backed AWS clients to cover SSO and MFA credential flows.

Changes

Authentication flows

Layer / File(s) Summary
SSO token validity
leverage/modules/auth.py, tests/test_modules/test_auth.py
The expiration check compares the SSO token’s UTC expiration with the current UTC time. Tests cover token selection, presence, validity, expiry, and timezone independence.
SSO credential refresh
tests/test_modules/test_auth.py
Tests cover provider and account credential refresh, including valid credentials, forced refresh, missing profiles or tokens, and SSO errors.
MFA role assumption errors
leverage/modules/auth.py, tests/test_modules/test_auth.py
The MFA handler recognizes AccessDenied and AccessDeniedException. Other unmatched errors reach the generic exit-code-50 error. Tests cover caching, retries, and failures.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Suggested reviewers: angelofenoglio, borland667

Merge Risk: Minimal · up to 05797

This PR makes SSO token expiry checks use UTC, so they no longer depend on the machine's local time zone. It also makes MFA role assumption fail with distinct exit codes for access-denied and other STS errors instead of silently prompting again. The changes are small and reversible, and no merge-blocking issue was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the two main changes in the PR: SSO token expiry is now compared in UTC, and the MFA handler now recognizes STS errors. It also mentions the auth.py test rewrite with Stubber. The raw …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two main changes in the PR: SSO token expiry is now compared in UTC, and the MFA handler now recognizes STS errors. It also mentions the auth.py test rewrite with Stubber. The raw summary and objectives confirm all three. The title is long and lists three items, but it is specific and accurate and does not mislead.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the token’s time,
And finds UTC in every line.
The MFA codes get one more try,
While cached credentials rest nearby.
SSO paths are tested through,
Then hops away beneath the moon.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6d8888ca-9465-443f-9e26-9f62ccebfa60
Commits

Reviewing files that changed from the base of the PR and between 0105847 and 057979c.

Files selected for processing: 2

  • leverage/modules/auth.py
  • tests/test_modules/test_auth.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@diego-ojeda-binbash diego-ojeda-binbash self-assigned this Nov 14, 2026
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 43647474237

Coverage increased (+4.1%) to 77.112%

Details

  • Coverage increased (+4.1%) from the base build.
  • Patch coverage: 8 of 8 lines across 1 file are fully covered (100%).
  • 6 coverage regressions across 1 file.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

6 previously-covered lines in 1 file lost coverage.

File Lines Losing Coverage Coverage
modules/auth.py 6 93.46%

Coverage Stats

Coverage Status
Relevant Lines: 4066
Covered Lines: 3252
Line Coverage: 79.98%
Relevant Branches: 1072
Covered Branches: 710
Branch Coverage: 66.23%
Branches in Coverage %: Yes
Coverage Strength: 0.8 hits per line

💛 - Coveralls

@diego-ojeda-binbash
diego-ojeda-binbash merged commit c488224 into master Nov 14, 2026
28 of 30 checks passed
@diego-ojeda-binbash
diego-ojeda-binbash deleted the feature/auth-stubber-tests branch November 14, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fix patch test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants