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

Implement better error message when the AWS region or AWS default region is missing #507

Merged
merged 1 commit into from
Apr 4, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion v2/internal/providers/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
smithyhttp "github.com/aws/smithy-go/transport/http"
"github.com/google/uuid"
"log"
"os"
)

type AWSProvider struct {
Expand All @@ -34,7 +35,17 @@ func (m *AWSProvider) IsAuthenticatedAgainstAWS() bool {
// instead of sts:GetCallerIdentity, to ensure an AWS region was properly set
ec2Client := ec2.NewFromConfig(m.GetConnection())
_, err := ec2Client.DescribeAccountAttributes(context.Background(), &ec2.DescribeAccountAttributesInput{})
return err == nil
if err != nil {
return false
}

// Note: Explicitly setting AWS_REGION/AWS_DEFAULT_REGION is not strictly required for the AWS SDK to work, but it is necessary for Terraform
// If it's not set, we get a user-unfriendly error such as the one describe at https://github.com/DataDog/stratus-red-team/issues/506
if os.Getenv("AWS_REGION") == "" && os.Getenv("AWS_DEFAULT_REGION") == "" {
return false
}

return true
}

// Functions below are related to customization of the user-agent header
Expand Down
7 changes: 4 additions & 3 deletions v2/pkg/stratus/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ func EnsureAuthenticated(platform Platform) error {
switch platform {
case AWS:
if !providerFactory.AWS().IsAuthenticatedAgainstAWS() {
return errors.New("you are not authenticated against AWS, or you have not set your region. " +
"Make sure you are authenticated against AWS, and you have a default region set in your AWS config " +
"or environment (export AWS_DEFAULT_REGION=us-east-1)")
return errors.New("you are not authenticated against AWS, *or* you have not set your region. \n\n" +
"Troubleshooting:\n" +
"1. Are you authenticated against AWS?\n" +
"2. Do you have a region or default region set (whether in your AWS configuration file or in your environment)? If not, run 'export AWS_REGION=xxx'")
}
case Azure:
if !providerFactory.Azure().IsAuthenticatedAgainstAzure() {
Expand Down
Loading