2018-10-08 19:20:28 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2022-03-08 16:10:24 +00:00
|
|
|
"encoding/json"
|
2020-10-07 18:06:13 +00:00
|
|
|
"fmt"
|
2020-11-18 16:18:08 +00:00
|
|
|
"io"
|
2020-10-08 10:12:26 +00:00
|
|
|
"net/url"
|
2018-10-08 19:20:28 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-06-18 21:55:40 +00:00
|
|
|
"runtime"
|
2022-03-08 16:10:24 +00:00
|
|
|
"strconv"
|
2020-10-19 22:33:40 +00:00
|
|
|
"time"
|
2018-10-08 19:20:28 +00:00
|
|
|
|
2021-03-23 14:30:43 +00:00
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
2020-11-09 16:32:47 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-03-08 16:46:23 +00:00
|
|
|
"github.com/rs/zerolog"
|
2020-08-05 10:49:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2022-04-20 20:28:49 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-06-25 18:25:39 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/validation"
|
2018-10-08 19:20:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-18 21:55:40 +00:00
|
|
|
// DefaultConfigFiles is the file names from which we attempt to read configuration.
|
2018-10-08 19:20:28 +00:00
|
|
|
DefaultConfigFiles = []string{"config.yml", "config.yaml"}
|
|
|
|
|
2020-06-18 21:55:40 +00:00
|
|
|
// DefaultUnixConfigLocation is the primary location to find a config file
|
|
|
|
DefaultUnixConfigLocation = "/usr/local/etc/cloudflared"
|
|
|
|
|
|
|
|
// DefaultUnixLogLocation is the primary location to find log files
|
|
|
|
DefaultUnixLogLocation = "/var/log/cloudflared"
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
// Launchd doesn't set root env variables, so there is default
|
|
|
|
// Windows default config dir was ~/cloudflare-warp in documentation; let's keep it compatible
|
2020-08-14 20:52:47 +00:00
|
|
|
defaultUserConfigDirs = []string{"~/.cloudflared", "~/.cloudflare-warp", "~/cloudflare-warp"}
|
|
|
|
defaultNixConfigDirs = []string{"/etc/cloudflared", DefaultUnixConfigLocation}
|
2020-10-07 18:06:13 +00:00
|
|
|
|
|
|
|
ErrNoConfigFile = fmt.Errorf("Cannot determine default configuration path. No file %v in %v", DefaultConfigFiles, DefaultConfigSearchDirectories())
|
2018-10-08 19:20:28 +00:00
|
|
|
)
|
|
|
|
|
2020-10-15 21:41:03 +00:00
|
|
|
const (
|
|
|
|
DefaultCredentialFile = "cert.pem"
|
|
|
|
|
|
|
|
// BastionFlag is to enable bastion, or jump host, operation
|
|
|
|
BastionFlag = "bastion"
|
|
|
|
)
|
2018-10-08 19:20:28 +00:00
|
|
|
|
2020-06-18 21:55:40 +00:00
|
|
|
// DefaultConfigDirectory returns the default directory of the config file
|
|
|
|
func DefaultConfigDirectory() string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
path := os.Getenv("CFDPATH")
|
|
|
|
if path == "" {
|
|
|
|
path = filepath.Join(os.Getenv("ProgramFiles(x86)"), "cloudflared")
|
2022-03-08 16:10:24 +00:00
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) { // doesn't exist, so return an empty failure string
|
2020-06-18 21:55:40 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return DefaultUnixConfigLocation
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultLogDirectory returns the default directory for log files
|
|
|
|
func DefaultLogDirectory() string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return DefaultConfigDirectory()
|
|
|
|
}
|
|
|
|
return DefaultUnixLogLocation
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConfigPath returns the default location of a config file
|
|
|
|
func DefaultConfigPath() string {
|
|
|
|
dir := DefaultConfigDirectory()
|
|
|
|
if dir == "" {
|
|
|
|
return DefaultConfigFiles[0]
|
|
|
|
}
|
|
|
|
return filepath.Join(dir, DefaultConfigFiles[0])
|
|
|
|
}
|
|
|
|
|
2020-08-14 20:52:47 +00:00
|
|
|
// DefaultConfigSearchDirectories returns the default folder locations of the config
|
|
|
|
func DefaultConfigSearchDirectories() []string {
|
|
|
|
dirs := make([]string, len(defaultUserConfigDirs))
|
|
|
|
copy(dirs, defaultUserConfigDirs)
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
dirs = append(dirs, defaultNixConfigDirs...)
|
|
|
|
}
|
|
|
|
return dirs
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
// FileExists checks to see if a file exist at the provided path.
|
|
|
|
func FileExists(path string) (bool, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// ignore missing files
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = f.Close()
|
2018-10-08 19:20:28 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindDefaultConfigPath returns the first path that contains a config file.
|
2020-08-14 20:52:47 +00:00
|
|
|
// If none of the combination of DefaultConfigSearchDirectories() and DefaultConfigFiles
|
2018-10-08 19:20:28 +00:00
|
|
|
// contains a config file, return empty string.
|
|
|
|
func FindDefaultConfigPath() string {
|
2020-08-14 20:52:47 +00:00
|
|
|
for _, configDir := range DefaultConfigSearchDirectories() {
|
2018-10-08 19:20:28 +00:00
|
|
|
for _, configFile := range DefaultConfigFiles {
|
|
|
|
dirPath, err := homedir.Expand(configDir)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path := filepath.Join(dirPath, configFile)
|
|
|
|
if ok, _ := FileExists(path); ok {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2018-10-19 20:44:35 +00:00
|
|
|
|
2020-06-18 21:55:40 +00:00
|
|
|
// FindOrCreateConfigPath returns the first path that contains a config file
|
|
|
|
// or creates one in the primary default path if it doesn't exist
|
|
|
|
func FindOrCreateConfigPath() string {
|
|
|
|
path := FindDefaultConfigPath()
|
|
|
|
|
|
|
|
if path == "" {
|
|
|
|
// create the default directory if it doesn't exist
|
|
|
|
path = DefaultConfigPath()
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// write a new config file out
|
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
logDir := DefaultLogDirectory()
|
2022-03-08 16:10:24 +00:00
|
|
|
_ = os.MkdirAll(logDir, os.ModePerm) // try and create it. Doesn't matter if it succeed or not, only byproduct will be no logs
|
2020-06-18 21:55:40 +00:00
|
|
|
|
|
|
|
c := Root{
|
|
|
|
LogDirectory: logDir,
|
|
|
|
}
|
|
|
|
if err := yaml.NewEncoder(file).Encode(&c); err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2019-02-14 10:40:54 +00:00
|
|
|
// ValidateUnixSocket ensures --unix-socket param is used exclusively
|
|
|
|
// i.e. it fails if a user specifies both --url and --unix-socket
|
|
|
|
func ValidateUnixSocket(c *cli.Context) (string, error) {
|
|
|
|
if c.IsSet("unix-socket") && (c.IsSet("url") || c.NArg() > 0) {
|
|
|
|
return "", errors.New("--unix-socket must be used exclusivly.")
|
|
|
|
}
|
|
|
|
return c.String("unix-socket"), nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:44:35 +00:00
|
|
|
// ValidateUrl will validate url flag correctness. It can be either from --url or argument
|
2019-02-14 10:40:54 +00:00
|
|
|
// Notice ValidateUnixSocket, it will enforce --unix-socket is not used with --url or argument
|
2020-11-02 11:21:34 +00:00
|
|
|
func ValidateUrl(c *cli.Context, allowURLFromArgs bool) (*url.URL, error) {
|
2018-10-19 20:44:35 +00:00
|
|
|
var url = c.String("url")
|
2020-11-02 11:21:34 +00:00
|
|
|
if allowURLFromArgs && c.NArg() > 0 {
|
2018-10-19 20:44:35 +00:00
|
|
|
if c.IsSet("url") {
|
2020-10-08 10:12:26 +00:00
|
|
|
return nil, errors.New("Specified origin urls using both --url and argument. Decide which one you want, I can only support one.")
|
2018-10-19 20:44:35 +00:00
|
|
|
}
|
|
|
|
url = c.Args().Get(0)
|
|
|
|
}
|
|
|
|
validUrl, err := validation.ValidateUrl(url)
|
|
|
|
return validUrl, err
|
|
|
|
}
|
2020-10-09 00:12:29 +00:00
|
|
|
|
2020-10-20 17:00:34 +00:00
|
|
|
type UnvalidatedIngressRule struct {
|
2022-04-27 10:51:06 +00:00
|
|
|
Hostname string `json:"hostname,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
Service string `json:"service,omitempty"`
|
2022-01-28 14:37:17 +00:00
|
|
|
OriginRequest OriginRequestConfig `yaml:"originRequest" json:"originRequest"`
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OriginRequestConfig is a set of optional fields that users may set to
|
|
|
|
// customize how cloudflared sends requests to origin services. It is used to set
|
|
|
|
// up general config that apply to all rules, and also, specific per-rule
|
|
|
|
// config.
|
2022-01-28 14:37:17 +00:00
|
|
|
// Note:
|
|
|
|
// - To specify a time.Duration in go-yaml, use e.g. "3s" or "24h".
|
|
|
|
// - To specify a time.Duration in json, use int64 of the nanoseconds
|
2020-10-15 21:41:03 +00:00
|
|
|
type OriginRequestConfig struct {
|
|
|
|
// HTTP proxy timeout for establishing a new connection
|
2022-04-27 10:51:06 +00:00
|
|
|
ConnectTimeout *CustomDuration `yaml:"connectTimeout" json:"connectTimeout,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// HTTP proxy timeout for completing a TLS handshake
|
2022-04-27 10:51:06 +00:00
|
|
|
TLSTimeout *CustomDuration `yaml:"tlsTimeout" json:"tlsTimeout,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// HTTP proxy TCP keepalive duration
|
2022-04-27 10:51:06 +00:00
|
|
|
TCPKeepAlive *CustomDuration `yaml:"tcpKeepAlive" json:"tcpKeepAlive,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// HTTP proxy should disable "happy eyeballs" for IPv4/v6 fallback
|
2022-04-27 10:51:06 +00:00
|
|
|
NoHappyEyeballs *bool `yaml:"noHappyEyeballs" json:"noHappyEyeballs,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// HTTP proxy maximum keepalive connection pool size
|
2022-04-27 10:51:06 +00:00
|
|
|
KeepAliveConnections *int `yaml:"keepAliveConnections" json:"keepAliveConnections,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// HTTP proxy timeout for closing an idle connection
|
2022-04-27 10:51:06 +00:00
|
|
|
KeepAliveTimeout *CustomDuration `yaml:"keepAliveTimeout" json:"keepAliveTimeout,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Sets the HTTP Host header for the local webserver.
|
2022-04-27 10:51:06 +00:00
|
|
|
HTTPHostHeader *string `yaml:"httpHostHeader" json:"httpHostHeader,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Hostname on the origin server certificate.
|
2022-04-27 10:51:06 +00:00
|
|
|
OriginServerName *string `yaml:"originServerName" json:"originServerName,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Path to the CA for the certificate of your origin.
|
|
|
|
// This option should be used only if your certificate is not signed by Cloudflare.
|
2022-04-27 10:51:06 +00:00
|
|
|
CAPool *string `yaml:"caPool" json:"caPool,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Disables TLS verification of the certificate presented by your origin.
|
|
|
|
// Will allow any certificate from the origin to be accepted.
|
|
|
|
// Note: The connection from your machine to Cloudflare's Edge is still encrypted.
|
2022-04-27 10:51:06 +00:00
|
|
|
NoTLSVerify *bool `yaml:"noTLSVerify" json:"noTLSVerify,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Disables chunked transfer encoding.
|
|
|
|
// Useful if you are running a WSGI server.
|
2022-04-27 10:51:06 +00:00
|
|
|
DisableChunkedEncoding *bool `yaml:"disableChunkedEncoding" json:"disableChunkedEncoding,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Runs as jump host
|
2022-04-27 10:51:06 +00:00
|
|
|
BastionMode *bool `yaml:"bastionMode" json:"bastionMode,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Listen address for the proxy.
|
2022-04-27 10:51:06 +00:00
|
|
|
ProxyAddress *string `yaml:"proxyAddress" json:"proxyAddress,omitempty"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Listen port for the proxy.
|
2022-04-27 10:51:06 +00:00
|
|
|
ProxyPort *uint `yaml:"proxyPort" json:"proxyPort,omitempty"`
|
2020-11-10 01:22:03 +00:00
|
|
|
// Valid options are 'socks' or empty.
|
2022-04-27 10:51:06 +00:00
|
|
|
ProxyType *string `yaml:"proxyType" json:"proxyType,omitempty"`
|
2021-03-01 22:26:37 +00:00
|
|
|
// IP rules for the proxy service
|
2022-04-27 10:51:06 +00:00
|
|
|
IPRules []IngressIPRule `yaml:"ipRules" json:"ipRules,omitempty"`
|
2022-06-01 00:51:59 +00:00
|
|
|
// Attempt to connect to origin with HTTP/2
|
|
|
|
Http2Origin *bool `yaml:"http2Origin" json:"http2Origin,omitempty"`
|
2022-09-20 14:14:31 +00:00
|
|
|
// Access holds all access related configs
|
|
|
|
Access *AccessConfig `yaml:"access" json:"access,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AccessConfig struct {
|
|
|
|
// Enabled when set to true will fail every request that does not arrive through an access authenticated endpoint.
|
|
|
|
Enabled bool
|
|
|
|
|
|
|
|
// TeamName is the organization team name to get the public key certificates for.
|
|
|
|
TeamName string `yaml:"teamName" json:"teamName,omitempty"`
|
|
|
|
|
|
|
|
// AudTag is the AudTag to verify access JWT against.
|
|
|
|
AudTag []string `yaml:"audTag" json:"audTag,omitempty"`
|
2021-03-01 22:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IngressIPRule struct {
|
2022-01-28 14:37:17 +00:00
|
|
|
Prefix *string `yaml:"prefix" json:"prefix"`
|
|
|
|
Ports []int `yaml:"ports" json:"ports"`
|
|
|
|
Allow bool `yaml:"allow" json:"allow"`
|
2020-10-20 17:00:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
type Configuration struct {
|
2020-10-15 21:41:03 +00:00
|
|
|
TunnelID string `yaml:"tunnel"`
|
|
|
|
Ingress []UnvalidatedIngressRule
|
2021-01-17 20:22:53 +00:00
|
|
|
WarpRouting WarpRoutingConfig `yaml:"warp-routing"`
|
2020-10-15 21:41:03 +00:00
|
|
|
OriginRequest OriginRequestConfig `yaml:"originRequest"`
|
|
|
|
sourceFile string
|
2020-10-19 22:33:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 20:22:53 +00:00
|
|
|
type WarpRoutingConfig struct {
|
2022-06-13 16:44:27 +00:00
|
|
|
Enabled bool `yaml:"enabled" json:"enabled"`
|
|
|
|
ConnectTimeout *CustomDuration `yaml:"connectTimeout" json:"connectTimeout,omitempty"`
|
|
|
|
TCPKeepAlive *CustomDuration `yaml:"tcpKeepAlive" json:"tcpKeepAlive,omitempty"`
|
2021-01-17 20:22:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
type configFileSettings struct {
|
|
|
|
Configuration `yaml:",inline"`
|
2020-10-20 17:00:34 +00:00
|
|
|
// older settings will be aggregated into the generic map, should be read via cli.Context
|
2020-10-20 14:29:13 +00:00
|
|
|
Settings map[string]interface{} `yaml:",inline"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) Source() string {
|
2020-10-19 22:33:40 +00:00
|
|
|
return c.sourceFile
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func (c *configFileSettings) Int(name string) (int, error) {
|
2020-10-19 22:33:40 +00:00
|
|
|
if raw, ok := c.Settings[name]; ok {
|
|
|
|
if v, ok := raw.(int); ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("expected int found %T for %s", raw, name)
|
2020-10-09 00:12:29 +00:00
|
|
|
}
|
2020-10-19 22:33:40 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func (c *configFileSettings) Duration(name string) (time.Duration, error) {
|
2020-10-19 22:33:40 +00:00
|
|
|
if raw, ok := c.Settings[name]; ok {
|
|
|
|
switch v := raw.(type) {
|
|
|
|
case time.Duration:
|
|
|
|
return v, nil
|
|
|
|
case string:
|
|
|
|
return time.ParseDuration(v)
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("expected duration found %T for %s", raw, name)
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func (c *configFileSettings) Float64(name string) (float64, error) {
|
2020-10-19 22:33:40 +00:00
|
|
|
if raw, ok := c.Settings[name]; ok {
|
|
|
|
if v, ok := raw.(float64); ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
2020-10-20 14:29:13 +00:00
|
|
|
return 0, fmt.Errorf("expected float found %T for %s", raw, name)
|
2020-10-19 22:33:40 +00:00
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func (c *configFileSettings) String(name string) (string, error) {
|
2020-10-19 22:33:40 +00:00
|
|
|
if raw, ok := c.Settings[name]; ok {
|
|
|
|
if v, ok := raw.(string); ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
2020-10-20 14:29:13 +00:00
|
|
|
return "", fmt.Errorf("expected string found %T for %s", raw, name)
|
2020-10-19 22:33:40 +00:00
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func (c *configFileSettings) StringSlice(name string) ([]string, error) {
|
2020-10-19 22:33:40 +00:00
|
|
|
if raw, ok := c.Settings[name]; ok {
|
2020-10-21 10:11:35 +00:00
|
|
|
if slice, ok := raw.([]interface{}); ok {
|
|
|
|
strSlice := make([]string, len(slice))
|
|
|
|
for i, v := range slice {
|
|
|
|
str, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("expected string, found %T for %v", i, v)
|
|
|
|
}
|
|
|
|
strSlice[i] = str
|
|
|
|
}
|
|
|
|
return strSlice, nil
|
2020-10-19 22:33:40 +00:00
|
|
|
}
|
2020-10-20 14:29:13 +00:00
|
|
|
return nil, fmt.Errorf("expected string slice found %T for %s", raw, name)
|
2020-10-19 22:33:40 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func (c *configFileSettings) IntSlice(name string) ([]int, error) {
|
2020-10-19 22:33:40 +00:00
|
|
|
if raw, ok := c.Settings[name]; ok {
|
2020-10-21 10:11:35 +00:00
|
|
|
if slice, ok := raw.([]interface{}); ok {
|
|
|
|
intSlice := make([]int, len(slice))
|
|
|
|
for i, v := range slice {
|
|
|
|
str, ok := v.(int)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("expected int, found %T for %v ", v, v)
|
|
|
|
}
|
|
|
|
intSlice[i] = str
|
|
|
|
}
|
|
|
|
return intSlice, nil
|
|
|
|
}
|
2020-10-19 22:33:40 +00:00
|
|
|
if v, ok := raw.([]int); ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
2020-10-20 14:29:13 +00:00
|
|
|
return nil, fmt.Errorf("expected int slice found %T for %s", raw, name)
|
2020-10-19 22:33:40 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func (c *configFileSettings) Generic(name string) (cli.Generic, error) {
|
2020-10-19 22:33:40 +00:00
|
|
|
return nil, errors.New("option type Generic not supported")
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func (c *configFileSettings) Bool(name string) (bool, error) {
|
2020-10-19 22:33:40 +00:00
|
|
|
if raw, ok := c.Settings[name]; ok {
|
|
|
|
if v, ok := raw.(bool); ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
2020-10-20 14:29:13 +00:00
|
|
|
return false, fmt.Errorf("expected boolean found %T for %s", raw, name)
|
2020-10-09 00:12:29 +00:00
|
|
|
}
|
2020-10-19 22:33:40 +00:00
|
|
|
return false, nil
|
2020-10-09 00:12:29 +00:00
|
|
|
}
|
2020-10-15 20:08:57 +00:00
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
var configuration configFileSettings
|
2020-10-19 22:33:40 +00:00
|
|
|
|
2020-10-20 14:29:13 +00:00
|
|
|
func GetConfiguration() *Configuration {
|
|
|
|
return &configuration.Configuration
|
2020-10-15 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 22:33:40 +00:00
|
|
|
// ReadConfigFile returns InputSourceContext initialized from the configuration file.
|
2020-10-15 20:08:57 +00:00
|
|
|
// On repeat calls returns with the same file, returns without reading the file again; however,
|
|
|
|
// if value of "config" flag changes, will read the new config file
|
2021-05-06 20:10:47 +00:00
|
|
|
func ReadConfigFile(c *cli.Context, log *zerolog.Logger) (settings *configFileSettings, warnings string, err error) {
|
2020-10-15 20:08:57 +00:00
|
|
|
configFile := c.String("config")
|
2020-10-19 22:33:40 +00:00
|
|
|
if configuration.Source() == configFile || configFile == "" {
|
2020-10-19 12:30:25 +00:00
|
|
|
if configuration.Source() == "" {
|
2021-05-06 20:10:47 +00:00
|
|
|
return nil, "", ErrNoConfigFile
|
2020-10-19 12:30:25 +00:00
|
|
|
}
|
2021-05-06 20:10:47 +00:00
|
|
|
return &configuration, "", nil
|
2020-10-15 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
log.Debug().Msgf("Loading configuration from %s", configFile)
|
2020-10-19 22:33:40 +00:00
|
|
|
file, err := os.Open(configFile)
|
2020-10-15 20:08:57 +00:00
|
|
|
if err != nil {
|
2020-10-19 22:33:40 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = ErrNoConfigFile
|
|
|
|
}
|
2021-05-06 20:10:47 +00:00
|
|
|
return nil, "", err
|
2020-10-19 22:33:40 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
if err := yaml.NewDecoder(file).Decode(&configuration); err != nil {
|
2020-11-18 16:18:08 +00:00
|
|
|
if err == io.EOF {
|
2020-11-25 06:55:13 +00:00
|
|
|
log.Error().Msgf("Configuration file %s was empty", configFile)
|
2021-05-06 20:10:47 +00:00
|
|
|
return &configuration, "", nil
|
2020-11-18 16:18:08 +00:00
|
|
|
}
|
2021-05-06 20:10:47 +00:00
|
|
|
return nil, "", errors.Wrap(err, "error parsing YAML in config file at "+configFile)
|
2020-10-15 20:08:57 +00:00
|
|
|
}
|
2020-10-19 22:33:40 +00:00
|
|
|
configuration.sourceFile = configFile
|
2021-05-06 20:10:47 +00:00
|
|
|
|
|
|
|
// Parse it again, with strict mode, to find warnings.
|
|
|
|
if file, err := os.Open(configFile); err == nil {
|
|
|
|
decoder := yaml.NewDecoder(file)
|
2022-04-20 20:28:49 +00:00
|
|
|
decoder.KnownFields(true)
|
2021-05-06 20:10:47 +00:00
|
|
|
var unusedConfig configFileSettings
|
|
|
|
if err := decoder.Decode(&unusedConfig); err != nil {
|
|
|
|
warnings = err.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &configuration, warnings, nil
|
2020-10-15 20:08:57 +00:00
|
|
|
}
|
2022-03-08 16:10:24 +00:00
|
|
|
|
|
|
|
// A CustomDuration is a Duration that has custom serialization for JSON.
|
|
|
|
// JSON in Javascript assumes that int fields are 32 bits and Duration fields are deserialized assuming that numbers
|
|
|
|
// are in nanoseconds, which in 32bit integers limits to just 2 seconds.
|
|
|
|
// This type assumes that when serializing/deserializing from JSON, that the number is in seconds, while it maintains
|
|
|
|
// the YAML serde assumptions.
|
|
|
|
type CustomDuration struct {
|
|
|
|
time.Duration
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
func (s CustomDuration) MarshalJSON() ([]byte, error) {
|
2022-03-08 16:10:24 +00:00
|
|
|
return json.Marshal(s.Duration.Seconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CustomDuration) UnmarshalJSON(data []byte) error {
|
|
|
|
seconds, err := strconv.ParseInt(string(data), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Duration = time.Duration(seconds * int64(time.Second))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CustomDuration) MarshalYAML() (interface{}, error) {
|
|
|
|
return s.Duration.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CustomDuration) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
return unmarshal(&s.Duration)
|
|
|
|
}
|