Skip to content

Commit 2769128

Browse files
committed
Add support for self-singed CA
1 parent bd96550 commit 2769128

9 files changed

Lines changed: 679 additions & 2 deletions

File tree

charts/stackrox-mcp/templates/_helpers.tpl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,21 @@ TLS Secret name - returns existingSecretName if set, otherwise generates name
8080
{{- include "stackrox-mcp.fullname" . }}-tls
8181
{{- end }}
8282
{{- end }}
83+
84+
{{/*
85+
Central CA Secret name - returns existingSecretName if set, otherwise generates name
86+
*/}}
87+
{{- define "stackrox-mcp.centralCASecretName" -}}
88+
{{- if .Values.centralCACert.existingSecretName }}
89+
{{- .Values.centralCACert.existingSecretName }}
90+
{{- else }}
91+
{{- include "stackrox-mcp.fullname" . }}-central-ca
92+
{{- end }}
93+
{{- end }}
94+
95+
{{/*
96+
Central CA enabled - returns "true" if either cert or existingSecretName is set
97+
*/}}
98+
{{- define "stackrox-mcp.centralCAEnabled" -}}
99+
{{- if or .Values.centralCACert.cert .Values.centralCACert.existingSecretName }}true{{- end }}
100+
{{- end }}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{- if and .Values.centralCACert.cert .Values.centralCACert.existingSecretName }}
2+
{{- fail "centralCACert: cannot set both 'cert' and 'existingSecretName' — use one or the other" }}
3+
{{- end }}
4+
{{- if and .Values.centralCACert.cert (not .Values.centralCACert.existingSecretName) }}
5+
apiVersion: v1
6+
kind: Secret
7+
metadata:
8+
name: {{ include "stackrox-mcp.fullname" . }}-central-ca
9+
labels:
10+
{{- include "stackrox-mcp.labels" . | nindent 4 }}
11+
type: Opaque
12+
data:
13+
ca.crt: {{ .Values.centralCACert.cert | b64enc }}
14+
{{- end }}

charts/stackrox-mcp/templates/configmap.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ data:
1313
auth_type: "passthrough"
1414
insecure_skip_tls_verify: {{ .Values.config.central.insecureSkipTLSVerify }}
1515
force_http1: {{ .Values.config.central.forceHTTP1 }}
16+
{{- if include "stackrox-mcp.centralCAEnabled" . }}
17+
ca_cert_path: "/central-ca/ca.crt"
18+
{{- end }}
1619
request_timeout: {{ .Values.config.central.requestTimeout | quote }}
1720
max_retries: {{ .Values.config.central.maxRetries }}
1821
initial_backoff: {{ .Values.config.central.initialBackoff | quote }}

charts/stackrox-mcp/templates/deployment.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ spec:
111111
mountPath: /certs
112112
readOnly: true
113113
{{- end }}
114+
{{- if include "stackrox-mcp.centralCAEnabled" . }}
115+
- name: central-ca
116+
mountPath: /central-ca
117+
readOnly: true
118+
{{- end }}
114119
volumes:
115120
- name: config
116121
configMap:
@@ -121,6 +126,12 @@ spec:
121126
secretName: {{ include "stackrox-mcp.tlsSecretName" . }}
122127
defaultMode: 0440
123128
{{- end }}
129+
{{- if include "stackrox-mcp.centralCAEnabled" . }}
130+
- name: central-ca
131+
secret:
132+
secretName: {{ include "stackrox-mcp.centralCASecretName" . }}
133+
defaultMode: 0440
134+
{{- end }}
124135
{{- with .Values.nodeSelector }}
125136
nodeSelector:
126137
{{- toYaml . | nindent 8 }}

charts/stackrox-mcp/values.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ tlsSecret:
6969
# Server TLS Private Key (PEM format)
7070
key: ""
7171

72+
# CA certificate for verifying Central's TLS certificate (e.g., self-signed)
73+
# Only one of cert or existingSecretName should be set.
74+
centralCACert:
75+
existingSecretName: ""
76+
cert: ""
77+
7278
# Resource limits and requests
7379
resources:
7480
limits:

internal/client/client.go

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ package client
44
import (
55
"context"
66
"crypto/tls"
7+
"crypto/x509"
8+
"encoding/pem"
79
"fmt"
10+
"log/slog"
11+
"os"
812
"sync"
913
"testing"
1014
"time"
@@ -23,6 +27,7 @@ import (
2327
const (
2428
minConnectTimeout = 5 * time.Second
2529
backoffJitter = 0.2
30+
maxCACertFileSize = 1 << 20 // 1MB
2631
)
2732

2833
// Client provides gRPC connection to StackRox Central API.
@@ -229,11 +234,112 @@ func (c *Client) tlsConfig() (*tls.Config, error) {
229234
return nil, errors.Wrap(err, "failed to get central URL hostname")
230235
}
231236

232-
return &tls.Config{
237+
tlsCfg := &tls.Config{
233238
InsecureSkipVerify: c.config.InsecureSkipTLSVerify, //nolint:gosec
234239
MinVersion: tls.VersionTLS12,
235240
ServerName: hostname,
236-
}, nil
241+
}
242+
243+
if c.config.CACertPath != "" {
244+
certPool, err := loadCACertPool(c.config.CACertPath)
245+
if err != nil {
246+
return nil, err
247+
}
248+
249+
tlsCfg.RootCAs = certPool
250+
}
251+
252+
return tlsCfg, nil
253+
}
254+
255+
func loadCACertPool(caCertPath string) (*x509.CertPool, error) {
256+
// File size guard
257+
fileInfo, err := os.Stat(caCertPath)
258+
if err != nil {
259+
return nil, errors.Wrapf(err, "failed to access CA certificate at %s", caCertPath)
260+
}
261+
262+
if !fileInfo.Mode().IsRegular() {
263+
return nil, errors.Errorf("CA certificate path %s is not a regular file", caCertPath)
264+
}
265+
266+
if fileInfo.Size() == 0 {
267+
return nil, errors.Errorf("CA certificate file %s is empty", caCertPath)
268+
}
269+
270+
if fileInfo.Size() > maxCACertFileSize {
271+
return nil, errors.Errorf(
272+
"CA certificate file %s is too large (%d bytes, max %d)",
273+
caCertPath, fileInfo.Size(),
274+
maxCACertFileSize,
275+
)
276+
}
277+
278+
//nolint: gosec
279+
caCert, err := os.ReadFile(caCertPath)
280+
if err != nil {
281+
return nil, errors.Wrapf(err, "failed to read CA certificate from %s", caCertPath)
282+
}
283+
284+
// Get system cert pool, warn on fallback
285+
certPool, err := x509.SystemCertPool()
286+
if err != nil {
287+
slog.Warn("Failed to load system CA pool, using custom CA only", "error", err)
288+
289+
certPool = x509.NewCertPool()
290+
}
291+
292+
if !certPool.AppendCertsFromPEM(caCert) {
293+
return nil, errors.Errorf("failed to parse CA certificate from %s: no valid PEM data found", caCertPath)
294+
}
295+
296+
showCertInfo(caCert)
297+
298+
return certPool, nil
299+
}
300+
301+
// showCertInfo parses and logs certificate metadata.
302+
func showCertInfo(caCert []byte) {
303+
block, _ := pem.Decode(caCert)
304+
if block == nil {
305+
slog.Warn("Unable to decode CA certificate")
306+
307+
return
308+
}
309+
310+
cert, err := x509.ParseCertificate(block.Bytes)
311+
if err != nil {
312+
slog.Warn("Failed to parse CA certificate", "error", err)
313+
314+
return
315+
}
316+
317+
slog.Info("Loaded CA certificate",
318+
"subject", cert.Subject.CommonName,
319+
"issuer", cert.Issuer.CommonName,
320+
"notAfter", cert.NotAfter,
321+
"isCA", cert.IsCA,
322+
)
323+
324+
if !cert.IsCA {
325+
slog.Warn("Provided certificate does not have the CA basic constraint set — TLS verification may fail",
326+
"subject", cert.Subject.CommonName,
327+
)
328+
}
329+
330+
if time.Now().After(cert.NotAfter) {
331+
slog.Warn("CA certificate is expired — TLS verification will fail",
332+
"subject", cert.Subject.CommonName,
333+
"expiredAt", cert.NotAfter,
334+
)
335+
}
336+
337+
if time.Now().Before(cert.NotBefore) {
338+
slog.Warn("CA certificate is not yet valid",
339+
"subject", cert.Subject.CommonName,
340+
"validFrom", cert.NotBefore,
341+
)
342+
}
237343
}
238344

239345
func (c *Client) connectHTTP1(

0 commit comments

Comments
 (0)