Skip to content

Commit 48dc64a

Browse files
Camila MCamila M
authored andcommitted
Replace kube-rbac-proxy with controller-runtime metrics authentication/authorization
This commit removes the use of the kube-rbac-proxy image and replaces it with metrics authentication/authorization provided by controller-runtime. The kube-rbac-proxy image is deprecated and will no longer be maintained, which introduces risks to production environments. For more details, see: kubernetes-sigs/kubebuilder#3907 Key changes: - Updated to configure metrics server options with secure authentication/authorization using controller-runtime filters. - Added support for disabling HTTP/2 by default to mitigate vulnerabilities (e.g., HTTP/2 Stream Cancellation CVE). - Changed the default metrics endpoint to HTTPS (port 8443) and removed the kube-rbac-proxy container from deployment configurations. - Updated RBAC files to include metrics-specific roles and bindings, ensuring secure access to metrics. This aligns with best practices for security and simplifies the metrics setup by leveraging built-in capabilities of controller-runtime.
1 parent 63ef902 commit 48dc64a

File tree

10 files changed

+73
-70
lines changed

10 files changed

+73
-70
lines changed

cmd/manager/main.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"context"
21+
"crypto/tls"
2122
"flag"
2223
"fmt"
2324
"net/http"
@@ -44,6 +45,7 @@ import (
4445
"sigs.k8s.io/controller-runtime/pkg/client"
4546
crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer"
4647
"sigs.k8s.io/controller-runtime/pkg/healthz"
48+
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
4749
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
4850

4951
catalogd "github.com/operator-framework/catalogd/api/v1"
@@ -89,6 +91,9 @@ func podNamespace() string {
8991
func main() {
9092
var (
9193
metricsAddr string
94+
secureMetrics bool
95+
tlsOpts []func(*tls.Config)
96+
enableHTTP2 bool
9297
enableLeaderElection bool
9398
probeAddr string
9499
cachePath string
@@ -97,7 +102,11 @@ func main() {
97102
caCertDir string
98103
globalPullSecret string
99104
)
100-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
105+
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8443", "The address the metric endpoint binds to.")
106+
flag.BoolVar(&secureMetrics, "metrics-secure", true,
107+
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
108+
flag.BoolVar(&enableHTTP2, "enable-http2", false,
109+
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
101110
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
102111
flag.StringVar(&caCertDir, "ca-certs-dir", "", "The directory of TLS certificate to use for verifying HTTPS connections to the Catalogd and docker-registry web servers.")
103112
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
@@ -161,9 +170,39 @@ func main() {
161170
},
162171
}
163172
}
173+
174+
// if the enable-http2 flag is false (the default), http/2 should be disabled
175+
// due to its vulnerabilities. More specifically, disabling http/2 will
176+
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
177+
// Rapid Reset CVEs. For more information see:
178+
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
179+
// - https://github.com/advisories/GHSA-4374-p667-p6c8
180+
disableHTTP2 := func(c *tls.Config) {
181+
setupLog.Info("disabling http/2")
182+
c.NextProtos = []string{"http/1.1"}
183+
}
184+
185+
if !enableHTTP2 {
186+
tlsOpts = append(tlsOpts, disableHTTP2)
187+
}
188+
189+
metricsServerOptions := server.Options{
190+
BindAddress: metricsAddr,
191+
SecureServing: secureMetrics,
192+
TLSOpts: tlsOpts,
193+
}
194+
195+
if secureMetrics {
196+
// FilterProvider is used to protect the metrics endpoint with authn/authz.
197+
// These configurations ensure that only authorized users and service accounts
198+
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
199+
// https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization
200+
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
201+
}
202+
164203
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
165204
Scheme: scheme.Scheme,
166-
Metrics: server.Options{BindAddress: metricsAddr},
205+
Metrics: metricsServerOptions,
167206
HealthProbeBindAddress: probeAddr,
168207
LeaderElection: enableLeaderElection,
169208
LeaderElectionID: "9c4404e7.operatorframework.io",

config/base/manager/manager.yaml

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ spec:
5252
- /manager
5353
args:
5454
- "--health-probe-bind-address=:8081"
55-
- "--metrics-bind-address=127.0.0.1:8080"
55+
- "--metrics-bind-address=127.0.0.1:8443"
5656
- "--leader-elect"
5757
image: controller:latest
5858
imagePullPolicy: IfNotPresent
@@ -84,27 +84,6 @@ spec:
8484
cpu: 10m
8585
memory: 64Mi
8686
terminationMessagePolicy: FallbackToLogsOnError
87-
- name: kube-rbac-proxy
88-
securityContext:
89-
allowPrivilegeEscalation: false
90-
capabilities:
91-
drop:
92-
- "ALL"
93-
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.15.0
94-
args:
95-
- --secure-listen-address=0.0.0.0:8443
96-
- --http2-disable
97-
- --upstream=http://127.0.0.1:8080/
98-
- --logtostderr=true
99-
ports:
100-
- containerPort: 8443
101-
protocol: TCP
102-
name: https
103-
resources:
104-
requests:
105-
cpu: 5m
106-
memory: 64Mi
107-
terminationMessagePolicy: FallbackToLogsOnError
10887
serviceAccountName: operator-controller-controller-manager
10988
terminationGracePeriodSeconds: 10
11089
volumes:

config/base/rbac/auth_proxy_role.yaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

config/base/rbac/auth_proxy_service.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.

config/base/rbac/kustomization.yaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ resources:
1717
- extension_editor_role.yaml
1818
- extension_viewer_role.yaml
1919

20-
# Comment the following 4 lines if you want to disable
21-
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
22-
# which protects your /metrics endpoint.
23-
- auth_proxy_service.yaml
24-
- auth_proxy_role.yaml
25-
- auth_proxy_role_binding.yaml
26-
- auth_proxy_client_clusterrole.yaml
20+
# The following RBAC configurations are used to protect
21+
# the metrics endpoint with authn/authz. These configurations
22+
# ensure that only authorized users and service accounts
23+
# can access the metrics endpoint. Comment the following
24+
# permissions if you want to disable this protection.
25+
# More info: https://book.kubebuilder.io/reference/metrics.html
26+
- metrics_auth_role.yaml
27+
- metrics_auth_role_binding.yaml
28+
- metrics_reader_role.yaml
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRole
3+
metadata:
4+
name: metrics-auth-role
5+
rules:
6+
- apiGroups:
7+
- authentication.k8s.io
8+
resources:
9+
- tokenreviews
10+
verbs:
11+
- create
12+
- apiGroups:
13+
- authorization.k8s.io
14+
resources:
15+
- subjectaccessreviews
16+
verbs:
17+
- create
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apiVersion: rbac.authorization.k8s.io/v1
22
kind: ClusterRoleBinding
33
metadata:
4-
name: proxy-rolebinding
4+
name: metrics-auth-rolebinding
55
roleRef:
66
apiGroup: rbac.authorization.k8s.io
77
kind: ClusterRole
8-
name: proxy-role
8+
name: metrics-auth-role
99
subjects:
10-
- kind: ServiceAccount
11-
name: controller-manager
12-
namespace: system
10+
- kind: ServiceAccount
11+
name: controller-manager
12+
namespace: system

config/components/coverage/manager_e2e_coverage_patch.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ spec:
77
template:
88
spec:
99
containers:
10-
- name: kube-rbac-proxy
1110
- name: manager
1211
env:
1312
- name: GOCOVERDIR

config/components/registries-conf/manager_e2e_registries_conf_patch.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ spec:
77
template:
88
spec:
99
containers:
10-
- name: kube-rbac-proxy
1110
- name: manager
1211
volumeMounts:
1312
- name: e2e-registries-conf

0 commit comments

Comments
 (0)