Skip to content

Commit c85c540

Browse files
hypnoglowaeneasr
authored andcommitted
Add sprig template library (#235)
1 parent 00c08ba commit c85c540

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/ory/oathkeeper
22

33
require (
4+
github.com/Masterminds/goutils v1.1.0 // indirect
5+
github.com/Masterminds/sprig v2.20.0+incompatible
46
github.com/Microsoft/go-winio v0.4.12 // indirect
57
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf
68
github.com/auth0/go-jwt-middleware v0.0.0-20170425171159-5493cabe49f7
@@ -28,6 +30,8 @@ require (
2830
github.com/google/uuid v1.1.1
2931
github.com/gorilla/handlers v1.4.0 // indirect
3032
github.com/gorilla/mux v1.7.1 // indirect
33+
github.com/huandu/xstrings v1.2.0 // indirect
34+
github.com/imdario/mergo v0.3.7 // indirect
3135
github.com/jessevdk/go-flags v1.4.0 // indirect
3236
github.com/julienschmidt/httprouter v1.2.0
3337
github.com/lib/pq v1.0.0

go.sum

+9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX
88
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
99
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
1010
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
11+
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
12+
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
13+
github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc=
1114
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
15+
github.com/Masterminds/sprig v2.20.0+incompatible h1:dJTKKuUkYW3RMFdQFXPU/s6hg10RgctmTjRcbZ98Ap8=
16+
github.com/Masterminds/sprig v2.20.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
1217
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
1318
github.com/Microsoft/go-winio v0.4.12 h1:xAfWHN1IrQ0NJ9TBC0KBZoqLjzDTr1ML+4MywiUOryc=
1419
github.com/Microsoft/go-winio v0.4.12/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
@@ -374,6 +379,10 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
374379
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
375380
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
376381
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
382+
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
383+
github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
384+
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
385+
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
377386
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
378387
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
379388
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ=

pipeline/mutate/mutator_cookie_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ func TestCredentialsIssuerCookies(t *testing.T) {
135135
},
136136
Err: nil,
137137
},
138+
"Advanced template with sprig function": {
139+
Session: &authn.AuthenticationSession{
140+
Subject: "foo",
141+
Extra: map[string]interface{}{
142+
"example.com/some-claims": []string{"Foo", "Bar"},
143+
},
144+
},
145+
Rule: &rule.Rule{ID: "test-rule9"},
146+
Config: json.RawMessage([]byte(`{"cookies":{
147+
"example-claims": "{{- (index .Extra \"example.com/some-claims\") | join \",\" -}}"
148+
}}`)),
149+
Request: &http.Request{Header: http.Header{}},
150+
Match: []*http.Cookie{
151+
{
152+
Name: "example-claims",
153+
Value: "Foo,Bar",
154+
},
155+
},
156+
Err: nil,
157+
},
138158
}
139159

140160
t.Run("caching=off", func(t *testing.T) {

pipeline/mutate/mutator_header_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ func TestCredentialsIssuerHeaders(t *testing.T) {
123123
Match: http.Header{},
124124
Err: errors.New(`json: unknown field "bar"`),
125125
},
126+
"Advanced template with sprig function": {
127+
Session: &authn.AuthenticationSession{
128+
Subject: "foo",
129+
Extra: map[string]interface{}{
130+
"example.com/some-claims": []string{"Foo", "Bar"},
131+
},
132+
},
133+
Rule: &rule.Rule{ID: "test-rule9"},
134+
Config: json.RawMessage([]byte(`{"headers":{
135+
"Example-Claims": "{{- (index .Extra \"example.com/some-claims\") | join \",\" -}}"
136+
}}`)),
137+
Request: &http.Request{Header: http.Header{}},
138+
Match: http.Header{
139+
"Example-Claims": []string{"Foo,Bar"},
140+
},
141+
Err: nil,
142+
},
126143
}
127144

128145
t.Run("cache=disabled", func(t *testing.T) {

pipeline/mutate/template.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package mutate
33
import (
44
"fmt"
55
"text/template"
6+
7+
"github.com/Masterminds/sprig"
68
)
79

810
func newTemplate(id string) *template.Template {
@@ -16,5 +18,6 @@ func newTemplate(id string) *template.Template {
1618
}
1719
return fmt.Sprintf("%v", i)
1820
},
19-
})
21+
}).
22+
Funcs(sprig.TxtFuncMap())
2023
}

0 commit comments

Comments
 (0)