Skip to content

Commit

Permalink
sts endpoint fix for china regions (#1444)
Browse files Browse the repository at this point in the history
  • Loading branch information
narasimman-elu authored Feb 2, 2021
1 parent c407228 commit 546cb78
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/credentials/iam_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"net/url"
"os"
"path"
"strings"
"time"

jsoniter "github.com/json-iterator/go"
Expand Down Expand Up @@ -82,7 +83,11 @@ func (m *IAM) Retrieve() (Value, error) {
case len(os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE")) > 0:
if len(endpoint) == 0 {
if len(os.Getenv("AWS_REGION")) > 0 {
endpoint = "https://sts." + os.Getenv("AWS_REGION") + ".amazonaws.com"
if strings.HasPrefix(os.Getenv("AWS_REGION"), "cn-") {
endpoint = "https://sts." + os.Getenv("AWS_REGION") + ".amazonaws.com.cn"
} else {
endpoint = "https://sts." + os.Getenv("AWS_REGION") + ".amazonaws.com"
}
} else {
endpoint = defaultSTSRoleEndpoint
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/credentials/iam_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,45 @@ func TestSts(t *testing.T) {
t.Error("Expected creds to be expired.")
}
}

func TestStsCn(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_REGION", "cn-northwest-1")
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.")
}
}

0 comments on commit 546cb78

Please sign in to comment.