Skip to content

Commit 2774d78

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 2774d78

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-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{

internal/httputil/certpoolwatcher_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func TestCertPoolWatcher(t *testing.T) {
7272
t.Logf("Create cert file at %q\n", certName)
7373
createCert(t, certName)
7474

75+
// Update environment variables for the watcher - some of these should not exist
76+
os.Setenv("SSL_CERT_DIR", tmpDir+":/tmp/does-not-exist.dir")
77+
os.Setenv("SSL_CERT_FILE", "/tmp/does-not-exist.file")
78+
7579
// Create the cert pool watcher
7680
cpw, err := httputil.NewCertPoolWatcher(tmpDir, log.FromContext(context.Background()))
7781
require.NoError(t, err)

0 commit comments

Comments
 (0)