Skip to content

Comments

Handle AWS role name permission issue#689

Merged
ehsandeep merged 1 commit intodevfrom
fix-aws-role-name-permission
Jul 11, 2025
Merged

Handle AWS role name permission issue#689
ehsandeep merged 1 commit intodevfrom
fix-aws-role-name-permission

Conversation

@mkrs2404
Copy link
Contributor

@mkrs2404 mkrs2404 commented Jul 11, 2025

This PR handles the scenario when a user does not have permission to DescribeRegions unless they assume the role first.
Currently, cloudlist describes the regions first and then assumes the role which causes an issue in this case.

Summary by CodeRabbit

  • New Features

    • Improved AWS provider initialization with enhanced handling for role assumption and region discovery, including a fallback mechanism for listing regions when initial permissions are insufficient.
  • Bug Fixes

    • Added more robust error handling and descriptive messages during AWS region discovery failures.

@mkrs2404 mkrs2404 requested a review from ShubhamRasal July 11, 2025 07:50
@mkrs2404 mkrs2404 self-assigned this Jul 11, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 11, 2025

Walkthrough

The changes update the AWS provider initialization to improve how roles are assumed and how AWS regions are discovered. Optional fields for role assumption are now set conditionally, and a fallback mechanism is introduced for region discovery if initial attempts fail due to insufficient permissions.

Changes

File(s) Change Summary
pkg/providers/aws/aws.go Refined role assumption by conditionally setting session name and external ID; added fallback logic for region discovery using alternative role assumption if initial call fails.

Sequence Diagram(s)

sequenceDiagram
    participant ProviderInit as AWS Provider Initialization
    participant BaseSession as Base AWS Session
    participant AssumeRole as Assume Role (if needed)
    participant DescribeRegions as DescribeRegions Call

    ProviderInit->>BaseSession: Create base session
    BaseSession->>DescribeRegions: Call DescribeRegions
    alt Success
        DescribeRegions-->>ProviderInit: Return regions
    else Failure & AssumeRoleName+AccountIds set
        ProviderInit->>AssumeRole: Construct role ARN, set session name/external ID
        AssumeRole->>BaseSession: Create temp session with assumed credentials
        BaseSession->>DescribeRegions: Retry DescribeRegions
        DescribeRegions-->>ProviderInit: Return regions or error
    end
Loading

Poem

In the cloud where regions hide,
A bunny tweaks the code with pride.
If first the list of lands is blocked,
A clever fallback is unlocked!
With session names and roles anew,
AWS, we’re hopping through.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
pkg/providers/aws/aws.go (2)

181-232: Review the fallback logic assumptions and consider refactoring.

The fallback mechanism addresses the core issue well, but there are several considerations:

  1. Error assumption: The code assumes that any DescribeRegions failure with AssumeRoleName configured is due to permission issues. Other errors (network, invalid regions, etc.) could trigger unnecessary role assumption attempts.

  2. Code duplication: The role assumption logic is duplicated between lines 140-153 and 191-203. Consider extracting this into a helper function.

  3. Single account limitation: Only the first account ID is used for role ARN construction. This may be intentional but could be a limitation for multi-account scenarios.

Consider this refactoring to reduce duplication:

+func (p *Provider) createAssumeRoleInput(roleArn string, options *ProviderOptions) *sts.AssumeRoleInput {
+	roleInput := &sts.AssumeRoleInput{
+		RoleArn: aws.String(roleArn),
+	}
+	
+	if options.AssumeRoleSessionName != "" {
+		roleInput.RoleSessionName = aws.String(options.AssumeRoleSessionName)
+	} else {
+		roleInput.RoleSessionName = aws.String("cloudlist-session")
+	}
+	
+	if options.ExternalId != "" {
+		roleInput.ExternalId = aws.String(options.ExternalId)
+	}
+	
+	return roleInput
+}

Then use it in both places:

-		roleInput := &sts.AssumeRoleInput{
-			RoleArn: aws.String(options.AssumeRoleArn),
-		}
-		
-		if options.AssumeRoleSessionName != "" {
-			roleInput.RoleSessionName = aws.String(options.AssumeRoleSessionName)
-		} else {
-			roleInput.RoleSessionName = aws.String("cloudlist-session")
-		}
-		
-		if options.ExternalId != "" {
-			roleInput.ExternalId = aws.String(options.ExternalId)
-		}
+		roleInput := p.createAssumeRoleInput(options.AssumeRoleArn, options)

186-189: Consider more specific error checking.

The current logic assumes any error is due to permission issues, but other errors could trigger unnecessary role assumption attempts. Consider checking for specific AWS error codes related to permissions.

-if err != nil && options.AssumeRoleName != "" && len(options.AccountIds) > 0 {
+if err != nil && options.AssumeRoleName != "" && len(options.AccountIds) > 0 {
+	// Check if error is specifically related to permissions
+	if awsErr, ok := err.(awserr.Error); ok {
+		if awsErr.Code() == "UnauthorizedOperation" || awsErr.Code() == "AccessDenied" {
+			// Permission-related error, try with assumed role
+		} else {
+			return nil, errors.Wrap(err, "could not get list of regions")
+		}
+	}

Note: This would require importing github.com/aws/aws-sdk-go/aws/awserr.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 280296a and dd6f071.

📒 Files selected for processing (1)
  • pkg/providers/aws/aws.go (4 hunks)
⏰ 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). (6)
  • GitHub Check: release-test
  • GitHub Check: Lint Test
  • GitHub Check: Test Builds (1.22.x, ubuntu-latest)
  • GitHub Check: Test Builds (1.22.x, macOS-latest)
  • GitHub Check: Test Builds (1.22.x, windows-latest)
  • GitHub Check: Analyze (go)
🔇 Additional comments (2)
pkg/providers/aws/aws.go (2)

5-5: LGTM: Required import addition.

The fmt import is necessary for the fmt.Sprintf call introduced in the fallback logic.


132-154: LGTM: Improved optional field handling.

The refactoring to conditionally set optional fields (RoleSessionName and ExternalId) is a good practice that avoids passing empty strings to the AWS API. The default session name "cloudlist-session" is appropriate.

@ehsandeep ehsandeep merged commit 1bf1103 into dev Jul 11, 2025
9 checks passed
@ehsandeep ehsandeep deleted the fix-aws-role-name-permission branch July 11, 2025 08:54
visnetodev pushed a commit to visnetotest/cloudlist that referenced this pull request Dec 7, 2025
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.

3 participants