Skip to content

fix(stacks): scope and cache per-component auth in describe stacks - #2652

Merged
Andriy Knysh (aknysh) merged 1 commit into
cloudposse:mainfrom
sgtoj:fix/describe-stacks-per-component-auth
Jun 22, 2026
Merged

Andriy Knysh (aknysh) merged 1 commit into
cloudposse:mainfrom
sgtoj:fix/describe-stacks-per-component-auth

Conversation

@sgtoj

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

Copy link
Copy Markdown
Contributor

what

  • Move the stack and component filters above resolveComponentAuthManager in processComponentEntry so only in-scope components authenticate (auth still precedes BuildTerraformWorkspace, template, and YAML-function processing).
  • Add a pass-scoped auth cache keyed by the parent chain + a deterministic JSON fingerprint of the component auth section, so components that share an auth section reuse one authenticated manager.
  • Regression tests: out-of-scope skip + cache reuse.

why

Any auth-enabled ExecuteDescribeStacks caller — atmos describe stacks, atmos list values/instances, atmos terraform --all/--query — resolves per-component auth before the stack/component filters and never reuses it. On a multi-stack repo where components declare their own default: true identity, atmos describe stacks -s <stack> authenticates components in other stacks before discarding them, and re-authenticates each same-identity component from scratch — so the command effectively hangs.

Per-component auth exists only to populate info.AuthContext for that component's later template (atmos.Component(...)) and YAML-function (!terraform.state, !terraform.output) processing, which is skipped for filtered-out components — so authenticating them is wasted work.

#2646 fixed atmos secret list by disabling per-component auth for that command; it did not touch the shared processor, so every other caller still hits this.

Measured with the identical command atmos describe stacks -s <stack> --logs-level Debug under a 45s budget, only the atmos binary varying:

binary result
latest release (v1.221.1) did not complete within 45s (authenticating mostly out-of-scope stacks)
current main (aa68d85be) did not complete within 45s
this PR completed in ~18s

With the fix, in-scope processor-path authentications drop to 2 and out-of-scope ones to zero (the ~42 remaining auths are legitimate nested !terraform.output / atmos.Component reads).

references

In processComponentEntry, per-component authentication ran before the stack and
component filters and was never memoized. Any auth-enabled ExecuteDescribeStacks
caller (atmos describe stacks, list values/instances, terraform --all/--query)
authenticated every component in every stack before discarding the out-of-scope
ones, and re-authenticated each same-identity component from scratch. On a large
multi-stack repo, `atmos describe stacks -s <stack>` authenticates components in
stacks other than the one requested and effectively hangs.

cloudposse#2646 sidestepped this for `atmos secret list` by disabling per-component auth
there; this fixes the root cause in the shared processor for every other caller.

- Move the stack and component filters above resolveComponentAuthManager so only
  in-scope components authenticate. Auth still precedes its consumers
  (BuildTerraformWorkspace, template and YAML-function processing).
- Add a pass-scoped cache keyed by the parent chain plus a deterministic JSON
  fingerprint of the component auth section, so components sharing an auth
  section reuse one authenticated manager instead of re-running the full auth
  cycle (credential writes, file locks, keyring rebuilds).
- Add regression tests (out-of-scope skip + cache reuse).

Measured with the identical command (`describe stacks -s <stack> --logs-level
Debug`, 45s budget, only the atmos binary varies): the latest release (v1.221.1)
and current main both fail to complete within 45s, authenticating mostly
out-of-scope components, while this change completes in ~18s (43 auths; in-scope
processor-path auth reduced to 2).

Related to cloudposse#2639.
@sgtoj
Brian Ojeda (sgtoj) requested a review from a team as a code owner June 22, 2026 19:07
@atmos-pro

atmos-pro Bot commented Jun 22, 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.

@sgtoj

Brian Ojeda (sgtoj) commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Intended semver label: patch (bug fix + perf; no new flag/command/config) — no blog post or roadmap update required. Branches off latest main as a single commit; supersedes #2642 and #2644.

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f2210d20-4780-47af-8ff8-7c9ac2be9343

📥 Commits

Reviewing files that changed from the base of the PR and between aa68d85 and a187844.

📒 Files selected for processing (3)
  • docs/fixes/2026-06-22-describe-stacks-scope-and-cache-per-component-auth.md
  • internal/exec/describe_stacks_component_processor.go
  • internal/exec/describe_stacks_component_processor_auth_test.go

📝 Walkthrough

Walkthrough

Moves stack and component-inclusion filter evaluation ahead of per-component auth resolution in processComponentEntry. Adds a pass-scoped authManagerCache to describeStacksProcessor that deduplicates AuthManager resolution by parent auth chain plus a JSON fingerprint of each component's auth section. Two new tests and a fix documentation page accompany the change.

Changes

Describe-stacks per-component auth scoping and caching fix

Layer / File(s) Summary
Filter-before-auth reorder and AuthManager cache
internal/exec/describe_stacks_component_processor.go
Adds authManagerCache field to describeStacksProcessor and initializes it in the constructor. Extends resolveComponentAuthManager with cache key derivation (JSON fingerprint + parent chain), lookup, and population via new helpers. Reorders processComponentEntry so stack and component-inclusion filters run before auth resolution; auth stays positioned before template/YAML-function processing.
Out-of-scope skip and cache-hit tests
internal/exec/describe_stacks_component_processor_auth_test.go
TestProcessComponentEntry_OutOfScopeSkipsAuth asserts the auth resolver spy is never called when a component is excluded by stack mismatch or component filter. TestResolveComponentAuthManager_CachesByAuthSection asserts correct call counts and manager identity for cache-miss, cache-hit, and distinct-section cases.
Fix documentation
docs/fixes/2026-06-22-describe-stacks-scope-and-cache-per-component-auth.md
Describes the symptom, root cause, the two coordinated fixes, verification commands with timing evidence, and a follow-up recommendation for the nested YAML/template auth path.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

patch

Suggested reviewers

  • 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 directly and accurately summarizes the main changes: scoping and caching per-component auth in describe stacks commands.
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.

@aknysh Andriy Knysh (aknysh) added the patch A minor, backward compatible change label Jun 22, 2026
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.06%. Comparing base (aa68d85) to head (a187844).

Files with missing lines Patch % Lines
...ternal/exec/describe_stacks_component_processor.go 84.61% 2 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2652      +/-   ##
==========================================
+ Coverage   80.02%   80.06%   +0.03%     
==========================================
  Files        1361     1361              
  Lines      127449   127469      +20     
==========================================
+ Hits       101994   102056      +62     
+ Misses      19790    19749      -41     
+ Partials     5665     5664       -1     
Flag Coverage Δ
unittests 80.06% <84.61%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ternal/exec/describe_stacks_component_processor.go 94.96% <84.61%> (-0.46%) ⬇️

... and 11 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aknysh Andriy Knysh (aknysh) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@aknysh
Andriy Knysh (aknysh) merged commit e56077c into cloudposse:main Jun 22, 2026
65 of 67 checks passed
@atmos-pro

atmos-pro Bot commented Jun 22, 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

Copy link
Copy Markdown

These changes were released in v1.222.0-rc.7.

Andriy Knysh (aknysh) added a commit that referenced this pull request Jul 10, 2026
…entity (#2712)

* fix(auth): atmos.Component() nested lookups could lose or reuse the wrong identity

Two defects in the v1.222.0 per-component/nested auth-caching work
(#2652/#2656) could surface as "no valid credential sources found /
no EC2 IMDS role found" under GitHub OIDC auth with no ambient AWS
credentials:

- authContextWrapper.GetChain() always returns an empty slice, which
  the nested-auth cache used to disambiguate parents. Two different
  real identities reaching a nested lookup via atmos.Component() or
  !terraform.state/!terraform.output could collide on the same cache
  key and silently reuse the wrong identity's AuthManager. Wrapper-
  rooted parents are now never cached.
- atmos.Component() always fetched a target's terraform output using
  the enclosing component's credentials verbatim, unlike
  !terraform.state/!terraform.output, even when the target declared
  its own default-identity auth section. It now resolves the
  target's own auth via resolveAuthManagerForNestedComponent,
  falling back to the enclosing component's context as before.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* fix(auth): !terraform.output never resolved the nested target's own identity

The !terraform.output YAML function fetched a nested component's outputs
using the enclosing component's authContext/authManager verbatim — the
same defect just fixed for atmos.Component(). Only !terraform.state
actually routed through resolveAuthManagerForNestedComponent; the
target-aware resolution was lost when output fetching moved into
pkg/terraform/output (which cannot call back into internal/exec).

- defaultOutputGetter.GetOutput now resolves the target's own auth
  section via resolveNestedOutputAuth (target default identity wins;
  enclosing auth kept when the target has none, on resolver error, or
  when auth is disabled), mirroring GetTerraformState.
- The YAML entry propagates AuthDisabled via authContextWrapper when no
  AuthManager exists, mirroring !terraform.state, so disabled-auth
  contexts never trigger target authentication.
- Corrected the fix doc's false parity claim and recorded two audited
  same-class follow-ups (identity-less result-cache keys; store
  default-identity injection asymmetry).

Tests: TestResolveNestedOutputAuth (8 sub-tests, written failing-first).

Co-Authored-By: Claude Fable 5 <[email protected]>

* test(auth): use generated MockAuthManager and cover AuthDisabled wrapper propagation

Address PR #2712 review feedback and patch coverage:

- Replace the hand-written fakeNestedOutputAuthManager stub with the
  mockgen-generated types.NewMockAuthManager, per the repo's
  mock-generation convention (CodeRabbit review).
- Add TestAuthDisabledPropagatesWrapperToOutputGetter covering both
  branches of the new AuthDisabled wrapper propagation in
  processTagTerraformOutputWithContext — the uncovered lines behind
  the 60% patch coverage on yaml_func_terraform_output.go.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
Co-authored-by: Andriy Knysh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch A minor, backward compatible change size/m Medium size PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants