Skip to content

Commit 7b7489f

Browse files
authored
feat(shard): Add audit policy options (#141)
Add an easy way to mount a ConfigMap containing the policy file into the shard container and let the operator configure the --audit-policy-file flag for the target path. on-behalf-of: @eon-se opensource@eon.com Signed-off-by: Maximilian Blatt <maximilian.blatt.external@eon.com>
1 parent 5d0a511 commit 7b7489f

File tree

10 files changed

+241
-5
lines changed

10 files changed

+241
-5
lines changed

config/crd/bases/operator.kcp.io_rootshards.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ spec:
5151
properties:
5252
audit:
5353
properties:
54+
policy:
55+
description: Audit policy configuration.
56+
properties:
57+
configMap:
58+
description: |-
59+
ConfigMap is a reference to the ConfigMap containing the audit policy
60+
file which is mounted into the container.
61+
properties:
62+
key:
63+
description: Key in the data.
64+
type: string
65+
name:
66+
description: Name of the object.
67+
type: string
68+
required:
69+
- key
70+
- name
71+
type: object
72+
type: object
5473
webhook:
5574
properties:
5675
batchBufferSize:

config/crd/bases/operator.kcp.io_shards.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ spec:
5151
properties:
5252
audit:
5353
properties:
54+
policy:
55+
description: Audit policy configuration.
56+
properties:
57+
configMap:
58+
description: |-
59+
ConfigMap is a reference to the ConfigMap containing the audit policy
60+
file which is mounted into the container.
61+
properties:
62+
key:
63+
description: Key in the data.
64+
type: string
65+
name:
66+
description: Name of the object.
67+
type: string
68+
required:
69+
- key
70+
- name
71+
type: object
72+
type: object
5473
webhook:
5574
properties:
5675
batchBufferSize:

internal/resources/utils/audit.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,55 @@ import (
2626
)
2727

2828
func applyAuditConfiguration(deployment *appsv1.Deployment, config *operatorv1alpha1.AuditSpec) *appsv1.Deployment {
29-
if config == nil || config.Webhook == nil {
29+
if config == nil {
3030
return deployment
3131
}
3232

33-
return applyAuditWebhookConfiguration(deployment, *config.Webhook)
33+
applyAuditPolicyConfiguration(deployment, config.Policy)
34+
applyAuditWebhookConfiguration(deployment, config.Webhook)
35+
return deployment
3436
}
3537

36-
func applyAuditWebhookConfiguration(deployment *appsv1.Deployment, config operatorv1alpha1.AuditWebhookSpec) *appsv1.Deployment {
38+
func applyAuditPolicyConfiguration(deployment *appsv1.Deployment, config *operatorv1alpha1.AuditPolicySpec) {
39+
if config == nil {
40+
return
41+
}
42+
43+
podSpec := deployment.Spec.Template.Spec
44+
45+
var extraArgs []string
46+
47+
if config.ConfigMap != nil {
48+
volumeName := "audit-policy"
49+
mountPath := "/etc/kcp/audit/policy"
50+
51+
podSpec.Volumes = append(podSpec.Volumes, corev1.Volume{
52+
Name: volumeName,
53+
VolumeSource: corev1.VolumeSource{
54+
ConfigMap: &corev1.ConfigMapVolumeSource{
55+
LocalObjectReference: corev1.LocalObjectReference{
56+
Name: config.ConfigMap.Name,
57+
},
58+
},
59+
},
60+
})
61+
podSpec.Containers[0].VolumeMounts = append(podSpec.Containers[0].VolumeMounts, corev1.VolumeMount{
62+
Name: volumeName,
63+
ReadOnly: true,
64+
MountPath: mountPath,
65+
})
66+
extraArgs = append(extraArgs, fmt.Sprintf("--audit-policy-file=%s/%s", mountPath, config.ConfigMap.Key))
67+
}
68+
69+
podSpec.Containers[0].Args = append(podSpec.Containers[0].Args, extraArgs...)
70+
deployment.Spec.Template.Spec = podSpec
71+
}
72+
73+
func applyAuditWebhookConfiguration(deployment *appsv1.Deployment, config *operatorv1alpha1.AuditWebhookSpec) {
74+
if config == nil {
75+
return
76+
}
77+
3778
podSpec := deployment.Spec.Template.Spec
3879

3980
var extraArgs []string
@@ -109,6 +150,4 @@ func applyAuditWebhookConfiguration(deployment *appsv1.Deployment, config operat
109150

110151
podSpec.Containers[0].Args = append(podSpec.Containers[0].Args, extraArgs...)
111152
deployment.Spec.Template.Spec = podSpec
112-
113-
return deployment
114153
}

sdk/apis/operator/v1alpha1/common.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ type ObjectReference struct {
7070
Group string `json:"group,omitempty"`
7171
}
7272

73+
// LocalDataKeyReference is a reference to a namespace-local object storing
74+
// key-value data, i.e. ConfigMap or Secret.
75+
type LocalDataKeyReference struct {
76+
// Name of the object.
77+
Name string `json:"name"`
78+
79+
// Key in the data.
80+
Key string `json:"key"`
81+
}
82+
7383
type Certificate string
7484

7585
const (

sdk/apis/operator/v1alpha1/shard_types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ type CommonShardSpec struct {
8787

8888
type AuditSpec struct {
8989
Webhook *AuditWebhookSpec `json:"webhook,omitempty"`
90+
91+
// Audit policy configuration.
92+
Policy *AuditPolicySpec `json:"policy,omitempty"`
93+
}
94+
95+
type AuditPolicySpec struct {
96+
// ConfigMap is a reference to the ConfigMap containing the audit policy
97+
// file which is mounted into the container.
98+
ConfigMap *LocalDataKeyReference `json:"configMap,omitempty"`
9099
}
91100

92101
type ShardCacheConfig struct {

sdk/apis/operator/v1alpha1/zz_generated.deepcopy.go

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

sdk/applyconfiguration/operator/v1alpha1/auditpolicyspec.go

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

sdk/applyconfiguration/operator/v1alpha1/auditspec.go

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

sdk/applyconfiguration/operator/v1alpha1/localdatakeyreference.go

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

sdk/applyconfiguration/utils.go

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

0 commit comments

Comments
 (0)