Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy: Allow injection of request headers #244

Merged
merged 2 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/proxy/oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,9 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) (er
req.Header.Set("X-Forwarded-Email", session.Email)
req.Header.Set("X-Forwarded-Groups", strings.Join(session.Groups, ","))

for key, val := range p.upstreamConfig.InjectRequestHeaders {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move these above setting of the X-Forwarded- headers. We don't want to allow impersonation by setting these headers from a config.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved

req.Header.Set(key, val)
}
// stash authenticated user so that it can be logged later (see func logRequest)
rw.Header().Set(loggingUserHeader, session.Email)

Expand Down
23 changes: 13 additions & 10 deletions internal/proxy/proxy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type UpstreamConfig struct {
ResetDeadline time.Duration
FlushInterval time.Duration
HeaderOverrides map[string]string
InjectRequestHeaders map[string]string
SkipRequestSigning bool
CookieName string
ProviderSlug string
Expand Down Expand Up @@ -87,16 +88,17 @@ type RouteConfig struct {
// * skip_request_signing - skip request signing if this behavior is problematic or undesired. For requests with large http bodies
// this maybe useful to unset as http bodies are read into memory in order to sign.
type OptionsConfig struct {
HeaderOverrides map[string]string `yaml:"header_overrides"`
SkipAuthRegex []string `yaml:"skip_auth_regex"`
AllowedGroups []string `yaml:"allowed_groups"`
TLSSkipVerify bool `yaml:"tls_skip_verify"`
PreserveHost bool `yaml:"preserve_host"`
Timeout time.Duration `yaml:"timeout"`
ResetDeadline time.Duration `yaml:"reset_deadline"`
FlushInterval time.Duration `yaml:"flush_interval"`
SkipRequestSigning bool `yaml:"skip_request_signing"`
ProviderSlug string `yaml:"provider_slug"`
HeaderOverrides map[string]string `yaml:"header_overrides"`
InjectRequestHeaders map[string]string `yaml:"inject_request_headers"`
SkipAuthRegex []string `yaml:"skip_auth_regex"`
AllowedGroups []string `yaml:"allowed_groups"`
TLSSkipVerify bool `yaml:"tls_skip_verify"`
PreserveHost bool `yaml:"preserve_host"`
Timeout time.Duration `yaml:"timeout"`
ResetDeadline time.Duration `yaml:"reset_deadline"`
FlushInterval time.Duration `yaml:"flush_interval"`
SkipRequestSigning bool `yaml:"skip_request_signing"`
ProviderSlug string `yaml:"provider_slug"`

// CookieName is still set globally, so we do not provide override behavior
CookieName string
Expand Down Expand Up @@ -398,6 +400,7 @@ func parseOptionsConfig(proxy *UpstreamConfig, defaultOpts *OptionsConfig) error
proxy.ResetDeadline = dst.ResetDeadline
proxy.FlushInterval = dst.FlushInterval
proxy.HeaderOverrides = dst.HeaderOverrides
proxy.InjectRequestHeaders = dst.InjectRequestHeaders
proxy.TLSSkipVerify = dst.TLSSkipVerify
proxy.PreserveHost = dst.PreserveHost
proxy.SkipRequestSigning = dst.SkipRequestSigning
Expand Down
33 changes: 33 additions & 0 deletions internal/proxy/proxy_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,39 @@ func TestUpstreamConfigHeaderOverrides(t *testing.T) {
}
}

func TestUpstreamConfigInjectRequestHeaders(t *testing.T) {
wantHeaders := map[string]string{
"Authorization": "Basic",
}
templateVars := map[string]string{
"cluster": "sso",
"root_domain": "dev",
}
upstreamConfigs, err := loadServiceConfigs([]byte(`
- service: foo
default:
from: foo.{{cluster}}.{{root_domain}}
to: foo-internal.{{cluster}}.{{root_domain}}
options:
inject_request_headers:
Authorization: Basic
`), "sso", "http", templateVars, nil)
if err != nil {
t.Fatalf("expected to parse upstream configs: %s", err)
}

if len(upstreamConfigs) == 0 {
t.Fatalf("expected service config")
}

upstreamConfig := upstreamConfigs[0]
if !reflect.DeepEqual(upstreamConfig.InjectRequestHeaders, wantHeaders) {
t.Logf("want: %v", wantHeaders)
t.Logf(" got: %v", upstreamConfig.InjectRequestHeaders)
t.Errorf("got unexpected header overrides")
}
}

func TestUpstreamConfigErrorParsing(t *testing.T) {
testCases := []struct {
Name string
Expand Down