@@ -18,6 +18,7 @@ package main
1818
1919import (
2020 "context"
21+ "crypto/tls"
2122 "flag"
2223 "os"
2324
@@ -34,6 +35,7 @@ import (
3435 "sigs.k8s.io/controller-runtime/pkg/client"
3536 "sigs.k8s.io/controller-runtime/pkg/healthz"
3637 "sigs.k8s.io/controller-runtime/pkg/log/zap"
38+ "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3739 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3840
3941 confidentialcontainersorgv1alpha1 "github.com/confidential-containers/trustee-operator/api/v1alpha1"
@@ -56,13 +58,18 @@ func init() {
5658
5759func main () {
5860 var metricsAddr string
61+ var secureMetrics bool
5962 var enableLeaderElection bool
6063 var probeAddr string
61- flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metric endpoint binds to." )
64+ var tlsOpts []func (* tls.Config )
65+ flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metrics endpoint binds to. " +
66+ "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
6267 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
6368 flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
6469 "Enable leader election for controller manager. " +
6570 "Enabling this will ensure there is only one active controller manager." )
71+ flag .BoolVar (& secureMetrics , "metrics-secure" , true ,
72+ "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
6673 opts := zap.Options {
6774 Development : true ,
6875 }
@@ -71,9 +78,33 @@ func main() {
7178
7279 ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
7380
81+ // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
82+ // More info:
83+ // - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/metrics/server 84+ // - https://book.kubebuilder.io/reference/metrics.html
85+ metricsServerOptions := metricsserver.Options {
86+ BindAddress : metricsAddr ,
87+ SecureServing : secureMetrics ,
88+ // TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
89+ // not provided, self-signed certificates will be generated by default. This option is not recommended for
90+ // production environments as self-signed certificates do not offer the same level of trust and security
91+ // as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
92+ // unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
93+ // to provide certificates, ensuring the server communicates using trusted and secure certificates.
94+ TLSOpts : tlsOpts ,
95+ }
96+
97+ if secureMetrics {
98+ // FilterProvider is used to protect the metrics endpoint with authn/authz.
99+ // These configurations ensure that only authorized users and service accounts
100+ // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
101+ // https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/metrics/filters#WithAuthenticationAndAuthorization 102+ metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
103+ }
104+
74105 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
75106 Scheme : scheme ,
76- Metrics : metricsserver. Options { BindAddress : metricsAddr } ,
107+ Metrics : metricsServerOptions ,
77108 HealthProbeBindAddress : probeAddr ,
78109 LeaderElection : enableLeaderElection ,
79110 LeaderElectionID : "178dc119.confidentialcontainers.org" ,
0 commit comments