Skip to content

Commit 7a745fc

Browse files
committed
fix: More error checking
1 parent 0b9d8d9 commit 7a745fc

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

internal/awshelper/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,18 @@ func CreateAwsConfig(ctx context.Context, l log.Logger, awsCfg *AwsSessionConfig
243243
cfg.Region = "us-east-1"
244244
}
245245

246-
if opts.IAMRoleOptions.RoleARN != "" {
246+
if opts != nil && opts.IAMRoleOptions.RoleARN != "" {
247247
if opts.IAMRoleOptions.WebIdentityToken != "" {
248248
l.Debugf("Assuming role %s using WebIdentity token", opts.IAMRoleOptions.RoleARN)
249249
cfg.Credentials = getWebIdentityCredentialsFromIAMRoleOptions(cfg, opts.IAMRoleOptions)
250250
} else {
251251
l.Debugf("Assuming role %s", opts.IAMRoleOptions.RoleARN)
252252
cfg.Credentials = getSTSCredentialsFromIAMRoleOptions(cfg, opts.IAMRoleOptions, "")
253253
}
254-
} else if creds := getCredentialsFromEnvs(opts); creds != nil {
255-
cfg.Credentials = creds
254+
} else if opts != nil {
255+
if creds := getCredentialsFromEnvs(opts); creds != nil {
256+
cfg.Credentials = creds
257+
}
256258
}
257259
} else {
258260
cfg, err = CreateAwsConfigFromConfig(ctx, awsCfg, opts)

internal/remotestate/backend/s3/client.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ func (client *Client) TagS3BucketAccessLogging(ctx context.Context, l log.Logger
567567

568568
_, err := client.s3Client.PutBucketTagging(ctx, &putBucketTaggingInput)
569569
if err != nil {
570+
if handleS3TaggingMethodNotAllowed(err, l, "access logging bucket") {
571+
return nil
572+
}
573+
570574
return errors.New(err)
571575
}
572576

@@ -601,6 +605,10 @@ func (client *Client) TagS3Bucket(ctx context.Context, l log.Logger) error {
601605

602606
_, err := client.s3Client.PutBucketTagging(ctx, &putBucketTaggingInput)
603607
if err != nil {
608+
if handleS3TaggingMethodNotAllowed(err, l, cfg.Bucket) {
609+
return nil
610+
}
611+
604612
return errors.New(err)
605613
}
606614

@@ -718,6 +726,7 @@ func (client *Client) EnableRootAccesstoS3Bucket(ctx context.Context, l log.Logg
718726

719727
// Access bucket name safely through defensive checking
720728
config := client.ExtendedRemoteStateConfigS3
729+
721730
bucket := config.RemoteStateConfigS3.Bucket
722731
if bucket == "" {
723732
return errors.Errorf("S3 bucket name is empty - cannot enable root access to S3 bucket")
@@ -733,6 +742,7 @@ func (client *Client) EnableRootAccesstoS3Bucket(ctx context.Context, l log.Logg
733742
if err != nil {
734743
return errors.Errorf("error getting AWS account ID %s for bucket %s: %w", accountID, bucket, err)
735744
}
745+
736746
if accountID == "" {
737747
return errors.Errorf("AWS account ID is empty - cannot enable root access to S3 bucket %s", bucket)
738748
}
@@ -741,6 +751,7 @@ func (client *Client) EnableRootAccesstoS3Bucket(ctx context.Context, l log.Logg
741751
if err != nil {
742752
return errors.Errorf("error getting AWS partition %s for bucket %s: %w", partition, bucket, err)
743753
}
754+
744755
if partition == "" {
745756
return errors.Errorf("AWS partition is empty - cannot enable root access to S3 bucket %s", bucket)
746757
}
@@ -1898,3 +1909,15 @@ func isAWSResourceNotFoundError(err error) bool {
18981909
var apiErr smithy.APIError
18991910
return errors.As(err, &apiErr) && apiErr.ErrorCode() == "ResourceNotFoundException"
19001911
}
1912+
1913+
// handleS3TaggingMethodNotAllowed handles MethodNotAllowed errors for S3 bucket tagging operations
1914+
// Returns true if the error was handled (caller should return nil), false otherwise
1915+
func handleS3TaggingMethodNotAllowed(err error, l log.Logger, bucketName string) bool {
1916+
var apiErr smithy.APIError
1917+
if errors.As(err, &apiErr) && apiErr.ErrorCode() == "MethodNotAllowed" {
1918+
l.Warnf("S3 bucket tagging is not supported for bucket %s - skipping tagging (this is normal for some AWS configurations)", bucketName)
1919+
return true
1920+
}
1921+
1922+
return false
1923+
}

0 commit comments

Comments
 (0)