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

Add strict checking of AWS API mock endpoints. #370

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
68 changes: 62 additions & 6 deletions aws_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func TestGetAwsConfig(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand Down Expand Up @@ -156,6 +157,7 @@ func TestGetAwsConfig(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"DurationSeconds": "3600"}),
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"DurationSeconds": "3600"}),
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand All @@ -175,6 +177,7 @@ func TestGetAwsConfig(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"ExternalId": servicemocks.MockStsAssumeRoleExternalId}),
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"ExternalId": servicemocks.MockStsAssumeRoleExternalId}),
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand All @@ -194,6 +197,7 @@ func TestGetAwsConfig(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"Policy": servicemocks.MockStsAssumeRolePolicy}),
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"Policy": servicemocks.MockStsAssumeRolePolicy}),
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand All @@ -213,6 +217,7 @@ func TestGetAwsConfig(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"PolicyArns.member.1.arn": servicemocks.MockStsAssumeRolePolicyArn}),
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"PolicyArns.member.1.arn": servicemocks.MockStsAssumeRolePolicyArn}),
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand All @@ -234,6 +239,7 @@ func TestGetAwsConfig(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"Tags.member.1.Key": servicemocks.MockStsAssumeRoleTagKey, "Tags.member.1.Value": servicemocks.MockStsAssumeRoleTagValue}),
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"Tags.member.1.Key": servicemocks.MockStsAssumeRoleTagKey, "Tags.member.1.Value": servicemocks.MockStsAssumeRoleTagValue}),
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand All @@ -256,6 +262,7 @@ func TestGetAwsConfig(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"Tags.member.1.Key": servicemocks.MockStsAssumeRoleTagKey, "Tags.member.1.Value": servicemocks.MockStsAssumeRoleTagValue, "TransitiveTagKeys.member.1": servicemocks.MockStsAssumeRoleTagKey}),
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"Tags.member.1.Key": servicemocks.MockStsAssumeRoleTagKey, "Tags.member.1.Value": servicemocks.MockStsAssumeRoleTagValue, "TransitiveTagKeys.member.1": servicemocks.MockStsAssumeRoleTagKey}),
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand All @@ -275,6 +282,7 @@ func TestGetAwsConfig(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"SourceIdentity": servicemocks.MockStsAssumeRoleSourceIdentity}),
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"SourceIdentity": servicemocks.MockStsAssumeRoleSourceIdentity}),
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand Down Expand Up @@ -402,6 +410,7 @@ aws_secret_access_key = SharedConfigurationSourceSecretKey
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand Down Expand Up @@ -552,6 +561,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand Down Expand Up @@ -599,6 +609,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand Down Expand Up @@ -628,6 +639,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
Expand All @@ -647,6 +659,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
},
Expand All @@ -663,8 +676,10 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
ExpectedRegion: "us-east-1",
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
},
Expand Down Expand Up @@ -1052,7 +1067,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
}
}

closeSts, _, stsEndpoint := mockdata.GetMockedAwsApiSession("STS", testCase.MockStsEndpoints)
closeSts, _, stsEndpoint := mockdata.GetMockedAwsApiSession("STS", &testCase.MockStsEndpoints)
defer closeSts()

testCase.Config.StsEndpoint = stsEndpoint
Expand Down Expand Up @@ -1133,6 +1148,11 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey
if expected, actual := testCase.ExpectedRegion, awsConfig.Region; expected != actual {
t.Fatalf("expected region (%s), got: %s", expected, actual)
}

numMockStsEndpoints := len(testCase.MockStsEndpoints)
if numMockStsEndpoints > 0 {
t.Fatalf("expected all mock endpoints exhausted, remaining: %d", numMockStsEndpoints)
}
})
}
}
Expand Down Expand Up @@ -2391,6 +2411,7 @@ func TestAssumeRole(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
},
},

Expand Down Expand Up @@ -2434,6 +2455,7 @@ aws_secret_access_key = SharedConfigurationSourceSecretKey
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
},
},

Expand All @@ -2450,6 +2472,7 @@ aws_secret_access_key = SharedConfigurationSourceSecretKey
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"DurationSeconds": "3600"}),
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"DurationSeconds": "3600"}),
},
},

Expand All @@ -2466,6 +2489,7 @@ aws_secret_access_key = SharedConfigurationSourceSecretKey
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"Policy": "{}"}),
servicemocks.MockStsAssumeRoleValidEndpointWithOptions(map[string]string{"Policy": "{}"}),
},
},

Expand All @@ -2489,7 +2513,7 @@ aws_secret_access_key = SharedConfigurationSourceSecretKey
oldEnv := servicemocks.InitSessionTestEnv()
defer servicemocks.PopEnv(oldEnv)

closeSts, _, stsEndpoint := mockdata.GetMockedAwsApiSession("STS", testCase.MockStsEndpoints)
closeSts, _, stsEndpoint := mockdata.GetMockedAwsApiSession("STS", &testCase.MockStsEndpoints)
defer closeSts()

testCase.Config.StsEndpoint = stsEndpoint
Expand Down Expand Up @@ -2545,6 +2569,11 @@ aws_secret_access_key = SharedConfigurationSourceSecretKey
if diff := cmp.Diff(credentialsValue, testCase.ExpectedCredentialsValue, cmpopts.IgnoreFields(aws.Credentials{}, "Expires")); diff != "" {
t.Fatalf("unexpected credentials: (- got, + expected)\n%s", diff)
}

numMockStsEndpoints := len(testCase.MockStsEndpoints)
if numMockStsEndpoints > 0 {
t.Fatalf("expected all mock endpoints exhausted, remaining: %d", numMockStsEndpoints)
}
})
}
}
Expand Down Expand Up @@ -2573,6 +2602,7 @@ func TestAssumeRoleWithWebIdentity(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleWithWebIdentityCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
},
},

Expand All @@ -2587,6 +2617,7 @@ func TestAssumeRoleWithWebIdentity(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleWithWebIdentityCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
},
},

Expand All @@ -2602,6 +2633,7 @@ func TestAssumeRoleWithWebIdentity(t *testing.T) {
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleWithWebIdentityCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
},
},

Expand Down Expand Up @@ -2648,6 +2680,7 @@ role_session_name = %[2]s
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleWithWebIdentityCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
},
},

Expand Down Expand Up @@ -2701,6 +2734,7 @@ web_identity_token_file = no-such-file
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleWithWebIdentityCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
servicemocks.MockStsAssumeRoleWithWebIdentityValidEndpoint,
},
},

Expand All @@ -2716,6 +2750,7 @@ web_identity_token_file = no-such-file
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleWithWebIdentityCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleWithWebIdentityValidWithOptions(map[string]string{"DurationSeconds": "3600"}),
servicemocks.MockStsAssumeRoleWithWebIdentityValidWithOptions(map[string]string{"DurationSeconds": "3600"}),
},
},

Expand All @@ -2731,6 +2766,7 @@ web_identity_token_file = no-such-file
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleWithWebIdentityCredentials,
MockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleWithWebIdentityValidWithOptions(map[string]string{"Policy": "{}"}),
servicemocks.MockStsAssumeRoleWithWebIdentityValidWithOptions(map[string]string{"Policy": "{}"}),
},
},

Expand Down Expand Up @@ -2768,7 +2804,7 @@ web_identity_token_file = no-such-file
os.Setenv(k, v)
}

closeSts, _, stsEndpoint := mockdata.GetMockedAwsApiSession("STS", testCase.MockStsEndpoints)
closeSts, _, stsEndpoint := mockdata.GetMockedAwsApiSession("STS", &testCase.MockStsEndpoints)
defer closeSts()

testCase.Config.StsEndpoint = stsEndpoint
Expand Down Expand Up @@ -2861,6 +2897,11 @@ web_identity_token_file = no-such-file
if diff := cmp.Diff(credentialsValue, testCase.ExpectedCredentialsValue, cmpopts.IgnoreFields(aws.Credentials{}, "Expires")); diff != "" {
t.Fatalf("unexpected credentials: (- got, + expected)\n%s", diff)
}

numMockStsEndpoints := len(testCase.MockStsEndpoints)
if numMockStsEndpoints > 0 {
t.Fatalf("expected all mock endpoints exhausted, remaining: %d", numMockStsEndpoints)
}
})
}
}
Expand All @@ -2886,6 +2927,7 @@ func TestGetAwsConfigWithAccountIDAndPartition(t *testing.T) {
expectedAcctID: "222222222222", expectedPartition: "aws",
mockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityValidEndpoint,
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
},
{
Expand Down Expand Up @@ -2925,6 +2967,8 @@ func TestGetAwsConfigWithAccountIDAndPartition(t *testing.T) {
expectedAcctID: "555555555555", expectedPartition: "aws",
mockStsEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsAssumeRoleValidEndpoint,
servicemocks.MockStsGetCallerIdentityValidAssumedRoleEndpoint,
servicemocks.MockStsGetCallerIdentityValidAssumedRoleEndpoint,
},
},
Expand All @@ -2934,7 +2978,7 @@ func TestGetAwsConfigWithAccountIDAndPartition(t *testing.T) {
tc := testCase

t.Run(tc.desc, func(t *testing.T) {
ts := servicemocks.MockAwsApiServer("STS", tc.mockStsEndpoints)
ts := servicemocks.MockAwsApiServer("STS", &tc.mockStsEndpoints)
defer ts.Close()
tc.config.StsEndpoint = ts.URL

Expand Down Expand Up @@ -2963,6 +3007,11 @@ func TestGetAwsConfigWithAccountIDAndPartition(t *testing.T) {
if part != tc.expectedPartition {
t.Errorf("expected partition (%s), got: %s", tc.expectedPartition, part)
}

numMockStsEndpoints := len(tc.mockStsEndpoints)
if numMockStsEndpoints > 0 {
t.Errorf("expected all mock endpoints exhausted, remaining: %d", numMockStsEndpoints)
}
})
}
}
Expand Down Expand Up @@ -3280,9 +3329,11 @@ func TestLogger(t *testing.T) {
SecretKey: servicemocks.MockStaticSecretKey,
}

ts := servicemocks.MockAwsApiServer("STS", []*servicemocks.MockEndpoint{
mockStsEndpoints := []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityValidEndpoint,
})
servicemocks.MockStsGetCallerIdentityValidEndpoint,
}
ts := servicemocks.MockAwsApiServer("STS", &mockStsEndpoints)
defer ts.Close()
config.StsEndpoint = ts.URL

Expand Down Expand Up @@ -3319,4 +3370,9 @@ func TestLogger(t *testing.T) {
t.Errorf("GetAwsAccountIDAndPartition: line %d: expected module %q, got %q", i+1, e, a)
}
}

numMockStsEndpoints := len(mockStsEndpoints)
if numMockStsEndpoints > 0 {
t.Errorf("expected all mock endpoints exhausted, remaining: %d", numMockStsEndpoints)
}
}
Loading