Skip to content

Commit fedc158

Browse files
Merge pull request #4868 from pacevedom/snyk-fix
[release-4.19] USHIFT-5711: Address snyk errors
2 parents 0fb7eea + c7d6664 commit fedc158

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

pkg/cmd/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ func cleanupStaleKubeconfigs(cfg *config.Config, path string) error {
611611
}
612612
}
613613
for _, deletePath := range deleteDirs {
614-
if err := os.RemoveAll(deletePath); err != nil {
614+
if err := os.RemoveAll(filepath.Clean(deletePath)); err != nil {
615615
klog.Warningf("Unable to remove %s: %v", deletePath, err)
616616
}
617617
klog.Infof("Removed stale kubeconfig %s", deletePath)

pkg/controllers/kube-controller-manager.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ func configure(ctx context.Context, cfg *config.Config) (args []string, applyFn
9898
"v": {strconv.Itoa(cfg.GetVerbosity())},
9999
"tls-cipher-suites": {strings.Join(cfg.ApiServer.TLS.CipherSuites, ",")},
100100
"tls-min-version": {cfg.ApiServer.TLS.MinVersion},
101+
// Use the same certificates as the apiserver for localhost communication
102+
// to avoid creating new certificates just for this component having the same CN/SAN.
103+
"tls-cert-file": {cryptomaterial.ServingCertPath(cryptomaterial.KubeAPIServerLocalhostServingCertDir(cryptomaterial.CertsDirectory(config.DataDir)))},
104+
"tls-private-key-file": {cryptomaterial.ServingKeyPath(cryptomaterial.KubeAPIServerLocalhostServingCertDir(cryptomaterial.CertsDirectory(config.DataDir)))},
101105
},
102106
}
103107

@@ -122,7 +126,7 @@ func (s *KubeControllerManager) Run(ctx context.Context, ready chan<- struct{},
122126
// run readiness check
123127
go func() {
124128
// This endpoint uses a self-signed certificate on purpose, we need to skip verification.
125-
healthcheckStatus := util.RetryInsecureGet(ctx, "https://localhost:10257/healthz")
129+
healthcheckStatus := util.RetryGet(ctx, "https://localhost:10257/healthz", cryptomaterial.CACertPath(cryptomaterial.KubeAPIServerLocalhostSigner(cryptomaterial.CertsDirectory(config.DataDir))))
126130
if healthcheckStatus != 200 {
127131
klog.Errorf("kube-controller-manager failed to start")
128132
errorChannel <- errors.New("kube-controller-manager failed to start")

pkg/controllers/kube-controller-manager_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/google/go-cmp/cmp"
2929
embedded "github.com/openshift/microshift/assets"
3030
"github.com/openshift/microshift/pkg/config"
31+
"github.com/openshift/microshift/pkg/util/cryptomaterial"
3132
)
3233

3334
func TestKCMDefaultConfigAsset(t *testing.T) {
@@ -72,8 +73,10 @@ func TestConfigure(t *testing.T) {
7273
"--secure-port=10257",
7374
fmt.Sprintf("--service-account-private-key-file=%s", kcmServiceAccountPrivateKeyFile()),
7475
fmt.Sprintf("--service-cluster-ip-range=%s", cfg.Network.ServiceNetwork[0]),
76+
fmt.Sprintf("--tls-cert-file=%s", cryptomaterial.ServingCertPath(cryptomaterial.KubeAPIServerLocalhostServingCertDir(cryptomaterial.CertsDirectory(config.DataDir)))),
7577
fmt.Sprintf("--tls-cipher-suites=%s", strings.Join(crypto.OpenSSLToIANACipherSuites(fixedTLSProfile.Ciphers), ",")),
7678
fmt.Sprintf("--tls-min-version=%s", string(fixedTLSProfile.MinTLSVersion)),
79+
fmt.Sprintf("--tls-private-key-file=%s", cryptomaterial.ServingKeyPath(cryptomaterial.KubeAPIServerLocalhostServingCertDir(cryptomaterial.CertsDirectory(config.DataDir)))),
7780
"--use-service-account-credentials=true",
7881
"-v=2",
7982
}

pkg/controllers/kube-scheduler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/openshift/microshift/pkg/config"
2626
"github.com/openshift/microshift/pkg/util"
27+
"github.com/openshift/microshift/pkg/util/cryptomaterial"
2728

2829
klog "k8s.io/klog/v2"
2930
kubescheduler "k8s.io/kubernetes/cmd/kube-scheduler/app"
@@ -55,6 +56,10 @@ func (s *KubeScheduler) configure(cfg *config.Config) {
5556
s.options.Authorization.RemoteKubeConfigFile = cfg.KubeConfigPath(config.KubeScheduler)
5657
s.options.SecureServing.MinTLSVersion = cfg.ApiServer.TLS.MinVersion
5758
s.options.SecureServing.CipherSuites = cfg.ApiServer.TLS.CipherSuites
59+
// Use the same certificates as the apiserver for localhost communication
60+
// to avoid creating new certificates just for this component having the same CN/SAN.
61+
s.options.SecureServing.ServerCert.CertKey.CertFile = cryptomaterial.ServingCertPath(cryptomaterial.KubeAPIServerLocalhostServingCertDir(cryptomaterial.CertsDirectory(config.DataDir)))
62+
s.options.SecureServing.ServerCert.CertKey.KeyFile = cryptomaterial.ServingKeyPath(cryptomaterial.KubeAPIServerLocalhostServingCertDir(cryptomaterial.CertsDirectory(config.DataDir)))
5863
s.kubeconfig = cfg.KubeConfigPath(config.KubeScheduler)
5964
}
6065

@@ -80,7 +85,7 @@ func (s *KubeScheduler) Run(ctx context.Context, ready chan<- struct{}, stopped
8085
// run readiness check
8186
go func() {
8287
// This endpoint uses a self-signed certificate on purpose, we need to skip verification.
83-
healthcheckStatus := util.RetryInsecureGet(ctx, "https://localhost:10259/healthz")
88+
healthcheckStatus := util.RetryGet(ctx, "https://localhost:10259/healthz", cryptomaterial.CACertPath(cryptomaterial.KubeAPIServerLocalhostSigner(cryptomaterial.CertsDirectory(config.DataDir))))
8489
if healthcheckStatus != 200 {
8590
klog.Errorf("%s healthcheck failed due to kube-scheduler failure to start", s.Name())
8691
errorChannel <- errors.New("kube-scheduler healthcheck failed")

pkg/util/net.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package util
1818
import (
1919
"context"
2020
"crypto/tls"
21+
"crypto/x509"
2122
"fmt"
2223
tcpnet "net"
2324
"net/http"
@@ -83,12 +84,34 @@ found:
8384
}
8485

8586
func RetryInsecureGet(ctx context.Context, url string) int {
87+
return RetryGet(ctx, url, "")
88+
}
89+
90+
func RetryGet(ctx context.Context, url, additionalCAPath string) int {
91+
rootCAs, err := x509.SystemCertPool()
92+
if err != nil {
93+
klog.Infof("Warning: Failed to load system CA certificates: %v. Creating an empty pool.", err)
94+
rootCAs = x509.NewCertPool()
95+
}
96+
if additionalCAPath != "" {
97+
caCert, err := os.ReadFile(additionalCAPath)
98+
if err != nil {
99+
klog.Errorf("failed to read CA certificate %s: %v", additionalCAPath, err)
100+
return 0
101+
}
102+
103+
if !rootCAs.AppendCertsFromPEM(caCert) {
104+
klog.Errorf("failed to append CA certificate %s to pool", additionalCAPath)
105+
return 0
106+
}
107+
}
86108
status := 0
87-
err := wait.PollUntilContextTimeout(ctx, 5*time.Second, 120*time.Second, false, func(ctx context.Context) (bool, error) {
109+
err = wait.PollUntilContextTimeout(ctx, 5*time.Second, 120*time.Second, false, func(ctx context.Context) (bool, error) {
88110
c := http.Client{
89111
Transport: &http.Transport{
90112
TLSClientConfig: &tls.Config{
91-
InsecureSkipVerify: true, //nolint:gosec
113+
RootCAs: rootCAs,
114+
MinVersion: tls.VersionTLS12,
92115
},
93116
},
94117
}

0 commit comments

Comments
 (0)