Skip to content

Commit a274795

Browse files
authored
add assurance tests for auth scheme select (#2730)
1 parent 2d43bf8 commit a274795

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "e9a1faf3-8229-4ca1-9baa-08d59a715f4d",
3+
"type": "bugfix",
4+
"description": "Add assurance tests for auth scheme selection logic.",
5+
"modules": [
6+
"service/s3"
7+
]
8+
}

service/s3/auth_scheme_test.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package s3
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
smithy "github.com/aws/smithy-go"
8+
"github.com/aws/smithy-go/auth"
9+
smithyauth "github.com/aws/smithy-go/auth"
10+
smithyhttp "github.com/aws/smithy-go/transport/http"
11+
)
12+
13+
// FUTURE: move to smithy-go, see https://github.com/aws/smithy-go/issues/528
14+
15+
type mockAuthScheme struct {
16+
id string
17+
configured bool
18+
}
19+
20+
var _ smithyhttp.AuthScheme = (*mockAuthScheme)(nil)
21+
22+
func (m *mockAuthScheme) SchemeID() string { return m.id }
23+
24+
func (m *mockAuthScheme) IdentityResolver(_ auth.IdentityResolverOptions) auth.IdentityResolver {
25+
if m.configured {
26+
return &mockIdentityResolver{}
27+
}
28+
return nil
29+
}
30+
31+
func (*mockAuthScheme) Signer() smithyhttp.Signer { return nil }
32+
33+
type mockIdentityResolver struct{}
34+
35+
var _ smithyauth.IdentityResolver = (*mockIdentityResolver)(nil)
36+
37+
func (*mockIdentityResolver) GetIdentity(ctx context.Context, props smithy.Properties) (smithyauth.Identity, error) {
38+
return nil, nil
39+
}
40+
41+
func contains(have []string, want string) bool {
42+
for _, v := range have {
43+
if v == want {
44+
return true
45+
}
46+
}
47+
return false
48+
}
49+
50+
func TestSelectScheme(t *testing.T) {
51+
for name, c := range map[string]struct {
52+
Supported []string
53+
Configured []string
54+
Expect string
55+
}{
56+
"support(sigv4, bearer) + cfg(sigv4, bearer) = sigv4": {
57+
Supported: []string{
58+
smithyauth.SchemeIDSigV4,
59+
smithyauth.SchemeIDHTTPBearer,
60+
},
61+
Configured: []string{
62+
smithyauth.SchemeIDSigV4,
63+
smithyauth.SchemeIDHTTPBearer,
64+
},
65+
Expect: smithyauth.SchemeIDSigV4,
66+
},
67+
"support(sigv4, bearer) + cfg(bearer) = bearer": {
68+
Supported: []string{
69+
smithyauth.SchemeIDSigV4,
70+
smithyauth.SchemeIDHTTPBearer,
71+
},
72+
Configured: []string{
73+
smithyauth.SchemeIDHTTPBearer,
74+
},
75+
Expect: smithyauth.SchemeIDHTTPBearer,
76+
},
77+
"support(sigv4, bearer) + cfg(sigv4) = sigv4": {
78+
Supported: []string{
79+
smithyauth.SchemeIDSigV4,
80+
smithyauth.SchemeIDHTTPBearer,
81+
},
82+
Configured: []string{
83+
smithyauth.SchemeIDSigV4,
84+
},
85+
Expect: smithyauth.SchemeIDSigV4,
86+
},
87+
"support(sigv4, bearer) + cfg(n/a) = error": {
88+
Supported: []string{
89+
smithyauth.SchemeIDSigV4,
90+
smithyauth.SchemeIDHTTPBearer,
91+
},
92+
Configured: []string{},
93+
Expect: "",
94+
},
95+
"support(sigv4) + cfg(bearer) = error": {
96+
Supported: []string{
97+
smithyauth.SchemeIDSigV4,
98+
},
99+
Configured: []string{
100+
smithyauth.SchemeIDHTTPBearer,
101+
},
102+
Expect: "",
103+
},
104+
"support(anon) + cfg(sigv4) = anon": {
105+
Supported: []string{
106+
smithyauth.SchemeIDAnonymous,
107+
},
108+
Configured: []string{
109+
smithyauth.SchemeIDSigV4,
110+
},
111+
Expect: smithyauth.SchemeIDAnonymous,
112+
},
113+
"support(anon) + cfg(n/a) = anon": {
114+
Supported: []string{
115+
smithyauth.SchemeIDAnonymous,
116+
},
117+
Configured: []string{},
118+
Expect: smithyauth.SchemeIDAnonymous,
119+
},
120+
} {
121+
t.Run(name, func(t *testing.T) {
122+
authopts := []*smithyauth.Option{}
123+
for _, id := range c.Supported {
124+
authopts = append(authopts, &smithyauth.Option{
125+
SchemeID: id,
126+
})
127+
}
128+
129+
m := &resolveAuthSchemeMiddleware{
130+
options: Options{
131+
AuthSchemes: []smithyhttp.AuthScheme{
132+
&mockAuthScheme{
133+
id: smithyauth.SchemeIDSigV4,
134+
configured: contains(c.Configured, smithyauth.SchemeIDSigV4),
135+
},
136+
&mockAuthScheme{
137+
id: smithyauth.SchemeIDHTTPBearer,
138+
configured: contains(c.Configured, smithyauth.SchemeIDHTTPBearer),
139+
},
140+
&mockAuthScheme{
141+
id: smithyauth.SchemeIDAnonymous,
142+
configured: true,
143+
},
144+
},
145+
},
146+
}
147+
148+
scheme, ok := m.selectScheme(authopts)
149+
if c.Expect != "" {
150+
if !ok {
151+
t.Errorf("expected scheme '%s', got none", c.Expect)
152+
}
153+
if actual := scheme.Scheme.SchemeID(); c.Expect != actual {
154+
t.Errorf("expected scheme '%s', got '%s'", c.Expect, actual)
155+
}
156+
} else {
157+
if ok {
158+
t.Errorf("expected no scheme, got '%s'", scheme.Scheme.SchemeID())
159+
}
160+
}
161+
})
162+
}
163+
}

0 commit comments

Comments
 (0)