forked from minio/minio-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Credentials: Support assuming role via WebIdentityTokenFile
This supports the new AWS_WEB_IDENTITY_TOKEN_FILE and AWS_ROLE_ARN environment variables, that allow exchanging OIDC tokens given to pods in EKS for access tokens. Fixes minio#1156
- Loading branch information
Showing
3 changed files
with
137 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ package credentials | |
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
|
@@ -51,6 +52,27 @@ const credsRespEcsTaskTmpl = `{ | |
"Expiration" : "%s" | ||
}` | ||
|
||
const credsRespStsImpl = `<AssumeRoleWithWebIdentityResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/"> | ||
<AssumeRoleWithWebIdentityResult> | ||
<SubjectFromWebIdentityToken>amzn1.account.AF6RHO7KZU5XRVQJGXK6HB56KR2A</SubjectFromWebIdentityToken> | ||
<Audience>[email protected]</Audience> | ||
<AssumedRoleUser> | ||
<Arn>arn:aws:sts::123456789012:assumed-role/FederatedWebIdentityRole/app1</Arn> | ||
<AssumedRoleId>AROACLKWSDQRAOEXAMPLE:app1</AssumedRoleId> | ||
</AssumedRoleUser> | ||
<Credentials> | ||
<SessionToken>token</SessionToken> | ||
<SecretAccessKey>secret</SecretAccessKey> | ||
<Expiration>%s</Expiration> | ||
<AccessKeyId>accessKey</AccessKeyId> | ||
</Credentials> | ||
<Provider>www.amazon.com</Provider> | ||
</AssumeRoleWithWebIdentityResult> | ||
<ResponseMetadata> | ||
<RequestId>ad4156e9-bce1-11e2-82e6-6b6efEXAMPLE</RequestId> | ||
</ResponseMetadata> | ||
</AssumeRoleWithWebIdentityResponse>` | ||
|
||
func initTestFailServer() *httptest.Server { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
http.Error(w, "Not allowed", http.StatusBadRequest) | ||
|
@@ -91,6 +113,22 @@ func initEcsTaskTestServer(expireOn string) *httptest.Server { | |
return server | ||
} | ||
|
||
func initStsTestServer(expireOn string) *httptest.Server { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
required := []string{"RoleArn", "RoleSessionName", "WebIdentityToken", "Version"} | ||
for _, field := range required { | ||
if _, ok := r.URL.Query()[field]; !ok { | ||
http.Error(w, fmt.Sprintf("%s missing", field), http.StatusBadRequest) | ||
return | ||
} | ||
} | ||
|
||
fmt.Fprintf(w, credsRespStsImpl, expireOn) | ||
})) | ||
|
||
return server | ||
} | ||
|
||
func TestIAMMalformedEndpoint(t *testing.T) { | ||
creds := NewIAM("%%%%") | ||
_, err := creds.Get() | ||
|
@@ -243,3 +281,44 @@ func TestEcsTask(t *testing.T) { | |
t.Error("Expected creds to be expired.") | ||
} | ||
} | ||
|
||
func TestSts(t *testing.T) { | ||
server := initStsTestServer("2014-12-16T01:51:37Z") | ||
defer server.Close() | ||
p := &IAM{ | ||
Client: http.DefaultClient, | ||
endpoint: server.URL, | ||
} | ||
|
||
f, err := ioutil.TempFile("", "minio-go") | ||
if err != nil { | ||
t.Errorf("Unexpected failure %s", err) | ||
} | ||
defer os.Remove(f.Name()) | ||
f.Write([]byte("token")) | ||
f.Close() | ||
|
||
os.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", f.Name()) | ||
os.Setenv("AWS_ROLE_ARN", "arn:aws:sts::123456789012:assumed-role/FederatedWebIdentityRole/app1") | ||
creds, err := p.Retrieve() | ||
os.Unsetenv("AWS_WEB_IDENTITY_TOKEN_FILE") | ||
os.Unsetenv("AWS_ROLE_ARN") | ||
if err != nil { | ||
t.Errorf("Unexpected failure %s", err) | ||
} | ||
if "accessKey" != creds.AccessKeyID { | ||
t.Errorf("Expected \"accessKey\", got %s", creds.AccessKeyID) | ||
} | ||
|
||
if "secret" != creds.SecretAccessKey { | ||
t.Errorf("Expected \"secret\", got %s", creds.SecretAccessKey) | ||
} | ||
|
||
if "token" != creds.SessionToken { | ||
t.Errorf("Expected \"token\", got %s", creds.SessionToken) | ||
} | ||
|
||
if !p.IsExpired() { | ||
t.Error("Expected creds to be expired.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters