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

Check for empty region before invoking API in AWS SDK #7523

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/eks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ func newAWSProvider(spec *api.ProviderConfig, configurationLoader AWSConfigurati
return nil, err
}

if cfg.Region == "" {
return nil, fmt.Errorf("AWS Region must be set, please set the AWS Region in AWS config file or as environment variable")
}

if spec.Region == "" {
spec.Region = cfg.Region
Copy link
Member

Choose a reason for hiding this comment

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

eksctl/pkg/eks/apiv2.go

Lines 48 to 50 in 4e3d0e4

if pc.Region != "" {
options = append(options, config.WithRegion(pc.Region))
}

Looks like cfg.Region is set from spec.Region in newV2Config() on L184.

We should just move this check on spec.Region to before L184 and error if it's empty then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your suggestion.

I understood the current logic as the following;

  • if Spec.Region is not empty, cfg.Region is set from spec.Region in newV2Config()
  • if Spec.Region is empty, cfg.Region is set from ~/.aws/config or AWS_REGION in newV2Config()

So, I think we should check the value of cfg.Region after L184.
If cfg.Region is empty, we can determine AWS region is not defined.

//note

	cfg, err := newV2Config(spec, credentialsCacheFilePath, configurationLoader)
	if err != nil {
		return nil, err
	}

	fmt.Printf("spec.Region : %v\n", spec.Region)
	fmt.Printf("cfg.Region : %v\n", cfg.Region)

	if cfg.Region == "" {
		return nil, fmt.Errorf("AWS Region must be set, please set the AWS Region in AWS config file or as AWS_REGION environment variable")
	}

[result]

$ ./eksctl create cluster --dry-run
spec.Region : 
cfg.Region : ap-northeast-1

$ rm ~/.aws/config
$ ./eksctl create cluster --dry-run 
spec.Region : 
cfg.Region : 
Error: AWS Region must be set, please set the AWS Region in AWS config file or as AWS_REGION environment variable

$ export AWS_REGION=us-east-1      
$ ./eksctl create cluster --dry-run
spec.Region : 
cfg.Region : us-east-1

$ ./eksctl create cluster -f sample.yaml --dry-run
spec.Region : ap-northeast-1
cfg.Region : ap-northeast-1

}
Expand Down
8 changes: 8 additions & 0 deletions pkg/eks/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ var _ = Describe("eksctl API", func() {
},
err: fmt.Sprintf("cache file %s is not private", cacheFilePath),
}),
Entry("region code is not set", newAWSProviderEntry{
updateFakes: func(fal *fakes.FakeAWSConfigurationLoader) {
fal.LoadDefaultConfigReturns(aws.Config{
Region: "",
}, nil)
},
err: "AWS Region must be set, please set the AWS Region in AWS config file or as environment variable",
}),
Entry("creates the AWS provider successfully", newAWSProviderEntry{}),
)

Expand Down