Skip to content

Commit

Permalink
Add support for SSL env vars to cert pool watcher
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tmshort committed Jan 30, 2025
1 parent 10e2754 commit 2ef1d2e
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions internal/httputil/certpoolwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/x509"
"fmt"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -44,8 +45,38 @@ func NewCertPoolWatcher(caDir string, log logr.Logger) (*CertPoolWatcher, error)
if err != nil {
return nil, err
}
if err = watcher.Add(caDir); err != nil {
return nil, err

if caDir != "" {
// only watch if we can Stat() it
if _, err := os.Stat(caDir); err == nil {
if err = watcher.Add(caDir); err != nil {
return nil, err
}
}
}

// If the SSL_CERT_DIR or SSL_CERT_FILE environment variables are
// specified, this means that we have some control over the system root
// location, thus they may change, thus we should watch those locations.
if d := os.Getenv("SSL_CERT_DIR"); d != "" {
dirs := strings.Split(d, ":")
for _, dir := range dirs {
// only watch if we can Stat() it
if _, err := os.Stat(dir); err == nil {
if err = watcher.Add(dir); err != nil {
return nil, err
}
}
}
}

if f := os.Getenv("SSL_CERT_FILE"); f != "" {
// only watch if we can Stat() it
if _, err := os.Stat(f); err == nil {
if err = watcher.Add(f); err != nil {
return nil, err
}
}
}

cpw := &CertPoolWatcher{
Expand Down

0 comments on commit 2ef1d2e

Please sign in to comment.