Skip to content

Commit

Permalink
Implement better error message when the AWS region or AWS default reg…
Browse files Browse the repository at this point in the history
…ion is missing (closes #506) (#507)
  • Loading branch information
christophetd committed Apr 4, 2024
1 parent 4a23f17 commit f84ad92
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
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

0 comments on commit f84ad92

Please sign in to comment.