Skip to content

Commit

Permalink
chore: move token request rbac markers to separate pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Anish Ramasekar <[email protected]>
  • Loading branch information
aramase committed Jan 27, 2022
1 parent b9b6a45 commit 52340aa
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 50 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,14 @@ manifests: $(CONTROLLER_GEN) $(KUSTOMIZE)
@sed -i '1s/^/{{ if .Values.enableSecretRotation }}\n/gm; $$s/$$/\n{{ end }}/gm' manifest_staging/charts/secrets-store-csi-driver/templates/role-rotation.yaml
@sed -i '1s/^/{{ if .Values.enableSecretRotation }}\n/gm; s/namespace: .*/namespace: {{ .Release.Namespace }}/gm; $$s/$$/\n{{ end }}/gm' manifest_staging/charts/secrets-store-csi-driver/templates/role-rotation_binding.yaml

# Generate token requests specific RBAC
$(CONTROLLER_GEN) rbac:roleName=secretprovidertokenrequest-role paths="./controllers/tokenrequest" output:dir=config/rbac-tokenrequest
$(KUSTOMIZE) build config/rbac-tokenrequest -o manifest_staging/deploy/rbac-secretprovidertokenrequest.yaml
cp config/rbac-tokenrequest/role.yaml manifest_staging/charts/secrets-store-csi-driver/templates/role-tokenrequest.yaml
cp config/rbac-tokenrequest/role_binding.yaml manifest_staging/charts/secrets-store-csi-driver/templates/role-tokenrequest_binding.yaml
@sed -i '1s/^/{{ if .Values.tokenRequests }}\n/gm; $$s/$$/\n{{ end }}/gm' manifest_staging/charts/secrets-store-csi-driver/templates/role-tokenrequest.yaml
@sed -i '1s/^/{{ if .Values.tokenRequests }}\n/gm; s/namespace: .*/namespace: {{ .Release.Namespace }}/gm; $$s/$$/\n{{ end }}/gm' manifest_staging/charts/secrets-store-csi-driver/templates/role-tokenrequest_binding.yaml

.PHONY: generate-protobuf
generate-protobuf: $(PROTOC) $(PROTOC_GEN_GO) # generates protobuf
$(PROTOC) -I . provider/v1alpha1/service.proto --go_out=plugins=grpc:. --plugin=$(PROTOC_GEN_GO)
Expand Down
3 changes: 3 additions & 0 deletions config/rbac-tokenrequest/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resources:
- role.yaml
- role_binding.yaml
24 changes: 24 additions & 0 deletions config/rbac-tokenrequest/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: secretprovidertokenrequest-role
rules:
- apiGroups:
- ""
resources:
- serviceaccounts/token
verbs:
- create
- apiGroups:
- storage.k8s.io
resourceNames:
- secrets-store.csi.k8s.io
resources:
- csidrivers
verbs:
- get
- list
- watch
12 changes: 12 additions & 0 deletions config/rbac-tokenrequest/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: secretprovidertokenrequest-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: secretprovidertokenrequest-role
subjects:
- kind: ServiceAccount
name: secrets-store-csi-driver
namespace: kube-system
16 changes: 0 additions & 16 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ rules:
- get
- list
- watch
- apiGroups:
- ""
resources:
- serviceaccounts/token
verbs:
- create
- apiGroups:
- secrets-store.csi.x-k8s.io
resources:
Expand Down Expand Up @@ -55,13 +49,3 @@ rules:
- get
- patch
- update
- apiGroups:
- storage.k8s.io
resourceNames:
- secrets-store.csi.k8s.io
resources:
- csidrivers
verbs:
- get
- list
- watch
2 changes: 0 additions & 2 deletions controllers/secretproviderclasspodstatus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ func (r *SecretProviderClassPodStatusReconciler) ListOptionsLabelSelector() clie
// +kubebuilder:rbac:groups=secrets-store.csi.x-k8s.io,resources=secretproviderclasses,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
// +kubebuilder:rbac:groups="storage.k8s.io",resources=csidrivers,verbs=get;list;watch,resourceNames=secrets-store.csi.k8s.io
// +kubebuilder:rbac:groups="",resources="serviceaccounts/token",verbs=create

func (r *SecretProviderClassPodStatusReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.mutex.Lock()
Expand Down
23 changes: 23 additions & 0 deletions controllers/tokenrequest/tokenrequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package tokenrequest holds the RBAC permission annotations for the controller
// to create a serviceaccount token and pass it as part of Mount Request.
// ref: https://kubernetes-csi.github.io/docs/token-requests.html
package tokenrequest

// +kubebuilder:rbac:groups="storage.k8s.io",resources=csidrivers,verbs=get;list;watch,resourceNames=secrets-store.csi.k8s.io
// +kubebuilder:rbac:groups="",resources="serviceaccounts/token",verbs=create
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{ if .Values.tokenRequests }}

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: secretprovidertokenrequest-role
rules:
- apiGroups:
- ""
resources:
- serviceaccounts/token
verbs:
- create
- apiGroups:
- storage.k8s.io
resourceNames:
- secrets-store.csi.k8s.io
resources:
- csidrivers
verbs:
- get
- list
- watch
{{ end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{ if .Values.tokenRequests }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: secretprovidertokenrequest-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: secretprovidertokenrequest-role
subjects:
- kind: ServiceAccount
name: secrets-store-csi-driver
namespace: {{ .Release.Namespace }}
{{ end }}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ rules:
- get
- list
- watch
- apiGroups:
- ""
resources:
- serviceaccounts/token
verbs:
- create
- apiGroups:
- secrets-store.csi.x-k8s.io
resources:
Expand Down Expand Up @@ -56,16 +50,6 @@ rules:
- get
- patch
- update
- apiGroups:
- storage.k8s.io
resourceNames:
- secrets-store.csi.k8s.io
resources:
- csidrivers
verbs:
- get
- list
- watch
{{- if .Values.rbac.pspEnabled }}
- apiGroups:
- policy
Expand Down
16 changes: 0 additions & 16 deletions manifest_staging/deploy/rbac-secretproviderclass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ rules:
- get
- list
- watch
- apiGroups:
- ""
resources:
- serviceaccounts/token
verbs:
- create
- apiGroups:
- secrets-store.csi.x-k8s.io
resources:
Expand Down Expand Up @@ -59,16 +53,6 @@ rules:
- get
- patch
- update
- apiGroups:
- storage.k8s.io
resourceNames:
- secrets-store.csi.k8s.io
resources:
- csidrivers
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
35 changes: 35 additions & 0 deletions manifest_staging/deploy/rbac-secretprovidertokenrequest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: secretprovidertokenrequest-role
rules:
- apiGroups:
- ""
resources:
- serviceaccounts/token
verbs:
- create
- apiGroups:
- storage.k8s.io
resourceNames:
- secrets-store.csi.k8s.io
resources:
- csidrivers
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: secretprovidertokenrequest-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: secretprovidertokenrequest-role
subjects:
- kind: ServiceAccount
name: secrets-store-csi-driver
namespace: kube-system
6 changes: 6 additions & 0 deletions test/bats/e2e-provider.bats
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export VALIDATE_TOKENS_AUDIENCE=$(get_token_requests_audience)
run kubectl get clusterrole/secretprovidersyncing-role
assert_success

run kubectl get clusterrole/secretprovidertokenrequest-role
assert_success

run kubectl get clusterrolebinding/secretproviderclasses-rolebinding
assert_success

Expand All @@ -88,6 +91,9 @@ export VALIDATE_TOKENS_AUDIENCE=$(get_token_requests_audience)

run kubectl get clusterrolebinding/secretprovidersyncing-rolebinding
assert_success

run kubectl get clusterrolebinding/secretprovidertokenrequest-rolebinding
assert_success
}

@test "[v1alpha1] deploy e2e-provider secretproviderclass crd" {
Expand Down

0 comments on commit 52340aa

Please sign in to comment.