Skip to content

Commit 23b1f03

Browse files
Enable ServiceMonitor to use cert-manager-managed serving-cert with TLS verification
Adds a patch to configure ServiceMonitor with to ensure TLS verification using cert-manager certificates. Updates documentation and corrects misaligned comments.
1 parent f7a02ad commit 23b1f03

File tree

31 files changed

+415
-153
lines changed

31 files changed

+415
-153
lines changed

docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,17 @@ func main() {
123123
metricsServerOptions := metricsserver.Options{
124124
BindAddress: metricsAddr,
125125
SecureServing: secureMetrics,
126-
// TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
127-
// not provided, self-signed certificates will be generated by default. This option is not recommended for
128-
// production environments as self-signed certificates do not offer the same level of trust and security
129-
// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
130-
// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
131-
// to provide certificates, ensuring the server communicates using trusted and secure certificates.
132-
TLSOpts: tlsOpts,
126+
TLSOpts: tlsOpts,
133127
}
134128

135129
if secureMetrics {
130+
// TODO(user): If cert-manager is enabled under config/default/kustomizaton.yaml, you can uncomment the following
131+
// lines to use the certificate managed by cert-manager, mounted as a Kubernetes secret named 'serving-cert'.
132+
// This setup is recommended for production environments to ensure trusted and secure communication.
133+
// metricsServerOptions.CertDir = "/var/run/secrets/kubernetes.io/certs"
134+
// metricsServerOptions.CertName = "tls.crt"
135+
// metricsServerOptions.KeyName = "tls.key"
136+
136137
// FilterProvider is used to protect the metrics endpoint with authn/authz.
137138
// These configurations ensure that only authorized users and service accounts
138139
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:

docs/book/src/cronjob-tutorial/testdata/project/config/default/kustomization.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ patches:
4141
target:
4242
kind: Deployment
4343

44+
# [CERTMANAGER] The following patch configures the ServiceMonitor under ../prometheus
45+
# to securely reference certificates created and managed by cert-manager. Also, uncomment the
46+
# [WEBHOOK] patch bellow to mount the "serving-cert" in the Manager Deployment.
47+
#- path: ../prometheus/monitor_tls_patch.yaml
48+
#target:
49+
# kind: ServiceMonitor
50+
4451
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
45-
# crd/kustomization.yaml
52+
# crd/kustomization.yaml.
4653
- path: manager_webhook_patch.yaml
4754

4855
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.

docs/book/src/cronjob-tutorial/testdata/project/config/prometheus/monitor.yaml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@ spec:
1616
bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
1717
tlsConfig:
1818
# TODO(user): The option insecureSkipVerify: true is not recommended for production since it disables
19-
# certificate verification. This poses a significant security risk by making the system vulnerable to
20-
# man-in-the-middle attacks, where an attacker could intercept and manipulate the communication between
21-
# Prometheus and the monitored services. This could lead to unauthorized access to sensitive metrics data,
22-
# compromising the integrity and confidentiality of the information.
23-
# Please use the following options for secure configurations:
24-
# caFile: /etc/metrics-certs/ca.crt
25-
# certFile: /etc/metrics-certs/tls.crt
26-
# keyFile: /etc/metrics-certs/tls.key
19+
# certificate verification, exposing the system to potential man-in-the-middle attacks.
20+
# For production environments, it is recommended to use cert-manager for automatic TLS certificate management.
21+
# To apply this configuration, enable cert-manager and use the patch located at config/prometheus/servicemonitor_tls_patch.yaml,
22+
# which securely references the certificate from the 'serving-cert' secret.
2723
insecureSkipVerify: true
2824
selector:
2925
matchLabels:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Patch for Prometheus ServiceMonitor to enable secure TLS configuration
2+
# using certificates managed by cert-manager
3+
apiVersion: monitoring.coreos.com/v1
4+
kind: ServiceMonitor
5+
metadata:
6+
name: controller-manager-metrics-monitor
7+
namespace: system
8+
spec:
9+
endpoints:
10+
- tlsConfig:
11+
insecureSkipVerify: false
12+
ca:
13+
secret:
14+
name: serving-cert
15+
key: ca.crt
16+
cert:
17+
secret:
18+
name: serving-cert
19+
key: tls.crt
20+
keySecret:
21+
name: serving-cert
22+
key: tls.key

docs/book/src/getting-started/testdata/project/cmd/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,17 @@ func main() {
103103
metricsServerOptions := metricsserver.Options{
104104
BindAddress: metricsAddr,
105105
SecureServing: secureMetrics,
106-
// TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
107-
// not provided, self-signed certificates will be generated by default. This option is not recommended for
108-
// production environments as self-signed certificates do not offer the same level of trust and security
109-
// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
110-
// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
111-
// to provide certificates, ensuring the server communicates using trusted and secure certificates.
112-
TLSOpts: tlsOpts,
106+
TLSOpts: tlsOpts,
113107
}
114108

115109
if secureMetrics {
110+
// TODO(user): If cert-manager is enabled under config/default/kustomizaton.yaml, you can uncomment the following
111+
// lines to use the certificate managed by cert-manager, mounted as a Kubernetes secret named 'serving-cert'.
112+
// This setup is recommended for production environments to ensure trusted and secure communication.
113+
// metricsServerOptions.CertDir = "/var/run/secrets/kubernetes.io/certs"
114+
// metricsServerOptions.CertName = "tls.crt"
115+
// metricsServerOptions.KeyName = "tls.key"
116+
116117
// FilterProvider is used to protect the metrics endpoint with authn/authz.
117118
// These configurations ensure that only authorized users and service accounts
118119
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:

docs/book/src/getting-started/testdata/project/config/default/kustomization.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ patches:
4141
target:
4242
kind: Deployment
4343

44+
# [CERTMANAGER] The following patch configures the ServiceMonitor under ../prometheus
45+
# to securely reference certificates created and managed by cert-manager. Also, uncomment the
46+
# [WEBHOOK] patch bellow to mount the "serving-cert" in the Manager Deployment.
47+
#- path: ../prometheus/monitor_tls_patch.yaml
48+
#target:
49+
# kind: ServiceMonitor
50+
4451
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
45-
# crd/kustomization.yaml
52+
# crd/kustomization.yaml.
4653
#- path: manager_webhook_patch.yaml
4754

4855
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.

docs/book/src/getting-started/testdata/project/config/prometheus/monitor.yaml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@ spec:
1616
bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
1717
tlsConfig:
1818
# TODO(user): The option insecureSkipVerify: true is not recommended for production since it disables
19-
# certificate verification. This poses a significant security risk by making the system vulnerable to
20-
# man-in-the-middle attacks, where an attacker could intercept and manipulate the communication between
21-
# Prometheus and the monitored services. This could lead to unauthorized access to sensitive metrics data,
22-
# compromising the integrity and confidentiality of the information.
23-
# Please use the following options for secure configurations:
24-
# caFile: /etc/metrics-certs/ca.crt
25-
# certFile: /etc/metrics-certs/tls.crt
26-
# keyFile: /etc/metrics-certs/tls.key
19+
# certificate verification, exposing the system to potential man-in-the-middle attacks.
20+
# For production environments, it is recommended to use cert-manager for automatic TLS certificate management.
21+
# To apply this configuration, enable cert-manager and use the patch located at config/prometheus/servicemonitor_tls_patch.yaml,
22+
# which securely references the certificate from the 'serving-cert' secret.
2723
insecureSkipVerify: true
2824
selector:
2925
matchLabels:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Patch for Prometheus ServiceMonitor to enable secure TLS configuration
2+
# using certificates managed by cert-manager
3+
apiVersion: monitoring.coreos.com/v1
4+
kind: ServiceMonitor
5+
metadata:
6+
name: controller-manager-metrics-monitor
7+
namespace: system
8+
spec:
9+
endpoints:
10+
- tlsConfig:
11+
insecureSkipVerify: false
12+
ca:
13+
secret:
14+
name: serving-cert
15+
key: ca.crt
16+
cert:
17+
secret:
18+
name: serving-cert
19+
key: tls.crt
20+
keySecret:
21+
name: serving-cert
22+
key: tls.key

docs/book/src/multiversion-tutorial/testdata/project/cmd/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,17 @@ func main() {
122122
metricsServerOptions := metricsserver.Options{
123123
BindAddress: metricsAddr,
124124
SecureServing: secureMetrics,
125-
// TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
126-
// not provided, self-signed certificates will be generated by default. This option is not recommended for
127-
// production environments as self-signed certificates do not offer the same level of trust and security
128-
// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
129-
// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
130-
// to provide certificates, ensuring the server communicates using trusted and secure certificates.
131-
TLSOpts: tlsOpts,
125+
TLSOpts: tlsOpts,
132126
}
133127

134128
if secureMetrics {
129+
// TODO(user): If cert-manager is enabled under config/default/kustomizaton.yaml, you can uncomment the following
130+
// lines to use the certificate managed by cert-manager, mounted as a Kubernetes secret named 'serving-cert'.
131+
// This setup is recommended for production environments to ensure trusted and secure communication.
132+
// metricsServerOptions.CertDir = "/var/run/secrets/kubernetes.io/certs"
133+
// metricsServerOptions.CertName = "tls.crt"
134+
// metricsServerOptions.KeyName = "tls.key"
135+
135136
// FilterProvider is used to protect the metrics endpoint with authn/authz.
136137
// These configurations ensure that only authorized users and service accounts
137138
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:

docs/book/src/multiversion-tutorial/testdata/project/config/default/kustomization.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ patches:
4141
target:
4242
kind: Deployment
4343

44+
# [CERTMANAGER] The following patch configures the ServiceMonitor under ../prometheus
45+
# to securely reference certificates created and managed by cert-manager. Also, uncomment the
46+
# [WEBHOOK] patch bellow to mount the "serving-cert" in the Manager Deployment.
47+
#- path: ../prometheus/monitor_tls_patch.yaml
48+
#target:
49+
# kind: ServiceMonitor
50+
4451
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
45-
# crd/kustomization.yaml
52+
# crd/kustomization.yaml.
4653
- path: manager_webhook_patch.yaml
4754

4855
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.

0 commit comments

Comments
 (0)