/
mutator_cookie_test.go
229 lines (206 loc) · 7.7 KB
/
mutator_cookie_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
229
package mutate_test
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"testing"
"text/template"
"github.com/ory/viper"
"github.com/ory/oathkeeper/driver/configuration"
"github.com/ory/oathkeeper/internal"
"github.com/ory/oathkeeper/pipeline/authn"
. "github.com/ory/oathkeeper/pipeline/mutate"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ory/oathkeeper/rule"
)
func TestCredentialsIssuerCookies(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistry(conf)
a, err := reg.PipelineMutator("cookie")
require.NoError(t, err)
assert.Equal(t, "cookie", a.GetID())
t.Run("method=mutate", func(t *testing.T) {
var testMap = map[string]struct {
Session *authn.AuthenticationSession
Rule *rule.Rule
Config json.RawMessage
Request *http.Request
Match []*http.Cookie
Err error
}{
"Simple Subject": {
Session: &authn.AuthenticationSession{Subject: "foo"},
Rule: &rule.Rule{ID: "test-rule"},
Config: json.RawMessage([]byte(`{"cookies": {"user": "{{ print .Subject }}"}}`)),
Request: &http.Request{Header: http.Header{}},
Match: []*http.Cookie{&http.Cookie{Name: "user", Value: "foo"}},
Err: nil,
},
"Unknown Config Field": {
Session: &authn.AuthenticationSession{},
Rule: &rule.Rule{ID: "test-rule2"},
Config: json.RawMessage([]byte(`{"bar": "baz"}`)),
Request: &http.Request{Header: http.Header{}},
Match: []*http.Cookie{},
Err: errors.New(`mutator matching this route is misconfigured or disabled`),
},
"Complex Subject": {
Session: &authn.AuthenticationSession{Subject: "foo"},
Rule: &rule.Rule{ID: "test-rule3"},
Config: json.RawMessage([]byte(`{"cookies": {"user": "realm:resources:users:{{ print .Subject }}"}}`)),
Request: &http.Request{Header: http.Header{}},
Match: []*http.Cookie{&http.Cookie{Name: "user", Value: "realm:resources:users:foo"}},
Err: nil,
},
"Subject & Extras": {
Session: &authn.AuthenticationSession{Subject: "foo", Extra: map[string]interface{}{"iss": "issuer", "aud": "audience"}},
Rule: &rule.Rule{ID: "test-rule4"},
Config: json.RawMessage([]byte(`{"cookies":{"user": "{{ print .Subject }}", "issuer": "{{ print .Extra.iss }}", "audience": "{{ print .Extra.aud }}"}}`)),
Request: &http.Request{Header: http.Header{}},
Match: []*http.Cookie{
&http.Cookie{Name: "user", Value: "foo"},
&http.Cookie{Name: "issuer", Value: "issuer"},
&http.Cookie{Name: "audience", Value: "audience"},
},
Err: nil,
},
"All In One Cookie": {
Session: &authn.AuthenticationSession{Subject: "foo", Extra: map[string]interface{}{"iss": "issuer", "aud": "audience"}},
Rule: &rule.Rule{ID: "test-rule5"},
Config: json.RawMessage([]byte(`{"cookies":{"kitchensink": "{{ print .Subject }} {{ print .Extra.iss }} {{ print .Extra.aud }}"}}`)),
Request: &http.Request{Header: http.Header{}},
Match: []*http.Cookie{
&http.Cookie{Name: "kitchensink", Value: "foo issuer audience"},
},
Err: nil,
},
"Scrub Incoming Cookies": {
Session: &authn.AuthenticationSession{Subject: "anonymous"},
Rule: &rule.Rule{ID: "test-rule6"},
Config: json.RawMessage([]byte(`{"cookies":{"user": "{{ print .Subject }}", "issuer": "{{ print .Extra.iss }}", "audience": "{{ print .Extra.aud }}"}}`)),
Request: &http.Request{
Header: http.Header{"Cookie": []string{"user=admin;issuer=issuer;audience=audience"}},
},
Match: []*http.Cookie{
&http.Cookie{Name: "user", Value: "anonymous"},
&http.Cookie{Name: "issuer", Value: ""},
&http.Cookie{Name: "audience", Value: ""},
},
Err: nil,
},
"Missing Extras": {
Session: &authn.AuthenticationSession{Subject: "foo", Extra: map[string]interface{}{}},
Rule: &rule.Rule{ID: "test-rule7"},
Config: json.RawMessage([]byte(`{"cookies":{"issuer": "{{ print .Extra.iss }}"}}`)),
Request: &http.Request{Header: http.Header{}},
Match: []*http.Cookie{&http.Cookie{Name: "issuer", Value: ""}},
Err: nil,
},
"Nested Extras": {
Session: &authn.AuthenticationSession{
Subject: "foo",
Extra: map[string]interface{}{
"nested": map[string]interface{}{
"int": int(10),
"float64": float64(3.14159),
"bool": true,
},
},
},
Rule: &rule.Rule{ID: "test-rule8"},
Config: json.RawMessage([]byte(`{"cookies":{
"nested-int": "{{ print .Extra.nested.int }}",
"nested-float64": "{{ print .Extra.nested.float64 }}",
"nested-bool": "{{ print .Extra.nested.bool}}",
"nested-nonexistent": "{{ print .Extra.nested.nil }}"
}}`)),
Request: &http.Request{Header: http.Header{}},
Match: []*http.Cookie{
&http.Cookie{Name: "nested-int", Value: "10"},
&http.Cookie{Name: "nested-float64", Value: "3.14159"},
&http.Cookie{Name: "nested-bool", Value: "true"},
&http.Cookie{Name: "nested-nonexistent", Value: ""},
},
Err: nil,
},
"Advanced template with sprig function": {
Session: &authn.AuthenticationSession{
Subject: "foo",
Extra: map[string]interface{}{
"example.com/some-claims": []string{"Foo", "Bar"},
},
},
Rule: &rule.Rule{ID: "test-rule9"},
Config: json.RawMessage([]byte(`{"cookies":{
"example-claims": "{{- (index .Extra \"example.com/some-claims\") | join \",\" -}}"
}}`)),
Request: &http.Request{Header: http.Header{}},
Match: []*http.Cookie{
{
Name: "example-claims",
Value: "Foo,Bar",
},
},
Err: nil,
},
}
t.Run("caching=off", func(t *testing.T) {
for testName, specs := range testMap {
t.Run(testName, func(t *testing.T) {
err := a.Mutate(specs.Request, specs.Session, specs.Config, specs.Rule)
if specs.Err == nil {
// Issuer must run without error
require.NoError(t, err)
} else {
assert.EqualError(t, err, specs.Err.Error())
}
specs.Request.Header = specs.Session.Header
assert.Equal(t, serializeCookies(specs.Match), serializeCookies(specs.Request.Cookies()))
})
}
})
t.Run("caching=on", func(t *testing.T) {
for _, specs := range testMap {
var overrideCookies []*http.Cookie
cache := template.New("rules")
var cfg CredentialsCookiesConfig
require.NoError(t, json.NewDecoder(bytes.NewBuffer(specs.Config)).Decode(&cfg))
for cookie := range cfg.Cookies {
templateId := fmt.Sprintf("%s:%s", specs.Rule.ID, cookie)
_, err := cache.New(templateId).Parse("override")
require.NoError(t, err)
overrideCookies = append(overrideCookies, &http.Cookie{Name: cookie, Value: "override"})
}
a.(*MutatorCookie).WithCache(cache)
err := a.Mutate(specs.Request, specs.Session, specs.Config, specs.Rule)
if specs.Err == nil {
// Issuer must run without error
require.NoError(t, err)
} else {
assert.EqualError(t, err, specs.Err.Error())
}
specs.Request.Header = specs.Session.Header
assert.Equal(t, serializeCookies(overrideCookies), serializeCookies(specs.Request.Cookies()))
}
})
t.Run("method=validate", func(t *testing.T) {
viper.Set(configuration.ViperKeyMutatorCookieIsEnabled, true)
require.Error(t, a.Validate(json.RawMessage(`{}`)))
viper.Reset()
viper.Set(configuration.ViperKeyMutatorCookieIsEnabled, false)
require.Error(t, a.Validate(json.RawMessage(`{"cookies":{}}`)))
})
})
}
// assert.Equal doesn't handle []*http.Cookie comparisons very well, so
// converting them to a simple map[string]string makes testing easier
func serializeCookies(cookies []*http.Cookie) map[string]string {
out := map[string]string{}
for _, cookie := range cookies {
out[cookie.Name] = cookie.Value
}
return out
}