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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
package s3
import (
"context"
"net/http"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/internal/sdk"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/smithy-go/middleware"
)
type mockCreateSession struct {
wg sync.WaitGroup
calls []mockCreateSessionCall
times int
}
type mockCreateSessionCall struct {
output *CreateSessionOutput
err error
}
func (m *mockCreateSession) expectCalled(t *testing.T, times int) {
if m.times != times {
t.Errorf("expected %d calls to CreateSession, got %d", times, m.times)
}
}
func (m *mockCreateSession) CreateSession(context.Context, *CreateSessionInput, ...func(*Options)) (*CreateSessionOutput, error) {
defer m.wg.Done()
o := m.calls[m.times]
m.times++
return o.output, o.err
}
type mockCreds struct {
akid, secret, session string
}
func newMockCreds(akid, secret, session string) *mockCreds {
return &mockCreds{akid: akid, secret: secret, session: session}
}
func (m *mockCreds) Retrieve(ctx context.Context) (aws.Credentials, error) {
return aws.Credentials{
AccessKeyID: m.akid,
SecretAccessKey: m.secret,
SessionToken: m.session,
}, nil
}
func TestS3Express_Retrieve(t *testing.T) {
sdk.NowTime = func() time.Time {
return time.Unix(0, 0)
}
mockClient := &mockCreateSession{
calls: []mockCreateSessionCall{
{
output: &CreateSessionOutput{
Credentials: &types.SessionCredentials{
AccessKeyId: aws.String("AccessKeyId-0"),
Expiration: aws.Time(time.Unix(3600, 0).UTC()),
SecretAccessKey: aws.String("SecretAccessKey-0"),
SessionToken: aws.String("SessionToken-0"),
},
},
},
{
output: &CreateSessionOutput{
Credentials: &types.SessionCredentials{
AccessKeyId: aws.String("AccessKeyId-1"),
Expiration: aws.Time(time.Unix(7200, 0).UTC()),
SecretAccessKey: aws.String("SecretAccessKey-1"),
SessionToken: aws.String("SessionToken-1"),
},
},
},
},
}
c := newDefaultS3ExpressCredentialsProvider()
c.client = mockClient
c.v4creds = newMockCreds("AKID", "SECRET", "SESSION")
mockClient.wg.Add(3)
c0, err := c.Retrieve(context.Background(), "bucket-0")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
c1, err := c.Retrieve(context.Background(), "bucket-1")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
c2, err := c.Retrieve(context.Background(), "bucket-0")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
expected0 := aws.Credentials{
AccessKeyID: "AccessKeyId-0",
SecretAccessKey: "SecretAccessKey-0",
SessionToken: "SessionToken-0",
CanExpire: true,
Expires: time.Unix(3600, 0).UTC(),
}
expected1 := aws.Credentials{
AccessKeyID: "AccessKeyId-1",
SecretAccessKey: "SecretAccessKey-1",
SessionToken: "SessionToken-1",
CanExpire: true,
Expires: time.Unix(7200, 0).UTC(),
}
// one should have been a cache hit
mockClient.expectCalled(t, 2)
if expected0 != c0 {
t.Errorf("expected credentials %v, got %v", expected0, c0)
}
if expected0 != c2 {
t.Errorf("expected credentials %v, got %v", expected0, c2)
}
if expected1 != c1 {
t.Errorf("expected credentials %v, got %v", expected1, c1)
}
}
func TestS3Express_AsyncRefresh(t *testing.T) {
sdk.NowTime = func() time.Time {
return time.Unix(0, 0)
}
mockClient := &mockCreateSession{
calls: []mockCreateSessionCall{
{
output: &CreateSessionOutput{
Credentials: &types.SessionCredentials{
AccessKeyId: aws.String("AccessKeyId-0"),
Expiration: aws.Time(time.Unix(30, 0).UTC()),
SecretAccessKey: aws.String("SecretAccessKey-0"),
SessionToken: aws.String("SessionToken-0"),
},
},
},
{
output: &CreateSessionOutput{
Credentials: &types.SessionCredentials{
AccessKeyId: aws.String("AccessKeyId-0"),
Expiration: aws.Time(time.Unix(3600, 0).UTC()),
SecretAccessKey: aws.String("SecretAccessKey-0"),
SessionToken: aws.String("SessionToken-0"),
},
},
},
},
}
c := newDefaultS3ExpressCredentialsProvider()
c.client = mockClient
c.v4creds = newMockCreds("AKID", "SECRET", "SESSION")
mockClient.wg.Add(2)
c0, err := c.Retrieve(context.Background(), "bucket-0")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
c1, err := c.Retrieve(context.Background(), "bucket-0")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
mockClient.wg.Wait() // block on the async retrieve that we've now triggered
// the first set should still be returned the 2nd time since it's still valid
expected := aws.Credentials{
AccessKeyID: "AccessKeyId-0",
SecretAccessKey: "SecretAccessKey-0",
SessionToken: "SessionToken-0",
CanExpire: true,
Expires: time.Unix(30, 0).UTC(),
}
// 2nd call should happen due to refresh window
mockClient.expectCalled(t, 2)
if expected != c0 {
t.Errorf("expected credentials %v, got %v", expected, c0)
}
if expected != c1 {
t.Errorf("expected credentials %v, got %v", expected, c1)
}
}
type mockHTTP struct{}
func (*mockHTTP) Do(*http.Request) (*http.Response, error) {
return &http.Response{}, nil
}
func TestS3Express_OperationCredentialOverride(t *testing.T) {
sdk.NowTime = func() time.Time {
return time.Unix(0, 0)
}
createSessionClient := &mockCreateSession{
calls: []mockCreateSessionCall{
{
output: &CreateSessionOutput{
Credentials: &types.SessionCredentials{
AccessKeyId: aws.String("EXPRESS_AKID0"),
SecretAccessKey: aws.String("EXPRESS_SECRET0"),
SessionToken: aws.String("EXPRESS_TOKEN0"),
Expiration: aws.Time(time.Unix(3600, 0).UTC()),
},
},
},
{
output: &CreateSessionOutput{
Credentials: &types.SessionCredentials{
AccessKeyId: aws.String("EXPRESS_AKID1"),
SecretAccessKey: aws.String("EXPRESS_SECRET1"),
SessionToken: aws.String("EXPRESS_TOKEN1"),
Expiration: aws.Time(time.Unix(3600, 0).UTC()),
},
},
},
},
}
createSessionClient.wg.Add(2)
svc := New(Options{
Region: "us-west-2",
Credentials: newMockCreds("AKID0", "SECRET0", "SESSION0"),
HTTPClient: &mockHTTP{},
APIOptions: []func(*middleware.Stack) error{
func(stack *middleware.Stack) error {
stack.Deserialize.Clear()
return stack.Deserialize.Add(
middleware.DeserializeMiddlewareFunc(
"mockResponse",
func(context.Context, middleware.DeserializeInput, middleware.DeserializeHandler) (middleware.DeserializeOutput, middleware.Metadata, error) {
out := middleware.DeserializeOutput{
Result: &GetObjectOutput{},
}
return out, middleware.Metadata{}, nil
},
),
middleware.After,
)
},
},
})
expressProvider, _ := svc.options.ExpressCredentials.(*defaultS3ExpressCredentialsProvider)
expressProvider.client = createSessionClient
_, err := svc.GetObject(context.Background(), &GetObjectInput{
Bucket: aws.String("bucket--usw2-az1--x-s3"),
Key: aws.String("key"),
})
if err != nil {
t.Errorf("get object: %v", err)
}
// there should be one set of credentials in the cache
key0 := cacheKey{
CredentialsHash: gethmac("AKID0", "SECRET0"),
Bucket: "bucket--usw2-az1--x-s3",
}
_, ok := expressProvider.cache.Get(key0)
if !ok {
t.Errorf("creds for AKID0/SECRET0 are missing")
}
_, err = svc.GetObject(context.Background(), &GetObjectInput{
Bucket: aws.String("bucket--usw2-az1--x-s3"),
Key: aws.String("key"),
}, func(o *Options) {
o.Credentials = newMockCreds("AKID1", "SECRET1", "SESSION1")
})
if err != nil {
t.Errorf("get object: %v", err)
}
// checking two things here:
// - we have a new cache entry since creds changed
// - note we're still using the original pointer, the operation finalizer
// should have copied it and passed the cache along
key1 := cacheKey{
CredentialsHash: gethmac("AKID1", "SECRET1"),
Bucket: "bucket--usw2-az1--x-s3",
}
_, ok = expressProvider.cache.Get(key1)
if !ok {
t.Errorf("creds for AKID1/SECRET1 are missing")
}
// repeat of 1st call, should be a cache hit
_, err = svc.GetObject(context.Background(), &GetObjectInput{
Bucket: aws.String("bucket--usw2-az1--x-s3"),
Key: aws.String("key"),
})
if err != nil {
t.Errorf("get object: %v", err)
}
createSessionClient.expectCalled(t, 2)
}
|