Skip to content

Commit 10f0f77

Browse files
Replace kube-rbac-proxy to ensure the same level of protection with controller-runtime feature (#1475)
Utilise Controller-Runtime's WithAuthenticationAndAuthorization feature to protect the metrics endpoint. This approach provides access control, similar to the functionality of kube-rbac-proxy. kube-rbac-proxy image from gcr.io/kubebuilder/kube-rbac-proxy is deprecated and should no longer be used More info: kubernetes-sigs/kubebuilder#3907
1 parent b67bd38 commit 10f0f77

File tree

9 files changed

+92
-31
lines changed

9 files changed

+92
-31
lines changed

cmd/manager/main.go

Lines changed: 72 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"
@@ -41,9 +42,11 @@ import (
4142
"k8s.io/klog/v2/textlogger"
4243
ctrl "sigs.k8s.io/controller-runtime"
4344
crcache "sigs.k8s.io/controller-runtime/pkg/cache"
45+
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
4446
"sigs.k8s.io/controller-runtime/pkg/client"
4547
crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer"
4648
"sigs.k8s.io/controller-runtime/pkg/healthz"
49+
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
4750
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
4851

4952
catalogd "github.com/operator-framework/catalogd/api/v1"
@@ -70,6 +73,7 @@ import (
7073
var (
7174
setupLog = ctrl.Log.WithName("setup")
7275
defaultSystemNamespace = "olmv1-system"
76+
certWatcher *certwatcher.CertWatcher
7377
)
7478

7579
const authFilePrefix = "operator-controller-global-pull-secrets"
@@ -89,6 +93,8 @@ func podNamespace() string {
8993
func main() {
9094
var (
9195
metricsAddr string
96+
certFile string
97+
keyFile string
9298
enableLeaderElection bool
9399
probeAddr string
94100
cachePath string
@@ -97,9 +103,11 @@ func main() {
97103
caCertDir string
98104
globalPullSecret string
99105
)
100-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
106+
flag.StringVar(&metricsAddr, "metrics-bind-address", "", "The address for the metrics endpoint. Requires tls-cert and tls-key. (Default: ':8443')")
101107
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
102108
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.")
109+
flag.StringVar(&certFile, "tls-cert", "", "The certificate file used for the metrics server. Required to enable the metrics server. Requires tls-key.")
110+
flag.StringVar(&keyFile, "tls-key", "", "The key file used for the metrics server. Required to enable the metrics server. Requires tls-cert")
103111
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
104112
"Enable leader election for controller manager. "+
105113
"Enabling this will ensure there is only one active controller manager.")
@@ -119,6 +127,20 @@ func main() {
119127
os.Exit(0)
120128
}
121129

130+
if (certFile != "" && keyFile == "") || (certFile == "" && keyFile != "") {
131+
setupLog.Error(nil, "unable to configure TLS certificates: tls-cert and tls-key flags must be used together")
132+
os.Exit(1)
133+
}
134+
135+
if metricsAddr != "" && certFile == "" && keyFile == "" {
136+
setupLog.Error(nil, "metrics-bind-address requires tls-cert and tls-key flags to be set")
137+
os.Exit(1)
138+
}
139+
140+
if certFile != "" && keyFile != "" && metricsAddr == "" {
141+
metricsAddr = ":8443"
142+
}
143+
122144
ctrl.SetLogger(textlogger.NewLogger(textlogger.NewConfig()))
123145

124146
setupLog.Info("starting up the controller", "version info", version.String())
@@ -161,9 +183,49 @@ func main() {
161183
},
162184
}
163185
}
186+
187+
metricsServerOptions := server.Options{}
188+
if len(certFile) > 0 && len(keyFile) > 0 {
189+
setupLog.Info("Starting metrics server with TLS enabled", "addr", metricsAddr, "tls-cert", certFile, "tls-key", keyFile)
190+
191+
metricsServerOptions.BindAddress = metricsAddr
192+
metricsServerOptions.SecureServing = true
193+
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
194+
195+
// If the certificate files change, the watcher will reload them.
196+
var err error
197+
certWatcher, err = certwatcher.New(certFile, keyFile)
198+
if err != nil {
199+
setupLog.Error(err, "Failed to initialize certificate watcher")
200+
os.Exit(1)
201+
}
202+
203+
metricsServerOptions.TLSOpts = append(metricsServerOptions.TLSOpts, func(config *tls.Config) {
204+
config.GetCertificate = certWatcher.GetCertificate
205+
// If the enable-http2 flag is false (the default), http/2 should be disabled
206+
// due to its vulnerabilities. More specifically, disabling http/2 will
207+
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
208+
// Rapid Reset CVEs. For more information see:
209+
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
210+
// - https://github.com/advisories/GHSA-4374-p667-p6c8
211+
// Besides, those CVEs are solved already; the solution is still insufficient, and we need to mitigate
212+
// the risks. More info https://github.com/golang/go/issues/63417
213+
config.NextProtos = []string{"http/1.1"}
214+
})
215+
} else {
216+
// Note that the metrics server is not serving if the BindAddress is set to "0".
217+
// Therefore, the metrics server is disabled by default. It is only enabled
218+
// if certFile and keyFile are provided. The intention is not allowing the metrics
219+
// be served with the default self-signed certificate generated by controller-runtime.
220+
metricsServerOptions.BindAddress = "0"
221+
222+
setupLog.Info("WARNING: Metrics Server is disabled. " +
223+
"Metrics will not be served since the TLS certificate and key file are not provided.")
224+
}
225+
164226
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
165227
Scheme: scheme.Scheme,
166-
Metrics: server.Options{BindAddress: metricsAddr},
228+
Metrics: metricsServerOptions,
167229
HealthProbeBindAddress: probeAddr,
168230
LeaderElection: enableLeaderElection,
169231
LeaderElectionID: "9c4404e7.operatorframework.io",
@@ -220,6 +282,14 @@ func main() {
220282
os.Exit(1)
221283
}
222284

285+
if certWatcher != nil {
286+
setupLog.Info("Adding certificate watcher to manager")
287+
if err := mgr.Add(certWatcher); err != nil {
288+
setupLog.Error(err, "unable to add certificate watcher to manager")
289+
os.Exit(1)
290+
}
291+
}
292+
223293
unpacker := &source.ContainersImageRegistry{
224294
BaseCachePath: filepath.Join(cachePath, "unpack"),
225295
SourceContextFunc: func(logger logr.Logger) (*types.SystemContext, error) {

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=: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_service.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ spec:
1010
- name: https
1111
port: 8443
1212
protocol: TCP
13-
targetPort: https
13+
targetPort: 8443
1414
selector:
1515
control-plane: operator-controller-controller-manager

config/base/rbac/kustomization.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ 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.
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
2326
- auth_proxy_service.yaml
2427
- auth_proxy_role.yaml
2528
- auth_proxy_role_binding.yaml
2629
- auth_proxy_client_clusterrole.yaml
30+

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
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
- op: add
22
path: /spec/template/spec/volumes/-
3-
value: {"name":"olmv1-certificate", "secret":{"secretName":"olmv1-cert", "optional": false, "items": [{"key": "ca.crt", "path": "olm-ca.crt"}]}}
3+
value: {"name":"olmv1-certificate", "secret":{"secretName":"olmv1-cert", "optional": false, "items": [{"key": "ca.crt", "path": "olm-ca.crt"}, {"key": "tls.crt", "path": "tls.cert"}, {"key": "tls.key", "path": "tls.key"}]}}
44
- op: add
55
path: /spec/template/spec/containers/0/volumeMounts/-
66
value: {"name":"olmv1-certificate", "readOnly": true, "mountPath":"/var/certs/"}
77
- op: add
88
path: /spec/template/spec/containers/0/args/-
99
value: "--ca-certs-dir=/var/certs"
10+
- op: add
11+
path: /spec/template/spec/containers/0/args/-
12+
value: "--tls-cert=/var/certs/tls.cert"
13+
- op: add
14+
path: /spec/template/spec/containers/0/args/-
15+
value: "--tls-key=/var/certs/tls.key"

config/components/tls/resources/manager_cert.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ spec:
77
dnsNames:
88
- operator-controller.olmv1-system.svc
99
- operator-controller.olmv1-system.svc.cluster.local
10+
- operator-controller-controller-manager-metrics-service.olmv1-system.svc
11+
- operator-controller-controller-manager-metrics-service.olmv1-system.svc.cluster.local
1012
privateKey:
1113
algorithm: ECDSA
1214
size: 256

testdata/build-test-registry.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ spec:
4242
dnsNames:
4343
- ${name}.${namespace}.svc
4444
- ${name}.${namespace}.svc.cluster.local
45+
- ${name}-controller-manager-metrics-service.${namespace}.svc
46+
- ${name}-controller-manager-metrics-service.${namespace}.svc.cluster.local
4547
privateKey:
4648
algorithm: ECDSA
4749
size: 256

0 commit comments

Comments
 (0)