Skip to content

Commit

Permalink
Merge pull request #3 from sorah/optional-tls
Browse files Browse the repository at this point in the history
Fix null pointer dereferecne when no TLSConfig
  • Loading branch information
rrreeeyyy authored Dec 14, 2018
2 parents a9411b3 + 750fcd0 commit aa45e33
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
33 changes: 19 additions & 14 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ func Start(args []string) {
log.Fatal("ERROR: option -config is mandatory")
}

config, err := config.LoadConfigFromYAML(options.Config)
cfg, err := config.LoadConfigFromYAML(options.Config)
if err != nil {
log.Fatal(err)
}

err = config.Validate()
err = cfg.Validate()
if err != nil {
log.Fatal(err)
}

start(config)
start(cfg)
}

func openWritableFile(path string) (*os.File, error) {
return os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
}

func start(config *config.Config) {
func start(cfg *config.Config) {
var err error
var errorLogger *log.Logger
if config.ErrorLogConfig != nil {
ef, err := openWritableFile(*config.ErrorLogConfig.Path)
if cfg.ErrorLogConfig != nil {
ef, err := openWritableFile(*cfg.ErrorLogConfig.Path)
if err != nil {
log.Fatal(err)
}
Expand All @@ -64,34 +64,34 @@ func start(config *config.Config) {
errorLogger.Printf("INFO: ExporterProxy v%s", Version)

var accessLogger accesslogger.AccessLogger
if config.AccessLogConfig != nil {
if config.AccessLogConfig.Path != nil {
af, err := openWritableFile(*config.AccessLogConfig.Path)
if cfg.AccessLogConfig != nil {
if cfg.AccessLogConfig.Path != nil {
af, err := openWritableFile(*cfg.AccessLogConfig.Path)
if err != nil {
errorLogger.Fatal(err)
}
defer af.Close()
accessLogger, err = accesslogger.New(*config.AccessLogConfig.Format, af, config.AccessLogConfig.Fields)
accessLogger, err = accesslogger.New(*cfg.AccessLogConfig.Format, af, cfg.AccessLogConfig.Fields)
if err != nil {
errorLogger.Fatal(err)
}
} else {
accessLogger, err = accesslogger.New(*config.AccessLogConfig.Format, os.Stdout, config.AccessLogConfig.Fields)
accessLogger, err = accesslogger.New(*cfg.AccessLogConfig.Format, os.Stdout, cfg.AccessLogConfig.Fields)
if err != nil {
errorLogger.Fatal(err)
}
}
}

for _, e := range config.ExporterConfigs {
for _, e := range cfg.ExporterConfigs {
proxy, err := server.NewExporterProxy(&e, accessLogger, errorLogger)
if err != nil {
errorLogger.Fatal(err)
}
http.Handle(*proxy.ServePath, proxy)
}

lsn, err := listener.Listen(*config.Listen)
lsn, err := listener.Listen(*cfg.Listen)
if err != nil {
errorLogger.Fatal(err)
}
Expand All @@ -101,7 +101,12 @@ func start(config *config.Config) {
Handler: http.DefaultServeMux,
}

err = server.ServeHTTPAndHandleSignal(lsn, *srv, *config.ShutDownTimeout, *config.TLSConfig)
var tlsConfig config.TLSConfig
if cfg.TLSConfig != nil {
tlsConfig = *cfg.TLSConfig
}

err = server.ServeHTTPAndHandleSignal(lsn, *srv, *cfg.ShutDownTimeout, tlsConfig)
if err != nil {
errorLogger.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package cli

const Version = "0.2.0"
const Version = "0.3.0"
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func ServeHTTPAndHandleSignal(listener net.Listener, server http.Server, timeout time.Duration, tlsconfig config.TLSConfig) error {
if &tlsconfig != nil {
if tlsconfig.CertFile != nil && tlsconfig.KeyFile != nil {
go func() {
server.ServeTLS(listener, *tlsconfig.CertFile, *tlsconfig.KeyFile)
}()
Expand Down

0 comments on commit aa45e33

Please sign in to comment.