Skip to content

Commit

Permalink
proxy: move headers into new config field
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hutchinson <[email protected]>
  • Loading branch information
Jason Hutchinson authored and arekkas committed Aug 16, 2018
1 parent 0e1ef1b commit 51eb9fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
7 changes: 5 additions & 2 deletions proxy/credentials_issuer_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/pkg/errors"
)

type CredentialsHeadersConfig map[string]string
type CredentialsHeadersConfig struct {
Headers map[string]string `json:"headers"`
}

type CredentialsHeaders struct {
RulesCache *template.Template
Expand Down Expand Up @@ -43,11 +45,12 @@ func (a *CredentialsHeaders) Issue(r *http.Request, session *AuthenticationSessi

var cfg CredentialsHeadersConfig
d := json.NewDecoder(bytes.NewBuffer(config))
d.DisallowUnknownFields()
if err := d.Decode(&cfg); err != nil {
return errors.WithStack(err)
}

for hdr, templateString := range cfg {
for hdr, templateString := range cfg.Headers {
var tmpl *template.Template
var err error

Expand Down
19 changes: 10 additions & 9 deletions proxy/credentials_issuer_headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,42 @@ func TestCredentialsIssuerHeaders(t *testing.T) {
"Simple Subject": {
Session: &AuthenticationSession{Subject: "foo"},
Rule: &rule.Rule{ID: "test-rule"},
Config: json.RawMessage([]byte(`{"X-User": "{{ print .Subject }}"}`)),
Config: json.RawMessage([]byte(`{"headers":{"X-User": "{{ print .Subject }}"}}`)),
Request: &http.Request{Header: http.Header{}},
Match: http.Header{"X-User": []string{"foo"}},
},
"Complex Subject": {
Session: &AuthenticationSession{Subject: "foo"},
Rule: &rule.Rule{ID: "test-rule2"},
Config: json.RawMessage([]byte(`{"X-User": "realm:resources:users:{{ print .Subject }}"}`)),
Config: json.RawMessage([]byte(`{"headers":{"X-User": "realm:resources:users:{{ print .Subject }}"}}`)),
Request: &http.Request{Header: http.Header{}},
Match: http.Header{"X-User": []string{"realm:resources:users:foo"}},
},
"Subject & Extras": {
Session: &AuthenticationSession{Subject: "foo", Extra: map[string]interface{}{"iss": "issuer", "aud": "audience"}},
Rule: &rule.Rule{ID: "test-rule3"},
Config: json.RawMessage([]byte(`{"X-User": "{{ print .Subject }}", "X-Issuer": "{{ print .Extra.iss }}", "X-Audience": "{{ print .Extra.aud }}"}`)),
Config: json.RawMessage([]byte(`{"headers":{"X-User": "{{ print .Subject }}", "X-Issuer": "{{ print .Extra.iss }}", "X-Audience": "{{ print .Extra.aud }}"}}`)),
Request: &http.Request{Header: http.Header{}},
Match: http.Header{"X-User": []string{"foo"}, "X-Issuer": []string{"issuer"}, "X-Audience": []string{"audience"}},
},
"All In One Header": {
Session: &AuthenticationSession{Subject: "foo", Extra: map[string]interface{}{"iss": "issuer", "aud": "audience"}},
Rule: &rule.Rule{ID: "test-rule4"},
Config: json.RawMessage([]byte(`{"X-Kitchen-Sink": "{{ print .Subject }} {{ print .Extra.iss }} {{ print .Extra.aud }}"}`)),
Config: json.RawMessage([]byte(`{"headers":{"X-Kitchen-Sink": "{{ print .Subject }} {{ print .Extra.iss }} {{ print .Extra.aud }}"}}`)),
Request: &http.Request{Header: http.Header{}},
Match: http.Header{"X-Kitchen-Sink": []string{"foo issuer audience"}},
},
"Scrub Incoming Headers": {
Session: &AuthenticationSession{Subject: "anonymous"},
Rule: &rule.Rule{ID: "test-rule5"},
Config: json.RawMessage([]byte(`{"X-User": "{{ print .Subject }}", "X-Issuer": "{{ print .Extra.iss }}", "X-Audience": "{{ print .Extra.aud }}"}`)),
Config: json.RawMessage([]byte(`{"headers":{"X-User": "{{ print .Subject }}", "X-Issuer": "{{ print .Extra.iss }}", "X-Audience": "{{ print .Extra.aud }}"}}`)),
Request: &http.Request{Header: http.Header{"X-User": []string{"admin"}, "X-Issuer": []string{"issuer"}, "X-Audience": []string{"audience"}}},
Match: http.Header{"X-User": []string{"anonymous"}, "X-Issuer": []string{""}, "X-Audience": []string{""}},
},
"Missing Extras": {
Session: &AuthenticationSession{Subject: "foo", Extra: map[string]interface{}{}},
Rule: &rule.Rule{ID: "test-rule6"},
Config: json.RawMessage([]byte(`{"X-Issuer": "{{ print .Extra.iss }}"}`)),
Config: json.RawMessage([]byte(`{"headers":{"X-Issuer": "{{ print .Extra.iss }}"}}`)),
Request: &http.Request{Header: http.Header{}},
Match: http.Header{"X-Issuer": []string{""}},
},
Expand All @@ -76,12 +76,12 @@ func TestCredentialsIssuerHeaders(t *testing.T) {
},
},
Rule: &rule.Rule{ID: "test-rule7"},
Config: json.RawMessage([]byte(`{
Config: json.RawMessage([]byte(`{"headers":{
"X-Nested-Int": "{{ print .Extra.nested.int }}",
"X-Nested-Float64": "{{ print .Extra.nested.float64 }}",
"X-Nested-Bool": "{{ print .Extra.nested.bool}}",
"X-Nested-Nonexistent": "{{ print .Extra.nested.nil }}"
}`)),
}}`)),
Request: &http.Request{Header: http.Header{}},
Match: http.Header{
"X-Nested-Int": []string{"10"},
Expand Down Expand Up @@ -120,9 +120,10 @@ func TestCredentialsIssuerHeaders(t *testing.T) {

var cfg CredentialsHeadersConfig
d := json.NewDecoder(bytes.NewBuffer(specs.Config))
d.DisallowUnknownFields()
require.NoError(t, d.Decode(&cfg))

for hdr, _ := range cfg {
for hdr, _ := range cfg.Headers {
templateId := fmt.Sprintf("%s:%s", specs.Rule.ID, hdr)
cache.New(templateId).Parse("override")
overrideHeaders.Add(hdr, "override")
Expand Down

0 comments on commit 51eb9fb

Please sign in to comment.