File: credentials_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.24.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 554,032 kB
  • sloc: java: 15,941; makefile: 419; sh: 175
file content (76 lines) | stat: -rw-r--r-- 2,066 bytes parent folder | download | duplicates (5)
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
package v4a

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/aws"
	"testing"
)

type rotatingCredsProvider struct {
	count int
	fail  chan struct{}
}

func (r *rotatingCredsProvider) Retrieve(ctx context.Context) (aws.Credentials, error) {
	select {
	case <-r.fail:
		return aws.Credentials{}, fmt.Errorf("rotatingCredsProvider error")
	default:
	}
	credentials := aws.Credentials{
		AccessKeyID:     fmt.Sprintf("ACCESS_KEY_ID_%d", r.count),
		SecretAccessKey: fmt.Sprintf("SECRET_ACCESS_KEY_%d", r.count),
		SessionToken:    fmt.Sprintf("SESSION_TOKEN_%d", r.count),
	}
	return credentials, nil
}

func TestSymmetricCredentialAdaptor(t *testing.T) {
	provider := &rotatingCredsProvider{
		count: 0,
		fail:  make(chan struct{}),
	}

	adaptor := &SymmetricCredentialAdaptor{SymmetricProvider: provider}

	if symCreds, err := adaptor.Retrieve(context.Background()); err != nil {
		t.Fatalf("expect no error, got %v", err)
	} else if !symCreds.HasKeys() {
		t.Fatalf("expect symmetric credentials to have keys")
	}

	if load := adaptor.asymmetric.Load(); load != nil {
		t.Errorf("expect asymmetric credentials to be nil")
	}

	if asymCreds, err := adaptor.RetrievePrivateKey(context.Background()); err != nil {
		t.Fatalf("expect no error, got %v", err)
	} else if !asymCreds.HasKeys() {
		t.Fatalf("expect asymmetric credentials to have keys")
	}

	if _, err := adaptor.Retrieve(context.Background()); err != nil {
		t.Fatalf("expect no error, got %v", err)
	}

	if load := adaptor.asymmetric.Load(); load.(*Credentials) == nil {
		t.Errorf("expect asymmetric credentials to be not nil")
	}

	provider.count++

	if _, err := adaptor.Retrieve(context.Background()); err != nil {
		t.Fatalf("expect no error, got %v", err)
	}

	if load := adaptor.asymmetric.Load(); load.(*Credentials) != nil {
		t.Errorf("expect asymmetric credentials to be nil")
	}

	close(provider.fail) // All requests to the original provider will now fail from this point-on.
	_, err := adaptor.Retrieve(context.Background())
	if err == nil {
		t.Error("expect error, got nil")
	}
}