A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/kubernetes-sigs/kubebuilder/commit/de1cc60900b896b2195e403a40c976a892df4921 below:

✨ Add protection to metrics endpoint using authn/authz via controller… · kubernetes-sigs/kubebuilder@de1cc60 · GitHub

Expand file treeCollapse file tree 96 files changed

+1722

-241

lines changed Original file line number Diff line number Diff line change

@@ -24,7 +24,7 @@ jobs:

24 24

run: |

25 25

KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"

26 26

sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH

27 -

sed -i '47s/^#//' $KUSTOMIZATION_FILE_PATH

27 +

sed -i '46s/^#//' $KUSTOMIZATION_FILE_PATH

28 28 29 29

- name: Test

30 30

run: |

Original file line number Diff line number Diff line change

@@ -32,6 +32,7 @@ import (

32 32

ctrl "sigs.k8s.io/controller-runtime"

33 33

"sigs.k8s.io/controller-runtime/pkg/healthz"

34 34

"sigs.k8s.io/controller-runtime/pkg/log/zap"

35 +

"sigs.k8s.io/controller-runtime/pkg/metrics/filters"

35 36

metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

36 37

"sigs.k8s.io/controller-runtime/pkg/webhook"

37 38

@@ -76,14 +77,14 @@ func main() {

76 77

var probeAddr string

77 78

var secureMetrics bool

78 79

var enableHTTP2 bool

79 -

flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+

80 -

"Use the port :8080. If not set, it will be 0 in order to disable the metrics server")

80 +

flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+

81 +

"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")

81 82

flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")

82 83

flag.BoolVar(&enableLeaderElection, "leader-elect", false,

83 84

"Enable leader election for controller manager. "+

84 85

"Enabling this will ensure there is only one active controller manager.")

85 -

flag.BoolVar(&secureMetrics, "metrics-secure", false,

86 -

"If set the metrics endpoint is served securely")

86 +

flag.BoolVar(&secureMetrics, "metrics-secure", true,

87 +

"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")

87 88

flag.BoolVar(&enableHTTP2, "enable-http2", false,

88 89

"If set, HTTP/2 will be enabled for the metrics and webhook servers")

89 90

opts := zap.Options{

@@ -116,10 +117,25 @@ func main() {

116 117 117 118

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{

118 119

Scheme: scheme,

120 +

// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.

121 +

// More info:

122 +

// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/server

123 +

// - https://book.kubebuilder.io/reference/metrics.html

119 124

Metrics: metricsserver.Options{

120 125

BindAddress: metricsAddr,

121 126

SecureServing: secureMetrics,

122 -

TLSOpts: tlsOpts,

127 +

// TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are

128 +

// not provided, self-signed certificates will be generated by default. This option is not recommended for

129 +

// production environments as self-signed certificates do not offer the same level of trust and security

130 +

// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing

131 +

// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName

132 +

// to provide certificates, ensuring the server communicates using trusted and secure certificates.

133 +

TLSOpts: tlsOpts,

134 +

// FilterProvider is used to protect the metrics endpoint with authn/authz.

135 +

// These configurations ensure that only authorized users and service accounts

136 +

// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:

137 +

// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/filters#WithAuthenticationAndAuthorization

138 +

FilterProvider: filters.WithAuthenticationAndAuthorization,

123 139

},

124 140

WebhookServer: webhookServer,

125 141

HealthProbeBindAddress: probeAddr,

Original file line number Diff line number Diff line change

@@ -25,17 +25,16 @@ resources:

25 25

- ../certmanager

26 26

# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.

27 27

- ../prometheus

28 -

# [METRICS] To enable the controller manager metrics service, uncomment the following line.

29 -

#- metrics_service.yaml

28 +

# [METRICS] Expose the controller manager metrics service.

29 +

- metrics_service.yaml

30 30 31 31

# Uncomment the patches line if you enable Metrics, and/or are using webhooks and cert-manager

32 32

patches:

33 -

# [METRICS] The following patch will enable the metrics endpoint. Ensure that you also protect this endpoint.

33 +

# [METRICS] The following patch will enable the metrics endpoint using HTTPS and the port :8443.

34 34

# More info: https://book.kubebuilder.io/reference/metrics

35 -

# If you want to expose the metric endpoint of your controller-manager uncomment the following line.

36 -

#- path: manager_metrics_patch.yaml

37 -

# target:

38 -

# kind: Deployment

35 +

- path: manager_metrics_patch.yaml

36 +

target:

37 +

kind: Deployment

39 38 40 39

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in

41 40

# crd/kustomization.yaml

Original file line number Diff line number Diff line change

@@ -1,4 +1,4 @@

1 -

# This patch adds the args to allow exposing the metrics endpoint securely

1 +

# This patch adds the args to allow exposing the metrics endpoint using HTTPS

2 2

- op: add

3 3

path: /spec/template/spec/containers/0/args/0

4 -

value: --metrics-bind-address=:8080

4 +

value: --metrics-bind-address=:8443

Original file line number Diff line number Diff line change

@@ -9,9 +9,9 @@ metadata:

9 9

namespace: system

10 10

spec:

11 11

ports:

12 -

- name: http

13 -

port: 8080

12 +

- name: https

13 +

port: 8443

14 14

protocol: TCP

15 -

targetPort: 8080

15 +

targetPort: 8443

16 16

selector:

17 17

control-plane: controller-manager

Original file line number Diff line number Diff line change

@@ -11,8 +11,20 @@ metadata:

11 11

spec:

12 12

endpoints:

13 13

- path: /metrics

14 -

port: http # Ensure this is the name of the port that exposes HTTP metrics

15 -

scheme: http

14 +

port: https # Ensure this is the name of the port that exposes HTTPS metrics

15 +

scheme: https

16 +

bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token

17 +

tlsConfig:

18 +

# 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

27 +

insecureSkipVerify: true

16 28

selector:

17 29

matchLabels:

18 30

control-plane: controller-manager

Original file line number Diff line number Diff line change

@@ -9,6 +9,15 @@ resources:

9 9

- role_binding.yaml

10 10

- leader_election_role.yaml

11 11

- leader_election_role_binding.yaml

12 +

# The following RBAC configurations are used to protect

13 +

# the metrics endpoint with authn/authz. These configurations

14 +

# ensure that only authorized users and service accounts

15 +

# can access the metrics endpoint. Comment the following

16 +

# permissions if you want to disable this protection.

17 +

# More info: https://book.kubebuilder.io/reference/metrics.html

18 +

- metrics_auth_role.yaml

19 +

- metrics_auth_role_binding.yaml

20 +

- metrics_reader_role.yaml

12 21

# For each CRD, "Editor" and "Viewer" roles are scaffolded by

13 22

# default, aiding admins in cluster management. Those roles are

14 23

# not used by the Project itself. You can comment the following lines

Original file line number Diff line number Diff 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

Original file line number Diff line number Diff line change

@@ -0,0 +1,12 @@

1 +

apiVersion: rbac.authorization.k8s.io/v1

2 +

kind: ClusterRoleBinding

3 +

metadata:

4 +

name: metrics-auth-rolebinding

5 +

roleRef:

6 +

apiGroup: rbac.authorization.k8s.io

7 +

kind: ClusterRole

8 +

name: metrics-auth-role

9 +

subjects:

10 +

- kind: ServiceAccount

11 +

name: controller-manager

12 +

namespace: system

Original file line number Diff line number Diff line change

@@ -0,0 +1,9 @@

1 +

apiVersion: rbac.authorization.k8s.io/v1

2 +

kind: ClusterRole

3 +

metadata:

4 +

name: metrics-reader

5 +

rules:

6 +

- nonResourceURLs:

7 +

- "/metrics"

8 +

verbs:

9 +

- get

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4