Skip to content

Commit 2ef1d2e

Browse files
committed
Add support for SSL env vars to cert pool watcher
The SystemRoot store looks at the SSL_CERT_DIR and SSL_CERT_FILE environment variables for certificate locations. Because these variables are under control of the user, we should assume that the user wants to control the contents of the SystemRoot, and subsequently that those contents could change (as compared to certs located in the default /etc/pki location). Thus, we should watch those locations if they exist. Signed-off-by: Todd Short <tshort@redhat.com>
1 parent 10e2754 commit 2ef1d2e

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

internal/httputil/certpoolwatcher.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/x509"
55
"fmt"
66
"os"
7+
"strings"
78
"sync"
89
"time"
910

@@ -44,8 +45,38 @@ func NewCertPoolWatcher(caDir string, log logr.Logger) (*CertPoolWatcher, error)
4445
if err != nil {
4546
return nil, err
4647
}
47-
if err = watcher.Add(caDir); err != nil {
48-
return nil, err
48+
49+
if caDir != "" {
50+
// only watch if we can Stat() it
51+
if _, err := os.Stat(caDir); err == nil {
52+
if err = watcher.Add(caDir); err != nil {
53+
return nil, err
54+
}
55+
}
56+
}
57+
58+
// If the SSL_CERT_DIR or SSL_CERT_FILE environment variables are
59+
// specified, this means that we have some control over the system root
60+
// location, thus they may change, thus we should watch those locations.
61+
if d := os.Getenv("SSL_CERT_DIR"); d != "" {
62+
dirs := strings.Split(d, ":")
63+
for _, dir := range dirs {
64+
// only watch if we can Stat() it
65+
if _, err := os.Stat(dir); err == nil {
66+
if err = watcher.Add(dir); err != nil {
67+
return nil, err
68+
}
69+
}
70+
}
71+
}
72+
73+
if f := os.Getenv("SSL_CERT_FILE"); f != "" {
74+
// only watch if we can Stat() it
75+
if _, err := os.Stat(f); err == nil {
76+
if err = watcher.Add(f); err != nil {
77+
return nil, err
78+
}
79+
}
4980
}
5081

5182
cpw := &CertPoolWatcher{

0 commit comments

Comments
 (0)