Skip to content

Commit

Permalink
Merge pull request #38 from inovex/refactor-tls-checking
Browse files Browse the repository at this point in the history
Refactor tls checking
  • Loading branch information
arnisoph authored Jul 9, 2020
2 parents 5ad0273 + 62d3a52 commit 1643f46
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,30 @@ func fileExists(filename string) bool {
return !info.IsDir()
}

func validateTLSFiles(argCafile, argKey, argCert *string) error {
if len(*argCafile) > 0 {
if !fileExists(*argCafile) {
return fmt.Errorf("CA file '%s' does not exist", *argCafile)
func validateTLSFiles(argCafile, argKey, argCert string) error {
if len(argCafile) > 0 {
if !fileExists(argCafile) {
return fmt.Errorf("CA file '%s' does not exist", argCafile)
}
}
if len(*argKey) > 0 {
if !fileExists(*argKey) {
return fmt.Errorf("key file '%s' does not exist", *argKey)
if len(argKey) > 0 {
if !fileExists(argKey) {
return fmt.Errorf("key file '%s' does not exist", argKey)
}
}
if len(*argCert) > 0 {
if !fileExists(*argCert) {
return fmt.Errorf("cert file '%s' does not exist", *argCert)
if len(argCert) > 0 {
if !fileExists(argCert) {
return fmt.Errorf("cert file '%s' does not exist", argCert)
}
}

if len(argKey) > 0 && len(argCert) < 1 {
return fmt.Errorf("A key file is specified but no certificate file")
}

if len(argKey) < 1 && len(argCert) > 0 {
return fmt.Errorf("A cert file is specified but no key file")
}
return nil
}

Expand Down Expand Up @@ -175,7 +182,7 @@ func main() {
subscriberQoS = lvl
}
var ca, cert, key []byte
if err := validateTLSFiles(argCafile, argKey, argCert); err != nil {
if err := validateTLSFiles(*argCafile, *argKey, *argCert); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
} else {
Expand Down

0 comments on commit 1643f46

Please sign in to comment.