Skip to content

Commit bd9b011

Browse files
committed
UPSTREAM: <drop>: Add support for SSL env vars to cert pool watcher
1 parent eaa199e commit bd9b011

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

internal/httputil/certpoolwatcher.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"crypto/x509"
55
"fmt"
66
"os"
7+
"slices"
8+
"strings"
79
"sync"
810
"time"
911

@@ -44,8 +46,25 @@ func NewCertPoolWatcher(caDir string, log logr.Logger) (*CertPoolWatcher, error)
4446
if err != nil {
4547
return nil, err
4648
}
47-
if err = watcher.Add(caDir); err != nil {
48-
return nil, err
49+
// If the SSL_CERT_DIR or SSL_CERT_FILE environment variables are
50+
// specified, this means that we have some control over the system root
51+
// location, thus they may change, thus we should watch those locations.
52+
watchPaths := strings.Split(os.Getenv("SSL_CERT_DIR"), ":")
53+
watchPaths = append(watchPaths, caDir, os.Getenv("SSL_CERT_FILE"))
54+
watchPaths = slices.DeleteFunc(watchPaths, func(p string) bool {
55+
if p == "" {
56+
return true
57+
}
58+
if _, err := os.Stat(p); err != nil {
59+
return true
60+
}
61+
return false
62+
})
63+
64+
for _, p := range watchPaths {
65+
if err := watcher.Add(p); err != nil {
66+
return nil, err
67+
}
4968
}
5069

5170
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)