Add cors bits into configutil listener (#9054)

* Add cors config to configutil listener parsing

* Set purposeraw to nil after parsing like other params

* Use canonical mime headers
This commit is contained in:
Jeff Mitchell 2020-05-20 21:56:12 -04:00 committed by GitHub
parent 34cae3f4e5
commit 0b3923c289
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package configutil
import (
"errors"
"fmt"
"net/textproto"
"strings"
"time"
@ -11,6 +12,7 @@ import (
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/vault/sdk/helper/parseutil"
"github.com/hashicorp/vault/sdk/helper/strutil"
"github.com/hashicorp/vault/sdk/helper/tlsutil"
)
@ -81,6 +83,12 @@ type Listener struct {
// RandomPort is used only for some testing purposes
RandomPort bool `hcl:"-"`
CorsEnabledRaw interface{} `hcl:"cors_enabled"`
CorsEnabled bool `hcl:"-"`
CorsAllowedOrigins []string `hcl:"cors_allowed_origins"`
CorsAllowedHeaders []string `hcl:"-"`
CorsAllowedHeadersRaw []string `hcl:"cors_allowed_headers"`
}
func (l *Listener) GoString() string {
@ -127,6 +135,8 @@ func ParseListeners(result *SharedConfig, list *ast.ObjectList) error {
for i, v := range l.Purpose {
l.Purpose[i] = strings.ToLower(v)
}
l.PurposeRaw = nil
}
}
@ -308,6 +318,27 @@ func ParseListeners(result *SharedConfig, list *ast.ObjectList) error {
}
}
// CORS
{
if l.CorsEnabledRaw != nil {
if l.CorsEnabled, err = parseutil.ParseBool(l.CorsEnabledRaw); err != nil {
return multierror.Prefix(fmt.Errorf("invalid value for cors_enabled: %w", err), fmt.Sprintf("listeners.%d", i))
}
l.CorsEnabledRaw = nil
}
if strutil.StrListContains(l.CorsAllowedOrigins, "*") && len(l.CorsAllowedOrigins) > 1 {
return multierror.Prefix(errors.New("cors_allowed_origins must only contain a wildcard or only non-wildcard values"), fmt.Sprintf("listeners.%d", i))
}
if len(l.CorsAllowedHeadersRaw) > 0 {
for _, header := range l.CorsAllowedHeadersRaw {
l.CorsAllowedHeaders = append(l.CorsAllowedHeaders, textproto.CanonicalMIMEHeaderKey(header))
}
}
}
result.Listeners = append(result.Listeners, &l)
}