2020-02-12 13:53:27 -05:00
package config
import (
"bytes"
"crypto/rand"
"encoding/base64"
2021-12-03 11:47:00 +00:00
"encoding/json"
2020-09-16 09:28:39 -04:00
"errors"
2020-02-12 13:53:27 -05:00
"fmt"
"io"
"io/ioutil"
2021-12-07 13:01:04 +00:00
"reflect"
2021-12-08 15:44:13 -05:00
"strconv"
2020-09-16 09:28:39 -04:00
"strings"
2020-11-16 10:39:18 -08:00
"time"
2020-02-12 13:53:27 -05:00
2022-05-16 15:14:12 -07:00
"github.com/hashicorp/boundary/internal/db"
2021-07-08 15:50:25 -04:00
"github.com/hashicorp/boundary/internal/observability/event"
2022-03-16 17:04:07 -04:00
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
configutil "github.com/hashicorp/go-secure-stdlib/configutil/v2"
2021-07-14 17:40:39 -07:00
"github.com/hashicorp/go-secure-stdlib/parseutil"
2021-07-14 17:37:40 -07:00
"github.com/hashicorp/go-secure-stdlib/strutil"
2020-02-12 13:53:27 -05:00
"github.com/hashicorp/hcl"
2021-08-04 14:01:58 -04:00
"github.com/hashicorp/hcl/hcl/ast"
2021-01-21 16:27:20 -05:00
"github.com/mitchellh/mapstructure"
2020-02-12 13:53:27 -05:00
)
2020-03-20 12:51:39 -04:00
const (
2021-04-14 15:26:21 -05:00
desktopCorsOrigin = "serve://boundary"
2020-03-20 12:51:39 -04:00
devConfig = `
2020-02-12 13:53:27 -05:00
disable_mlock = true
telemetry {
prometheus_retention_time = "24h"
disable_hostname = true
}
2020-03-20 12:51:39 -04:00
`
devControllerExtraConfig = `
2020-08-12 00:40:39 -04:00
controller {
name = "dev-controller"
description = "A default controller created in dev mode"
}
2020-02-12 13:53:27 -05:00
kms "aead" {
2020-08-14 10:12:31 -04:00
purpose = "root"
2020-02-12 13:53:27 -05:00
aead_type = "aes-gcm"
key = "%s"
2020-08-19 16:47:18 -04:00
key_id = "global_root"
2020-02-12 13:53:27 -05:00
}
kms "aead" {
purpose = "worker-auth"
aead_type = "aes-gcm"
key = "%s"
2020-08-19 16:47:18 -04:00
key_id = "global_worker-auth"
2020-02-12 13:53:27 -05:00
}
2020-03-22 08:27:24 -04:00
2020-08-20 13:34:03 -04:00
kms "aead" {
purpose = "recovery"
aead_type = "aes-gcm"
key = "%s"
key_id = "global_recovery"
}
2020-03-22 08:27:24 -04:00
listener "tcp" {
purpose = "api"
tls_disable = true
2020-05-21 12:12:08 -04:00
cors_enabled = true
cors_allowed_origins = ["*"]
2020-03-22 08:27:24 -04:00
}
listener "tcp" {
purpose = "cluster"
}
2022-03-28 17:16:39 +01:00
listener "tcp" {
purpose = "ops"
tls_disable = true
}
2020-03-22 08:27:24 -04:00
`
devWorkerExtraConfig = `
listener "tcp" {
2020-09-03 13:24:34 -04:00
purpose = "proxy"
2020-03-22 08:27:24 -04:00
}
2020-07-20 13:52:16 -04:00
worker {
2022-05-16 15:14:12 -07:00
name = "w_1234567890"
2020-07-20 13:52:16 -04:00
description = "A default worker created in dev mode"
2022-06-13 15:06:03 -07:00
initial_upstreams = ["127.0.0.1"]
2021-01-21 16:27:20 -05:00
tags {
type = ["dev", "local"]
}
2020-07-20 13:52:16 -04:00
}
2022-08-05 12:12:07 -04:00
kms "aead" {
purpose = "worker-auth-storage"
aead_type = "aes-gcm"
key = "%s"
key_id = "worker-auth-storage"
}
2020-02-12 13:53:27 -05:00
`
2020-03-20 12:51:39 -04:00
)
2020-08-10 17:15:02 -04:00
// Config is the configuration for the boundary controller
2020-03-20 12:51:39 -04:00
type Config struct {
* configutil . SharedConfig `hcl:"-"`
2020-09-23 01:23:10 -04:00
Worker * Worker `hcl:"worker"`
Controller * Controller `hcl:"controller"`
// Dev-related options
2022-06-03 13:25:58 -07:00
DevController bool `hcl:"-"`
DevUiPassthroughDir string `hcl:"-"`
DevControllerKey string `hcl:"-"`
DevWorkerAuthKey string `hcl:"-"`
DevWorkerAuthStorageKey string `hcl:"-"`
DevRecoveryKey string `hcl:"-"`
2021-07-08 15:50:25 -04:00
// Eventing configuration for the controller
Eventing * event . EventerConfig `hcl:"events"`
2021-10-14 11:56:08 -04:00
// Plugin-related options
Plugins Plugins `hcl:"plugins"`
2022-06-09 16:39:20 -04:00
// Internal field for use with HCP deployments. Used if controllers/ initial_upstreams is not set
2022-06-16 09:21:11 -05:00
HcpbClusterId string `hcl:"hcp_boundary_cluster_id"`
2020-08-12 00:40:39 -04:00
}
type Controller struct {
2020-10-30 16:54:53 -04:00
Name string `hcl:"name"`
Description string `hcl:"description"`
Database * Database `hcl:"database"`
PublicClusterAddr string `hcl:"public_cluster_addr"`
2020-11-16 10:39:18 -08:00
// AuthTokenTimeToLive is the total valid lifetime of a token denoted by time.Duration
AuthTokenTimeToLive interface {} `hcl:"auth_token_time_to_live"`
AuthTokenTimeToLiveDuration time . Duration
// AuthTokenTimeToStale is the total time a token can go unused before becoming invalid
// denoted by time.Duration
AuthTokenTimeToStale interface {} `hcl:"auth_token_time_to_stale"`
AuthTokenTimeToStaleDuration time . Duration
2021-07-14 09:14:24 -07:00
2022-03-28 17:16:39 +01:00
// GracefulShutdownWait is the amount of time that we'll wait before actually
// starting the Controller shutdown. This allows the health endpoint to
// return a status code to indicate that the instance is shutting down.
GracefulShutdownWait interface {} `hcl:"graceful_shutdown_wait_duration"`
GracefulShutdownWaitDuration time . Duration
2021-07-14 09:14:24 -07:00
// StatusGracePeriod represents the period of time (as a duration) that the
// controller will wait before marking connections from a disconnected worker
// as invalid.
//
// TODO: This field is currently internal.
StatusGracePeriodDuration time . Duration `hcl:"-"`
2021-11-02 14:06:20 -07:00
// SchedulerRunJobInterval is the time interval between waking up the
// scheduler to run pending jobs.
//
// TODO: This field is currently internal.
SchedulerRunJobInterval time . Duration `hcl:"-"`
2020-07-20 13:52:16 -04:00
}
2022-06-15 20:01:03 -04:00
func ( c * Controller ) InitNameIfEmpty () error {
2021-07-25 17:40:54 -04:00
if c == nil {
2022-06-15 20:01:03 -04:00
return fmt . Errorf ( "controller config is empty" )
2021-07-25 17:40:54 -04:00
}
2022-06-15 20:01:03 -04:00
if c . Name != "" {
return nil
2021-07-25 17:40:54 -04:00
}
2022-06-15 20:01:03 -04:00
var err error
c . Name , err = db . NewPublicId ( "c" )
if err != nil {
return fmt . Errorf ( "error auto-generating controller name: %w" , err )
}
return nil
2021-07-25 17:40:54 -04:00
}
2020-07-20 13:52:16 -04:00
type Worker struct {
2021-12-07 13:01:04 +00:00
Name string `hcl:"name"`
Description string `hcl:"description"`
PublicAddr string `hcl:"public_addr"`
// We use a raw interface here so that we can take in a string
// value pointing to an env var or file. We then resolve that
2022-06-13 15:06:03 -07:00
// and get the actual upstream controller or worker addresses.
InitialUpstreams [] string `hcl:"-"`
InitialUpstreamsRaw any `hcl:"initial_upstreams"`
// The ControllersRaw field is deprecated and users should use InitialUpstreamsRaw instead.
// TODO: remove this field when support is discontinued.
2021-12-07 13:01:04 +00:00
ControllersRaw interface {} `hcl:"controllers"`
2021-01-21 16:27:20 -05:00
// We use a raw interface for parsing so that people can use JSON-like
// syntax that maps directly to the filter input or possibly more familiar
2021-12-03 11:47:00 +00:00
// key=value syntax, as well as accepting a string denoting an env or file
// pointer. This is trued up in the Parse function below.
2021-01-21 16:27:20 -05:00
Tags map [ string ][] string `hcl:"-"`
2021-12-03 11:47:00 +00:00
TagsRaw interface {} `hcl:"tags"`
2021-07-14 09:14:24 -07:00
// StatusGracePeriod represents the period of time (as a duration) that the
// worker will wait before disconnecting connections if it cannot make a
// status report to a controller.
//
// TODO: This field is currently internal.
StatusGracePeriodDuration time . Duration `hcl:"-"`
2022-05-11 17:15:38 -07:00
2022-06-03 17:32:33 -04:00
// AuthStoragePath represents the location a worker stores its node credentials, if set
AuthStoragePath string `hcl:"auth_storage_path"`
2020-03-20 12:51:39 -04:00
}
2020-09-16 09:28:39 -04:00
type Database struct {
2022-07-05 15:25:55 -04:00
Url string `hcl:"url"`
MigrationUrl string `hcl:"migration_url"`
MaxOpenConnections int `hcl:"-"`
MaxOpenConnectionsRaw interface {} `hcl:"max_open_connections"`
MaxIdleConnections * int `hcl:"-"`
MaxIdleConnectionsRaw interface {} `hcl:"max_idle_connections"`
ConnMaxIdleTime interface {} `hcl:"max_idle_time"`
ConnMaxIdleTimeDuration * time . Duration `hcl:"-"`
2020-09-16 09:28:39 -04:00
}
2021-10-14 11:56:08 -04:00
type Plugins struct {
ExecutionDir string `hcl:"execution_dir"`
}
2020-08-10 17:15:02 -04:00
// DevWorker is a Config that is used for dev mode of Boundary
2020-03-20 12:51:39 -04:00
// workers
func DevWorker () ( * Config , error ) {
2022-08-05 12:12:07 -04:00
workerAuthStorageKey := DevKeyGeneration ()
hclStr := fmt . Sprintf ( devConfig + devWorkerExtraConfig , workerAuthStorageKey )
parsed , err := Parse ( hclStr )
2020-03-20 12:51:39 -04:00
if err != nil {
return nil , fmt . Errorf ( "error parsing dev config: %w" , err )
}
return parsed , nil
}
2022-06-03 13:25:58 -07:00
func DevKeyGeneration () string {
var numBytes int64 = 32
2020-03-20 12:51:39 -04:00
randBuf := new ( bytes . Buffer )
n , err := randBuf . ReadFrom ( & io . LimitedReader {
R : rand . Reader ,
2020-08-20 13:34:03 -04:00
N : numBytes ,
2020-03-20 12:51:39 -04:00
})
if err != nil {
2020-07-20 13:52:16 -04:00
panic ( err )
2020-03-20 12:51:39 -04:00
}
2020-08-20 13:34:03 -04:00
if n != numBytes {
2022-06-03 13:25:58 -07:00
panic ( fmt . Errorf ( "expected to read 32 bytes, read %d" , n ))
2020-03-20 12:51:39 -04:00
}
2022-06-03 13:25:58 -07:00
devKey := base64 . StdEncoding . EncodeToString ( randBuf . Bytes ())[: numBytes ]
return devKey
2020-07-20 13:52:16 -04:00
}
2020-08-10 17:15:02 -04:00
// DevController is a Config that is used for dev mode of Boundary
2020-07-20 13:52:16 -04:00
// controllers
func DevController () ( * Config , error ) {
2022-06-03 13:25:58 -07:00
controllerKey := DevKeyGeneration ()
workerAuthKey := DevKeyGeneration ()
recoveryKey := DevKeyGeneration ()
2020-02-12 13:53:27 -05:00
2022-08-05 12:12:07 -04:00
hclStr := fmt . Sprintf ( devConfig + devControllerExtraConfig , controllerKey , workerAuthKey , recoveryKey )
2020-03-10 15:48:04 -04:00
parsed , err := Parse ( hclStr )
2020-02-12 13:53:27 -05:00
if err != nil {
return nil , fmt . Errorf ( "error parsing dev config: %w" , err )
}
2020-03-20 12:51:39 -04:00
parsed . DevController = true
2020-09-23 01:23:10 -04:00
parsed . DevControllerKey = controllerKey
parsed . DevWorkerAuthKey = workerAuthKey
parsed . DevRecoveryKey = recoveryKey
2020-02-12 13:53:27 -05:00
return parsed , nil
}
2020-07-20 13:52:16 -04:00
func DevCombined () ( * Config , error ) {
2022-06-03 13:25:58 -07:00
controllerKey := DevKeyGeneration ()
workerAuthKey := DevKeyGeneration ()
workerAuthStorageKey := DevKeyGeneration ()
recoveryKey := DevKeyGeneration ()
2022-08-05 12:12:07 -04:00
hclStr := fmt . Sprintf ( devConfig + devControllerExtraConfig + devWorkerExtraConfig , controllerKey , workerAuthKey , recoveryKey , workerAuthStorageKey )
2020-07-20 13:52:16 -04:00
parsed , err := Parse ( hclStr )
if err != nil {
return nil , fmt . Errorf ( "error parsing dev config: %w" , err )
}
parsed . DevController = true
2020-09-23 01:23:10 -04:00
parsed . DevControllerKey = controllerKey
parsed . DevWorkerAuthKey = workerAuthKey
2022-06-03 13:25:58 -07:00
parsed . DevWorkerAuthStorageKey = workerAuthStorageKey
2020-09-23 01:23:10 -04:00
parsed . DevRecoveryKey = recoveryKey
2020-07-20 13:52:16 -04:00
return parsed , nil
}
2020-03-10 15:48:04 -04:00
func New () * Config {
2020-02-12 13:53:27 -05:00
return & Config {
SharedConfig : new ( configutil . SharedConfig ),
}
}
2020-03-10 15:48:04 -04:00
// LoadFile loads the configuration from the given file.
2020-08-27 16:38:44 -04:00
func LoadFile ( path string , wrapper wrapping . Wrapper ) ( * Config , error ) {
2020-02-12 13:53:27 -05:00
d , err := ioutil . ReadFile ( path )
if err != nil {
return nil , err
}
2020-06-24 13:08:13 -07:00
raw := string ( d )
2020-08-27 16:38:44 -04:00
if wrapper != nil {
raw , err = configutil . EncryptDecrypt ( raw , true , true , wrapper )
2020-06-24 13:08:13 -07:00
if err != nil {
return nil , err
}
2020-02-12 13:53:27 -05:00
}
2020-06-24 13:08:13 -07:00
return Parse ( raw )
2020-02-12 13:53:27 -05:00
}
2020-03-10 15:48:04 -04:00
func Parse ( d string ) ( * Config , error ) {
2020-02-12 13:53:27 -05:00
obj , err := hcl . Parse ( d )
if err != nil {
return nil , err
}
2020-03-10 15:48:04 -04:00
result := New ()
2020-02-12 13:53:27 -05:00
if err := hcl . DecodeObject ( result , obj ); err != nil {
return nil , err
}
2020-11-16 10:39:18 -08:00
// Perform controller configuration overrides for auth token settings
if result . Controller != nil {
2021-07-14 17:50:15 -07:00
result . Controller . Name , err = parseutil . ParsePath ( result . Controller . Name )
if err != nil && ! errors . Is ( err , parseutil . ErrNotAUrl ) {
2021-05-04 08:37:56 -05:00
return nil , fmt . Errorf ( "Error parsing controller name: %w" , err )
}
2021-01-21 16:27:20 -05:00
if result . Controller . Name != strings . ToLower ( result . Controller . Name ) {
return nil , errors . New ( "Controller name must be all lower-case" )
}
if ! strutil . Printable ( result . Controller . Name ) {
return nil , errors . New ( "Controller name contains non-printable characters" )
}
2021-12-07 11:28:12 -05:00
result . Controller . Description , err = parseutil . ParsePath ( result . Controller . Description )
if err != nil && ! errors . Is ( err , parseutil . ErrNotAUrl ) {
return nil , fmt . Errorf ( "Error parsing controller description: %w" , err )
}
if ! strutil . Printable ( result . Controller . Description ) {
return nil , errors . New ( "Controller description contains non-printable characters" )
}
2020-11-16 10:39:18 -08:00
if result . Controller . AuthTokenTimeToLive != "" {
t , err := parseutil . ParseDurationSecond ( result . Controller . AuthTokenTimeToLive )
if err != nil {
return result , err
}
result . Controller . AuthTokenTimeToLiveDuration = t
}
if result . Controller . AuthTokenTimeToStale != "" {
t , err := parseutil . ParseDurationSecond ( result . Controller . AuthTokenTimeToStale )
if err != nil {
return result , err
}
result . Controller . AuthTokenTimeToStaleDuration = t
}
2021-12-08 15:44:13 -05:00
2022-03-28 17:16:39 +01:00
if result . Controller . GracefulShutdownWait != "" {
t , err := parseutil . ParseDurationSecond ( result . Controller . GracefulShutdownWait )
if err != nil {
return result , err
}
result . Controller . GracefulShutdownWaitDuration = t
}
2021-12-08 15:44:13 -05:00
if result . Controller . Database != nil {
if result . Controller . Database . MaxOpenConnectionsRaw != nil {
switch t := result . Controller . Database . MaxOpenConnectionsRaw .( type ) {
case string :
maxOpenConnectionsString , err := parseutil . ParsePath ( t )
2022-07-05 15:25:55 -04:00
if err != nil && ! errors . Is ( err , parseutil . ErrNotAUrl ) {
2021-12-08 15:44:13 -05:00
return nil , fmt . Errorf ( "Error parsing database max open connections: %w" , err )
}
result . Controller . Database . MaxOpenConnections , err = strconv . Atoi ( maxOpenConnectionsString )
if err != nil {
return nil , fmt . Errorf ( "Database max open connections value is not an int: %w" , err )
}
case int :
result . Controller . Database . MaxOpenConnections = t
default :
return nil , fmt . Errorf ( "Database max open connections: unsupported type %q" ,
reflect . TypeOf ( t ). String ())
}
}
2022-07-05 15:25:55 -04:00
if result . Controller . Database . MaxIdleConnectionsRaw != nil {
switch t := result . Controller . Database . MaxIdleConnectionsRaw .( type ) {
case string :
maxIdleConnectionsString , err := parseutil . ParsePath ( t )
if err != nil && ! errors . Is ( err , parseutil . ErrNotAUrl ) {
return nil , fmt . Errorf ( "Error parsing database max idle connections: %w" , err )
}
idleConns , err := strconv . Atoi ( maxIdleConnectionsString )
if err != nil {
return nil , fmt . Errorf ( "Database max idle connections value is not a uint: %w" , err )
}
result . Controller . Database . MaxIdleConnections = & idleConns
case int :
result . Controller . Database . MaxIdleConnections = & t
default :
return nil , fmt . Errorf ( "Database max idle connections: unsupported type %q" ,
reflect . TypeOf ( t ). String ())
}
}
if result . Controller . Database . ConnMaxIdleTime != nil {
switch t := result . Controller . Database . ConnMaxIdleTime .( type ) {
case string :
durationString , err := parseutil . ParsePath ( t )
if err != nil && ! errors . Is ( err , parseutil . ErrNotAUrl ) {
return nil , fmt . Errorf ( "Error parsing connection max idle time: %w" , err )
}
connMaxIdleTime , err := parseutil . ParseDurationSecond ( durationString )
if err != nil {
return nil , fmt . Errorf ( "Connection max idle time is not a duration: %w" , err )
}
result . Controller . Database . ConnMaxIdleTimeDuration = & connMaxIdleTime
default :
return nil , fmt . Errorf ( "Database connection max idle time: unsupported type %q" ,
reflect . TypeOf ( t ). String ())
}
}
2021-12-08 15:44:13 -05:00
}
2020-11-16 10:39:18 -08:00
}
2021-01-21 16:27:20 -05:00
// Parse worker tags
if result . Worker != nil {
2021-07-14 17:50:15 -07:00
result . Worker . Name , err = parseutil . ParsePath ( result . Worker . Name )
if err != nil && ! errors . Is ( err , parseutil . ErrNotAUrl ) {
2021-05-04 08:37:56 -05:00
return nil , fmt . Errorf ( "Error parsing worker name: %w" , err )
}
2021-01-21 16:27:20 -05:00
if result . Worker . Name != strings . ToLower ( result . Worker . Name ) {
return nil , errors . New ( "Worker name must be all lower-case" )
}
if ! strutil . Printable ( result . Worker . Name ) {
return nil , errors . New ( "Worker name contains non-printable characters" )
}
2021-12-03 11:47:00 +00:00
2021-12-10 18:16:20 +00:00
result . Worker . Description , err = parseutil . ParsePath ( result . Worker . Description )
if err != nil && ! errors . Is ( err , parseutil . ErrNotAUrl ) {
return nil , fmt . Errorf ( "Error parsing worker description: %w" , err )
}
if ! strutil . Printable ( result . Worker . Description ) {
return nil , errors . New ( "Worker description contains non-printable characters" )
}
2021-01-21 16:27:20 -05:00
if result . Worker . TagsRaw != nil {
switch t := result . Worker . TagsRaw .( type ) {
2021-12-03 11:47:00 +00:00
// We allow `tags` to be a simple string containing a URL with schema.
// See: https://github.com/hashicorp/go-secure-stdlib/blob/main/parseutil/parsepath.go
case string :
rawTags , err := parseutil . ParsePath ( t )
if err != nil {
return nil , fmt . Errorf ( "Error parsing worker tags: %w" , err )
}
var temp [] map [ string ] interface {}
err = hcl . Decode ( & temp , rawTags )
if err != nil {
return nil , fmt . Errorf ( "Error decoding raw worker tags: %w" , err )
}
if err := mapstructure . WeakDecode ( temp , & result . Worker . Tags ); err != nil {
return nil , fmt . Errorf ( "Error decoding the worker's tags: %w" , err )
}
2021-01-21 16:27:20 -05:00
// HCL allows multiple labeled blocks with the same name, turning it
// into a slice of maps, hence the slice here. This format is the
// one that ends up matching the JSON that we use in the expression.
case [] map [ string ] interface {}:
2021-12-03 11:47:00 +00:00
for _ , m := range t {
for k , v := range m {
// We allow the user to pass in only the keys in HCL, and
// then set the values to point to a URL with schema.
valStr , ok := v .( string )
if ! ok {
continue
}
parsed , err := parseutil . ParsePath ( valStr )
if err != nil && ! errors . Is ( err , parseutil . ErrNotAUrl ) {
return nil , fmt . Errorf ( "Error parsing worker tag values: %w" , err )
}
if valStr == parsed {
// Nothing was found, ignore.
// WeakDecode will still parse it though as we
// don't know if this could be a valid tag.
continue
}
var tags [] string
err = json . Unmarshal ([] byte ( parsed ), & tags )
if err != nil {
2022-08-03 19:29:17 -04:00
return nil , fmt . Errorf ( "Error unmarshaling env var/file contents: %w" , err )
2021-12-03 11:47:00 +00:00
}
m [ k ] = tags
}
}
2021-01-21 16:27:20 -05:00
if err := mapstructure . WeakDecode ( t , & result . Worker . Tags ); err != nil {
return nil , fmt . Errorf ( "Error decoding the worker's %q section: %w" , "tags" , err )
}
// However for those that are used to other systems, we also accept
// key=value pairs
case [] interface {}:
var strs [] string
if err := mapstructure . WeakDecode ( t , & strs ); err != nil {
return nil , fmt . Errorf ( "Error decoding the worker's %q section: %w" , "tags" , err )
}
result . Worker . Tags = make ( map [ string ][] string , len ( strs ))
// Aggregate the values by key. We care about the first equal
// sign only, to allow equals to be in values if needed. This
// also means we don't support equal signs in keys.
for _ , str := range strs {
splitStr := strings . SplitN ( str , "=" , 2 )
switch len ( splitStr ) {
case 1 :
return nil , fmt . Errorf ( "Error decoding tag %q from string: must be in key = value format" , str )
case 2 :
key := splitStr [ 0 ]
v := result . Worker . Tags [ key ]
if len ( v ) == 0 {
v = make ([] string , 0 , 1 )
}
result . Worker . Tags [ key ] = append ( v , splitStr [ 1 ])
}
}
}
}
2021-12-03 11:47:00 +00:00
2021-01-21 16:27:20 -05:00
for k , v := range result . Worker . Tags {
if k != strings . ToLower ( k ) {
return nil , fmt . Errorf ( "Tag key %q is not all lower-case letters" , k )
}
if ! strutil . Printable ( k ) {
return nil , fmt . Errorf ( "Tag key %q contains non-printable characters" , k )
}
for _ , val := range v {
if val != strings . ToLower ( val ) {
return nil , fmt . Errorf ( "Tag value %q for tag key %q is not all lower-case letters" , val , k )
}
if ! strutil . Printable ( k ) {
return nil , fmt . Errorf ( "Tag value %q for tag key %q contains non-printable characters" , v , k )
}
}
}
2021-12-07 13:01:04 +00:00
2022-06-13 15:06:03 -07:00
result . Worker . InitialUpstreams , err = parseWorkerUpstreams ( result )
2021-12-07 13:01:04 +00:00
if err != nil {
2022-06-13 15:06:03 -07:00
return nil , fmt . Errorf ( "Failed to parse worker upstreams: %w" , err )
2021-12-07 13:01:04 +00:00
}
2021-01-21 16:27:20 -05:00
}
2020-02-12 13:53:27 -05:00
sharedConfig , err := configutil . ParseConfig ( d )
if err != nil {
return nil , err
}
result . SharedConfig = sharedConfig
2021-02-11 20:10:48 -05:00
for _ , listener := range result . SharedConfig . Listeners {
2021-04-14 15:26:21 -05:00
if strutil . StrListContains ( listener . Purpose , "api" ) &&
( listener . CorsDisableDefaultAllowedOriginValues == nil || ! * listener . CorsDisableDefaultAllowedOriginValues ) {
2021-05-21 12:33:02 -05:00
switch listener . CorsEnabled {
case nil :
// If CORS wasn't specified, enable default value of *, which allows
// both the admin UI (without the user having to explicitly set an
// origin) and the desktop origin
2021-02-11 20:10:48 -05:00
listener . CorsEnabled = new ( bool )
* listener . CorsEnabled = true
2021-05-21 12:33:02 -05:00
listener . CorsAllowedOrigins = [] string { "*" }
default :
// If not the wildcard and they haven't disabled us auto-adding
// origin values, add the desktop client origin
if * listener . CorsEnabled &&
! strutil . StrListContains ( listener . CorsAllowedOrigins , "*" ) {
listener . CorsAllowedOrigins = strutil . AppendIfMissing ( listener . CorsAllowedOrigins , desktopCorsOrigin )
}
2021-02-11 20:10:48 -05:00
}
}
}
2021-08-04 14:01:58 -04:00
list , ok := obj . Node .( * ast . ObjectList )
if ! ok {
return nil , fmt . Errorf ( "error parsing: file doesn't contain a root object" )
}
eventList := list . Filter ( "events" )
switch len ( eventList . Items ) {
case 0 :
2021-07-08 15:50:25 -04:00
result . Eventing = event . DefaultEventerConfig ()
2021-08-04 14:01:58 -04:00
case 1 :
if result . Eventing , err = parseEventing ( eventList . Items [ 0 ]); err != nil {
return nil , fmt . Errorf ( `error parsing "events": %w` , err )
2021-07-08 15:50:25 -04:00
}
2021-08-04 14:01:58 -04:00
default :
return nil , fmt . Errorf ( `too many "events" nodes (max 1, got %d)` , len ( eventList . Items ))
2021-07-08 15:50:25 -04:00
}
2021-12-08 09:39:04 -05:00
if result . Plugins . ExecutionDir != "" {
result . Plugins . ExecutionDir , err = parseutil . ParsePath ( result . Plugins . ExecutionDir )
if err != nil && ! errors . Is ( err , parseutil . ErrNotAUrl ) {
return nil , fmt . Errorf ( "Error parsing plugins execution dir: %w" , err )
}
}
2020-02-12 13:53:27 -05:00
return result , nil
}
2022-06-13 15:06:03 -07:00
// supportControllersRawConfig returns either initialUpstreamsRaw or controllersRaw depending on which is populated. Errors when both fields are populated.
//
func supportControllersRawConfig ( initialUpstreamsRaw , controllersRaw any ) ( any , error ) {
switch {
case initialUpstreamsRaw == nil && controllersRaw != nil :
return controllersRaw , nil
case initialUpstreamsRaw != nil && controllersRaw != nil :
return nil , fmt . Errorf ( "both initial_upstreams and controllers fields are populated" )
}
return initialUpstreamsRaw , nil
}
func parseWorkerUpstreams ( c * Config ) ([] string , error ) {
2021-12-07 13:01:04 +00:00
if c == nil || c . Worker == nil {
return nil , fmt . Errorf ( "config or worker field is nil" )
}
2022-06-13 15:06:03 -07:00
if c . Worker . InitialUpstreamsRaw == nil && c . Worker . ControllersRaw == nil {
// return nil here so that other address sources can be provided outside of config
2021-12-07 13:01:04 +00:00
return nil , nil
}
2022-06-13 15:06:03 -07:00
rawUpstreams , err := supportControllersRawConfig ( c . Worker . InitialUpstreamsRaw , c . Worker . ControllersRaw )
if err != nil {
return nil , err
}
2021-12-07 13:01:04 +00:00
2022-06-13 15:06:03 -07:00
switch t := rawUpstreams .( type ) {
2021-12-07 13:01:04 +00:00
case [] interface {}: // An array was configured directly in Boundary's HCL Config file.
2022-06-13 15:06:03 -07:00
var upstreams [] string
err := mapstructure . WeakDecode ( rawUpstreams , & upstreams )
2021-12-07 13:01:04 +00:00
if err != nil {
2022-06-13 15:06:03 -07:00
return nil , fmt . Errorf ( "failed to decode worker initial_upstreams block into config field: %w" , err )
2021-12-07 13:01:04 +00:00
}
2022-06-13 15:06:03 -07:00
return upstreams , nil
2021-12-07 13:01:04 +00:00
case string :
2022-06-13 15:06:03 -07:00
upstreamsStr , err := parseutil . ParsePath ( t )
2021-12-07 13:01:04 +00:00
if err != nil {
return nil , fmt . Errorf ( "bad env var or file pointer: %w" , err )
}
2022-06-13 15:06:03 -07:00
var upstreams [] string
err = json . Unmarshal ([] byte ( upstreamsStr ), & upstreams )
2021-12-07 13:01:04 +00:00
if err != nil {
return nil , fmt . Errorf ( "failed to unmarshal env/file contents: %w" , err )
}
2022-06-13 15:06:03 -07:00
return upstreams , nil
2021-12-07 13:01:04 +00:00
default :
typ := reflect . TypeOf ( t )
return nil , fmt . Errorf ( "unexpected type %q" , typ . String ())
}
}
2021-08-04 14:01:58 -04:00
func parseEventing ( eventObj * ast . ObjectItem ) ( * event . EventerConfig , error ) {
// Decode the outside struct
var result event . EventerConfig
if err := hcl . DecodeObject ( & result , eventObj . Val ); err != nil {
return nil , fmt . Errorf ( `error decoding "events" node: %w` , err )
}
// Now, find the sinks
eventObjType , ok := eventObj . Val .( * ast . ObjectType )
if ! ok {
return nil , fmt . Errorf ( `error interpreting "events" node as an object type` )
}
list := eventObjType . List
sinkList := list . Filter ( "sink" )
// Go through each sink and decode
for i , item := range sinkList . Items {
var s event . SinkConfig
if err := hcl . DecodeObject ( & s , item . Val ); err != nil {
return nil , fmt . Errorf ( "error decoding eventer sink entry %d" , i )
}
// Fix up type and validate
switch {
case s . Type != "" :
case len ( item . Keys ) == 1 :
s . Type = event . SinkType ( item . Keys [ 0 ]. Token . Value ().( string ))
default :
switch {
case s . StderrConfig != nil :
// If we haven't found the type any other way, they _must_
// specify this block even though there are no config parameters
s . Type = event . StderrSink
case s . FileConfig != nil :
s . Type = event . FileSink
default :
return nil , fmt . Errorf ( "sink type could not be determined" )
}
}
s . Type = event . SinkType ( strings . ToLower ( string ( s . Type )))
if s . Type == event . StderrSink && s . StderrConfig == nil {
// StderrConfig is optional as it has no values, but ensure it's
// always populated if it's the type
s . StderrConfig = new ( event . StderrSinkTypeConfig )
}
2021-08-04 17:57:36 -04:00
// parse the duration string specified in a file config into a time.Duration
if s . FileConfig != nil && s . FileConfig . RotateDurationHCL != "" {
var err error
2021-08-04 19:11:00 -04:00
s . FileConfig . RotateDuration , err = parseutil . ParseDurationSecond ( s . FileConfig . RotateDurationHCL )
2021-08-04 17:57:36 -04:00
if err != nil {
return nil , fmt . Errorf ( "can't parse rotation duration %s" , s . FileConfig . RotateDurationHCL )
}
}
2022-04-18 11:03:10 -04:00
// parse map into event types
if s . AuditConfig != nil && s . AuditConfig . FilterOverridesHCL != nil {
s . AuditConfig . FilterOverrides = make ( map [ event . DataClassification ] event . FilterOperation , len ( s . AuditConfig . FilterOverridesHCL ))
for k , v := range s . AuditConfig . FilterOverridesHCL {
s . AuditConfig . FilterOverrides [ event . DataClassification ( k )] = event . FilterOperation ( v )
}
}
2021-08-04 14:01:58 -04:00
if err := s . Validate (); err != nil {
return nil , err
}
// Append to result
2021-08-30 19:02:48 -04:00
result . Sinks = append ( result . Sinks , & s )
2021-08-04 14:01:58 -04:00
}
if len ( result . Sinks ) == 0 {
2021-08-30 19:02:48 -04:00
result . Sinks = [] * event . SinkConfig { event . DefaultSink ()}
2021-08-04 14:01:58 -04:00
}
return & result , nil
}
2020-02-12 13:53:27 -05:00
// Sanitized returns a copy of the config with all values that are considered
// sensitive stripped. It also strips all `*Raw` values that are mainly
// used for parsing.
//
// Specifically, the fields that this method strips are:
// - KMS.Config
// - Telemetry.CirconusAPIToken
func ( c * Config ) Sanitized () map [ string ] interface {} {
// Create shared config if it doesn't exist (e.g. in tests) so that map
// keys are actually populated
if c . SharedConfig == nil {
c . SharedConfig = new ( configutil . SharedConfig )
}
sharedResult := c . SharedConfig . Sanitized ()
result := map [ string ] interface {}{}
for k , v := range sharedResult {
result [ k ] = v
}
return result
}