forked from open-cluster-management-io/policy-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy-create-ns-backup-triliovault-for-kubernetes-templatized.yaml
254 lines (253 loc) · 10.5 KB
/
policy-create-ns-backup-triliovault-for-kubernetes-templatized.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# This policy creates a namespace based backup using Triliovault for Kubernetes (TVK)
# on all OpenShift managed clusters with a label "protected-by=triliovault".
#
# This policy creates backup for all the namespaces with a lable "protected-by=tvk-ns-backup"
#
# To create the namespace backup, following process is followed
# 1. Create the target object to store backup data
# This policy creates AWS S3 based target storage for storing backup images.
# 2. Create a backup plan that references the following:
# Namespace to be backed up
# Default Scheduling Policy - create daily 1 backup
# Target to where the data should be backed up
# 3. Apply the Backup CR that references the Backup
# This policy creates a full backup.
#
# IMPORTANT: Please follow below instructions for the policy to work
# On the hub cluster, create a secret aws-s3-secret and a configmap aws-s3-configmap
# in the namespace where this policy is placed. The secret and configmap have AWS S3
# details for target creation.
#
# 1 Secret Example yaml (replace the values)
# apiVersion: v1
# kind: Secret
# metadata:
# name: aws-s3-secret
# type: Opaque
# stringData:
# accessKey: "PROVIDE_ACCESS_KEY"
# secretKey: "PROVIDE_SECRET_KEY"
#
# 2 ConfigMap Example yaml (replace the values)
# apiVersion: v1
# kind: ConfigMap
# metadata:
# name: aws-s3-configmap
# Data:
# bucketName: "PROVIDE_S3_BUCKET_NAME"
# region: "PROVIDE_REGION"
# thresholdCapacity: "100Gi"
#
apiVersion: policy.open-cluster-management.io/v1
kind: Policy
metadata:
name: policy-kyverno-tvk-create-ns-backup
annotations:
policy.open-cluster-management.io/categories: CA Security Assessment and Authorization
policy.open-cluster-management.io/standards: NIST SP 800-53
policy.open-cluster-management.io/controls: CA-2 Security Assessments, CA-7 Continuous Monitoring
spec:
disabled: false
remediationAction: inform
policy-templates:
- objectDefinition:
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:
name: policy-kyverno-tvk-s3-secret
spec:
remediationAction: enforce
severity: high
object-templates:
- complianceType: musthave
objectDefinition:
apiVersion: v1
kind: Secret
metadata:
name: tvk-s3-secret
namespace: default
type: Opaque
data:
accessKey: '{{hub fromSecret "" "aws-s3-secret" "accessKey" hub}}'
secretKey: '{{hub fromSecret "" "aws-s3-secret" "secretKey" hub}}'
- objectDefinition:
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:
name: policy-kyverno-tvk-scheduling-policy
spec:
remediationAction: inform
severity: high
object-templates:
- complianceType: musthave
objectDefinition:
apiVersion: triliovault.trilio.io/v1
kind: Policy
metadata:
name: trilio-daily-schedule-policy
namespace: default
spec:
scheduleConfig:
schedule:
- 0 23 * * *
type: Schedule
- objectDefinition:
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:
name: policy-kyverno-tvk-retention-policy
spec:
remediationAction: inform
severity: high
object-templates:
- complianceType: musthave
objectDefinition:
apiVersion: triliovault.trilio.io/v1
kind: Policy
metadata:
name: trilio-latest-retention-policy
namespace: default
spec:
default: false
retentionConfig:
latest: 5
type: Retention
- objectDefinition:
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:
name: policy-kyverno-tvk-create-ns-backup
spec:
remediationAction: enforce
severity: high
object-templates:
- complianceType: musthave
objectDefinition:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: kyverno-tvk-create-ns-backup
annotations:
policies.kyverno.io/title: Create Namespace Backup
policies.kyverno.io/category: TrilioVault for Kubernetes
kyverno.io/kyverno-version: 1.6.2
policies.kyverno.io/minversion: 1.6.2
kyverno.io/kubernetes-version: "1.21-1.22"
policies.kyverno.io/subject: Policy
policies.kyverno.io/description: >-
Create Target, BackupPlan and Backup for any namespace with the label "protected-by: tvk-ns-backup"
This policy works best to protect a namespace by creating backup via lables.
spec:
background: true
rules:
- name: kyverno-tvk-create-target
match:
resources:
kinds:
- Namespace
selector:
matchLabels:
protected-by: tvk-ns-backup # match with a corresponding ClusterPolicy that checks for this label
generate:
apiVersion: triliovault.trilio.io/v1
kind: Target
name: tvk-{{ `{{request.object.metadata.name}}` }}-s3-target
namespace: "{{ `{{request.object.metadata.name}}` }}"
data:
metadata:
name: tvk-{{ `{{request.object.metadata.name}}` }}-s3-target
namespace: "{{ `{{request.object.metadata.name}}` }}"
spec:
type: "ObjectStore"
vendor: AWS
objectStoreCredentials:
bucketName: '{{hub fromConfigMap "" "aws-s3-configmap" "bucketName" hub}}'
region: '{{hub fromConfigMap "" "aws-s3-configmap" "region" hub}}'
credentialSecret:
name: tvk-s3-secret
namespace: default
thresholdCapacity: '{{hub fromConfigMap "" "aws-s3-configmap" "thresholdCapacity" hub}}'
- name: kyverno-tvk-create-backupplan
match:
resources:
kinds:
- Namespace
selector:
matchLabels:
protected-by: tvk-ns-backup # match with a corresponding ClusterPolicy that checks for this label
generate:
apiVersion: triliovault.trilio.io/v1
kind: BackupPlan
name: tvk-{{ `{{request.object.metadata.name}}` }}-backupplan
namespace: "{{ `{{request.object.metadata.name}}` }}"
data:
metadata:
name: tvk-{{ `{{request.object.metadata.name}}` }}-backupplan
namespace: "{{ `{{request.object.metadata.name}}` }}"
spec:
backupNamespace: "{{ `{{request.object.metadata.name}}` }}"
backupConfig:
target:
name: tvk-{{ `{{request.object.metadata.name}}` }}-s3-target
namespace: "{{ `{{request.object.metadata.name}}` }}"
schedulePolicy:
fullBackupPolicy:
name: trilio-daily-schedule-policy
namespace: default
retentionPolicy:
name: trilio-latest-retention-policy
namespace: default
backupPlanFlags:
skipImageBackup: true
retainHelmApps: false
- name: kyverno-tvk-create-backup
match:
resources:
kinds:
- Namespace
selector:
matchLabels:
protected-by: tvk-ns-backup # match with a corresponding ClusterPolicy that checks for this label
generate:
apiVersion: triliovault.trilio.io/v1
kind: Backup
name: tvk-{{ `{{request.object.metadata.name}}` }}-backup
namespace: "{{ `{{request.object.metadata.name}}` }}"
data:
metadata:
name: tvk-{{ `{{request.object.metadata.name}}` }}-backup
namespace: "{{ `{{request.object.metadata.name}}` }}"
spec:
type: Full
backupPlan:
name: tvk-{{ `{{request.object.metadata.name}}` }}-backupplan
namespace: "{{ `{{request.object.metadata.name}}` }}"
---
apiVersion: apps.open-cluster-management.io/v1
kind: PlacementRule
metadata:
name: policy-kyverno-tvk-create-ns-backup-placement
spec:
clusterSelector:
matchExpressions:
- key: protected-by
operator: In
values:
- triliovault
- key: vendor
operator: In
values:
- OpenShift
---
apiVersion: policy.open-cluster-management.io/v1
kind: PlacementBinding
metadata:
name: policy-kyverno-tvk-create-ns-backup-placement
placementRef:
name: policy-kyverno-tvk-create-ns-backup-placement
apiGroup: apps.open-cluster-management.io
kind: PlacementRule
subjects:
- name: policy-kyverno-tvk-create-ns-backup
apiGroup: policy.open-cluster-management.io
kind: Policy