Skip to content

Commit

Permalink
Merge pull request #2116 from Nordix/kashif/add-missing-rbac-metrics
Browse files Browse the repository at this point in the history
✨ Add RBAC files for metrics authentication and authorization
  • Loading branch information
metal3-io-bot authored Dec 17, 2024
2 parents 1c29480 + a6c6a15 commit 4c82478
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 51 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ issues:
linters:
- gci
- goconst
- gosec
- path: _test\.go
linters:
- errcheck
Expand Down
96 changes: 50 additions & 46 deletions config/base/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,56 @@ spec:
webhook: metal3-io-v1alpha1-baremetalhost
spec:
containers:
- command:
- /baremetal-operator
args:
- --enable-leader-election
- --tls-min-version=TLS13
image: quay.io/metal3-io/baremetal-operator
imagePullPolicy: Always
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
envFrom:
- configMapRef:
name: ironic
name: manager
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsUser: 65532
runAsGroup: 65532
livenessProbe:
httpGet:
path: /healthz
port: 9440
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 2
successThreshold: 1
failureThreshold: 10
readinessProbe:
httpGet:
path: /readyz
port: 9440
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 2
successThreshold: 1
failureThreshold: 10
- command:
- /baremetal-operator
args:
- --enable-leader-election
- --tls-min-version=TLS13
ports:
- containerPort: 8443
protocol: TCP
name: https
image: quay.io/metal3-io/baremetal-operator
imagePullPolicy: Always
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
envFrom:
- configMapRef:
name: ironic
name: manager
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsUser: 65532
runAsGroup: 65532
livenessProbe:
httpGet:
path: /healthz
port: 9440
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 2
successThreshold: 1
failureThreshold: 10
readinessProbe:
httpGet:
path: /readyz
port: 9440
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 2
successThreshold: 1
failureThreshold: 10
terminationGracePeriodSeconds: 10
securityContext:
runAsNonRoot: true
Expand Down
10 changes: 10 additions & 0 deletions config/base/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ resources:
- role_binding.yaml
- leader_election_role.yaml
- leader_election_role_binding.yaml
# The following RBAC configurations are used to protect
# the metrics endpoint with authn/authz. These configurations
# ensure that only authorized users and service accounts
# can access the metrics endpoint. Comment the following
# permissions if you want to disable this protection.
# More info: https://book.kubebuilder.io/reference/metrics.html
- metrics_auth_role.yaml
- metrics_auth_role_binding.yaml
- metrics_reader_role.yaml
- metrics_service.yaml
17 changes: 17 additions & 0 deletions config/base/rbac/metrics_auth_role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: metrics-auth-role
rules:
- apiGroups:
- authentication.k8s.io
resources:
- tokenreviews
verbs:
- create
- apiGroups:
- authorization.k8s.io
resources:
- subjectaccessreviews
verbs:
- create
12 changes: 12 additions & 0 deletions config/base/rbac/metrics_auth_role_binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: metrics-auth-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: metrics-auth-role
subjects:
- kind: ServiceAccount
name: controller-manager
namespace: system
9 changes: 9 additions & 0 deletions config/base/rbac/metrics_reader_role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: metrics-reader
rules:
- nonResourceURLs:
- "/metrics"
verbs:
- get
14 changes: 14 additions & 0 deletions config/base/rbac/metrics_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
labels:
control-plane: controller-manager
name: controller-manager-metrics-service
namespace: system
spec:
ports:
- name: https
port: 8443
targetPort: https
selector:
control-plane: controller-manager
8 changes: 4 additions & 4 deletions config/overlays/e2e/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ generatorOptions:

# NOTE: These credentials are generated automatically in hack/ci-e2e.sh
secretGenerator:
- name: ironic-credentials
files:
- username=ironic-username
- password=ironic-password
- name: ironic-credentials
files:
- username=ironic-username
- password=ironic-password
59 changes: 59 additions & 0 deletions config/render/capm3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2435,6 +2435,34 @@ rules:
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: baremetal-operator-metrics-auth-role
rules:
- apiGroups:
- authentication.k8s.io
resources:
- tokenreviews
verbs:
- create
- apiGroups:
- authorization.k8s.io
resources:
- subjectaccessreviews
verbs:
- create
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: baremetal-operator-metrics-reader
rules:
- nonResourceURLs:
- /metrics
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: baremetal-operator-leader-election-rolebinding
Expand All @@ -2461,6 +2489,19 @@ subjects:
name: baremetal-operator-controller-manager
namespace: baremetal-operator-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: baremetal-operator-metrics-auth-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: baremetal-operator-metrics-auth-role
subjects:
- kind: ServiceAccount
name: baremetal-operator-controller-manager
namespace: baremetal-operator-system
---
apiVersion: v1
data:
CACHEURL: http://172.22.0.1/images
Expand All @@ -2477,6 +2518,21 @@ metadata:
---
apiVersion: v1
kind: Service
metadata:
labels:
control-plane: controller-manager
name: baremetal-operator-controller-manager-metrics-service
namespace: baremetal-operator-system
spec:
ports:
- name: https
port: 8443
targetPort: https
selector:
control-plane: controller-manager
---
apiVersion: v1
kind: Service
metadata:
name: baremetal-operator-webhook-service
namespace: baremetal-operator-system
Expand Down Expand Up @@ -2542,6 +2598,9 @@ spec:
- containerPort: 9443
name: webhook-server
protocol: TCP
- containerPort: 8443
name: https
protocol: TCP
readinessProbe:
failureThreshold: 10
httpGet:
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func main() {
// namespace.
flag.StringVar(&watchNamespace, "namespace", os.Getenv("WATCH_NAMESPACE"),
"Namespace that the controller watches to reconcile host resources.")
flag.StringVar(&metricsBindAddr, "metrics-addr", "127.0.0.1:8085",
flag.StringVar(&metricsBindAddr, "metrics-addr", ":8443",
"The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. "+
Expand Down Expand Up @@ -217,7 +217,9 @@ func main() {
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsBindAddr,
SecureServing: true,
FilterProvider: filters.WithAuthenticationAndAuthorization,
TLSOpts: tlsOptionOverrides,
},
WebhookServer: webhook.NewServer(webhook.Options{
Port: webhookPort,
Expand Down
Loading

0 comments on commit 4c82478

Please sign in to comment.