forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys_test.go
213 lines (186 loc) · 5.87 KB
/
keys_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
package keys
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/open-policy-agent/opa/util/test"
)
func TestParseKeysConfig(t *testing.T) {
key := `-----BEGIN PUBLIC KEY----- MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA7nJwME0QNM6g0Ou9Sylj lcIY4cnBcs8oWVHe74bJ7JTgYmDOk2CA14RE3wJNkUKERP/cRdesKDA/BToJXJUr oYvhjXxUYn+i3wK5vOGRY9WUtTF9paIIpIV4USUOwDh3ufhA9K3tyh+ZVsqn80em 0Lj2ME0EgScuk6u0/UYjjNvcmnQl+uDmghG8xBZh7TZW2+aceMwlb4LJIP36VRhg jKQGIxg2rW8ROXgJaFbNRCbiOUUqlq9SUZuhHo8TNOARXXxp9R4Fq7Cl7ZbwWtNP wAtM1y+Z+iyu/i91m0YLlU2XBOGLu9IA8IZjPlbCnk/SygpV9NNwTY9DSQ0QfXcP TGlsbFwzRzTlhH25wEl3j+2Ub9w/NX7Yo+j/Ei9eGZ8cq0bcvEwDeIo98HeNZWrL UUArayRYvh8zutOlzqehw8waFk9AxpfEp9oWekSz8gZw9OL773EhnglYxxjkPHNz k66CufLuTEf6uE9NLE5HnlQMbiqBFirIyAWGKyU3v2tphKvcogxmzzWA51p0GY0l GZvlLNt2NrJv2oGecyl3BLqHnBi+rGAosa/8XgfQT8RIk7YR/tDPDmPfaqSIc0po +NcHYEH82Yv+gfKSK++1fyssGCsSRJs8PFMuPGgv62fFrE/EHSsHJaNWojSYce/T rxm2RaHhw/8O4oKcfrbaRf8CAwEAAQ== -----END PUBLIC KEY-----`
config := fmt.Sprintf(`{"foo": {"algorithm": "HS256", "key": "FdFYFzERwC2uCBB46pZQi4GG85LujR8obt-KWRBICVQ"},
"bar": {"key": %v}
}`, key)
tests := map[string]struct {
input string
result map[string]*Config
wantErr bool
err error
}{
"valid_config_one_key": {
`{"foo": {"algorithm": "HS256", "key": "FdFYFzERwC2uCBB46pZQi4GG85LujR8obt-KWRBICVQ"}}`,
map[string]*Config{"foo": {Key: "FdFYFzERwC2uCBB46pZQi4GG85LujR8obt-KWRBICVQ", Algorithm: "HS256"}},
false, nil,
},
"valid_config_two_key": {
config,
map[string]*Config{
"foo": {Key: "FdFYFzERwC2uCBB46pZQi4GG85LujR8obt-KWRBICVQ", Algorithm: "HS256"},
"bar": {Key: key, Algorithm: "RS256"},
},
false, nil,
},
"invalid_config_no_key": {
`{"foo": {"algorithm": "HS256"}}`,
nil,
true, fmt.Errorf("invalid keys configuration: no keys provided for key ID foo"),
},
"valid_config_default_alg": {
`{"foo": {"key": "FdFYFzERwC2uCBB46pZQi4GG85LujR8obt-KWRBICVQ"}}`,
map[string]*Config{"foo": {Key: "FdFYFzERwC2uCBB46pZQi4GG85LujR8obt-KWRBICVQ", Algorithm: "RS256"}},
false, nil,
},
"invalid_raw_key_config": {
`{"bar": [1,2,3]}`,
nil,
true, fmt.Errorf("json: cannot unmarshal array into Go value of type keys.Config"),
},
"invalid_raw_config": {
`[1,2,3]`,
nil,
true, fmt.Errorf("json: cannot unmarshal array into Go value of type map[string]json.RawMessage"),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
kc, err := ParseKeysConfig([]byte(tc.input))
if tc.wantErr {
if err == nil {
t.Fatal("Expected error but got nil")
}
if tc.err != nil && tc.err.Error() != err.Error() {
t.Fatalf("Expected error message %v but got %v", tc.err.Error(), err.Error())
}
} else {
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
}
if !reflect.DeepEqual(kc, tc.result) {
t.Fatalf("Expected key config %v but got %v", tc.result, kc)
}
})
}
}
func TestNewKeyConfig(t *testing.T) {
publicKey := `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9KaakMv1XKKDaSch3PFR
3a27oaHp1GNTTNqvb1ZaHZXp+wuhYDwc/MTE67x9GCifvQBWzEGorgTq7aisiOyl
vKifwz6/wQ+62WHKG/sqKn2Xikp3P63aBIPlZcHbkyyRmL62yeyuzYoGvLEYel+m
z5SiKGBwviSY0Th2L4e5sGJuk2HOut6emxDi+E2Fuuj5zokFJvIT6Urlq8f3h6+l
GeR6HUOXqoYVf7ff126GP7dticTVBgibxkkuJFmpvQSW6xmxruT4k6iwjzbZHY7P
ypZ/TdlnuGC1cOpAVyU7k32IJ9CRbt3nwEf5U54LRXLLQjFixWZHwKdDiMTF4ws0
+wIDAQAB
-----END PUBLIC KEY-----`
files := map[string]string{
"public.pem": publicKey,
}
test.WithTempFS(files, func(rootDir string) {
kc, err := NewKeyConfig(filepath.Join(rootDir, "public.pem"), "RS256", "read")
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
expected := &Config{
Key: publicKey,
Algorithm: "RS256",
Scope: "read",
}
if !reflect.DeepEqual(kc, expected) {
t.Fatalf("Expected key config %v but got %v", expected, kc)
}
// secret provided on command-line
kc, err = NewKeyConfig(publicKey, "HS256", "")
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
expected = &Config{
Key: publicKey,
Algorithm: "HS256",
Scope: "",
}
if !reflect.DeepEqual(kc, expected) {
t.Fatalf("Expected key config %v but got %v", expected, kc)
}
})
}
func TestNewKeyConfigFileError(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("cannot run as root")
}
publicKey := `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9KaakMv1XKKDaSch3PFR
3a27oaHp1GNTTNqvb1ZaHZXp+wuhYDwc/MTE67x9GCifvQBWzEGorgTq7aisiOyl
vKifwz6/wQ+62WHKG/sqKn2Xikp3P63aBIPlZcHbkyyRmL62yeyuzYoGvLEYel+m
z5SiKGBwviSY0Th2L4e5sGJuk2HOut6emxDi+E2Fuuj5zokFJvIT6Urlq8f3h6+l
GeR6HUOXqoYVf7ff126GP7dticTVBgibxkkuJFmpvQSW6xmxruT4k6iwjzbZHY7P
ypZ/TdlnuGC1cOpAVyU7k32IJ9CRbt3nwEf5U54LRXLLQjFixWZHwKdDiMTF4ws0
+wIDAQAB
-----END PUBLIC KEY-----`
files := map[string]string{
"public.pem": publicKey,
}
test.WithTempFS(files, func(rootDir string) {
// simulate error while reading file
err := os.Chmod(filepath.Join(rootDir, "public.pem"), 0111)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
_, err = NewKeyConfig(filepath.Join(rootDir, "public.pem"), "RS256", "read")
if err == nil {
t.Fatal("Expected error but got nil")
}
})
}
func TestKeyConfigEqual(t *testing.T) {
tests := map[string]struct {
a *Config
b *Config
exp bool
}{
"equal": {
&Config{
Key: "foo",
Algorithm: "RS256",
Scope: "read",
},
&Config{
Key: "foo",
Algorithm: "RS256",
Scope: "read",
},
true,
},
"not_equal": {
&Config{
Key: "foo",
Algorithm: "RS256",
Scope: "read",
},
&Config{
Key: "foo",
Algorithm: "RS256",
Scope: "write",
},
false,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
actual := tc.a.Equal(tc.b)
if actual != tc.exp {
t.Fatalf("Expected config equal result %v but got %v", tc.exp, actual)
}
})
}
}