forked from xmidt-org/scytale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WRPAccessControl_test.go
228 lines (201 loc) · 6.01 KB
/
WRPAccessControl_test.go
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
// SPDX-FileCopyrightText: 2019 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"testing"
"github.com/go-kit/kit/metrics"
"github.com/stretchr/testify/assert"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/wrp-go/v3"
)
func TestAuthorizeWRP(t *testing.T) {
testCases := []struct {
Name string
PartnerIDs []string
AllowedPartners []string
TokenType string
InjectSecurityToken bool
ExpectAutocorrect bool
Error error
BaseLabelPairs map[string]string
ExpectedPartnerIDs []string
}{
{
Name: "Bascule token Missing",
Error: ErrTokenMissing,
TokenType: "jwt",
BaseLabelPairs: map[string]string{
ReasonLabel: TokenMissing,
ClientIDLabel: "none",
},
},
{
Name: "Bad bascule token type",
Error: ErrTokenTypeMismatch,
InjectSecurityToken: true,
TokenType: "basic",
AllowedPartners: []string{"partner0"},
BaseLabelPairs: map[string]string{
ReasonLabel: TokenTypeMismatch,
ClientIDLabel: "none",
},
},
{
Name: "Invalid AllowedPartners",
Error: ErrInvalidAllowedPartners,
InjectSecurityToken: true,
TokenType: "jwt",
AllowedPartners: []string{},
BaseLabelPairs: map[string]string{
ReasonLabel: JWTPIDInvalid,
ClientIDLabel: "tester",
},
},
{
Name: "No AllowedPartners",
Error: ErrAllowedPartnersNotFound,
InjectSecurityToken: true,
TokenType: "jwt",
AllowedPartners: nil,
BaseLabelPairs: map[string]string{
ReasonLabel: JWTPIDInvalid,
ClientIDLabel: "tester",
},
},
{
Name: "PartnerIDs missing from WRP",
Error: ErrPIDMissing,
InjectSecurityToken: true,
TokenType: "jwt",
AllowedPartners: []string{"p0", "p1"},
ExpectAutocorrect: true,
BaseLabelPairs: map[string]string{
ReasonLabel: WRPPIDMissing,
ClientIDLabel: "tester",
},
ExpectedPartnerIDs: []string{"p0", "p1"},
},
{
Name: "PartnerIDs is not subset of allowerPartners",
InjectSecurityToken: true,
TokenType: "jwt",
PartnerIDs: []string{"p2"},
AllowedPartners: []string{"p0", "p1"},
Error: ErrPIDMismatch,
BaseLabelPairs: map[string]string{
ReasonLabel: WRPPIDMismatch,
ClientIDLabel: "tester",
},
ExpectedPartnerIDs: []string{"p0", "p1"},
ExpectAutocorrect: true,
},
{
Name: "Wildcard in allowedPartners",
InjectSecurityToken: true,
TokenType: "jwt",
PartnerIDs: []string{"p2"}, //TODO: is this the behavior we actually want? '*' giving user superpowers!
AllowedPartners: []string{"p0", "p1", "*"},
BaseLabelPairs: map[string]string{
ReasonLabel: JWTPIDWildcard,
ClientIDLabel: "tester",
},
ExpectedPartnerIDs: []string{"p2"},
},
{
Name: "Non-empty partnerIDs is subset of allowerPartners",
InjectSecurityToken: true,
TokenType: "jwt",
PartnerIDs: []string{"p0"},
AllowedPartners: []string{"p0", "p1"},
BaseLabelPairs: map[string]string{
ReasonLabel: WRPPIDMatch,
ClientIDLabel: "tester",
},
ExpectedPartnerIDs: []string{"p0"},
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()
if testCase.InjectSecurityToken {
ctx = enrichWithBasculeToken(context.Background(), testCase.TokenType, testCase.AllowedPartners)
}
wrpMsg := &wrp.Message{
PartnerIDs: testCase.PartnerIDs,
}
var (
wrpAccessAuthority wrpAccessAuthority
counter = newTestCounter()
)
expectedStrictLabels, expectedLenientLabels := createLabelMaps(testCase.Error != nil, testCase.BaseLabelPairs)
//strict mode
wrpAccessAuthority = &wrpPartnersAccess{
strict: true,
receivedWRPMessageCount: counter,
}
modified, err := wrpAccessAuthority.authorizeWRP(ctx, wrpMsg)
assert.False(modified)
assert.Equal(testCase.Error, err)
assert.Equal(float64(1), counter.count)
assert.Equal(expectedStrictLabels, counter.labelPairs)
//lenient mode
counter = newTestCounter()
wrpAccessAuthority = &wrpPartnersAccess{
strict: false,
receivedWRPMessageCount: counter,
}
modified, err = wrpAccessAuthority.authorizeWRP(ctx, wrpMsg)
assert.Equal(testCase.ExpectAutocorrect, modified)
assert.Nil(err)
assert.Equal(float64(1), counter.count)
assert.Equal(expectedLenientLabels, counter.labelPairs)
})
}
}
func createLabelMaps(rejected bool, baseLabelPairs map[string]string) (strict map[string]string, lenient map[string]string) {
strict = make(map[string]string)
lenient = make(map[string]string)
for k, v := range baseLabelPairs {
strict[k] = v
lenient[k] = v
}
if rejected {
strict[OutcomeLabel] = Rejected
} else {
strict[OutcomeLabel] = Accepted
}
lenient[OutcomeLabel] = Accepted
return
}
func enrichWithBasculeToken(ctx context.Context, tokenType string, allowedPartners []string) context.Context {
attrs := map[string]interface{}{
"allowedResources": map[string]interface{}{"allowedPartners": allowedPartners},
}
if allowedPartners == nil {
attrs = map[string]interface{}{"allowedResources": map[string]interface{}{}}
}
auth := bascule.Authentication{
Token: bascule.NewToken(tokenType, "tester", bascule.NewAttributes(attrs)),
}
return bascule.WithAuthentication(ctx, auth)
}
type testCounter struct {
count float64
labelPairs map[string]string
}
func (c *testCounter) Add(delta float64) {
c.count += delta
}
func (c *testCounter) With(labelValues ...string) metrics.Counter {
for i := 0; i < len(labelValues)-1; i += 2 {
c.labelPairs[labelValues[i]] = labelValues[i+1]
}
return c
}
func newTestCounter() *testCounter {
return &testCounter{
labelPairs: make(map[string]string),
}
}