Skip to content
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

Use local aws config in cli to get account and regions #7758

Conversation

vishwahiremat
Copy link
Contributor

Description

Today for rad init --full command while configuring aws provider, we are using accesskey and secretkey provided by user to get accountid and regions, but for irsa we cannot get the account id and region information.

So instead of using accesskey and secretkey to authenticate , updated it to use the local aws config to authenticate and get account id and regions informaton.

Type of change

  • This pull request fixes a bug in Radius and has an approved issue (issue link required).
  • This pull request adds or changes features of Radius and has an approved issue (issue link required).
  • This pull request is a minor refactor, code cleanup, test improvement, or other maintenance task and doesn't change the functionality of Radius (issue link optional).

Fixes: #issue_number

Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
@vishwahiremat vishwahiremat requested review from a team as code owners July 23, 2024 21:16
Copy link

codecov bot commented Jul 23, 2024

Codecov Report

Attention: Patch coverage is 44.73684% with 21 lines in your changes missing coverage. Please review.

Project coverage is 61.04%. Comparing base (63490d6) to head (8c95e50).

Files Patch % Lines
pkg/cli/aws/client.go 0.00% 10 Missing ⚠️
pkg/cli/cmd/radinit/aws.go 56.25% 5 Missing and 2 partials ⚠️
pkg/cli/aws/client_mock.go 66.66% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7758      +/-   ##
==========================================
- Coverage   61.05%   61.04%   -0.02%     
==========================================
  Files         523      523              
  Lines       27361    27367       +6     
==========================================
  Hits        16706    16706              
- Misses       9184     9187       +3     
- Partials     1471     1474       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

func (c *client) GetCallerIdentity(ctx context.Context, region string) (*sts.GetCallerIdentityOutput, error) {
// Load the AWS SDK config and credentials
cfg, err := config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile("default"))
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

should "default" be a const?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the config.WithSharedConfigProfile

pkg/cli/aws/client.go Outdated Show resolved Hide resolved
func (r *Runner) getAccountId(ctx context.Context, accessKeyID, secretAccessKey string) (string, error) {
callerIdentityOutput, err := r.awsClient.GetCallerIdentity(ctx, QueryRegion, accessKeyID, secretAccessKey)
func (r *Runner) getAccountId(ctx context.Context) (string, error) {
callerIdentityOutput, err := r.awsClient.GetCallerIdentity(ctx, QueryRegion)
if err != nil {
return "", clierrors.MessageWithCause(err, "AWS credential verification failed.")
}
Copy link
Contributor

@nithyatsu nithyatsu Jul 23, 2024

Choose a reason for hiding this comment

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

here, would it help user if we add some detail, like "AWS credential verification failed. Please use aws configure to configure credentials and then try again " ? cc @Reshrahim

pkg/cli/aws/client.go Outdated Show resolved Hide resolved
pkg/cli/aws/client.go Outdated Show resolved Hide resolved
svc := ec2.NewFromConfig(cfg)

// Call DescribeRegions
input := &ec2.DescribeRegionsInput{}
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the motivation for initializing empty input?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated it

Copy link
Contributor

Choose a reason for hiding this comment

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

the update doesn't seem to be reflected here

pkg/cli/aws/client.go Outdated Show resolved Hide resolved
Comment on lines 46 to 49
func (c *client) GetCallerIdentity(ctx context.Context, region string) (*sts.GetCallerIdentityOutput, error) {
// Load the AWS SDK config and credentials
cfg, err := config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile("default"))
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Switching this to use local configuration is more nuanced than the call to list regions. The credentials registered with the environment do not necessarily need to belong to the same account as the account the user has setup locally, and it will certainly be different for all production scenarios. So I'd suggest making this a user input. Please bounce it by @Reshrahim @willtsai to confirm if there are any concerns with updating this user experience.

Copy link
Contributor

Choose a reason for hiding this comment

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

Chatted offline with @vishwahiremat and @willtsai, agreed to go forward with the following approach -

  • User provides irsa role to register
  • Radius fetches aws account id for the credentials configured in users local aws config. This account could be different from the account id that the irsa role belongs to.
    - Ask user if this account id should be configured with the environment, or do they want to provide another account id.
  • List region and selection experience stays the same, internally we will fetch it using locally configured aws credentials. It will fail if no local aws credentials are configured.

Here's a refresher on the current state:

  • User provides aws access key and secret key
  • Radius fetches corresponding account id using the provides access key and configures that on the environment
  • Radius lists regions and asks the user to pick a region

pkg/cli/aws/client.go Outdated Show resolved Hide resolved
@radius-functional-tests
Copy link

radius-functional-tests bot commented Jul 24, 2024

Radius functional test overview

🔍 Go to test action run

Name Value
Repository vishwahiremat/radius
Commit ref 94d35f7
Unique ID func314350b3cf
Image tag pr-func314350b3cf
Click here to see the list of tools in the current test run
  • gotestsum 1.12.0
  • KinD: v0.20.0
  • Dapr: 1.12.0
  • Azure KeyVault CSI driver: 1.4.2
  • Azure Workload identity webhook: 1.1.0
  • Bicep recipe location ghcr.io/radius-project/dev/test/testrecipes/test-bicep-recipes/<name>:pr-func314350b3cf
  • Terraform recipe location http://tf-module-server.radius-test-tf-module-server.svc.cluster.local/<name>.zip (in cluster)
  • applications-rp test image location: ghcr.io/radius-project/dev/applications-rp:pr-func314350b3cf
  • controller test image location: ghcr.io/radius-project/dev/controller:pr-func314350b3cf
  • ucp test image location: ghcr.io/radius-project/dev/ucpd:pr-func314350b3cf
  • deployment-engine test image location: ghcr.io/radius-project/deployment-engine:latest

Test Status

⌛ Building Radius and pushing container images for functional tests...
✅ Container images build succeeded
⌛ Publishing Bicep Recipes for functional tests...
✅ Recipe publishing succeeded
⌛ Starting ucp-cloud functional tests...
⌛ Starting corerp-cloud functional tests...
⌛ Starting datastoresrp-cloud functional tests...
✅ ucp-cloud functional tests succeeded
✅ datastoresrp-cloud functional tests succeeded
✅ corerp-cloud functional tests succeeded

result, err := stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
func (c *client) GetCallerIdentity(ctx context.Context, region string) (*sts.GetCallerIdentityOutput, error) {
// Load the AWS SDK config and credentials
cfg, err := config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile("default"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we ever use anything other than default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the config.WithSharedConfigProfile

return nil, err
}

// Create an STS client
Copy link
Contributor

Choose a reason for hiding this comment

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

What is an STS Client?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Security Token Service Client and it is generally used when short-term access is needed to privileged AWS resources

pkg/cli/aws/client.go Outdated Show resolved Hide resolved
Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
Comment on lines 38 to 39
enterAWSAccountIDPrompt = "Enter the account ID you want to use:"
enterAWSAccountIDPlaceholder = "Enter the account ID you want to use.."
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please check that the format/phrasing is consistent with rest of the prompts?

Suggested change
enterAWSAccountIDPrompt = "Enter the account ID you want to use:"
enterAWSAccountIDPlaceholder = "Enter the account ID you want to use.."
enterAWSAccountIDPrompt = "Enter the account ID:"
enterAWSAccountIDPlaceholder = "Enter the account ID you want to use..."

if err != nil {
return nil, err
}

region, err := r.selectAWSRegion(ctx, QueryRegion, accessKeyID, secretAccessKey)
addingCloudProvider, err := prompt.YesOrNoPrompt(fmt.Sprintf(confirmAWSAccountIDPromptFmt, accountId), prompt.ConfirmYes, r.Prompter)
Copy link
Contributor

@kachawla kachawla Jul 30, 2024

Choose a reason for hiding this comment

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

this code is a bit difficult to understand, what is the intended usage of addingCloudProvider? Isn't this decided based on the inputs for confirmCloudProviderPrompt and selectCloudProviderPrompt prompts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i missed to change the variable name here, updated it. changed it from addingCloudProvider -> addingAccountID

region, err := r.selectAWSRegion(ctx, QueryRegion, accessKeyID, secretAccessKey)
addingCloudProvider, err := prompt.YesOrNoPrompt(fmt.Sprintf(confirmAWSAccountIDPromptFmt, accountId), prompt.ConfirmYes, r.Prompter)
if err != nil {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

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

what would cause this to error out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

error could be caused when creating a new Program in GetListInput function.

model, err := tea.NewProgram(tm).Run()
if err != nil {
return "", err
}

}

if !addingCloudProvider {
accountId, err = r.Prompter.GetTextInput("Enter the account ID you want to use: ", prompt.TextInputOptions{Placeholder: enterAWSAccountIDPlaceholder})
Copy link
Contributor

Choose a reason for hiding this comment

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

use the constant defined above for the prompt text

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated it

@@ -84,8 +97,8 @@ func (r *Runner) getAccountId(ctx context.Context, accessKeyID, secretAccessKey
return *callerIdentityOutput.Account, nil
}

func (r *Runner) selectAWSRegion(ctx context.Context, region, accessKeyID, secretAccessKey string) (string, error) {
listRegionsOutput, err := r.awsClient.ListRegions(ctx, region, accessKeyID, secretAccessKey)
func (r *Runner) selectAWSRegion(ctx context.Context) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

just for easier context, can you please add a comment here that it uses local aws config to list regions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added it

}

if !addingCloudProvider {
accountId, err = r.Prompter.GetTextInput("Enter the account ID you want to use: ", prompt.TextInputOptions{Placeholder: enterAWSAccountIDPlaceholder})
Copy link
Contributor

Choose a reason for hiding this comment

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

can all of the accountID related code be abstracted out into getAccountId?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved accoutID related code into getAccountID

@radius-functional-tests
Copy link

radius-functional-tests bot commented Jul 30, 2024

Radius functional test overview

🔍 Go to test action run

Name Value
Repository vishwahiremat/radius
Commit ref b9ecbfe
Unique ID func941a852dd7
Image tag pr-func941a852dd7
Click here to see the list of tools in the current test run
  • gotestsum 1.12.0
  • KinD: v0.20.0
  • Dapr: 1.12.0
  • Azure KeyVault CSI driver: 1.4.2
  • Azure Workload identity webhook: 1.1.0
  • Bicep recipe location ghcr.io/radius-project/dev/test/testrecipes/test-bicep-recipes/<name>:pr-func941a852dd7
  • Terraform recipe location http://tf-module-server.radius-test-tf-module-server.svc.cluster.local/<name>.zip (in cluster)
  • applications-rp test image location: ghcr.io/radius-project/dev/applications-rp:pr-func941a852dd7
  • controller test image location: ghcr.io/radius-project/dev/controller:pr-func941a852dd7
  • ucp test image location: ghcr.io/radius-project/dev/ucpd:pr-func941a852dd7
  • deployment-engine test image location: ghcr.io/radius-project/deployment-engine:latest

Test Status

⌛ Building Radius and pushing container images for functional tests...
✅ Container images build succeeded
⌛ Publishing Bicep Recipes for functional tests...
✅ Recipe publishing succeeded
⌛ Starting ucp-cloud functional tests...
⌛ Starting datastoresrp-cloud functional tests...
✅ ucp-cloud functional tests succeeded
✅ datastoresrp-cloud functional tests succeeded
⌛ Starting corerp-cloud functional tests...
✅ corerp-cloud functional tests succeeded

Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
return nil, err
}

// Create an EC2 client
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if we need these comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed it

Comment on lines 62 to 72
// addAccountID, err := prompt.YesOrNoPrompt(fmt.Sprintf(confirmAWSAccountIDPromptFmt, accountId), prompt.ConfirmYes, r.Prompter)
// if err != nil {
// return nil, err
// }

// if !addAccountID {
// accountId, err = r.Prompter.GetTextInput(enterAWSAccountIDPrompt, prompt.TextInputOptions{Placeholder: enterAWSAccountIDPlaceholder})
// if err != nil {
// return nil, err
// }
// }
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this supposed to be uncommented?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

missed to removed thess, updated in the pr

return "", err
}

if !addAccountID {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be true so that the user can enter account id? Like the logic here is that if addAccountID is false then ask the user to enter AWS account id? Am I missing something here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we retrieve the account id using the local config , and in the yes or no prompt ask the user if they want to use the account id from local config, if user selects no then we propmpt to provide the account id.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can have a more meaningful name instead of addAccountID then I guess.

func (r *Runner) selectAWSRegion(ctx context.Context, region, accessKeyID, secretAccessKey string) (string, error) {
listRegionsOutput, err := r.awsClient.ListRegions(ctx, region, accessKeyID, secretAccessKey)
// selectAWSRegion prompts the user to select an AWS region from a list of available regions.
// regions list is retrieved using the locally configured AWS account.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// regions list is retrieved using the locally configured AWS account.
// Region list is retrieved using the locally configured AWS account.

Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
ytimocin
ytimocin previously approved these changes Aug 5, 2024
return "", err
}

if !addAccountID {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can have a more meaningful name instead of addAccountID then I guess.

@radius-functional-tests
Copy link

radius-functional-tests bot commented Aug 5, 2024

Radius functional test overview

🔍 Go to test action run

Name Value
Repository vishwahiremat/radius
Commit ref 77004be
Unique ID func79236b5f64
Image tag pr-func79236b5f64
Click here to see the list of tools in the current test run
  • gotestsum 1.12.0
  • KinD: v0.20.0
  • Dapr: 1.12.0
  • Azure KeyVault CSI driver: 1.4.2
  • Azure Workload identity webhook: 1.1.0
  • Bicep recipe location ghcr.io/radius-project/dev/test/testrecipes/test-bicep-recipes/<name>:pr-func79236b5f64
  • Terraform recipe location http://tf-module-server.radius-test-tf-module-server.svc.cluster.local/<name>.zip (in cluster)
  • applications-rp test image location: ghcr.io/radius-project/dev/applications-rp:pr-func79236b5f64
  • controller test image location: ghcr.io/radius-project/dev/controller:pr-func79236b5f64
  • ucp test image location: ghcr.io/radius-project/dev/ucpd:pr-func79236b5f64
  • deployment-engine test image location: ghcr.io/radius-project/deployment-engine:latest

Test Status

⌛ Building Radius and pushing container images for functional tests...
✅ Container images build succeeded
⌛ Publishing Bicep Recipes for functional tests...
✅ Recipe publishing succeeded
⌛ Starting ucp-cloud functional tests...
⌛ Starting datastoresrp-cloud functional tests...
⌛ Starting corerp-cloud functional tests...
✅ datastoresrp-cloud functional tests succeeded
✅ corerp-cloud functional tests succeeded
❌ Test tool installation for ucp-cloud failed. Please check the logs for more details
❌ Failed to install Radius for ucp-cloud functional test. Please check the logs for more details
❌ ucp-cloud functional test failed. Please check the logs for more details

Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
@radius-functional-tests
Copy link

radius-functional-tests bot commented Aug 5, 2024

Radius functional test overview

🔍 Go to test action run

Name Value
Repository vishwahiremat/radius
Commit ref 8c95e50
Unique ID func1af5eef32b
Image tag pr-func1af5eef32b
Click here to see the list of tools in the current test run
  • gotestsum 1.12.0
  • KinD: v0.20.0
  • Dapr: 1.12.0
  • Azure KeyVault CSI driver: 1.4.2
  • Azure Workload identity webhook: 1.1.0
  • Bicep recipe location ghcr.io/radius-project/dev/test/testrecipes/test-bicep-recipes/<name>:pr-func1af5eef32b
  • Terraform recipe location http://tf-module-server.radius-test-tf-module-server.svc.cluster.local/<name>.zip (in cluster)
  • applications-rp test image location: ghcr.io/radius-project/dev/applications-rp:pr-func1af5eef32b
  • controller test image location: ghcr.io/radius-project/dev/controller:pr-func1af5eef32b
  • ucp test image location: ghcr.io/radius-project/dev/ucpd:pr-func1af5eef32b
  • deployment-engine test image location: ghcr.io/radius-project/deployment-engine:latest

Test Status

⌛ Building Radius and pushing container images for functional tests...
✅ Container images build succeeded
⌛ Publishing Bicep Recipes for functional tests...
✅ Recipe publishing succeeded
⌛ Starting datastoresrp-cloud functional tests...
⌛ Starting corerp-cloud functional tests...
✅ datastoresrp-cloud functional tests succeeded
⌛ Starting ucp-cloud functional tests...
✅ corerp-cloud functional tests succeeded
✅ ucp-cloud functional tests succeeded

@sk593 sk593 merged commit 4ba025d into radius-project:main Aug 5, 2024
26 checks passed
superbeeny pushed a commit to superbeeny/radius that referenced this pull request Aug 14, 2024
…t#7758)

# Description

Today for `rad init --full` command while configuring `aws` provider, we
are using `accesskey` and `secretkey` provided by user to get accountid
and regions, but for `irsa` we cannot get the account id and region
information.

So instead of using `accesskey` and `secretkey` to authenticate ,
updated it to use the local aws config to authenticate and get account
id and regions informaton.

## Type of change

<!--

Please select **one** of the following options that describes your
change and delete the others. Clearly identifying the type of change you
are making will help us review your PR faster, and is used in authoring
release notes.

If you are making a bug fix or functionality change to Radius and do not
have an associated issue link please create one now.

-->

- This pull request fixes a bug in Radius and has an approved issue
(issue link required).
- This pull request adds or changes features of Radius and has an
approved issue (issue link required).
- This pull request is a minor refactor, code cleanup, test improvement,
or other maintenance task and doesn't change the functionality of Radius
(issue link optional).

<!--

Please update the following to link the associated issue. This is
required for some kinds of changes (see above).

-->

Fixes: #issue_number

---------

Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
Co-authored-by: Karishma Chawla <kachawla@microsoft.com>
Reshrahim pushed a commit to Reshrahim/radius that referenced this pull request Aug 27, 2024
…t#7758)

# Description

Today for `rad init --full` command while configuring `aws` provider, we
are using `accesskey` and `secretkey` provided by user to get accountid
and regions, but for `irsa` we cannot get the account id and region
information.

So instead of using `accesskey` and `secretkey` to authenticate ,
updated it to use the local aws config to authenticate and get account
id and regions informaton.

## Type of change

<!--

Please select **one** of the following options that describes your
change and delete the others. Clearly identifying the type of change you
are making will help us review your PR faster, and is used in authoring
release notes.

If you are making a bug fix or functionality change to Radius and do not
have an associated issue link please create one now.

-->

- This pull request fixes a bug in Radius and has an approved issue
(issue link required).
- This pull request adds or changes features of Radius and has an
approved issue (issue link required).
- This pull request is a minor refactor, code cleanup, test improvement,
or other maintenance task and doesn't change the functionality of Radius
(issue link optional).

<!--

Please update the following to link the associated issue. This is
required for some kinds of changes (see above).

-->

Fixes: #issue_number

---------

Signed-off-by: Vishwanath Hiremath <vhiremath@microsoft.com>
Co-authored-by: Karishma Chawla <kachawla@microsoft.com>
Signed-off-by: Reshma Abdul Rahim <reshmarahim.abdul@microsoft.com>
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.

5 participants