Skip to content

Commit 4c82478

Browse files
Merge pull request #2116 from Nordix/kashif/add-missing-rbac-metrics
✨ Add RBAC files for metrics authentication and authorization
2 parents 1c29480 + a6c6a15 commit 4c82478

11 files changed

Lines changed: 302 additions & 51 deletions

.golangci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ issues:
120120
linters:
121121
- gci
122122
- goconst
123+
- gosec
123124
- path: _test\.go
124125
linters:
125126
- errcheck

config/base/manager.yaml

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,56 @@ spec:
1919
webhook: metal3-io-v1alpha1-baremetalhost
2020
spec:
2121
containers:
22-
- command:
23-
- /baremetal-operator
24-
args:
25-
- --enable-leader-election
26-
- --tls-min-version=TLS13
27-
image: quay.io/metal3-io/baremetal-operator
28-
imagePullPolicy: Always
29-
env:
30-
- name: POD_NAME
31-
valueFrom:
32-
fieldRef:
33-
fieldPath: metadata.name
34-
- name: POD_NAMESPACE
35-
valueFrom:
36-
fieldRef:
37-
fieldPath: metadata.namespace
38-
envFrom:
39-
- configMapRef:
40-
name: ironic
41-
name: manager
42-
securityContext:
43-
allowPrivilegeEscalation: false
44-
capabilities:
45-
drop:
46-
- ALL
47-
privileged: false
48-
runAsUser: 65532
49-
runAsGroup: 65532
50-
livenessProbe:
51-
httpGet:
52-
path: /healthz
53-
port: 9440
54-
initialDelaySeconds: 10
55-
periodSeconds: 10
56-
timeoutSeconds: 2
57-
successThreshold: 1
58-
failureThreshold: 10
59-
readinessProbe:
60-
httpGet:
61-
path: /readyz
62-
port: 9440
63-
initialDelaySeconds: 10
64-
periodSeconds: 10
65-
timeoutSeconds: 2
66-
successThreshold: 1
67-
failureThreshold: 10
22+
- command:
23+
- /baremetal-operator
24+
args:
25+
- --enable-leader-election
26+
- --tls-min-version=TLS13
27+
ports:
28+
- containerPort: 8443
29+
protocol: TCP
30+
name: https
31+
image: quay.io/metal3-io/baremetal-operator
32+
imagePullPolicy: Always
33+
env:
34+
- name: POD_NAME
35+
valueFrom:
36+
fieldRef:
37+
fieldPath: metadata.name
38+
- name: POD_NAMESPACE
39+
valueFrom:
40+
fieldRef:
41+
fieldPath: metadata.namespace
42+
envFrom:
43+
- configMapRef:
44+
name: ironic
45+
name: manager
46+
securityContext:
47+
allowPrivilegeEscalation: false
48+
capabilities:
49+
drop:
50+
- ALL
51+
privileged: false
52+
runAsUser: 65532
53+
runAsGroup: 65532
54+
livenessProbe:
55+
httpGet:
56+
path: /healthz
57+
port: 9440
58+
initialDelaySeconds: 10
59+
periodSeconds: 10
60+
timeoutSeconds: 2
61+
successThreshold: 1
62+
failureThreshold: 10
63+
readinessProbe:
64+
httpGet:
65+
path: /readyz
66+
port: 9440
67+
initialDelaySeconds: 10
68+
periodSeconds: 10
69+
timeoutSeconds: 2
70+
successThreshold: 1
71+
failureThreshold: 10
6872
terminationGracePeriodSeconds: 10
6973
securityContext:
7074
runAsNonRoot: true

config/base/rbac/kustomization.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ resources:
99
- role_binding.yaml
1010
- leader_election_role.yaml
1111
- 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
21+
- metrics_service.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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
labels:
5+
control-plane: controller-manager
6+
name: controller-manager-metrics-service
7+
namespace: system
8+
spec:
9+
ports:
10+
- name: https
11+
port: 8443
12+
targetPort: https
13+
selector:
14+
control-plane: controller-manager

config/overlays/e2e/kustomization.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ generatorOptions:
3131

3232
# NOTE: These credentials are generated automatically in hack/ci-e2e.sh
3333
secretGenerator:
34-
- name: ironic-credentials
35-
files:
36-
- username=ironic-username
37-
- password=ironic-password
34+
- name: ironic-credentials
35+
files:
36+
- username=ironic-username
37+
- password=ironic-password

config/render/capm3.yaml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,6 +2435,34 @@ rules:
24352435
- watch
24362436
---
24372437
apiVersion: rbac.authorization.k8s.io/v1
2438+
kind: ClusterRole
2439+
metadata:
2440+
name: baremetal-operator-metrics-auth-role
2441+
rules:
2442+
- apiGroups:
2443+
- authentication.k8s.io
2444+
resources:
2445+
- tokenreviews
2446+
verbs:
2447+
- create
2448+
- apiGroups:
2449+
- authorization.k8s.io
2450+
resources:
2451+
- subjectaccessreviews
2452+
verbs:
2453+
- create
2454+
---
2455+
apiVersion: rbac.authorization.k8s.io/v1
2456+
kind: ClusterRole
2457+
metadata:
2458+
name: baremetal-operator-metrics-reader
2459+
rules:
2460+
- nonResourceURLs:
2461+
- /metrics
2462+
verbs:
2463+
- get
2464+
---
2465+
apiVersion: rbac.authorization.k8s.io/v1
24382466
kind: RoleBinding
24392467
metadata:
24402468
name: baremetal-operator-leader-election-rolebinding
@@ -2461,6 +2489,19 @@ subjects:
24612489
name: baremetal-operator-controller-manager
24622490
namespace: baremetal-operator-system
24632491
---
2492+
apiVersion: rbac.authorization.k8s.io/v1
2493+
kind: ClusterRoleBinding
2494+
metadata:
2495+
name: baremetal-operator-metrics-auth-rolebinding
2496+
roleRef:
2497+
apiGroup: rbac.authorization.k8s.io
2498+
kind: ClusterRole
2499+
name: baremetal-operator-metrics-auth-role
2500+
subjects:
2501+
- kind: ServiceAccount
2502+
name: baremetal-operator-controller-manager
2503+
namespace: baremetal-operator-system
2504+
---
24642505
apiVersion: v1
24652506
data:
24662507
CACHEURL: http://172.22.0.1/images
@@ -2477,6 +2518,21 @@ metadata:
24772518
---
24782519
apiVersion: v1
24792520
kind: Service
2521+
metadata:
2522+
labels:
2523+
control-plane: controller-manager
2524+
name: baremetal-operator-controller-manager-metrics-service
2525+
namespace: baremetal-operator-system
2526+
spec:
2527+
ports:
2528+
- name: https
2529+
port: 8443
2530+
targetPort: https
2531+
selector:
2532+
control-plane: controller-manager
2533+
---
2534+
apiVersion: v1
2535+
kind: Service
24802536
metadata:
24812537
name: baremetal-operator-webhook-service
24822538
namespace: baremetal-operator-system
@@ -2542,6 +2598,9 @@ spec:
25422598
- containerPort: 9443
25432599
name: webhook-server
25442600
protocol: TCP
2601+
- containerPort: 8443
2602+
name: https
2603+
protocol: TCP
25452604
readinessProbe:
25462605
failureThreshold: 10
25472606
httpGet:

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func main() {
137137
// namespace.
138138
flag.StringVar(&watchNamespace, "namespace", os.Getenv("WATCH_NAMESPACE"),
139139
"Namespace that the controller watches to reconcile host resources.")
140-
flag.StringVar(&metricsBindAddr, "metrics-addr", "127.0.0.1:8085",
140+
flag.StringVar(&metricsBindAddr, "metrics-addr", ":8443",
141141
"The address the metric endpoint binds to.")
142142
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
143143
"Enable leader election for controller manager. "+
@@ -217,7 +217,9 @@ func main() {
217217
Scheme: scheme,
218218
Metrics: metricsserver.Options{
219219
BindAddress: metricsBindAddr,
220+
SecureServing: true,
220221
FilterProvider: filters.WithAuthenticationAndAuthorization,
222+
TLSOpts: tlsOptionOverrides,
221223
},
222224
WebhookServer: webhook.NewServer(webhook.Options{
223225
Port: webhookPort,

0 commit comments

Comments
 (0)