|
|
|
|
@@ -474,6 +474,38 @@ func createDefaultAwsTestServer() *testAwsServer {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createDefaultAwsTestServerWithImdsv2(t *testing.T) *testAwsServer {
|
|
|
|
|
validateSessionTokenHeaders := func(r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/latest/api/token" {
|
|
|
|
|
headerValue := r.Header.Get(awsIMDSv2SessionTtlHeader)
|
|
|
|
|
if headerValue != awsIMDSv2SessionTtl {
|
|
|
|
|
t.Errorf("%q = \n%q\n want \n%q", awsIMDSv2SessionTtlHeader, headerValue, awsIMDSv2SessionTtl)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
headerValue := r.Header.Get(awsIMDSv2SessionTokenHeader)
|
|
|
|
|
if headerValue != "sessiontoken" {
|
|
|
|
|
t.Errorf("%q = \n%q\n want \n%q", awsIMDSv2SessionTokenHeader, headerValue, "sessiontoken")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return createAwsTestServer(
|
|
|
|
|
"/latest/meta-data/iam/security-credentials",
|
|
|
|
|
"/latest/meta-data/placement/availability-zone",
|
|
|
|
|
"https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15",
|
|
|
|
|
"/latest/api/token",
|
|
|
|
|
"gcp-aws-role",
|
|
|
|
|
"us-east-2b",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"SecretAccessKey": secretAccessKey,
|
|
|
|
|
"AccessKeyId": accessKeyID,
|
|
|
|
|
"Token": securityToken,
|
|
|
|
|
},
|
|
|
|
|
"sessiontoken",
|
|
|
|
|
validateSessionTokenHeaders,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (server *testAwsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
switch p := r.URL.Path; p {
|
|
|
|
|
case server.url:
|
|
|
|
|
@@ -553,16 +585,25 @@ func getExpectedSubjectToken(url, region, accessKeyID, secretAccessKey, security
|
|
|
|
|
func TestAWSCredential_BasicRequest(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
oldNow := now
|
|
|
|
|
defer func() { now = oldNow }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
now = oldNow
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
now = setTime(defaultTime)
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -588,46 +629,27 @@ func TestAWSCredential_BasicRequest(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAWSCredential_IMDSv2(t *testing.T) {
|
|
|
|
|
validateSessionTokenHeaders := func(r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/latest/api/token" {
|
|
|
|
|
headerValue := r.Header.Get(awsIMDSv2SessionTtlHeader)
|
|
|
|
|
if headerValue != awsIMDSv2SessionTtl {
|
|
|
|
|
t.Errorf("%q = \n%q\n want \n%q", awsIMDSv2SessionTtlHeader, headerValue, awsIMDSv2SessionTtl)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
headerValue := r.Header.Get(awsIMDSv2SessionTokenHeader)
|
|
|
|
|
if headerValue != "sessiontoken" {
|
|
|
|
|
t.Errorf("%q = \n%q\n want \n%q", awsIMDSv2SessionTokenHeader, headerValue, "sessiontoken")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
server := createAwsTestServer(
|
|
|
|
|
"/latest/meta-data/iam/security-credentials",
|
|
|
|
|
"/latest/meta-data/placement/availability-zone",
|
|
|
|
|
"https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15",
|
|
|
|
|
"/latest/api/token",
|
|
|
|
|
"gcp-aws-role",
|
|
|
|
|
"us-east-2b",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"SecretAccessKey": secretAccessKey,
|
|
|
|
|
"AccessKeyId": accessKeyID,
|
|
|
|
|
"Token": securityToken,
|
|
|
|
|
},
|
|
|
|
|
"sessiontoken",
|
|
|
|
|
validateSessionTokenHeaders,
|
|
|
|
|
)
|
|
|
|
|
server := createDefaultAwsTestServerWithImdsv2(t)
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
oldNow := now
|
|
|
|
|
defer func() { now = oldNow }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
now = oldNow
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
now = setTime(defaultTime)
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -655,17 +677,26 @@ func TestAWSCredential_IMDSv2(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_BasicRequestWithoutSecurityToken(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
delete(server.Credentials, "Token")
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
oldNow := now
|
|
|
|
|
defer func() { now = oldNow }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
now = oldNow
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
now = setTime(defaultTime)
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -693,20 +724,29 @@ func TestAWSCredential_BasicRequestWithoutSecurityToken(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_BasicRequestWithEnv(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldNow := now
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
now = oldNow
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{
|
|
|
|
|
"AWS_ACCESS_KEY_ID": "AKIDEXAMPLE",
|
|
|
|
|
"AWS_SECRET_ACCESS_KEY": "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
|
|
|
|
|
"AWS_REGION": "us-west-1",
|
|
|
|
|
})
|
|
|
|
|
oldNow := now
|
|
|
|
|
defer func() { now = oldNow }()
|
|
|
|
|
now = setTime(defaultTime)
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -734,20 +774,29 @@ func TestAWSCredential_BasicRequestWithEnv(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_BasicRequestWithDefaultEnv(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldNow := now
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
now = oldNow
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{
|
|
|
|
|
"AWS_ACCESS_KEY_ID": "AKIDEXAMPLE",
|
|
|
|
|
"AWS_SECRET_ACCESS_KEY": "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
|
|
|
|
|
"AWS_DEFAULT_REGION": "us-west-1",
|
|
|
|
|
"AWS_REGION": "us-west-1",
|
|
|
|
|
})
|
|
|
|
|
oldNow := now
|
|
|
|
|
defer func() { now = oldNow }()
|
|
|
|
|
now = setTime(defaultTime)
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -774,21 +823,30 @@ func TestAWSCredential_BasicRequestWithDefaultEnv(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_BasicRequestWithTwoRegions(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldNow := now
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
now = oldNow
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{
|
|
|
|
|
"AWS_ACCESS_KEY_ID": "AKIDEXAMPLE",
|
|
|
|
|
"AWS_SECRET_ACCESS_KEY": "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
|
|
|
|
|
"AWS_REGION": "us-west-1",
|
|
|
|
|
"AWS_DEFAULT_REGION": "us-east-1",
|
|
|
|
|
})
|
|
|
|
|
oldNow := now
|
|
|
|
|
defer func() { now = oldNow }()
|
|
|
|
|
now = setTime(defaultTime)
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -815,16 +873,25 @@ func TestAWSCredential_BasicRequestWithTwoRegions(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_RequestWithBadVersion(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
tfc.CredentialSource.EnvironmentID = "aws3"
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
_, err := tfc.parse(context.Background())
|
|
|
|
|
_, err = tfc.parse(context.Background())
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatalf("parse() should have failed")
|
|
|
|
|
}
|
|
|
|
|
@@ -836,14 +903,23 @@ func TestAWSCredential_RequestWithBadVersion(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_RequestWithNoRegionURL(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
tfc.CredentialSource.RegionURL = ""
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -863,14 +939,23 @@ func TestAWSCredential_RequestWithNoRegionURL(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_RequestWithBadRegionURL(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
server.WriteRegion = notFound
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -890,6 +975,10 @@ func TestAWSCredential_RequestWithBadRegionURL(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_RequestWithMissingCredential(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
server.WriteSecurityCredentials = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Write([]byte("{}"))
|
|
|
|
|
}
|
|
|
|
|
@@ -898,8 +987,13 @@ func TestAWSCredential_RequestWithMissingCredential(t *testing.T) {
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -919,6 +1013,10 @@ func TestAWSCredential_RequestWithMissingCredential(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_RequestWithIncompleteCredential(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
server.WriteSecurityCredentials = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Write([]byte(`{"AccessKeyId":"FOOBARBAS"}`))
|
|
|
|
|
}
|
|
|
|
|
@@ -927,8 +1025,13 @@ func TestAWSCredential_RequestWithIncompleteCredential(t *testing.T) {
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -948,14 +1051,23 @@ func TestAWSCredential_RequestWithIncompleteCredential(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_RequestWithNoCredentialURL(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
tfc.CredentialSource.URL = ""
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -975,14 +1087,23 @@ func TestAWSCredential_RequestWithNoCredentialURL(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_RequestWithBadCredentialURL(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
server.WriteRolename = notFound
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -1002,14 +1123,23 @@ func TestAWSCredential_RequestWithBadCredentialURL(t *testing.T) {
|
|
|
|
|
func TestAWSCredential_RequestWithBadFinalCredentialURL(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
server.WriteSecurityCredentials = notFound
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -1025,3 +1155,290 @@ func TestAWSCredential_RequestWithBadFinalCredentialURL(t *testing.T) {
|
|
|
|
|
t.Errorf("subjectToken = %q, want %q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAWSCredential_ShouldNotCallMetadataEndpointWhenCredsAreInEnv(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServer()
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metadataTs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
t.Error("Metadata server should not have been called.")
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
tfc.CredentialSource.IMDSv2SessionTokenURL = metadataTs.URL
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
oldNow := now
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
now = oldNow
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{
|
|
|
|
|
"AWS_ACCESS_KEY_ID": "AKIDEXAMPLE",
|
|
|
|
|
"AWS_SECRET_ACCESS_KEY": "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
|
|
|
|
|
"AWS_REGION": "us-west-1",
|
|
|
|
|
})
|
|
|
|
|
now = setTime(defaultTime)
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("parse() failed %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := base.subjectToken()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("retrieveSubjectToken() failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected := getExpectedSubjectToken(
|
|
|
|
|
"https://sts.us-west-1.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15",
|
|
|
|
|
"us-west-1",
|
|
|
|
|
"AKIDEXAMPLE",
|
|
|
|
|
"wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
|
|
|
|
|
"",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if got, want := out, expected; !reflect.DeepEqual(got, want) {
|
|
|
|
|
t.Errorf("subjectToken = \n%q\n want \n%q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAWSCredential_ShouldCallMetadataEndpointWhenNoRegion(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServerWithImdsv2(t)
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
oldNow := now
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
now = oldNow
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{
|
|
|
|
|
"AWS_ACCESS_KEY_ID": accessKeyID,
|
|
|
|
|
"AWS_SECRET_ACCESS_KEY": secretAccessKey,
|
|
|
|
|
})
|
|
|
|
|
now = setTime(defaultTime)
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("parse() failed %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := base.subjectToken()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("retrieveSubjectToken() failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected := getExpectedSubjectToken(
|
|
|
|
|
"https://sts.us-east-2.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15",
|
|
|
|
|
"us-east-2",
|
|
|
|
|
accessKeyID,
|
|
|
|
|
secretAccessKey,
|
|
|
|
|
"",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if got, want := out, expected; !reflect.DeepEqual(got, want) {
|
|
|
|
|
t.Errorf("subjectToken = \n%q\n want \n%q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAWSCredential_ShouldCallMetadataEndpointWhenNoAccessKey(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServerWithImdsv2(t)
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
oldNow := now
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
now = oldNow
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{
|
|
|
|
|
"AWS_SECRET_ACCESS_KEY": "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
|
|
|
|
|
"AWS_REGION": "us-west-1",
|
|
|
|
|
})
|
|
|
|
|
now = setTime(defaultTime)
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("parse() failed %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := base.subjectToken()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("retrieveSubjectToken() failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected := getExpectedSubjectToken(
|
|
|
|
|
"https://sts.us-west-1.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15",
|
|
|
|
|
"us-west-1",
|
|
|
|
|
accessKeyID,
|
|
|
|
|
secretAccessKey,
|
|
|
|
|
securityToken,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if got, want := out, expected; !reflect.DeepEqual(got, want) {
|
|
|
|
|
t.Errorf("subjectToken = \n%q\n want \n%q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAWSCredential_ShouldCallMetadataEndpointWhenNoSecretAccessKey(t *testing.T) {
|
|
|
|
|
server := createDefaultAwsTestServerWithImdsv2(t)
|
|
|
|
|
ts := httptest.NewServer(server)
|
|
|
|
|
tsURL, err := neturl.Parse(ts.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("couldn't parse httptest servername")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = server.getCredentialSource(ts.URL)
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
oldNow := now
|
|
|
|
|
oldValidHostnames := validHostnames
|
|
|
|
|
defer func() {
|
|
|
|
|
getenv = oldGetenv
|
|
|
|
|
now = oldNow
|
|
|
|
|
validHostnames = oldValidHostnames
|
|
|
|
|
}()
|
|
|
|
|
getenv = setEnvironment(map[string]string{
|
|
|
|
|
"AWS_ACCESS_KEY_ID": "AKIDEXAMPLE",
|
|
|
|
|
"AWS_REGION": "us-west-1",
|
|
|
|
|
})
|
|
|
|
|
now = setTime(defaultTime)
|
|
|
|
|
validHostnames = []string{tsURL.Hostname()}
|
|
|
|
|
|
|
|
|
|
base, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("parse() failed %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := base.subjectToken()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("retrieveSubjectToken() failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected := getExpectedSubjectToken(
|
|
|
|
|
"https://sts.us-west-1.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15",
|
|
|
|
|
"us-west-1",
|
|
|
|
|
accessKeyID,
|
|
|
|
|
secretAccessKey,
|
|
|
|
|
securityToken,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if got, want := out, expected; !reflect.DeepEqual(got, want) {
|
|
|
|
|
t.Errorf("subjectToken = \n%q\n want \n%q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAWSCredential_Validations(t *testing.T) {
|
|
|
|
|
var metadataServerValidityTests = []struct {
|
|
|
|
|
name string
|
|
|
|
|
credSource CredentialSource
|
|
|
|
|
errText string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "No Metadata Server URLs",
|
|
|
|
|
credSource: CredentialSource{
|
|
|
|
|
EnvironmentID: "aws1",
|
|
|
|
|
RegionURL: "",
|
|
|
|
|
URL: "",
|
|
|
|
|
IMDSv2SessionTokenURL: "",
|
|
|
|
|
},
|
|
|
|
|
}, {
|
|
|
|
|
name: "IPv4 Metadata Server URLs",
|
|
|
|
|
credSource: CredentialSource{
|
|
|
|
|
EnvironmentID: "aws1",
|
|
|
|
|
RegionURL: "http://169.254.169.254/latest/meta-data/placement/availability-zone",
|
|
|
|
|
URL: "http://169.254.169.254/latest/meta-data/iam/security-credentials",
|
|
|
|
|
IMDSv2SessionTokenURL: "http://169.254.169.254/latest/api/token",
|
|
|
|
|
},
|
|
|
|
|
}, {
|
|
|
|
|
name: "IPv6 Metadata Server URLs",
|
|
|
|
|
credSource: CredentialSource{
|
|
|
|
|
EnvironmentID: "aws1",
|
|
|
|
|
RegionURL: "http://[fd00:ec2::254]/latest/meta-data/placement/availability-zone",
|
|
|
|
|
URL: "http://[fd00:ec2::254]/latest/meta-data/iam/security-credentials",
|
|
|
|
|
IMDSv2SessionTokenURL: "http://[fd00:ec2::254]/latest/api/token",
|
|
|
|
|
},
|
|
|
|
|
}, {
|
|
|
|
|
name: "Faulty RegionURL",
|
|
|
|
|
credSource: CredentialSource{
|
|
|
|
|
EnvironmentID: "aws1",
|
|
|
|
|
RegionURL: "http://abc.com/latest/meta-data/placement/availability-zone",
|
|
|
|
|
URL: "http://169.254.169.254/latest/meta-data/iam/security-credentials",
|
|
|
|
|
IMDSv2SessionTokenURL: "http://169.254.169.254/latest/api/token",
|
|
|
|
|
},
|
|
|
|
|
errText: "oauth2/google: invalid hostname http://abc.com/latest/meta-data/placement/availability-zone for region_url",
|
|
|
|
|
}, {
|
|
|
|
|
name: "Faulty CredVerificationURL",
|
|
|
|
|
credSource: CredentialSource{
|
|
|
|
|
EnvironmentID: "aws1",
|
|
|
|
|
RegionURL: "http://169.254.169.254/latest/meta-data/placement/availability-zone",
|
|
|
|
|
URL: "http://abc.com/latest/meta-data/iam/security-credentials",
|
|
|
|
|
IMDSv2SessionTokenURL: "http://169.254.169.254/latest/api/token",
|
|
|
|
|
},
|
|
|
|
|
errText: "oauth2/google: invalid hostname http://abc.com/latest/meta-data/iam/security-credentials for url",
|
|
|
|
|
}, {
|
|
|
|
|
name: "Faulty IMDSv2SessionTokenURL",
|
|
|
|
|
credSource: CredentialSource{
|
|
|
|
|
EnvironmentID: "aws1",
|
|
|
|
|
RegionURL: "http://169.254.169.254/latest/meta-data/placement/availability-zone",
|
|
|
|
|
URL: "http://169.254.169.254/latest/meta-data/iam/security-credentials",
|
|
|
|
|
IMDSv2SessionTokenURL: "http://abc.com/latest/api/token",
|
|
|
|
|
},
|
|
|
|
|
errText: "oauth2/google: invalid hostname http://abc.com/latest/api/token for imdsv2_session_token_url",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range metadataServerValidityTests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
tfc := testFileConfig
|
|
|
|
|
tfc.CredentialSource = tt.credSource
|
|
|
|
|
|
|
|
|
|
oldGetenv := getenv
|
|
|
|
|
defer func() { getenv = oldGetenv }()
|
|
|
|
|
getenv = setEnvironment(map[string]string{})
|
|
|
|
|
|
|
|
|
|
_, err := tfc.parse(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
if tt.errText == "" {
|
|
|
|
|
t.Errorf("Didn't expect an error, but got %v", err)
|
|
|
|
|
} else if tt.errText != err.Error() {
|
|
|
|
|
t.Errorf("Expected %v, but got %v", tt.errText, err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if tt.errText != "" {
|
|
|
|
|
t.Errorf("Expected error %v, but got none", tt.errText)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|