|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "github.com/aws/aws-sdk-go/aws" |
4 | 5 | "github.com/aws/aws-sdk-go/aws/endpoints" |
| 6 | + "github.com/aws/aws-sdk-go/aws/session" |
| 7 | + "github.com/aws/aws-sdk-go/service/cloudformation" |
| 8 | + "github.com/aws/aws-sdk-go/service/sts" |
5 | 9 | "github.com/onsi/gomega" |
| 10 | + "os" |
6 | 11 | "strings" |
7 | 12 | "testing" |
8 | 13 | ) |
9 | 14 |
|
10 | 15 | func TestResolverEndpointAWSGov(t *testing.T) { |
11 | 16 | g := gomega.NewGomegaWithT(t) |
12 | 17 |
|
13 | | - resolver := CustomEndpointResolverForAWS() |
14 | | - result, err := resolver(endpoints.CloudformationServiceID, endpoints.UsGovEast1RegionID, endpoints.UseFIPSEndpointOption) |
15 | | - g.Expect(err).ToNot(gomega.HaveOccurred()) |
16 | | - g.Expect(strings.Contains(result.URL, "cloudformation.us-gov-east-1.amazonaws.com")).To(gomega.BeTrue()) |
| 18 | + err := os.Setenv("AWS_USE_FIPS_ENDPOINT", "true") |
| 19 | + if err != nil { |
| 20 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 21 | + } |
17 | 22 |
|
18 | | - result, err = resolver(endpoints.CloudformationServiceID, endpoints.UsGovWest1RegionID, endpoints.UseFIPSEndpointOption) |
19 | | - g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 23 | + // Test us-gov and fips enabled regions |
| 24 | + err = os.Setenv("AWS_REGION", "us-west-2") |
| 25 | + if err != nil { |
| 26 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 27 | + } |
| 28 | + sess, err := session.NewSessionWithOptions(session.Options{ |
| 29 | + SharedConfigState: session.SharedConfigEnable, |
| 30 | + }) |
| 31 | + if err != nil { |
| 32 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 33 | + } |
20 | 34 |
|
21 | | - g.Expect(strings.Contains(result.URL, "cloudformation.us-gov-west-1.amazonaws.com")).To(gomega.BeTrue()) |
| 35 | + stsSvc := sts.New(sess, aws.NewConfig().WithEndpointResolver(CustomEndpointResolverForAWS())) |
| 36 | + g.Expect(stsSvc.ServiceID).ToNot(gomega.BeEmpty()) |
| 37 | + g.Expect(strings.Contains(stsSvc.Endpoint, "sts-fips.us-west-2.amazonaws.com")).To(gomega.BeTrue()) |
22 | 38 |
|
23 | | - result, err = resolver(endpoints.StsServiceID, endpoints.UsGovWest1RegionID, endpoints.UseFIPSEndpointOption) |
24 | | - g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 39 | + cfnSvc := cloudformation.New(sess, aws.NewConfig().WithEndpointResolver(CustomEndpointResolverForAWS())) |
| 40 | + g.Expect(cfnSvc.ServiceID).ToNot(gomega.BeEmpty()) |
| 41 | + g.Expect(strings.Contains(cfnSvc.Endpoint, "cloudformation-fips.us-west-2.amazonaws.com")).To(gomega.BeTrue()) |
25 | 42 |
|
26 | | - g.Expect(strings.Contains(result.URL, "sts.us-gov-west-1.amazonaws.com")).To(gomega.BeTrue()) |
| 43 | + err = os.Setenv("AWS_REGION", "us-east-2") |
| 44 | + if err != nil { |
| 45 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 46 | + } |
| 47 | + sess, err = session.NewSessionWithOptions(session.Options{ |
| 48 | + SharedConfigState: session.SharedConfigEnable, |
| 49 | + }) |
| 50 | + if err != nil { |
| 51 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 52 | + } |
27 | 53 |
|
28 | | - result, err = resolver(endpoints.IamServiceID, endpoints.UsGovWest1RegionID, endpoints.UseFIPSEndpointOption) |
29 | | - g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 54 | + stsSvc = sts.New(sess, aws.NewConfig().WithEndpointResolver(CustomEndpointResolverForAWS())) |
| 55 | + g.Expect(stsSvc.ServiceID).ToNot(gomega.BeEmpty()) |
| 56 | + g.Expect(strings.Contains(stsSvc.Endpoint, "sts-fips.us-east-2.amazonaws.com")).To(gomega.BeTrue()) |
30 | 57 |
|
31 | | - g.Expect(strings.Contains(result.URL, "iam.us-gov.amazonaws.com")).To(gomega.BeTrue()) |
| 58 | + cfnSvc = cloudformation.New(sess, aws.NewConfig().WithEndpointResolver(CustomEndpointResolverForAWS())) |
| 59 | + g.Expect(cfnSvc.ServiceID).ToNot(gomega.BeEmpty()) |
| 60 | + g.Expect(strings.Contains(cfnSvc.Endpoint, "cloudformation-fips.us-east-2.amazonaws.com")).To(gomega.BeTrue()) |
| 61 | + |
| 62 | + err = os.Setenv("AWS_REGION", endpoints.UsGovWest1RegionID) |
| 63 | + if err != nil { |
| 64 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 65 | + } |
| 66 | + |
| 67 | + sess, err = session.NewSessionWithOptions(session.Options{ |
| 68 | + SharedConfigState: session.SharedConfigEnable, |
| 69 | + }) |
| 70 | + if err != nil { |
| 71 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 72 | + } |
| 73 | + |
| 74 | + cfnSvc = cloudformation.New(sess, aws.NewConfig().WithEndpointResolver(CustomEndpointResolverForAWS())) |
| 75 | + g.Expect(cfnSvc.ServiceID).ToNot(gomega.BeEmpty()) |
| 76 | + g.Expect(strings.Contains(cfnSvc.Endpoint, "cloudformation.us-gov-west-1.amazonaws.com")).To(gomega.BeTrue()) |
| 77 | + |
| 78 | + stsSvc = sts.New(sess, aws.NewConfig().WithEndpointResolver(CustomEndpointResolverForAWS())) |
| 79 | + g.Expect(stsSvc.ServiceID).ToNot(gomega.BeEmpty()) |
| 80 | + g.Expect(strings.Contains(stsSvc.Endpoint, "sts.us-gov-west-1.amazonaws.com")).To(gomega.BeTrue()) |
| 81 | + |
| 82 | + // Test non fips endpoint regions |
| 83 | + err = os.Unsetenv("AWS_USE_FIPS_ENDPOINT") |
| 84 | + if err != nil { |
| 85 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 86 | + } |
| 87 | + err = os.Setenv("AWS_REGION", endpoints.EuNorth1RegionID) |
| 88 | + if err != nil { |
| 89 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 90 | + } |
| 91 | + sess, err = session.NewSessionWithOptions(session.Options{ |
| 92 | + SharedConfigState: session.SharedConfigEnable, |
| 93 | + }) |
| 94 | + if err != nil { |
| 95 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 96 | + } |
| 97 | + |
| 98 | + cfnSvc = cloudformation.New(sess, aws.NewConfig().WithEndpointResolver(CustomEndpointResolverForAWS())) |
| 99 | + g.Expect(cfnSvc.ServiceID).ToNot(gomega.BeEmpty()) |
| 100 | + g.Expect(strings.Contains(cfnSvc.Endpoint, "cloudformation.eu-north-1.amazonaws.com")).To(gomega.BeTrue()) |
| 101 | + |
| 102 | + stsSvc = sts.New(sess, aws.NewConfig().WithEndpointResolver(CustomEndpointResolverForAWS())) |
| 103 | + g.Expect(stsSvc.ServiceID).ToNot(gomega.BeEmpty()) |
| 104 | + g.Expect(strings.Contains(stsSvc.Endpoint, "sts.amazonaws.com")).To(gomega.BeTrue()) |
| 105 | + |
| 106 | + err = os.Setenv("AWS_REGION", endpoints.ApSoutheast1RegionID) |
| 107 | + if err != nil { |
| 108 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 109 | + } |
| 110 | + |
| 111 | + sess, err = session.NewSessionWithOptions(session.Options{ |
| 112 | + SharedConfigState: session.SharedConfigEnable, |
| 113 | + }) |
| 114 | + if err != nil { |
| 115 | + g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 116 | + } |
| 117 | + |
| 118 | + cfnSvc = cloudformation.New(sess, aws.NewConfig().WithEndpointResolver(CustomEndpointResolverForAWS())) |
| 119 | + g.Expect(cfnSvc.ServiceID).ToNot(gomega.BeEmpty()) |
| 120 | + g.Expect(strings.Contains(cfnSvc.Endpoint, "cloudformation.ap-southeast-1.amazonaws.com")).To(gomega.BeTrue()) |
| 121 | + |
| 122 | + stsSvc = sts.New(sess, aws.NewConfig().WithEndpointResolver(CustomEndpointResolverForAWS())) |
| 123 | + g.Expect(stsSvc.ServiceID).ToNot(gomega.BeEmpty()) |
| 124 | + g.Expect(strings.Contains(stsSvc.Endpoint, "sts.amazonaws.com")).To(gomega.BeTrue()) |
32 | 125 | } |
0 commit comments