Skip to content

Commit

Permalink
Fix the loading of the TLS files
Browse files Browse the repository at this point in the history
Don't error if the file is not specified.
Fixes #36.
  • Loading branch information
Christoph Petrausch authored and arnisoph committed Jul 9, 2020
1 parent 1643f46 commit b09881a
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func fileExists(filename string) bool {
return !info.IsDir()
}

// An error is returned of the given TLS configuration is invalid.
func validateTLSFiles(argCafile, argKey, argCert string) error {
if len(argCafile) > 0 {
if !fileExists(argCafile) {
Expand Down Expand Up @@ -109,6 +110,18 @@ func validateTLSFiles(argCafile, argKey, argCert string) error {
return nil
}

// loadTLSFile loads the given file. If the filename is empty neither data nor an error is returned.
func loadTLSFile(fileName string) ([]byte, error) {
if len(fileName) > 0 {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("failed to load TLS file: %s: %w", fileName, err)
}
return data, nil
}
return nil, nil
}

func main() {
flag.Parse()

Expand Down Expand Up @@ -185,23 +198,22 @@ func main() {
if err := validateTLSFiles(*argCafile, *argKey, *argCert); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
} else {
var err error
ca, err = ioutil.ReadFile(*argCafile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
cert, err = ioutil.ReadFile(*argCert)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
key, err = ioutil.ReadFile(*argKey)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

ca, err = loadTLSFile(*argCafile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
cert, err = loadTLSFile(*argCert)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
key, err = loadTLSFile(*argKey)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

signalChan := make(chan os.Signal, 1)
Expand Down

0 comments on commit b09881a

Please sign in to comment.