Skip to content

Commit 468c718

Browse files
authored
feat: detect host ca bundle (#2168)
1 parent ba5a81b commit 468c718

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

cmd/installer/cli/ca.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cli
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
)
8+
9+
// findHostCABundle locates the system CA certificate bundle on the host.
10+
// It first checks the SSL_CERT_FILE environment variable, then searches
11+
// common file paths used by various Linux distributions.
12+
//
13+
// The search follows the same order as the Go standard library's crypto/x509 package.
14+
//
15+
// Returns the path to the first found CA certificate bundle and nil error on success.
16+
// Returns an empty string and error if SSL_CERT_FILE is set but inaccessible
17+
// or if no CA certificate bundle is found.
18+
func findHostCABundle() (string, error) {
19+
// First check if SSL_CERT_FILE environment variable is set
20+
if envFile := os.Getenv("SSL_CERT_FILE"); envFile != "" {
21+
if _, err := os.Stat(envFile); err != nil {
22+
return "", fmt.Errorf("SSL_CERT_FILE set to %s but file cannot be accessed: %w", envFile, err)
23+
}
24+
return envFile, nil
25+
}
26+
27+
// From https://github.com/golang/go/blob/go1.24.3/src/crypto/x509/root_linux.go
28+
certFiles := []string{
29+
"/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
30+
"/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL 6
31+
"/etc/ssl/ca-bundle.pem", // OpenSUSE
32+
"/etc/pki/tls/cacert.pem", // OpenELEC
33+
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // CentOS/RHEL 7
34+
"/etc/ssl/cert.pem", // Alpine Linux
35+
}
36+
37+
// Check each file in the order of preference returning the first found
38+
for _, file := range certFiles {
39+
if _, err := os.Stat(file); err == nil {
40+
return file, nil
41+
}
42+
}
43+
44+
return "", errors.New("no CA certificate file found")
45+
}

cmd/installer/cli/install.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ func preRunInstall(cmd *cobra.Command, flags *InstallCmdFlags) error {
259259
}
260260

261261
func runInstall(ctx context.Context, name string, flags InstallCmdFlags, metricsReporter preflights.MetricsReporter) error {
262+
hostCABundle, err := findHostCABundle()
263+
if err != nil {
264+
return fmt.Errorf("unable to find host CA bundle: %w", err)
265+
}
266+
logrus.Debugf("using host CA bundle: %s", hostCABundle)
267+
262268
if err := runInstallVerifyAndPrompt(ctx, name, &flags); err != nil {
263269
return err
264270
}
@@ -348,6 +354,7 @@ func runInstall(ctx context.Context, name string, flags InstallCmdFlags, metrics
348354
License: flags.license,
349355
IsAirgap: flags.airgapBundle != "",
350356
Proxy: flags.proxy,
357+
HostCABundle: hostCABundle,
351358
PrivateCAs: flags.privateCAs,
352359
ServiceCIDR: flags.cidrCfg.ServiceCIDR,
353360
DisasterRecoveryEnabled: flags.license.Spec.IsDisasterRecoverySupported,

cmd/installer/cli/restore.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ func runRestoreStepNew(ctx context.Context, name string, flags InstallCmdFlags,
364364
}
365365
}
366366

367+
hostCABundle, err := findHostCABundle()
368+
if err != nil {
369+
return fmt.Errorf("unable to find host CA bundle: %w", err)
370+
}
371+
logrus.Debugf("using host CA bundle: %s", hostCABundle)
372+
367373
logrus.Debugf("configuring sysctl")
368374
if err := configutils.ConfigureSysctl(); err != nil {
369375
logrus.Debugf("unable to configure sysctl: %v", err)
@@ -437,6 +443,7 @@ func runRestoreStepNew(ctx context.Context, name string, flags InstallCmdFlags,
437443
if err := addons.Install(ctx, hcli, addons.InstallOptions{
438444
IsAirgap: flags.airgapBundle != "",
439445
Proxy: flags.proxy,
446+
HostCABundle: hostCABundle,
440447
PrivateCAs: flags.privateCAs,
441448
ServiceCIDR: flags.cidrCfg.ServiceCIDR,
442449
IsRestore: true,

pkg/addons/install.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type InstallOptions struct {
2323
License *kotsv1beta1.License
2424
IsAirgap bool
2525
Proxy *ecv1beta1.ProxySpec
26+
HostCABundle string
2627
PrivateCAs []string
2728
ServiceCIDR string
2829
DisasterRecoveryEnabled bool

0 commit comments

Comments
 (0)