Skip to content

perf(stacks): cache per-component auth within a describe-stacks pass - #2644

Closed
Brian Ojeda (sgtoj) wants to merge 2 commits into
cloudposse:mainfrom
sgtoj:perf/dedupe-per-identity-auth
Closed

Brian Ojeda (sgtoj) wants to merge 2 commits into
cloudposse:mainfrom
sgtoj:perf/dedupe-per-identity-auth

Conversation

@sgtoj

@sgtoj Brian Ojeda (sgtoj) commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

what

  • Add a pass-scoped cache (authManagerCache) to the describe-stacks processor so components that share an auth section authenticate once per pass instead of once each.
  • Key it by the parent auth chain plus a deterministic JSON fingerprint of the component's auth section; reuse the cached manager on a match.
  • Add a regression test (TestResolveComponentAuthManager_CachesByAuthSection) and a fix doc.

why

  • Follow-up to fix(stacks): scope per-component auth to requested stack/components #2642. Even after scoping per-component auth to the requested stack, every component that declares its own default: true identity ran a full authentication cycle — a credentials-file rewrite (under a file lock) plus a keyring rebuild. On a stack whose components import the same default identity from a shared _defaults.yaml, that is N redundant cycles for one identity.
  • createComponentAuthManager derives its result solely from the component's auth section, the (constant) global config, and the parent manager (componentName/stackName are used only for logging), so components with an identical auth section produce an equivalent, identity-scoped manager — safe to reuse. Fingerprinting the auth section (rather than a bare identity name) keeps reuse provably correct: it never merges two components whose sections differ.
  • Measured on a representative multi-stack repo, atmos secret list -s <stack> (no --component): processor-path authentications 68 → 1, total auth cycles 110 → 43, wall time ~34s → ~21s, with identical output. Errors are never cached; sections that can't be serialized fall back to no caching.

references

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Fixed atmos secret list -s <stack> command hanging by eliminating unnecessary authentication for out-of-scope components.
  • Performance

    • Improved performance by reducing redundant authentication cycles when multiple components share identical authentication settings.
  • Tests

    • Added test coverage for scope-based authentication filtering and authentication result caching.

`processComponentEntry` resolved per-component auth (a full credential
write + file lock + keyring rebuild) before applying the stack and
component filters. Because describe-stacks walks every stack file,
`atmos secret list -s <stack>` authenticated every component in every
other stack before discarding it, scaling with the whole repo and
appearing to hang on large repos.

Move the stack and component filters ahead of resolveComponentAuthManager
so per-component auth runs only for in-scope components. Auth still
precedes its consumers (BuildTerraformWorkspace, Go-template and
YAML-function processing), so nothing downstream loses its AuthContext.
This also benefits `describe stacks -s` and the `list` family.

Adds a table-driven regression test asserting the resolver is not invoked
for out-of-scope stacks or components.

Closes cloudposse#2639
Even after scoping per-component auth to the requested stack (cloudposse#2642), every
component declaring its own default identity ran a full authentication cycle
(a credentials-file rewrite under a file lock plus a keyring rebuild). On a
stack whose components import the same default identity from a shared
_defaults.yaml, that is N redundant cycles for one identity.

Add a pass-scoped cache on the describe-stacks processor, keyed by the parent
auth chain plus a deterministic JSON fingerprint of the component auth section.
createComponentAuthManager derives its result solely from the auth section, the
constant global config, and the parent manager (componentName/stackName affect
only logging), so an identical auth section yields an equivalent, identity-scoped
manager — safe to reuse. Fingerprinting the auth section rather than a bare
identity name keeps reuse provably correct: it never merges components whose
sections differ.

Measured on a representative multi-stack repo, `atmos secret list -s <stack>`
without --component dropped from 68 processor-path authentications to 1 (total
auth cycles 110 -> 43, wall time ~34s -> ~21s) with identical output. Errors are
never cached; sections that cannot be serialized fall back to no caching.

Follows cloudposse#2642.
@sgtoj
Brian Ojeda (sgtoj) requested a review from a team as a code owner June 20, 2026 23:01
@atmos-pro

atmos-pro Bot commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Tip

Atmos Pro  

No affected stacks workflow was detected for this pull request.
If this is expected, no action is needed.
Learn More. Ask AI.

@github-actions github-actions Bot added the size/m Medium size PR label Jun 20, 2026
@sgtoj

Copy link
Copy Markdown
Contributor Author

Intended semver label: patch (performance fix; no new flag/command/config) — no blog post or roadmap update required. A maintainer will need to apply it (no label permission from a fork). Note: stacked on #2642, so the diff includes that fix commit until #2642 merges and this is rebased onto main.

@coderabbitai

coderabbitai Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a pass-scoped authManagerCache to describeStacksProcessor keyed by a JSON fingerprint of the component auth section plus the parent manager chain, and moves stack/component filters before resolveComponentAuthManager in processComponentEntry. Two test functions and two fix documentation articles accompany the changes.

Changes

describe-stacks Auth Filter Ordering and Per-Pass Cache

Layer / File(s) Summary
Filter reordering and authManagerCache implementation
internal/exec/describe_stacks_component_processor.go
Adds encoding/json import, authManagerCache field, and constructor init. Rewrites resolveComponentAuthManager with deterministic cache-key generation (JSON fingerprint + parent chain) and memoization. Moves the stack filter check above auth resolution in processComponentEntry so out-of-scope components skip auth entirely.
Unit tests for filter skip and cache behavior
internal/exec/describe_stacks_component_processor_auth_test.go
Adds TestProcessComponentEntry_OutOfScopeSkipsAuth (spy resolver asserts zero invocations for out-of-scope components) and TestResolveComponentAuthManager_CachesByAuthSection (asserts same AuthManager reused for identical auth sections, new resolver call for distinct sections).
Fix documentation
docs/fixes/2026-06-20-describe-stacks-per-identity-auth-cache.md, docs/fixes/2026-06-20-secret-list-stack-scope-per-component-auth.md
Documents the auth-cache memoization patch (symptom, root cause, fingerprinting strategy, verification) and the stack-scope filter ordering fix for atmos secret list -s <stack> (hang symptom, filter-before-auth behavioral change, follow-up recommendations).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

  • cloudposse/atmos#2411: Modifies the same resolveComponentAuthManager and processComponentEntry paths in describe_stacks_component_processor.go around per-component auth resolution error handling.

Suggested labels

patch

Suggested reviewers

  • osterman
  • aknysh
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely summarizes the main change: adding a pass-scoped cache for per-component authentication in the describe-stacks processor.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/exec/describe_stacks_component_processor_auth_test.go`:
- Line 431: Replace the three instances of assert.EqualValues with
require.EqualValues at lines 431, 467, and 477 in the test file. These
assertions validate critical filter behavior and cache correctness—if they fail,
the test should stop immediately rather than continue. Change assert.EqualValues
to require.EqualValues for each of the three occurrences to ensure test failures
are caught immediately and prevent false test passes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4b437e98-e05c-4dc5-8d33-146959a360a0

📥 Commits

Reviewing files that changed from the base of the PR and between 8a9754b and b7b6bc8.

📒 Files selected for processing (4)
  • docs/fixes/2026-06-20-describe-stacks-per-identity-auth-cache.md
  • docs/fixes/2026-06-20-secret-list-stack-scope-per-component-auth.md
  • internal/exec/describe_stacks_component_processor.go
  • internal/exec/describe_stacks_component_processor_auth_test.go

)

require.NoError(t, err, "out-of-scope component must be skipped without resolving auth")
assert.EqualValues(t, 0, spy.calls.Load(), "per-component auth must not run for an out-of-scope component")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Use require for critical test assertions, not assert.

Lines 431, 467, and 477 use assert.EqualValues to verify that the resolver was (or was not) invoked as expected. These are core assertions about filter behavior and cache correctness—if they fail, the test should fail immediately, not silently. Replace assert.EqualValues with require.EqualValues.

- assert.EqualValues(t, 0, spy.calls.Load(), "per-component auth must not run for an out-of-scope component")
+ require.EqualValues(t, 0, spy.calls.Load(), "per-component auth must not run for an out-of-scope component")

- assert.EqualValues(t, 1, spy.calls.Load(), "second component with an identical auth section must reuse the cached manager")
+ require.EqualValues(t, 1, spy.calls.Load(), "second component with an identical auth section must reuse the cached manager")

- assert.EqualValues(t, 2, spy.calls.Load(), "a distinct auth section must miss the cache and resolve anew")
+ require.EqualValues(t, 2, spy.calls.Load(), "a distinct auth section must miss the cache and resolve anew")

Also applies to: 467-467, 477-477

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/exec/describe_stacks_component_processor_auth_test.go` at line 431,
Replace the three instances of assert.EqualValues with require.EqualValues at
lines 431, 467, and 477 in the test file. These assertions validate critical
filter behavior and cache correctness—if they fail, the test should stop
immediately rather than continue. Change assert.EqualValues to
require.EqualValues for each of the three occurrences to ensure test failures
are caught immediately and prevent false test passes.

Source: Coding guidelines

@sgtoj

Copy link
Copy Markdown
Contributor Author

This extends #2642 . I didn't include the change in the previous PR because I didn't want to do anything to delay that fix being merged. This new PR extend that previous PR and improves performance. It reduced the time it took to complete this atmos secret list -s <stack> against a real world stack of 72 components. With only the changes in #2642, it 34 seconds. While with changes in this new PR took only 21 seconds.

@sgtoj

Copy link
Copy Markdown
Contributor Author

Superseded by #2652, which branches off the latest main as a single commit and contains both optimizations (filter-before-auth reorder + per-component auth cache), reframed as a general describe stacks fix (the original secret list symptom in #2639 was resolved separately by #2646).

@atmos-pro

atmos-pro Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Note

Atmos Pro  

Waiting for your GitHub Actions workflow to upload affected stacks.
Learn More.

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

Labels

size/m Medium size PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant