Skip to content

Commit 6e4f782

Browse files
authored
✨ add secure metrics support with secure endpoint instead of the RBAC proxy and improve helm values structure (#1458)
* ✨ add secure metrics support with TLS and RBAC - Introduced `SecureMetrics` field in `MondooOperatorConfigSpec` to enable authentication and authorization on the metrics endpoint. - Updated deployment configuration to serve metrics over HTTPS on port 8443 when `SecureMetrics` is enabled. - Modified ServiceMonitor to handle both HTTP and HTTPS metrics endpoints based on the `SecureMetrics` setting. - Added tests for metrics endpoint behavior under both secure and non-secure configurations. - Updated Helm chart values to include `secureMetrics` option. - Adjusted deployment and verification scripts to support the new secure metrics feature. * ✨ refactor: update controller manager args and introduce extraArgs for flexibility * ✨ feat: add secure metrics support with RBAC and update verification script
1 parent 2ff3d69 commit 6e4f782

19 files changed

Lines changed: 281 additions & 56 deletions

api/v1alpha2/mondoooperatorconfig_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ type MondooOperatorConfigSpec struct {
5555

5656
type Metrics struct {
5757
Enable bool `json:"enable,omitempty"`
58+
// SecureMetrics enables authentication and authorization on the metrics endpoint
59+
// using controller-runtime's built-in TLS and RBAC-based auth.
60+
// When enabled, metrics are served over HTTPS on port 8443 with token-based auth.
61+
// When disabled, metrics are served over HTTP on port 8080 without auth.
62+
SecureMetrics bool `json:"secureMetrics,omitempty"`
5863
// ResourceLabels allows providing a list of extra labels to apply to the metrics-related
5964
// resources (eg. ServiceMonitor)
6065
ResourceLabels map[string]string `json:"resourceLabels,omitempty"`

charts/mondoo-operator/files/crds/k8s.mondoo.com_mondoooperatorconfigs.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ spec:
9393
ResourceLabels allows providing a list of extra labels to apply to the metrics-related
9494
resources (eg. ServiceMonitor)
9595
type: object
96+
secureMetrics:
97+
description: |-
98+
SecureMetrics enables authentication and authorization on the metrics endpoint
99+
using controller-runtime's built-in TLS and RBAC-based auth.
100+
When enabled, metrics are served over HTTPS on port 8443 with token-based auth.
101+
When disabled, metrics are served over HTTP on port 8080 without auth.
102+
type: boolean
96103
type: object
97104
noProxy:
98105
description: NoProxy specifies a comma-separated list of hosts that

charts/mondoo-operator/templates/deployment.yaml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@ spec:
1717
kubectl.kubernetes.io/default-container: manager
1818
spec:
1919
containers:
20-
- args: {{- toYaml .Values.controllerManager.manager.args | nindent 8 }}
20+
- args:
21+
- operator
22+
- --health-probe-bind-address=:8081
23+
- --leader-elect
24+
{{- if .Values.controllerManager.manager.secureMetrics }}
25+
- --metrics-bind-address=:8443
26+
- --secure-metrics
27+
{{- else }}
28+
- --metrics-bind-address=:8080
29+
{{- end }}
30+
{{- with .Values.controllerManager.manager.extraArgs }}
31+
{{- toYaml . | nindent 8 }}
32+
{{- end }}
2133
command:
2234
- /mondoo-operator
2335
env:
@@ -59,9 +71,15 @@ spec:
5971
periodSeconds: 20
6072
name: manager
6173
ports:
74+
{{- if .Values.controllerManager.manager.secureMetrics }}
75+
- containerPort: 8443
76+
name: https
77+
protocol: TCP
78+
{{- else }}
6279
- containerPort: 8080
6380
name: metrics
6481
protocol: TCP
82+
{{- end }}
6583
readinessProbe:
6684
httpGet:
6785
path: /readyz

charts/mondoo-operator/templates/metrics-reader-rbac.yaml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,21 @@ rules:
1111
- nonResourceURLs:
1212
- /metrics
1313
verbs:
14-
- get
14+
- get
15+
{{- if .Values.controllerManager.manager.secureMetrics }}
16+
---
17+
apiVersion: rbac.authorization.k8s.io/v1
18+
kind: ClusterRoleBinding
19+
metadata:
20+
name: {{ include "mondoo-operator.fullname" . }}-metrics-reader-binding
21+
labels:
22+
{{- include "mondoo-operator.labels" . | nindent 4 }}
23+
roleRef:
24+
apiGroup: rbac.authorization.k8s.io
25+
kind: ClusterRole
26+
name: {{ include "mondoo-operator.fullname" . }}-metrics-reader
27+
subjects:
28+
- kind: ServiceAccount
29+
name: {{ include "mondoo-operator.fullname" . }}-controller-manager
30+
namespace: {{ .Release.Namespace }}
31+
{{- end }}

charts/mondoo-operator/templates/metrics-service.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ spec:
99
selector:
1010
{{- include "mondoo-operator.selectorLabels" . | nindent 4 }}
1111
ports:
12+
{{- if .Values.controllerManager.manager.secureMetrics }}
13+
- name: https
14+
port: 8443
15+
protocol: TCP
16+
targetPort: https
17+
{{- else }}
1218
{{- .Values.metricsService.ports | toYaml | nindent 2 }}
19+
{{- end }}

charts/mondoo-operator/templates/mondoooperatorconfig.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@ spec:
3434
{{- if .Values.operator.skipProxyForCnspec }}
3535
{{- $_ := set $spec "skipProxyForCnspec" .Values.operator.skipProxyForCnspec }}
3636
{{- end }}
37+
{{- if .Values.controllerManager.manager.secureMetrics }}
38+
{{- $metrics := dict "enable" true "secureMetrics" true }}
39+
{{- $_ := set $spec "metrics" $metrics }}
40+
{{- end }}
3741
{{- $spec | toYaml | nindent 2 }}
3842
{{- end }}

charts/mondoo-operator/values.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33

44
controllerManager:
55
manager:
6-
## @param controllerManager.manager.args Command-line arguments passed to the operator manager container
7-
args:
8-
- operator
9-
- --health-probe-bind-address=:8081
10-
- --metrics-bind-address=:8080
11-
- --leader-elect
6+
## @param controllerManager.manager.extraArgs Additional command-line arguments passed to the operator manager container
7+
extraArgs: []
128
## @param controllerManager.manager.containerSecurityContext [object] Security context for the manager container
139
containerSecurityContext:
1410
allowPrivilegeEscalation: false
@@ -22,6 +18,8 @@ controllerManager:
2218
repository: ghcr.io/mondoohq/mondoo-operator
2319
## @param controllerManager.manager.image.tag Container image tag for the operator (defaults to .Chart.AppVersion)
2420
tag: ""
21+
## @param controllerManager.manager.secureMetrics Enable RBAC-authenticated HTTPS metrics (port 8443)
22+
secureMetrics: false
2523
## @param controllerManager.manager.imagePullPolicy Image pull policy for the operator container
2624
imagePullPolicy: IfNotPresent
2725
## @param controllerManager.manager.resources [object] Resource requests and limits for the manager container

cmd/mondoo-operator/operator/cmd.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
package operator
55

66
import (
7+
"crypto/tls"
78
"errors"
89
"fmt"
910
"time"
1011

1112
"github.com/go-logr/logr"
1213
"github.com/spf13/cobra"
14+
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
1315
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
1416

1517
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
@@ -52,6 +54,8 @@ func init() {
5254
probeAddr := Cmd.Flags().String("health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
5355
enableLeaderElection := Cmd.Flags().Bool("leader-elect", false,
5456
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
57+
secureMetrics := Cmd.Flags().Bool("secure-metrics", false,
58+
"Enable authentication and authorization on the metrics endpoint via HTTPS and RBAC.")
5559

5660
Cmd.RunE = func(cmd *cobra.Command, args []string) error {
5761
// TODO: opts.BindFlags(flag.CommandLine) is not supported with cobra. If we want to support that we should manually
@@ -67,9 +71,18 @@ func init() {
6771
utilruntime.Must(certmanagerv1.AddToScheme(scheme))
6872
utilruntime.Must(monitoringv1.AddToScheme(scheme))
6973

74+
metricsOpts := metricsserver.Options{BindAddress: *metricsAddr}
75+
if *secureMetrics {
76+
metricsOpts.SecureServing = true
77+
metricsOpts.FilterProvider = filters.WithAuthenticationAndAuthorization
78+
metricsOpts.TLSOpts = []func(*tls.Config){
79+
func(c *tls.Config) { c.MinVersion = tls.VersionTLS12 },
80+
}
81+
}
82+
7083
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
7184
Scheme: scheme,
72-
Metrics: metricsserver.Options{BindAddress: *metricsAddr},
85+
Metrics: metricsOpts,
7386
HealthProbeBindAddress: *probeAddr,
7487
LeaderElection: *enableLeaderElection,
7588
LeaderElectionID: "60679458.mondoo.com",

config/crd/bases/k8s.mondoo.com_mondoooperatorconfigs.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ spec:
9393
ResourceLabels allows providing a list of extra labels to apply to the metrics-related
9494
resources (eg. ServiceMonitor)
9595
type: object
96+
secureMetrics:
97+
description: |-
98+
SecureMetrics enables authentication and authorization on the metrics endpoint
99+
using controller-runtime's built-in TLS and RBAC-based auth.
100+
When enabled, metrics are served over HTTPS on port 8443 with token-based auth.
101+
When disabled, metrics are served over HTTP on port 8080 without auth.
102+
type: boolean
96103
type: object
97104
noProxy:
98105
description: NoProxy specifies a comma-separated list of hosts that

config/default/kustomization.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ bases:
3030
#- ../prometheus
3131

3232
patchesStrategicMerge:
33-
# Uncomment the following line to protect the /metrics endpoint with kube-rbac-proxy.
34-
# - manager_auth_proxy_patch.yaml
35-
3633
- manager_public_metrics_patch.yaml
3734

3835
# Mount the controller config file for loading manager configurations

0 commit comments

Comments
 (0)