forked from operator-framework/operator-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertutil.go
59 lines (51 loc) · 1.35 KB
/
certutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package http
import (
"crypto/x509"
"fmt"
"os"
"path/filepath"
"github.com/go-logr/logr"
)
func NewCertPool(caDir string, log logr.Logger) (*x509.CertPool, error) {
// Note that this already looks at SSL_CERT_DIR and SSL_CERT_FILE
// So, we don't explicitly load certs from those locations
caCertPool, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
if caDir == "" {
return caCertPool, nil
}
dirEntries, err := os.ReadDir(caDir)
if err != nil {
return nil, err
}
count := 0
for _, e := range dirEntries {
file := filepath.Join(caDir, e.Name())
// These might be symlinks pointing to directories, so use Stat() to resolve
fi, err := os.Stat(file)
if err != nil {
return nil, err
}
if fi.IsDir() {
log.V(defaultLogLevel).Info("skip directory", "name", e.Name())
continue
}
log.V(defaultLogLevel).Info("load certificate", "name", e.Name(), "size", fi.Size(), "modtime", fi.ModTime())
data, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("error reading cert file %q: %w", file, err)
}
// The return indicates if any certs were added
if caCertPool.AppendCertsFromPEM(data) {
count++
}
logPem(data, e.Name(), caDir, "loading certificate file", log)
}
// Found no certs!
if count == 0 {
return nil, fmt.Errorf("no certificates found in %q", caDir)
}
return caCertPool, nil
}