1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
package config
import (
"context"
"path/filepath"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials/ssocreds"
"github.com/aws/aws-sdk-go-v2/internal/sdk"
smithybearer "github.com/aws/smithy-go/auth/bearer"
)
func TestResolveBearerAuthToken(t *testing.T) {
cases := map[string]struct {
setNowTime func() func()
configs configs
expectProvider bool
expectToken smithybearer.Token
}{
"no provider": {},
"config source provider": {
configs: configs{
LoadOptions{
BearerAuthTokenProvider: smithybearer.StaticTokenProvider{
Token: smithybearer.Token{Value: "abc123"},
},
},
},
expectProvider: true,
expectToken: smithybearer.Token{Value: "abc123"},
},
"sso session legacy": {
setNowTime: func() func() {
return sdk.TestingUseReferenceTime(time.Date(2044, 4, 4, 0, 0, 0, 0, time.UTC))
},
configs: configs{
LoadOptions{
SSOTokenProviderOptions: func(o *ssocreds.SSOTokenProviderOptions) {
expectPath, _ := ssocreds.StandardCachedTokenFilepath("https://example.aws/start")
if e, a := expectPath, o.CachedTokenFilepath; e != a {
t.Errorf("expect %v cache file path, got %v", e, a)
}
o.CachedTokenFilepath = filepath.Join("testdata", "cached_sso_token.json")
},
},
SharedConfig{
SSORegion: "us-west-2",
SSOStartURL: "https://example.aws/start",
},
},
expectProvider: false,
expectToken: smithybearer.Token{
Value: "access token",
CanExpire: true,
Expires: time.Date(2044, 4, 4, 7, 0, 1, 0, time.UTC),
},
},
"sso session named": {
setNowTime: func() func() {
return sdk.TestingUseReferenceTime(time.Date(2044, 4, 4, 0, 0, 0, 0, time.UTC))
},
configs: configs{
LoadOptions{
SSOTokenProviderOptions: func(o *ssocreds.SSOTokenProviderOptions) {
expectPath, _ := ssocreds.StandardCachedTokenFilepath("test-session")
if e, a := expectPath, o.CachedTokenFilepath; e != a {
t.Errorf("expect %v cache file path, got %v", e, a)
}
o.CachedTokenFilepath = filepath.Join("testdata", "cached_sso_token.json")
},
},
SharedConfig{
SSOSessionName: "test-session",
SSOSession: &SSOSession{
Name: "test-session",
SSORegion: "us-west-2",
SSOStartURL: "https://example.aws/start",
},
},
},
expectProvider: true,
expectToken: smithybearer.Token{
Value: "access token",
CanExpire: true,
Expires: time.Date(2044, 4, 4, 7, 0, 1, 0, time.UTC),
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
if c.setNowTime != nil {
restoreTime := c.setNowTime()
defer restoreTime()
}
var cfg aws.Config
err := resolveBearerAuthToken(context.Background(), &cfg, c.configs)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if !c.expectProvider {
if v := cfg.BearerAuthTokenProvider; v != nil {
t.Errorf("expect no provider, got %T, %v", v, v)
}
return
}
token, err := cfg.BearerAuthTokenProvider.RetrieveBearerToken(context.Background())
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if diff := cmpDiff(c.expectToken, token); diff != "" {
t.Errorf("expect token match\n%s", diff)
}
})
}
}
func TestWrapWithBearerAuthTokenProvider(t *testing.T) {
cases := map[string]struct {
configs configs
provider smithybearer.TokenProvider
optFns []func(*smithybearer.TokenCacheOptions)
compareProvider bool
expectToken smithybearer.Token
}{
"already wrapped": {
provider: smithybearer.NewTokenCache(smithybearer.StaticTokenProvider{
Token: smithybearer.Token{Value: "abc123"},
}),
compareProvider: true,
expectToken: smithybearer.Token{Value: "abc123"},
},
"to be wrapped": {
provider: smithybearer.StaticTokenProvider{
Token: smithybearer.Token{Value: "abc123"},
},
expectToken: smithybearer.Token{Value: "abc123"},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
provider, err := wrapWithBearerAuthTokenCache(context.Background(),
c.configs, c.provider, c.optFns...)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if p, ok := provider.(*smithybearer.TokenCache); !ok {
t.Fatalf("expect provider wrapped in %T, got %T", p, provider)
}
if c.compareProvider && provider != c.provider {
t.Errorf("expect same provider, was not")
}
token, err := provider.RetrieveBearerToken(context.Background())
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if diff := cmpDiff(c.expectToken, token); diff != "" {
t.Errorf("expect token match\n%s", diff)
}
})
}
}
|