-
Notifications
You must be signed in to change notification settings - Fork 1
[BB-1481] Improve trust policy parsing and add test #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BB-1481] Improve trust policy parsing and add test #94
Conversation
…cy analysis and add coverage testing
WalkthroughRefactors AWS trust policy parsing from ad-hoc map handling to structured types (TrustPolicy, Statement, Action, Principal) with custom JSON unmarshalling for single/array forms. Adds detectPrincipalResource to classify principal ARNs. Adjusts extractTrustPrincipals filtering and makes trust-policy parse failures non-fatal in Grants. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🧰 Additional context used🧠 Learnings (2)📓 Common learnings📚 Learning: 2025-12-01T15:27:53.050ZApplied to files:
🧬 Code graph analysis (1)pkg/connector/helpers_test.go (1)
🔇 Additional comments (8)
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
pkg/connector/helpers.go(2 hunks)pkg/connector/helpers_test.go(1 hunks)pkg/connector/role.go(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: mateovespConductor
Repo: ConductorOne/baton-aws PR: 91
File: pkg/connector/helpers.go:191-213
Timestamp: 2025-12-01T15:27:53.050Z
Learning: AWS IAM role trust policies strictly forbid wildcards ("*" or "sts:*") in the Action field. AWS enforces this at the API level with the error "MalformedPolicyDocument: AssumeRole policy may only specify STS AssumeRole actions". Trust policies must explicitly specify "sts:AssumeRole" in the Action field.
📚 Learning: 2025-12-01T15:27:53.050Z
Learnt from: mateovespConductor
Repo: ConductorOne/baton-aws PR: 91
File: pkg/connector/helpers.go:191-213
Timestamp: 2025-12-01T15:27:53.050Z
Learning: AWS IAM role trust policies strictly forbid wildcards ("*" or "sts:*") in the Action field. AWS enforces this at the API level with the error "MalformedPolicyDocument: AssumeRole policy may only specify STS AssumeRole actions". Trust policies must explicitly specify "sts:AssumeRole" in the Action field.
Applied to files:
pkg/connector/helpers.go
🧬 Code graph analysis (1)
pkg/connector/helpers_test.go (1)
pkg/connector/helpers.go (4)
TrustPolicy(198-201)Statement(238-242)Action(245-245)Principal(268-270)
🪛 GitHub Check: go-lint
pkg/connector/helpers.go
[failure] 178-178:
Comment should end in a period (godot)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: test
- GitHub Check: go-lint
🔇 Additional comments (6)
pkg/connector/role.go (1)
178-183: LGTM! Non-fatal error handling is a reasonable approach.Logging the warning with
role_name,role_arn, and the error provides good observability while allowing other roles to continue processing.pkg/connector/helpers_test.go (1)
1-568: Comprehensive test coverage for the new structured types.The tests thoroughly cover:
- Polymorphic JSON unmarshalling (single vs array forms)
- Error handling for invalid inputs
- Edge cases (empty arrays, empty strings, wildcards)
- Real-world URL-encoded policy documents
The inline comments documenting the decoded JSON for URL-encoded test data are helpful for maintainability.
pkg/connector/helpers.go (4)
145-175: Clean refactor using structured types.The implementation correctly:
- Filters by
Effect == "Allow"andActioncontaining"sts:AssumeRole"- Uses
slices.Containsfor idiomatic action matching- Filters out empty string principals
Based on learnings, AWS enforces that trust policies must explicitly specify
"sts:AssumeRole"(wildcards are rejected at API level), so the exact match approach is appropriate.
203-235: LGTM! Correct use of type alias to avoid infinite recursion.The pattern of using a type alias (
Alias) when callingjson.Unmarshalwithin a customUnmarshalJSONis the correct approach to prevent stack overflow.
244-264: LGTM! Handles polymorphic Action field correctly.The unmarshalling logic properly handles both
"sts:AssumeRole"(string) and["sts:AssumeRole", "sts:TagSession"](array) forms.
266-310: LGTM! Properly handles AWS principal formats and ignores non-AWS principals.The implementation correctly:
- Ignores wildcard
"*"and service principals- Handles both
{"AWS": "arn:..."}and{"AWS": ["arn:...", "arn:..."]}forms- Returns
nilfor non-AWS principals without error (correct behavior)
Replace map[string]any with typed structures for trust policy analysis and add coverage testing
Description
Useful links:
Summary by CodeRabbit
New Features
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.