Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 82241c8

Browse files
committed
Add ServiceAccount name field to Hooks CRD
This is used to optionally override the default serviceAccount of a Hook.
1 parent 46efea1 commit 82241c8

File tree

5 files changed

+47
-18
lines changed

5 files changed

+47
-18
lines changed

operator/apis/execution/v1/scancompletionhook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type ScanCompletionHookSpec struct {
4444
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
4545
Env []corev1.EnvVar `json:"env,omitempty"`
4646
Type HookType `json:"type"`
47+
// ServiceAccountName Name of the serviceAccount Name used. Should only be used if your hook needs specifc RBAC Access. Otherwise the hook is run using a "scan-completion-hook" service account. The service account should have at least "get" rights on scans.execution.experimental.securecodebox.io, and "get" & "patch" scans.execution.experimental.securecodebox.io/status
48+
ServiceAccountName *string `json:"serviceAccountName,omitempty"`
4749
}
4850

4951
// ScanCompletionHookStatus defines the observed state of ScanCompletionHook

operator/apis/execution/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

operator/config/crd/bases/execution.experimental.securecodebox.io_scancompletionhooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ spec:
156156
type: string
157157
type: object
158158
type: array
159+
serviceAccountName:
160+
description: ServiceAccountName Name of the serviceAccount Name used.
161+
Should only be used if your hook needs specifc RBAC Access. Otherwise
162+
the hook is run using a "scan-completion-hook" service account. The
163+
service account should have at least "get" rights on scans.execution.experimental.securecodebox.io,
164+
and "get" & "patch" scans.execution.experimental.securecodebox.io/status
165+
type: string
159166
type:
160167
description: HookType Defines weather the hook should be able to change
161168
the findings or is run in a read only mode.

operator/controllers/execution/scan_controller.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -985,25 +985,33 @@ func (r *ScanReconciler) setHookStatus(scan *executionv1.Scan) error {
985985

986986
func (r *ScanReconciler) createJobForHook(hook *executionv1.ScanCompletionHook, scan *executionv1.Scan, cliArgs []string) (string, error) {
987987
ctx := context.Background()
988-
rules := []rbacv1.PolicyRule{
989-
{
990-
APIGroups: []string{"execution.experimental.securecodebox.io"},
991-
Resources: []string{"scans"},
992-
Verbs: []string{"get", "list", "create"},
993-
},
994-
{
995-
APIGroups: []string{"execution.experimental.securecodebox.io"},
996-
Resources: []string{"scans/status"},
997-
Verbs: []string{"get", "patch"},
998-
},
999-
}
988+
1000989
serviceAccountName := "scan-completion-hook"
1001-
r.ensureServiceAccountExists(
1002-
hook.Namespace,
1003-
serviceAccountName,
1004-
"ScanCompletionHooks need to access the current scan to view where its results are stored",
1005-
rules,
1006-
)
990+
if hook.Spec.ServiceAccountName != nil {
991+
// Hook uses a custom ServiceAccount
992+
serviceAccountName = *hook.Spec.ServiceAccountName
993+
} else {
994+
// Check and create a serviceAccount for the hook in its namespace, if it doesn't already exist.
995+
rules := []rbacv1.PolicyRule{
996+
{
997+
APIGroups: []string{"execution.experimental.securecodebox.io"},
998+
Resources: []string{"scans"},
999+
Verbs: []string{"get"},
1000+
},
1001+
{
1002+
APIGroups: []string{"execution.experimental.securecodebox.io"},
1003+
Resources: []string{"scans/status"},
1004+
Verbs: []string{"get", "patch"},
1005+
},
1006+
}
1007+
1008+
r.ensureServiceAccountExists(
1009+
hook.Namespace,
1010+
serviceAccountName,
1011+
"ScanCompletionHooks need to access the current scan to view where its results are stored",
1012+
rules,
1013+
)
1014+
}
10071015

10081016
standardEnvVars := []corev1.EnvVar{
10091017
{

operator/crds/execution.experimental.securecodebox.io_scancompletionhooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ spec:
156156
type: string
157157
type: object
158158
type: array
159+
serviceAccountName:
160+
description: ServiceAccountName Name of the serviceAccount Name used.
161+
Should only be used if your hook needs specifc RBAC Access. Otherwise
162+
the hook is run using a "scan-completion-hook" service account. The
163+
service account should have at least "get" rights on scans.execution.experimental.securecodebox.io,
164+
and "get" & "patch" scans.execution.experimental.securecodebox.io/status
165+
type: string
159166
type:
160167
description: HookType Defines weather the hook should be able to change
161168
the findings or is run in a read only mode.

0 commit comments

Comments
 (0)