2016-09-03 17:25:16 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2016-09-12 17:26:56 +00:00
|
|
|
"fmt"
|
2019-04-23 22:59:02 +00:00
|
|
|
"os"
|
2016-09-03 17:25:16 +00:00
|
|
|
"strings"
|
2016-09-14 16:11:06 +00:00
|
|
|
"time"
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
"github.com/spf13/cast"
|
2016-09-03 17:25:16 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
const (
|
|
|
|
envPrefix = "KOLIDE"
|
2016-09-04 05:13:42 +00:00
|
|
|
)
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// MysqlConfig defines configs related to MySQL
|
|
|
|
type MysqlConfig struct {
|
2020-08-19 21:56:44 +00:00
|
|
|
Protocol string
|
|
|
|
Address string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Database string
|
|
|
|
TLSCert string `yaml:"tls_cert"`
|
|
|
|
TLSKey string `yaml:"tls_key"`
|
|
|
|
TLSCA string `yaml:"tls_ca"`
|
|
|
|
TLSServerName string `yaml:"tls_server_name"`
|
|
|
|
TLSConfig string `yaml:"tls_config"` //tls=customValue in DSN
|
|
|
|
MaxOpenConns int `yaml:"max_open_conns"`
|
|
|
|
MaxIdleConns int `yaml:"max_idle_conns"`
|
2020-07-30 16:00:42 +00:00
|
|
|
ConnMaxLifetime int `yaml:"conn_max_lifetime"`
|
2016-09-12 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 00:13:23 +00:00
|
|
|
// RedisConfig defines configs related to Redis
|
|
|
|
type RedisConfig struct {
|
|
|
|
Address string
|
|
|
|
Password string
|
2020-07-30 15:57:25 +00:00
|
|
|
Database int
|
2020-10-01 23:25:48 +00:00
|
|
|
UseTLS bool `yaml:"use_tls"`
|
2016-12-16 00:13:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 04:21:48 +00:00
|
|
|
const (
|
|
|
|
TLSProfileKey = "server.tls_compatibility"
|
|
|
|
TLSProfileModern = "modern"
|
|
|
|
TLSProfileIntermediate = "intermediate"
|
|
|
|
)
|
|
|
|
|
2019-01-24 17:39:32 +00:00
|
|
|
// ServerConfig defines configs related to the Fleet server
|
2016-09-12 17:26:56 +00:00
|
|
|
type ServerConfig struct {
|
2017-03-28 04:21:48 +00:00
|
|
|
Address string
|
|
|
|
Cert string
|
|
|
|
Key string
|
|
|
|
TLS bool
|
|
|
|
TLSProfile string
|
2019-10-16 23:40:45 +00:00
|
|
|
URLPrefix string `yaml:"url_prefix"`
|
2016-09-12 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthConfig defines configs related to user authorization
|
|
|
|
type AuthConfig struct {
|
2017-02-22 15:22:19 +00:00
|
|
|
JwtKey string `yaml:"jwt_key"`
|
|
|
|
BcryptCost int `yaml:"bcrypt_cost"`
|
|
|
|
SaltKeySize int `yaml:"salt_key_size"`
|
2016-09-12 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppConfig defines configs related to HTTP
|
|
|
|
type AppConfig struct {
|
2017-02-22 15:22:19 +00:00
|
|
|
TokenKeySize int `yaml:"token_key_size"`
|
|
|
|
InviteTokenValidityPeriod time.Duration `yaml:"invite_token_validity_period"`
|
2016-09-12 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SessionConfig defines configs related to user sessions
|
|
|
|
type SessionConfig struct {
|
2017-02-22 15:22:19 +00:00
|
|
|
KeySize int `yaml:"key_size"`
|
2016-09-14 16:11:06 +00:00
|
|
|
Duration time.Duration
|
2016-09-12 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OsqueryConfig defines configs related to osquery
|
|
|
|
type OsqueryConfig struct {
|
2020-03-02 19:08:08 +00:00
|
|
|
NodeKeySize int `yaml:"node_key_size"`
|
|
|
|
StatusLogPlugin string `yaml:"status_log_plugin"`
|
|
|
|
ResultLogPlugin string `yaml:"result_log_plugin"`
|
|
|
|
LabelUpdateInterval time.Duration `yaml:"label_update_interval"`
|
|
|
|
DetailUpdateInterval time.Duration `yaml:"detail_update_interval"`
|
|
|
|
StatusLogFile string `yaml:"status_log_file"`
|
|
|
|
ResultLogFile string `yaml:"result_log_file"`
|
|
|
|
EnableLogRotation bool `yaml:"enable_log_rotation"`
|
2016-09-12 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoggingConfig defines configs related to logging
|
|
|
|
type LoggingConfig struct {
|
|
|
|
Debug bool
|
2017-01-19 14:41:28 +00:00
|
|
|
JSON bool
|
2017-02-22 15:22:19 +00:00
|
|
|
DisableBanner bool `yaml:"disable_banner"`
|
2016-09-12 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 18:47:15 +00:00
|
|
|
// FirehoseConfig defines configs for the AWS Firehose logging plugin
|
|
|
|
type FirehoseConfig struct {
|
2020-08-19 21:56:44 +00:00
|
|
|
Region string
|
|
|
|
AccessKeyID string `yaml:"access_key_id"`
|
|
|
|
SecretAccessKey string `yaml:"secret_access_key"`
|
|
|
|
StsAssumeRoleArn string `yaml:"sts_assume_role_arn"`
|
|
|
|
StatusStream string `yaml:"status_stream"`
|
|
|
|
ResultStream string `yaml:"result_stream"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// KinesisConfig defines configs for the AWS Kinesis logging plugin
|
|
|
|
type KinesisConfig struct {
|
|
|
|
Region string
|
|
|
|
AccessKeyID string `yaml:"access_key_id"`
|
|
|
|
SecretAccessKey string `yaml:"secret_access_key"`
|
|
|
|
StsAssumeRoleArn string `yaml:"sts_assume_role_arn"`
|
|
|
|
StatusStream string `yaml:"status_stream"`
|
|
|
|
ResultStream string `yaml:"result_stream"`
|
2019-04-08 18:47:15 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 22:41:50 +00:00
|
|
|
// PubSubConfig defines configs the for Google PubSub logging plugin
|
|
|
|
type PubSubConfig struct {
|
|
|
|
Project string
|
|
|
|
StatusTopic string `yaml:"status_topic"`
|
|
|
|
ResultTopic string `yaml:"result_topic"`
|
|
|
|
}
|
|
|
|
|
2019-04-08 18:47:15 +00:00
|
|
|
// FilesystemConfig defines configs for the Filesystem logging plugin
|
|
|
|
type FilesystemConfig struct {
|
2020-09-09 20:33:32 +00:00
|
|
|
StatusLogFile string `yaml:"status_log_file"`
|
|
|
|
ResultLogFile string `yaml:"result_log_file"`
|
|
|
|
EnableLogRotation bool `yaml:"enable_log_rotation"`
|
|
|
|
EnableLogCompression bool `yaml:"enable_log_compression"`
|
2019-04-08 18:47:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// KolideConfig stores the application configuration. Each subcategory is
|
|
|
|
// broken up into it's own struct, defined above. When editing any of these
|
|
|
|
// structs, Manager.addConfigs and Manager.LoadConfig should be
|
|
|
|
// updated to set and retrieve the configurations as appropriate.
|
|
|
|
type KolideConfig struct {
|
2019-04-08 18:47:15 +00:00
|
|
|
Mysql MysqlConfig
|
|
|
|
Redis RedisConfig
|
|
|
|
Server ServerConfig
|
|
|
|
Auth AuthConfig
|
|
|
|
App AppConfig
|
|
|
|
Session SessionConfig
|
|
|
|
Osquery OsqueryConfig
|
|
|
|
Logging LoggingConfig
|
|
|
|
Firehose FirehoseConfig
|
2020-08-19 21:56:44 +00:00
|
|
|
Kinesis KinesisConfig
|
2019-07-16 22:41:50 +00:00
|
|
|
PubSub PubSubConfig
|
2019-04-08 18:47:15 +00:00
|
|
|
Filesystem FilesystemConfig
|
2016-09-12 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// addConfigs adds the configuration keys and default values that will be
|
|
|
|
// filled into the KolideConfig struct
|
|
|
|
func (man Manager) addConfigs() {
|
|
|
|
// MySQL
|
2019-12-04 17:48:24 +00:00
|
|
|
man.addConfigString("mysql.protocol", "tcp",
|
|
|
|
"MySQL server communication protocol (tcp,unix,...)")
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigString("mysql.address", "localhost:3306",
|
|
|
|
"MySQL server address (host:port)")
|
|
|
|
man.addConfigString("mysql.username", "kolide",
|
|
|
|
"MySQL server username")
|
|
|
|
man.addConfigString("mysql.password", "kolide",
|
|
|
|
"MySQL server password (prefer env variable for security)")
|
|
|
|
man.addConfigString("mysql.database", "kolide",
|
|
|
|
"MySQL database name")
|
2017-02-22 15:22:19 +00:00
|
|
|
man.addConfigString("mysql.tls_cert", "",
|
|
|
|
"MySQL TLS client certificate path")
|
2017-02-17 00:14:00 +00:00
|
|
|
man.addConfigString("mysql.tls_key", "",
|
2017-02-22 15:22:19 +00:00
|
|
|
"MySQL TLS client key path")
|
2017-02-17 00:14:00 +00:00
|
|
|
man.addConfigString("mysql.tls_ca", "",
|
|
|
|
"MySQL TLS server CA")
|
|
|
|
man.addConfigString("mysql.tls_server_name", "",
|
2017-02-22 15:22:19 +00:00
|
|
|
"MySQL TLS server name")
|
2017-02-17 00:14:00 +00:00
|
|
|
man.addConfigString("mysql.tls_config", "",
|
|
|
|
"MySQL TLS config value. Use skip-verify, true, false or custom key.")
|
2017-12-19 21:52:52 +00:00
|
|
|
man.addConfigInt("mysql.max_open_conns", 50, "MySQL maximum open connection handles.")
|
|
|
|
man.addConfigInt("mysql.max_idle_conns", 50, "MySQL maximum idle connection handles.")
|
2020-07-30 16:00:42 +00:00
|
|
|
man.addConfigInt("mysql.conn_max_lifetime", 0, "MySQL maximum amount of time a connection may be reused.")
|
2016-09-12 17:26:56 +00:00
|
|
|
|
2016-12-16 00:13:23 +00:00
|
|
|
// Redis
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigString("redis.address", "localhost:6379",
|
|
|
|
"Redis server address (host:port)")
|
|
|
|
man.addConfigString("redis.password", "",
|
|
|
|
"Redis server password (prefer env variable for security)")
|
2020-07-30 15:57:25 +00:00
|
|
|
man.addConfigInt("redis.database", 0,
|
|
|
|
"Redis server database number")
|
2020-10-01 23:25:48 +00:00
|
|
|
man.addConfigBool("redis.use_tls", false, "Redis server enable TLS")
|
2016-12-16 00:13:23 +00:00
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// Server
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigString("server.address", "0.0.0.0:8080",
|
2019-01-24 17:39:32 +00:00
|
|
|
"Fleet server address (host:port)")
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigString("server.cert", "./tools/osquery/kolide.crt",
|
2019-01-24 17:39:32 +00:00
|
|
|
"Fleet TLS certificate path")
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigString("server.key", "./tools/osquery/kolide.key",
|
2019-01-24 17:39:32 +00:00
|
|
|
"Fleet TLS key path")
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigBool("server.tls", true,
|
|
|
|
"Enable TLS (required for osqueryd communication)")
|
2017-03-28 04:21:48 +00:00
|
|
|
man.addConfigString(TLSProfileKey, TLSProfileModern,
|
2020-09-10 16:31:01 +00:00
|
|
|
fmt.Sprintf("TLS security profile choose one of %s or %s",
|
|
|
|
TLSProfileModern, TLSProfileIntermediate))
|
2019-10-16 23:40:45 +00:00
|
|
|
man.addConfigString("server.url_prefix", "",
|
|
|
|
"URL prefix used on server and frontend endpoints")
|
2016-09-12 17:26:56 +00:00
|
|
|
|
|
|
|
// Auth
|
2017-04-12 22:05:56 +00:00
|
|
|
man.addConfigString("auth.jwt_key", "",
|
|
|
|
"JWT session token key (required)")
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigInt("auth.bcrypt_cost", 12,
|
|
|
|
"Bcrypt iterations")
|
|
|
|
man.addConfigInt("auth.salt_key_size", 24,
|
|
|
|
"Size of salt for passwords")
|
2016-09-12 17:26:56 +00:00
|
|
|
|
|
|
|
// App
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigString("app.token_key", "CHANGEME",
|
|
|
|
"Secret key for generating invite and reset tokens")
|
|
|
|
man.addConfigDuration("app.invite_token_validity_period", 5*24*time.Hour,
|
|
|
|
"Duration invite tokens remain valid (i.e. 1h)")
|
|
|
|
man.addConfigInt("app.token_key_size", 24,
|
|
|
|
"Size of generated tokens")
|
2016-09-12 17:26:56 +00:00
|
|
|
|
|
|
|
// Session
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigInt("session.key_size", 64,
|
|
|
|
"Size of generated session keys")
|
|
|
|
man.addConfigDuration("session.duration", 24*90*time.Hour,
|
|
|
|
"Duration session keys remain valid (i.e. 24h)")
|
2016-09-12 17:26:56 +00:00
|
|
|
|
|
|
|
// Osquery
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigInt("osquery.node_key_size", 24,
|
|
|
|
"Size of generated osqueryd node keys")
|
2019-04-08 18:47:15 +00:00
|
|
|
man.addConfigString("osquery.status_log_plugin", "filesystem",
|
|
|
|
"Log plugin to use for status logs")
|
|
|
|
man.addConfigString("osquery.result_log_plugin", "filesystem",
|
|
|
|
"Log plugin to use for result logs")
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigDuration("osquery.label_update_interval", 1*time.Hour,
|
|
|
|
"Interval to update host label membership (i.e. 1h)")
|
2020-03-02 19:08:08 +00:00
|
|
|
man.addConfigDuration("osquery.detail_update_interval", 1*time.Hour,
|
|
|
|
"Interval to update host details (i.e. 1h)")
|
2019-04-08 18:47:15 +00:00
|
|
|
man.addConfigString("osquery.status_log_file", "",
|
|
|
|
"(DEPRECATED: Use filesystem.status_log_file) Path for osqueryd status logs")
|
|
|
|
man.addConfigString("osquery.result_log_file", "",
|
|
|
|
"(DEPRECATED: Use filesystem.result_log_file) Path for osqueryd result logs")
|
2017-04-03 21:48:50 +00:00
|
|
|
man.addConfigBool("osquery.enable_log_rotation", false,
|
2019-04-08 18:47:15 +00:00
|
|
|
"(DEPRECATED: Use filesystem.enable_log_rotation) Enable automatic rotation for osquery log files")
|
2016-09-12 17:26:56 +00:00
|
|
|
|
|
|
|
// Logging
|
2017-01-31 18:06:30 +00:00
|
|
|
man.addConfigBool("logging.debug", false,
|
|
|
|
"Enable debug logging")
|
|
|
|
man.addConfigBool("logging.json", false,
|
|
|
|
"Log in JSON format")
|
|
|
|
man.addConfigBool("logging.disable_banner", false,
|
|
|
|
"Disable startup banner")
|
2019-04-08 18:47:15 +00:00
|
|
|
|
|
|
|
// Firehose
|
|
|
|
man.addConfigString("firehose.region", "", "AWS Region to use")
|
|
|
|
man.addConfigString("firehose.access_key_id", "", "Access Key ID for AWS authentication")
|
|
|
|
man.addConfigString("firehose.secret_access_key", "", "Secret Access Key for AWS authentication")
|
2020-08-19 21:56:44 +00:00
|
|
|
man.addConfigString("firehose.sts_assume_role_arn", "",
|
|
|
|
"ARN of role to assume for AWS")
|
2019-04-08 18:47:15 +00:00
|
|
|
man.addConfigString("firehose.status_stream", "",
|
|
|
|
"Firehose stream name for status logs")
|
|
|
|
man.addConfigString("firehose.result_stream", "",
|
|
|
|
"Firehose stream name for result logs")
|
|
|
|
|
2020-08-19 21:56:44 +00:00
|
|
|
// Kinesis
|
|
|
|
man.addConfigString("kinesis.region", "", "AWS Region to use")
|
|
|
|
man.addConfigString("kinesis.access_key_id", "", "Access Key ID for AWS authentication")
|
|
|
|
man.addConfigString("kinesis.secret_access_key", "", "Secret Access Key for AWS authentication")
|
|
|
|
man.addConfigString("kinesis.sts_assume_role_arn", "",
|
|
|
|
"ARN of role to assume for AWS")
|
|
|
|
man.addConfigString("kinesis.status_stream", "",
|
|
|
|
"Kinesis stream name for status logs")
|
|
|
|
man.addConfigString("kinesis.result_stream", "",
|
|
|
|
"Kinesis stream name for result logs")
|
|
|
|
|
2019-07-16 22:41:50 +00:00
|
|
|
// PubSub
|
|
|
|
man.addConfigString("pubsub.project", "", "Google Cloud Project to use")
|
|
|
|
man.addConfigString("pubsub.status_topic", "", "PubSub topic for status logs")
|
|
|
|
man.addConfigString("pubsub.result_topic", "", "PubSub topic for result logs")
|
|
|
|
|
2019-04-08 18:47:15 +00:00
|
|
|
// Filesystem
|
|
|
|
man.addConfigString("filesystem.status_log_file", "/tmp/osquery_status",
|
|
|
|
"Log file path to use for status logs")
|
|
|
|
man.addConfigString("filesystem.result_log_file", "/tmp/osquery_result",
|
|
|
|
"Log file path to use for result logs")
|
|
|
|
man.addConfigBool("filesystem.enable_log_rotation", false,
|
|
|
|
"Enable automatic rotation for osquery log files")
|
2020-09-09 20:33:32 +00:00
|
|
|
man.addConfigBool("filesystem.enable_log_compression", false,
|
|
|
|
"Enable compression for the rotated osquery log files")
|
2016-09-12 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfig will load the config variables into a fully initialized
|
|
|
|
// KolideConfig struct
|
|
|
|
func (man Manager) LoadConfig() KolideConfig {
|
|
|
|
man.loadConfigFile()
|
|
|
|
|
|
|
|
return KolideConfig{
|
|
|
|
Mysql: MysqlConfig{
|
2020-08-19 21:56:44 +00:00
|
|
|
Protocol: man.getConfigString("mysql.protocol"),
|
|
|
|
Address: man.getConfigString("mysql.address"),
|
|
|
|
Username: man.getConfigString("mysql.username"),
|
|
|
|
Password: man.getConfigString("mysql.password"),
|
|
|
|
Database: man.getConfigString("mysql.database"),
|
|
|
|
TLSCert: man.getConfigString("mysql.tls_cert"),
|
|
|
|
TLSKey: man.getConfigString("mysql.tls_key"),
|
|
|
|
TLSCA: man.getConfigString("mysql.tls_ca"),
|
|
|
|
TLSServerName: man.getConfigString("mysql.tls_server_name"),
|
|
|
|
TLSConfig: man.getConfigString("mysql.tls_config"),
|
|
|
|
MaxOpenConns: man.getConfigInt("mysql.max_open_conns"),
|
|
|
|
MaxIdleConns: man.getConfigInt("mysql.max_idle_conns"),
|
|
|
|
ConnMaxLifetime: man.getConfigInt("mysql.conn_max_lifetime"),
|
2016-09-12 17:26:56 +00:00
|
|
|
},
|
2016-12-16 00:13:23 +00:00
|
|
|
Redis: RedisConfig{
|
|
|
|
Address: man.getConfigString("redis.address"),
|
|
|
|
Password: man.getConfigString("redis.password"),
|
2020-07-30 15:57:25 +00:00
|
|
|
Database: man.getConfigInt("redis.database"),
|
2020-10-01 23:25:48 +00:00
|
|
|
UseTLS: man.getConfigBool("redis.use_tls"),
|
2016-12-16 00:13:23 +00:00
|
|
|
},
|
2016-09-12 17:26:56 +00:00
|
|
|
Server: ServerConfig{
|
2017-03-28 04:21:48 +00:00
|
|
|
Address: man.getConfigString("server.address"),
|
|
|
|
Cert: man.getConfigString("server.cert"),
|
|
|
|
Key: man.getConfigString("server.key"),
|
|
|
|
TLS: man.getConfigBool("server.tls"),
|
|
|
|
TLSProfile: man.getConfigTLSProfile(),
|
2019-10-16 23:40:45 +00:00
|
|
|
URLPrefix: man.getConfigString("server.url_prefix"),
|
2016-09-12 17:26:56 +00:00
|
|
|
},
|
|
|
|
Auth: AuthConfig{
|
|
|
|
JwtKey: man.getConfigString("auth.jwt_key"),
|
|
|
|
BcryptCost: man.getConfigInt("auth.bcrypt_cost"),
|
|
|
|
SaltKeySize: man.getConfigInt("auth.salt_key_size"),
|
|
|
|
},
|
|
|
|
App: AppConfig{
|
2016-09-29 02:44:05 +00:00
|
|
|
TokenKeySize: man.getConfigInt("app.token_key_size"),
|
|
|
|
InviteTokenValidityPeriod: man.getConfigDuration("app.invite_token_validity_period"),
|
2016-09-12 17:26:56 +00:00
|
|
|
},
|
|
|
|
Session: SessionConfig{
|
2016-09-14 16:11:06 +00:00
|
|
|
KeySize: man.getConfigInt("session.key_size"),
|
|
|
|
Duration: man.getConfigDuration("session.duration"),
|
2016-09-12 17:26:56 +00:00
|
|
|
},
|
|
|
|
Osquery: OsqueryConfig{
|
2020-03-02 19:08:08 +00:00
|
|
|
NodeKeySize: man.getConfigInt("osquery.node_key_size"),
|
|
|
|
StatusLogPlugin: man.getConfigString("osquery.status_log_plugin"),
|
|
|
|
ResultLogPlugin: man.getConfigString("osquery.result_log_plugin"),
|
|
|
|
StatusLogFile: man.getConfigString("osquery.status_log_file"),
|
|
|
|
ResultLogFile: man.getConfigString("osquery.result_log_file"),
|
|
|
|
LabelUpdateInterval: man.getConfigDuration("osquery.label_update_interval"),
|
|
|
|
DetailUpdateInterval: man.getConfigDuration("osquery.detail_update_interval"),
|
|
|
|
EnableLogRotation: man.getConfigBool("osquery.enable_log_rotation"),
|
2016-09-12 17:26:56 +00:00
|
|
|
},
|
|
|
|
Logging: LoggingConfig{
|
|
|
|
Debug: man.getConfigBool("logging.debug"),
|
2017-01-19 14:41:28 +00:00
|
|
|
JSON: man.getConfigBool("logging.json"),
|
2016-09-12 17:26:56 +00:00
|
|
|
DisableBanner: man.getConfigBool("logging.disable_banner"),
|
|
|
|
},
|
2019-04-08 18:47:15 +00:00
|
|
|
Firehose: FirehoseConfig{
|
2020-08-19 21:56:44 +00:00
|
|
|
Region: man.getConfigString("firehose.region"),
|
|
|
|
AccessKeyID: man.getConfigString("firehose.access_key_id"),
|
|
|
|
SecretAccessKey: man.getConfigString("firehose.secret_access_key"),
|
|
|
|
StsAssumeRoleArn: man.getConfigString("firehose.sts_assume_role_arn"),
|
|
|
|
StatusStream: man.getConfigString("firehose.status_stream"),
|
|
|
|
ResultStream: man.getConfigString("firehose.result_stream"),
|
|
|
|
},
|
|
|
|
Kinesis: KinesisConfig{
|
|
|
|
Region: man.getConfigString("kinesis.region"),
|
|
|
|
AccessKeyID: man.getConfigString("kinesis.access_key_id"),
|
|
|
|
SecretAccessKey: man.getConfigString("kinesis.secret_access_key"),
|
|
|
|
StatusStream: man.getConfigString("kinesis.status_stream"),
|
|
|
|
ResultStream: man.getConfigString("kinesis.result_stream"),
|
|
|
|
StsAssumeRoleArn: man.getConfigString("kinesis.sts_assume_role_arn"),
|
2019-04-08 18:47:15 +00:00
|
|
|
},
|
2019-07-16 22:41:50 +00:00
|
|
|
PubSub: PubSubConfig{
|
|
|
|
Project: man.getConfigString("pubsub.project"),
|
|
|
|
StatusTopic: man.getConfigString("pubsub.status_topic"),
|
|
|
|
ResultTopic: man.getConfigString("pubsub.result_topic"),
|
|
|
|
},
|
2019-04-08 18:47:15 +00:00
|
|
|
Filesystem: FilesystemConfig{
|
2020-09-09 20:33:32 +00:00
|
|
|
StatusLogFile: man.getConfigString("filesystem.status_log_file"),
|
|
|
|
ResultLogFile: man.getConfigString("filesystem.result_log_file"),
|
|
|
|
EnableLogRotation: man.getConfigBool("filesystem.enable_log_rotation"),
|
|
|
|
EnableLogCompression: man.getConfigBool("filesystem.enable_log_compression"),
|
2019-04-08 18:47:15 +00:00
|
|
|
},
|
2016-09-12 17:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 21:47:31 +00:00
|
|
|
// IsSet determines whether a given config key has been explicitly set by any
|
|
|
|
// of the configuration sources. If false, the default value is being used.
|
|
|
|
func (man Manager) IsSet(key string) bool {
|
|
|
|
return man.viper.IsSet(key)
|
|
|
|
}
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// envNameFromConfigKey converts a config key into the corresponding
|
|
|
|
// environment variable name
|
|
|
|
func envNameFromConfigKey(key string) string {
|
|
|
|
return envPrefix + "_" + strings.ToUpper(strings.Replace(key, ".", "_", -1))
|
|
|
|
}
|
|
|
|
|
|
|
|
// flagNameFromConfigKey converts a config key into the corresponding flag name
|
|
|
|
func flagNameFromConfigKey(key string) string {
|
|
|
|
return strings.Replace(key, ".", "_", -1)
|
|
|
|
}
|
|
|
|
|
2019-01-24 17:39:32 +00:00
|
|
|
// Manager manages the addition and retrieval of config values for Fleet
|
2016-09-12 17:26:56 +00:00
|
|
|
// configs. It's only public API method is LoadConfig, which will return the
|
|
|
|
// populated KolideConfig struct.
|
|
|
|
type Manager struct {
|
|
|
|
viper *viper.Viper
|
|
|
|
command *cobra.Command
|
|
|
|
defaults map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager initializes a Manager wrapping the provided cobra
|
|
|
|
// command. All config flags will be attached to that command (and inherited by
|
|
|
|
// the subcommands). Typically this should be called just once, with the root
|
|
|
|
// command.
|
|
|
|
func NewManager(command *cobra.Command) Manager {
|
|
|
|
man := Manager{
|
|
|
|
viper: viper.New(),
|
|
|
|
command: command,
|
|
|
|
defaults: map[string]interface{}{},
|
2016-09-03 17:25:16 +00:00
|
|
|
}
|
2016-09-12 17:26:56 +00:00
|
|
|
man.addConfigs()
|
|
|
|
return man
|
2016-09-03 17:25:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// addDefault will check for duplication, then add a default value to the
|
|
|
|
// defaults map
|
|
|
|
func (man Manager) addDefault(key string, defVal interface{}) {
|
|
|
|
if _, exists := man.defaults[key]; exists {
|
|
|
|
panic("Trying to add duplicate config for key " + key)
|
2016-09-03 17:25:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
man.defaults[key] = defVal
|
|
|
|
}
|
|
|
|
|
2017-01-31 18:06:30 +00:00
|
|
|
func getFlagUsage(key string, usage string) string {
|
|
|
|
return fmt.Sprintf("Env: %s\n\t\t%s", envNameFromConfigKey(key), usage)
|
|
|
|
}
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// getInterfaceVal is a helper function used by the getConfig* functions to
|
|
|
|
// retrieve the config value as interface{}, which will then be cast to the
|
|
|
|
// appropriate type by the getConfig* function.
|
|
|
|
func (man Manager) getInterfaceVal(key string) interface{} {
|
|
|
|
interfaceVal := man.viper.Get(key)
|
|
|
|
if interfaceVal == nil {
|
|
|
|
var ok bool
|
|
|
|
interfaceVal, ok = man.defaults[key]
|
|
|
|
if !ok {
|
|
|
|
panic("Tried to look up default value for nonexistent config option: " + key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return interfaceVal
|
|
|
|
}
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// addConfigString adds a string config to the config options
|
2017-01-31 18:06:30 +00:00
|
|
|
func (man Manager) addConfigString(key, defVal, usage string) {
|
|
|
|
man.command.PersistentFlags().String(flagNameFromConfigKey(key), defVal, getFlagUsage(key, usage))
|
2016-09-12 17:26:56 +00:00
|
|
|
man.viper.BindPFlag(key, man.command.PersistentFlags().Lookup(flagNameFromConfigKey(key)))
|
|
|
|
man.viper.BindEnv(key, envNameFromConfigKey(key))
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// Add default
|
|
|
|
man.addDefault(key, defVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConfigString retrieves a string from the loaded config
|
|
|
|
func (man Manager) getConfigString(key string) string {
|
|
|
|
interfaceVal := man.getInterfaceVal(key)
|
|
|
|
stringVal, err := cast.ToStringE(interfaceVal)
|
2016-09-03 17:25:16 +00:00
|
|
|
if err != nil {
|
2016-09-12 17:26:56 +00:00
|
|
|
panic("Unable to cast to string for key " + key + ": " + err.Error())
|
2016-09-03 17:25:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
return stringVal
|
|
|
|
}
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2017-03-28 04:21:48 +00:00
|
|
|
// Custom handling for TLSProfile which can only accept specific values
|
|
|
|
// for the argument
|
|
|
|
func (man Manager) getConfigTLSProfile() string {
|
|
|
|
ival := man.getInterfaceVal(TLSProfileKey)
|
|
|
|
sval, err := cast.ToStringE(ival)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("%s requires a string value: %s", TLSProfileKey, err.Error()))
|
|
|
|
}
|
|
|
|
switch sval {
|
2020-01-14 17:36:07 +00:00
|
|
|
case TLSProfileModern, TLSProfileIntermediate:
|
2017-03-28 04:21:48 +00:00
|
|
|
default:
|
2020-09-10 16:31:01 +00:00
|
|
|
panic(fmt.Sprintf("%s must be one of %s or %s", TLSProfileKey,
|
|
|
|
TLSProfileModern, TLSProfileIntermediate))
|
2017-03-28 04:21:48 +00:00
|
|
|
}
|
|
|
|
return sval
|
|
|
|
}
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// addConfigInt adds a int config to the config options
|
2017-01-31 18:06:30 +00:00
|
|
|
func (man Manager) addConfigInt(key string, defVal int, usage string) {
|
|
|
|
man.command.PersistentFlags().Int(flagNameFromConfigKey(key), defVal, getFlagUsage(key, usage))
|
2016-09-12 17:26:56 +00:00
|
|
|
man.viper.BindPFlag(key, man.command.PersistentFlags().Lookup(flagNameFromConfigKey(key)))
|
|
|
|
man.viper.BindEnv(key, envNameFromConfigKey(key))
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// Add default
|
|
|
|
man.addDefault(key, defVal)
|
|
|
|
}
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// getConfigInt retrieves a int from the loaded config
|
|
|
|
func (man Manager) getConfigInt(key string) int {
|
|
|
|
interfaceVal := man.getInterfaceVal(key)
|
|
|
|
intVal, err := cast.ToIntE(interfaceVal)
|
|
|
|
if err != nil {
|
|
|
|
panic("Unable to cast to int for key " + key + ": " + err.Error())
|
|
|
|
}
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
return intVal
|
|
|
|
}
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// addConfigBool adds a bool config to the config options
|
2017-01-31 18:06:30 +00:00
|
|
|
func (man Manager) addConfigBool(key string, defVal bool, usage string) {
|
|
|
|
man.command.PersistentFlags().Bool(flagNameFromConfigKey(key), defVal, getFlagUsage(key, usage))
|
2016-09-12 17:26:56 +00:00
|
|
|
man.viper.BindPFlag(key, man.command.PersistentFlags().Lookup(flagNameFromConfigKey(key)))
|
|
|
|
man.viper.BindEnv(key, envNameFromConfigKey(key))
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// Add default
|
|
|
|
man.addDefault(key, defVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConfigBool retrieves a bool from the loaded config
|
|
|
|
func (man Manager) getConfigBool(key string) bool {
|
|
|
|
interfaceVal := man.getInterfaceVal(key)
|
|
|
|
boolVal, err := cast.ToBoolE(interfaceVal)
|
|
|
|
if err != nil {
|
|
|
|
panic("Unable to cast to bool for key " + key + ": " + err.Error())
|
|
|
|
}
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
return boolVal
|
|
|
|
}
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-14 16:11:06 +00:00
|
|
|
// addConfigDuration adds a duration config to the config options
|
2017-01-31 18:06:30 +00:00
|
|
|
func (man Manager) addConfigDuration(key string, defVal time.Duration, usage string) {
|
|
|
|
man.command.PersistentFlags().Duration(flagNameFromConfigKey(key), defVal, getFlagUsage(key, usage))
|
2016-09-14 16:11:06 +00:00
|
|
|
man.viper.BindPFlag(key, man.command.PersistentFlags().Lookup(flagNameFromConfigKey(key)))
|
|
|
|
man.viper.BindEnv(key, envNameFromConfigKey(key))
|
|
|
|
|
|
|
|
// Add default
|
|
|
|
man.addDefault(key, defVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConfigDuration retrieves a duration from the loaded config
|
|
|
|
func (man Manager) getConfigDuration(key string) time.Duration {
|
|
|
|
interfaceVal := man.getInterfaceVal(key)
|
|
|
|
durationVal, err := cast.ToDurationE(interfaceVal)
|
|
|
|
if err != nil {
|
|
|
|
panic("Unable to cast to duration for key " + key + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return durationVal
|
|
|
|
}
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
// loadConfigFile handles the loading of the config file.
|
|
|
|
func (man Manager) loadConfigFile() {
|
2016-09-15 15:44:05 +00:00
|
|
|
man.viper.SetConfigType("yaml")
|
|
|
|
|
2016-09-12 17:26:56 +00:00
|
|
|
configFile := man.command.PersistentFlags().Lookup("config").Value.String()
|
2016-09-03 17:25:16 +00:00
|
|
|
|
2016-09-15 15:44:05 +00:00
|
|
|
if configFile == "" {
|
|
|
|
// No config file set, only use configs from env
|
|
|
|
// vars/flags/defaults
|
|
|
|
return
|
|
|
|
}
|
2016-09-12 17:26:56 +00:00
|
|
|
|
2016-09-15 15:44:05 +00:00
|
|
|
man.viper.SetConfigFile(configFile)
|
2016-09-12 17:26:56 +00:00
|
|
|
err := man.viper.ReadInConfig()
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-23 22:59:02 +00:00
|
|
|
fmt.Println("Error loading config file:", err)
|
|
|
|
os.Exit(1)
|
2016-09-03 17:25:16 +00:00
|
|
|
}
|
2019-04-23 22:59:02 +00:00
|
|
|
|
|
|
|
fmt.Println("Using config file: ", man.viper.ConfigFileUsed())
|
2016-09-03 17:25:16 +00:00
|
|
|
}
|
2016-09-14 16:11:06 +00:00
|
|
|
|
|
|
|
// TestConfig returns a barebones configuration suitable for use in tests.
|
|
|
|
// Individual tests may want to override some of the values provided.
|
|
|
|
func TestConfig() KolideConfig {
|
|
|
|
return KolideConfig{
|
2016-09-29 02:44:05 +00:00
|
|
|
App: AppConfig{
|
2017-04-12 00:13:38 +00:00
|
|
|
TokenKeySize: 24,
|
2016-09-29 02:44:05 +00:00
|
|
|
InviteTokenValidityPeriod: 5 * 24 * time.Hour,
|
|
|
|
},
|
2016-09-14 16:11:06 +00:00
|
|
|
Auth: AuthConfig{
|
|
|
|
JwtKey: "CHANGEME",
|
|
|
|
BcryptCost: 6, // Low cost keeps tests fast
|
|
|
|
SaltKeySize: 24,
|
|
|
|
},
|
|
|
|
Session: SessionConfig{
|
|
|
|
KeySize: 64,
|
|
|
|
Duration: 24 * 90 * time.Hour,
|
|
|
|
},
|
|
|
|
Osquery: OsqueryConfig{
|
2020-03-02 19:08:08 +00:00
|
|
|
NodeKeySize: 24,
|
|
|
|
StatusLogPlugin: "filesystem",
|
|
|
|
ResultLogPlugin: "filesystem",
|
|
|
|
LabelUpdateInterval: 1 * time.Hour,
|
|
|
|
DetailUpdateInterval: 1 * time.Hour,
|
2016-09-14 16:11:06 +00:00
|
|
|
},
|
|
|
|
Logging: LoggingConfig{
|
|
|
|
Debug: true,
|
|
|
|
DisableBanner: true,
|
|
|
|
},
|
2019-04-08 18:47:15 +00:00
|
|
|
Filesystem: FilesystemConfig{
|
|
|
|
StatusLogFile: "/dev/null",
|
|
|
|
ResultLogFile: "/dev/null",
|
|
|
|
},
|
2016-09-14 16:11:06 +00:00
|
|
|
}
|
|
|
|
}
|