Skip to content

Commit

Permalink
moved tls config parsing from Unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
lordvidex committed Aug 2, 2023
1 parent 9b6b10b commit 88bc082
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
81 changes: 42 additions & 39 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,47 @@ func ReadConfig(filename string, noLog bool) (*Config, error) {
return Unmarshal(body, noLog)
}

// Parse should be called to correctly set parameters in the config.
func (cfg *Config) Parse() error {
err := zapwriter.ApplyConfig(cfg.Logging)
if err != nil {
return err
}

if cfg.Common.FindCache, err = CreateCache("index", &cfg.Common.FindCacheConfig); err == nil {
if cfg.Common.FindCacheConfig.Type != "null" {
logger := zapwriter.Logger("config")
logger.Info("enable find cache", zap.String("type", cfg.Common.FindCacheConfig.Type))
}
} else {
return err
}

chURL, err := clickhouseURLValidate(cfg.ClickHouse.URL)
if err != nil {
return err
}
if cfg.ClickHouse.TLSParams != nil {
tlsConfig, warns, err := config.ParseClientTLSConfig(cfg.ClickHouse.TLSParams)
if err != nil {
return err
}
if chURL.Scheme == "https" {
cfg.ClickHouse.TLSConfig = tlsConfig
} else {
warns = append(warns, "TLS configurations is ignored because scheme is not HTTPS")
}
if len(warns) > 0 {
logger := zapwriter.Logger("clickhouse tlsconfig")
logger.Warn(
"insecure options detected while parsing HTTP Client TLS config for ClickHouse",
zap.Strings("warnings", warns),
)
}
}
return nil
}

// Unmarshal process the body to *Config
func Unmarshal(body []byte, noLog bool) (*Config, error) {
var err error
Expand Down Expand Up @@ -479,37 +520,10 @@ func Unmarshal(body []byte, noLog bool) (*Config, error) {
if err = zapwriter.CheckConfig(cfg.Logging, nil); err != nil {
return nil, err
}
localManager, err := zapwriter.NewManager(cfg.Logging)
if err != nil {
return nil, err
}

if cfg.ClickHouse.RenderMaxConcurrent > cfg.ClickHouse.RenderMaxQueries && cfg.ClickHouse.RenderMaxQueries > 0 {
cfg.ClickHouse.RenderMaxConcurrent = 0
}
chURL, err := clickhouseURLValidate(cfg.ClickHouse.URL)
if err != nil {
return nil, err
}
if cfg.ClickHouse.TLSParams != nil {
tlsConfig, warns, err := config.ParseClientTLSConfig(cfg.ClickHouse.TLSParams)
if err != nil {
return nil, err
}
if chURL.Scheme == "https" {
cfg.ClickHouse.TLSConfig = tlsConfig
} else {
warns = append(warns, "TLS configurations is ignored because scheme is not HTTPS")
}
if len(warns) > 0 && !noLog {
logger := localManager.Logger("clickhouse tlsconfig")
logger.Warn(
"insecure options detected while parsing HTTP Client TLS config for ClickHouse",
zap.Strings("warnings", warns),
)
}
}

for i := range cfg.ClickHouse.QueryParams {
if cfg.ClickHouse.QueryParams[i].MaxConcurrent > cfg.ClickHouse.QueryParams[i].MaxQueries &&
cfg.ClickHouse.QueryParams[i].MaxQueries > 0 {
Expand Down Expand Up @@ -552,17 +566,6 @@ func Unmarshal(body []byte, noLog bool) (*Config, error) {
return nil, err
}

if cfg.Common.FindCache, err = CreateCache("index", &cfg.Common.FindCacheConfig); err == nil {
if cfg.Common.FindCacheConfig.Type != "null" {
if !noLog {
logger := localManager.Logger("config")
logger.Info("enable find cache", zap.String("type", cfg.Common.FindCacheConfig.Type))
}
}
} else {
return nil, err
}

l := len(cfg.Common.TargetBlacklist)
if l > 0 {
cfg.Common.Blacklist = make([]*regexp.Regexp, l)
Expand Down Expand Up @@ -605,7 +608,7 @@ func Unmarshal(body []byte, noLog bool) (*Config, error) {
if noLog {
fmt.Fprintf(os.Stderr, "config deprecation %s: %s\n", name, message)
} else {
logger := localManager.Logger("config deprecation")
logger := zapwriter.Logger("config deprecation")
logger.Error(name, zap.Error(message))
}
}
Expand Down
8 changes: 2 additions & 6 deletions graphite-clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,11 @@ func main() {
return
}

if err = zapwriter.ApplyConfig(cfg.Logging); err != nil {
if err = cfg.Parse(); err != nil {
log.Fatal(err)
}

localManager, err := zapwriter.NewManager(cfg.Logging)
if err != nil {
log.Fatal(err)
}
logger := localManager.Logger("start")
logger := zapwriter.Logger("start")
if *verbose {
logger.Info("starting graphite-clickhouse",
zap.String("build_version", BuildVersion),
Expand Down

0 comments on commit 88bc082

Please sign in to comment.