Skip to content

Commit 207f550

Browse files
committed
fail on empty imds profile env
1 parent 49c542f commit 207f550

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

config/config_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"reflect"
8+
"strings"
89
"testing"
910

1011
"github.com/aws/aws-sdk-go-v2/aws"
@@ -148,6 +149,17 @@ func TestLoadDefaultConfig(t *testing.T) {
148149
}
149150
}
150151

152+
func TestLoadDefaultConfig_EmptyEC2InstanceProfileName(t *testing.T) {
153+
t.Setenv(awsEc2InstanceProfileNameEnv, "")
154+
_, err := LoadDefaultConfig(context.TODO())
155+
if err == nil {
156+
t.Fatal("expect error, got none")
157+
}
158+
if expect, actual := "env AWS_EC2_INSTANCE_PROFILE_NAME cannot be empty", err.Error(); !strings.Contains(actual, expect) {
159+
t.Fatalf("expect error %s, got %s", expect, actual)
160+
}
161+
}
162+
151163
func BenchmarkLoadProfile1(b *testing.B) {
152164
benchConfigLoad(b, 1)
153165
}

config/env_config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,11 @@ func NewEnvConfig() (EnvConfig, error) {
352352

353353
cfg.AppID = os.Getenv(awsSdkUaAppIDEnv)
354354

355-
cfg.EC2InstanceProfileName = os.Getenv(awsEc2InstanceProfileNameEnv)
355+
ec2InstanceProfileName, ok := os.LookupEnv(awsEc2InstanceProfileNameEnv)
356+
if ok && ec2InstanceProfileName == "" {
357+
return cfg, fmt.Errorf("env %s cannot be empty", awsEc2InstanceProfileNameEnv)
358+
}
359+
cfg.EC2InstanceProfileName = ec2InstanceProfileName
356360

357361
if err := setBoolPtrFromEnvVal(&cfg.DisableRequestCompression, []string{awsDisableRequestCompressionEnv}); err != nil {
358362
return cfg, err

0 commit comments

Comments
 (0)