If CA certificate file is incorrect return an error instead of use default syustem CA store

This commit is contained in:
Héctor Hurtado
2020-01-24 11:54:06 +01:00
parent cddc5ff351
commit f103b39a94
+7 -3
View File
@@ -19,6 +19,7 @@ package user
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
@@ -47,7 +48,7 @@ func Run(bindAddr, certFile, keyFile, cliCaFile string, cliAuth bool) {
var err error
Server.TLSConfig.ClientCAs, err = loadCertificatesFromFile(cliCaFile)
if err != nil {
log.Printf("UserServer failed to load CA certs: %s\nDefault to system CA store.", err)
log.Fatalf("UserServer failed to load CA certs: %s\n", err)
} else {
CAStore := "System store"
if Server.TLSConfig.ClientCAs != nil {
@@ -70,10 +71,13 @@ func Run(bindAddr, certFile, keyFile, cliCaFile string, cliAuth bool) {
func loadCertificatesFromFile(certFile string) (pool *x509.CertPool, err error) {
if certFile != "" {
caCerts, err := ioutil.ReadFile(certFile)
var caCerts []byte
caCerts, err = ioutil.ReadFile(certFile)
if err == nil {
pool = x509.NewCertPool()
pool.AppendCertsFromPEM(caCerts)
if !pool.AppendCertsFromPEM(caCerts) {
err = fmt.Errorf("Invalid certificate file %s", certFile)
}
}
}