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

Customizing HTTP headers in the config file #12485

Merged
merged 26 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5bf301c
Customizing HTTP headers in the config file
hghaf099 Sep 2, 2021
9dfa689
Add changelog, fix bad imports
hghaf099 Sep 3, 2021
65f3d3a
fixing some bugs
hghaf099 Sep 3, 2021
61c12eb
fixing interaction of custom headers and /ui
hghaf099 Sep 3, 2021
f4232cd
Defining a member in core to set custom response headers
hghaf099 Sep 5, 2021
5ec2510
missing additional file
hghaf099 Sep 5, 2021
6982b8a
Some refactoring
hghaf099 Sep 7, 2021
f804b26
Adding automated tests for the feature
hghaf099 Sep 8, 2021
e52db45
Changing some error messages based on some recommendations
hghaf099 Sep 9, 2021
06ef62f
Incorporating custom response headers struct into the request context
hghaf099 Sep 14, 2021
ad93253
removing some unused references
hghaf099 Sep 14, 2021
0905ab2
fixing a test
hghaf099 Sep 14, 2021
33a4aa5
changing some error messages, removing a default header value from /ui
hghaf099 Sep 14, 2021
92867b4
fixing a test
hghaf099 Sep 14, 2021
2ae11ea
wrapping ResponseWriter to set the custom headers
hghaf099 Sep 16, 2021
434d8cb
adding a new test
hghaf099 Sep 16, 2021
6a106f0
some cleanup
hghaf099 Sep 16, 2021
7881067
removing some extra lines
hghaf099 Sep 16, 2021
e266713
Addressing comments
hghaf099 Sep 21, 2021
879d489
fixing some agent tests
hghaf099 Sep 21, 2021
2b7dd50
skipping custom headers from agent listener config,
hghaf099 Sep 22, 2021
0964ef8
Removing default custom headers, and renaming some function varibles
hghaf099 Oct 5, 2021
96cb6df
Merge branch 'main' into hghaf099-VAULT-3190-Parsing-Custom-HTTP-Headers
hghaf099 Oct 5, 2021
b6eedd1
some refacotring
hghaf099 Oct 7, 2021
08d0157
Refactoring and addressing comments
hghaf099 Oct 8, 2021
5734ab1
removing a function and fixing comments
hghaf099 Oct 12, 2021
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
Prev Previous commit
Next Next commit
Defining a member in core to set custom response headers
  • Loading branch information
hghaf099 committed Sep 5, 2021
commit f4232cddd864178c9b2123326cc0fdf6221f4d47
10 changes: 4 additions & 6 deletions command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,9 +1341,6 @@ func (c *ServerCommand) Run(args []string) int {
}
}

// Sanitizing listener config from invalid custom headers
core.SanitizedCustomResponseHeader(config)

status, lns, clusterAddrs, errMsg := c.InitListeners(config, disableClustering, &infoKeys, &info)

if status != 0 {
Expand Down Expand Up @@ -1543,8 +1540,9 @@ func (c *ServerCommand) Run(args []string) int {
}

core.SetConfig(config)
// Sanitizing listener config from invalid patterns
core.SanitizedCustomResponseHeader(config)
if err = core.ReloadCustomListenerHeader(); err != nil {
c.UI.Error(err.Error())
}

if config.LogLevel != "" {
configLogLevel := strings.ToLower(strings.TrimSpace(config.LogLevel))
Expand Down Expand Up @@ -2636,7 +2634,7 @@ func startHttpServers(c *ServerCommand, core *vault.Core, config *server.Config,
})

if len(ln.Config.XForwardedForAuthorizedAddrs) > 0 {
handler = vaulthttp.WrapForwardedForHandler(handler, ln.Config)
handler = vaulthttp.WrapForwardedForHandler(handler, ln.Config, core.SetCustomResponseHeaders)
}

// server defaults
Expand Down
12 changes: 3 additions & 9 deletions http/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package http

import (
"fmt"
"github.com/hashicorp/vault/internalshared/listenerutil"
"net/http"
"strings"

Expand Down Expand Up @@ -38,21 +37,16 @@ func wrapCORSHandler(h http.Handler, core *vault.Core) http.Handler {
h.ServeHTTP(w, req)
return
}
// Getting custom headers from listener's config
la := w.Header().Get("X-Vault-Listener-Add")
lc, err := core.GetCustomResponseHeaders(la)
if err != nil {
core.Logger().Debug("failed to get custom headers from listener config")
}

// Return a 403 if the origin is not allowed to make cross-origin requests.
if !corsConf.IsValidOrigin(origin) {
respondError(w, http.StatusForbidden, fmt.Errorf("origin not allowed"), lc)
respondError(w, http.StatusForbidden, fmt.Errorf("origin not allowed"), core.SetCustomResponseHeaders)
return
}

if req.Method == http.MethodOptions && !strutil.StrListContains(allowedMethods, requestMethod) {
status := http.StatusMethodNotAllowed
listenerutil.SetCustomResponseHeaders(lc, w, status)
core.SetCustomResponseHeaders(w, status)
w.WriteHeader(status)
return
}
Expand Down
12 changes: 6 additions & 6 deletions http/forwarded_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestHandler_XForwardedFor(t *testing.T) {
})
listenerConfig := getListenerConfigForMarshalerTest(goodAddr)
listenerConfig.XForwardedForRejectNotPresent = true
return WrapForwardedForHandler(origHandler, listenerConfig)
return WrapForwardedForHandler(origHandler, listenerConfig, nil)
}

cluster := vault.NewTestCluster(t, nil, &vault.TestClusterOptions{
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestHandler_XForwardedFor(t *testing.T) {
})
listenerConfig := getListenerConfigForMarshalerTest(badAddr)
listenerConfig.XForwardedForRejectNotPresent = true
return WrapForwardedForHandler(origHandler, listenerConfig)
return WrapForwardedForHandler(origHandler, listenerConfig, nil)
}

cluster := vault.NewTestCluster(t, nil, &vault.TestClusterOptions{
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestHandler_XForwardedFor(t *testing.T) {
listenerConfig := getListenerConfigForMarshalerTest(badAddr)
listenerConfig.XForwardedForRejectNotPresent = true
listenerConfig.XForwardedForRejectNotAuthorized = true
return WrapForwardedForHandler(origHandler, listenerConfig)
return WrapForwardedForHandler(origHandler, listenerConfig, nil)
}

cluster := vault.NewTestCluster(t, nil, &vault.TestClusterOptions{
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestHandler_XForwardedFor(t *testing.T) {
listenerConfig.XForwardedForRejectNotPresent = true
listenerConfig.XForwardedForRejectNotAuthorized = true
listenerConfig.XForwardedForHopSkips = 4
return WrapForwardedForHandler(origHandler, listenerConfig)
return WrapForwardedForHandler(origHandler, listenerConfig, nil)
}

cluster := vault.NewTestCluster(t, nil, &vault.TestClusterOptions{
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestHandler_XForwardedFor(t *testing.T) {
listenerConfig.XForwardedForRejectNotPresent = true
listenerConfig.XForwardedForRejectNotAuthorized = true
listenerConfig.XForwardedForHopSkips = 1
return WrapForwardedForHandler(origHandler, listenerConfig)
return WrapForwardedForHandler(origHandler, listenerConfig, nil)
}

cluster := vault.NewTestCluster(t, nil, &vault.TestClusterOptions{
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestHandler_XForwardedFor(t *testing.T) {
listenerConfig.XForwardedForRejectNotPresent = true
listenerConfig.XForwardedForRejectNotAuthorized = true
listenerConfig.XForwardedForHopSkips = 1
return WrapForwardedForHandler(origHandler, listenerConfig)
return WrapForwardedForHandler(origHandler, listenerConfig, nil)
}

cluster := vault.NewTestCluster(t, nil, &vault.TestClusterOptions{
Expand Down
Loading