Inject Custom Response Headers By Status (#2587)
* add custom ResponseWriter struct to inject custom response headers based on status code * docs * upgrade listenerutil * fix config tests * replace all instances of "data:*" with "data:" to reflect changes to listenerutil * swapping strings.HasPrefix in favor of mux.Handler for ui request matching * set ui path as const * add custom response headers to changelog
This commit is contained in:
1 parent
4b03a39958
commit
5f8d292ea1
7 files changed
+322
-36
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/cmd/config"
|
||||
@@ -14,6 +15,23 @@ import (
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
apiHeaders := map[int]http.Header{
|
||||
0: {
|
||||
"Content-Security-Policy": {"default-src 'none'"},
|
||||
"X-Content-Type-Options": {"nosniff"},
|
||||
"Strict-Transport-Security": {"max-age=31536000; includeSubDomains"},
|
||||
"Cache-Control": {"no-store"},
|
||||
},
|
||||
}
|
||||
uiHeaders := map[int]http.Header{
|
||||
0: {
|
||||
"Content-Security-Policy": {"default-src 'none'; script-src 'self'; frame-src 'self'; font-src 'self'; connect-src 'self'; img-src 'self' data:; style-src 'self'; media-src 'self'; manifest-src 'self'; style-src-attr 'self'; frame-ancestors 'self'"},
|
||||
"X-Content-Type-Options": {"nosniff"},
|
||||
"Strict-Transport-Security": {"max-age=31536000; includeSubDomains"},
|
||||
"Cache-Control": {"no-store"},
|
||||
},
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
expected *config.Config
|
||||
@@ -91,6 +109,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: []string{"*"},
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -158,6 +178,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -225,6 +247,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -292,6 +316,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
},
|
||||
Seals: []*configutil.KMS{
|
||||
@@ -497,6 +523,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: []string{"*"},
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -564,6 +592,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -631,6 +661,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -698,6 +730,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
},
|
||||
Seals: []*configutil.KMS{
|
||||
@@ -903,6 +937,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: []string{"*"},
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -970,6 +1006,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -1037,6 +1075,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -1104,6 +1144,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
},
|
||||
Seals: []*configutil.KMS{
|
||||
@@ -1309,6 +1351,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: []string{"*"},
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -1376,6 +1420,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -1443,6 +1489,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
RawConfig: map[string]any{
|
||||
@@ -1510,6 +1558,8 @@ func TestLoad(t *testing.T) {
|
||||
CorsAllowedOrigins: nil,
|
||||
CorsAllowedHeaders: nil,
|
||||
CorsAllowedHeadersRaw: nil,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
},
|
||||
Seals: []*configutil.KMS{
|
||||
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -22,26 +23,50 @@ func TestDevController(t *testing.T) {
|
||||
|
||||
truePointer := new(bool)
|
||||
*truePointer = true
|
||||
|
||||
apiHeaders := map[int]http.Header{
|
||||
0: {
|
||||
"Content-Security-Policy": {"default-src 'none'"},
|
||||
"X-Content-Type-Options": {"nosniff"},
|
||||
"Strict-Transport-Security": {"max-age=31536000; includeSubDomains"},
|
||||
"Cache-Control": {"no-store"},
|
||||
},
|
||||
}
|
||||
uiHeaders := map[int]http.Header{
|
||||
0: {
|
||||
"Content-Security-Policy": {"default-src 'none'; script-src 'self'; frame-src 'self'; font-src 'self'; connect-src 'self'; img-src 'self' data:; style-src 'self'; media-src 'self'; manifest-src 'self'; style-src-attr 'self'; frame-ancestors 'self'"},
|
||||
"X-Content-Type-Options": {"nosniff"},
|
||||
"Strict-Transport-Security": {"max-age=31536000; includeSubDomains"},
|
||||
"Cache-Control": {"no-store"},
|
||||
},
|
||||
}
|
||||
|
||||
exp := &Config{
|
||||
Eventing: event.DefaultEventerConfig(),
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
DisableMlock: true,
|
||||
Listeners: []*listenerutil.ListenerConfig{
|
||||
{
|
||||
Type: "tcp",
|
||||
Purpose: []string{"api"},
|
||||
TLSDisable: true,
|
||||
CorsEnabled: truePointer,
|
||||
CorsAllowedOrigins: []string{"*"},
|
||||
Type: "tcp",
|
||||
Purpose: []string{"api"},
|
||||
TLSDisable: true,
|
||||
CorsEnabled: truePointer,
|
||||
CorsAllowedOrigins: []string{"*"},
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
Type: "tcp",
|
||||
Purpose: []string{"cluster"},
|
||||
Type: "tcp",
|
||||
Purpose: []string{"cluster"},
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
Type: "tcp",
|
||||
Purpose: []string{"ops"},
|
||||
TLSDisable: true,
|
||||
Type: "tcp",
|
||||
Purpose: []string{"ops"},
|
||||
TLSDisable: true,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
},
|
||||
Seals: []*configutil.KMS{
|
||||
@@ -176,6 +201,22 @@ func TestDevWorker(t *testing.T) {
|
||||
{
|
||||
Type: "tcp",
|
||||
Purpose: []string{"proxy"},
|
||||
CustomApiResponseHeaders: map[int]http.Header{
|
||||
0: {
|
||||
"Content-Security-Policy": {"default-src 'none'"},
|
||||
"X-Content-Type-Options": {"nosniff"},
|
||||
"Strict-Transport-Security": {"max-age=31536000; includeSubDomains"},
|
||||
"Cache-Control": {"no-store"},
|
||||
},
|
||||
},
|
||||
CustomUiResponseHeaders: map[int]http.Header{
|
||||
0: {
|
||||
"Content-Security-Policy": {"default-src 'none'; script-src 'self'; frame-src 'self'; font-src 'self'; connect-src 'self'; img-src 'self' data:; style-src 'self'; media-src 'self'; manifest-src 'self'; style-src-attr 'self'; frame-ancestors 'self'"},
|
||||
"X-Content-Type-Options": {"nosniff"},
|
||||
"Strict-Transport-Security": {"max-age=31536000; includeSubDomains"},
|
||||
"Cache-Control": {"no-store"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Seals: []*configutil.KMS{
|
||||
@@ -329,30 +370,56 @@ func TestDevCombined(t *testing.T) {
|
||||
|
||||
truePointer := new(bool)
|
||||
*truePointer = true
|
||||
|
||||
apiHeaders := map[int]http.Header{
|
||||
0: {
|
||||
"Content-Security-Policy": {"default-src 'none'"},
|
||||
"X-Content-Type-Options": {"nosniff"},
|
||||
"Strict-Transport-Security": {"max-age=31536000; includeSubDomains"},
|
||||
"Cache-Control": {"no-store"},
|
||||
},
|
||||
}
|
||||
uiHeaders := map[int]http.Header{
|
||||
0: {
|
||||
"Content-Security-Policy": {"default-src 'none'; script-src 'self'; frame-src 'self'; font-src 'self'; connect-src 'self'; img-src 'self' data:; style-src 'self'; media-src 'self'; manifest-src 'self'; style-src-attr 'self'; frame-ancestors 'self'"},
|
||||
"X-Content-Type-Options": {"nosniff"},
|
||||
"Strict-Transport-Security": {"max-age=31536000; includeSubDomains"},
|
||||
"Cache-Control": {"no-store"},
|
||||
},
|
||||
}
|
||||
|
||||
exp := &Config{
|
||||
Eventing: event.DefaultEventerConfig(),
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
DisableMlock: true,
|
||||
Listeners: []*listenerutil.ListenerConfig{
|
||||
{
|
||||
Type: "tcp",
|
||||
Purpose: []string{"api"},
|
||||
TLSDisable: true,
|
||||
CorsEnabled: truePointer,
|
||||
CorsAllowedOrigins: []string{"*"},
|
||||
Type: "tcp",
|
||||
Purpose: []string{"api"},
|
||||
TLSDisable: true,
|
||||
CorsEnabled: truePointer,
|
||||
CorsAllowedOrigins: []string{"*"},
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
Type: "tcp",
|
||||
Purpose: []string{"cluster"},
|
||||
Type: "tcp",
|
||||
Purpose: []string{"cluster"},
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
Type: "tcp",
|
||||
Purpose: []string{"ops"},
|
||||
TLSDisable: true,
|
||||
Type: "tcp",
|
||||
Purpose: []string{"ops"},
|
||||
TLSDisable: true,
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
{
|
||||
Type: "tcp",
|
||||
Purpose: []string{"proxy"},
|
||||
Type: "tcp",
|
||||
Purpose: []string{"proxy"},
|
||||
CustomApiResponseHeaders: apiHeaders,
|
||||
CustomUiResponseHeaders: uiHeaders,
|
||||
},
|
||||
},
|
||||
Seals: []*configutil.KMS{
|
||||
|
||||
Reference in new issue
Block a user