Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit 7d35921

Browse files
author
B&R
committed
feat: Support for access tokens injected by the Backup Maker Operator just before the build starts / riotkit-org/backup-repository#299
1 parent f93a966 commit 7d35921

12 files changed

+126
-12
lines changed

pkg/generate/chart/cronjob.yaml

+26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ metadata:
77
labels:
88
riotkit.org/backup-maker: "true"
99
riotkit.org/jobName: "{{ .Values.name }}"
10+
{{ if .Values.operator.scheduledBackupName }}
11+
# Name of the kind: ScheduledBackup that defined this execution
12+
riotkit.org/scheduledBackupName: "{{ .Values.operator.scheduledBackupName }}"
13+
{{ end }}>
1014
annotations:
1115
riotkit.org/action: "{{ .Values.operationType }}"
1216
spec:
@@ -26,6 +30,28 @@ spec:
2630

2731
# Pod template
2832
template:
33+
metadata:
34+
labels:
35+
riotkit.org/backup-maker: "true"
36+
riotkit.org/jobName: "{{ .Values.name }}"
37+
38+
{{ if .Values.operator }}
39+
{{ if eq .Values.operator.dynamicToken "true" }}
40+
# ----------------------------
41+
# Backup Maker Operator labels
42+
# ----------------------------
43+
# Notice: This label should stay only on a kind that is created during execution
44+
# for example on Pod, Job, but not on CronJob. This label means operator will
45+
# update the kind: Secret just before the object is created
46+
riotkit.org/usesDynamicJWT: "true"
47+
{{ end }}
48+
{{ if .Values.operator.scheduledBackupName }}
49+
# Name of the kind: ScheduledBackup that defined this execution
50+
riotkit.org/scheduledBackupName: "{{ .Values.operator.scheduledBackupName }}"
51+
{{ end }}
52+
{{ end }}
53+
annotations:
54+
riotkit.org/action: "{{ .Values.operationType }}"
2955
spec:
3056
{{ include "podSpecTemplate" . | nindent 20 }}
3157
{{- end }}

pkg/generate/chart/job.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ metadata:
77
labels:
88
riotkit.org/backup-maker: "true"
99
riotkit.org/jobName: "{{ .Values.name }}"
10+
11+
{{ if .Values.operator }}
12+
{{ if eq .Values.operator.dynamicToken "true" }}
13+
riotkit.org/usesDynamicJWT: "true"
14+
{{ end }}
15+
{{ if .Values.operator.scheduledBackupName }}
16+
riotkit.org/scheduledBackupName: "{{ .Values.operator.scheduledBackupName }}"
17+
{{ end }}
18+
{{ end }}
1019
annotations:
1120
riotkit.org/action: "{{ .Values.operationType }}"
1221
spec:

pkg/generate/chart/pod.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ metadata:
77
labels:
88
riotkit.org/backup-maker: "true"
99
riotkit.org/jobName: "{{ .Values.name }}"
10+
11+
{{ if .Values.operator }}
12+
{{ if eq .Values.operator.dynamicToken "true" }}
13+
riotkit.org/usesDynamicJWT: "true"
14+
{{ end }}
15+
{{ if .Values.operator.scheduledBackupName }}
16+
riotkit.org/scheduledBackupName: "{{ .Values.operator.scheduledBackupName }}"
17+
{{ end }}
18+
{{ end }}
1019
annotations:
1120
riotkit.org/action: "{{ .Values.operationType }}"
1221
spec:

pkg/generate/e2e_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package generate_test
22

33
import (
44
"fmt"
5+
"github.com/docker/docker/api/types/container"
56
"github.com/pkg/errors"
67
"github.com/riotkit-org/br-backup-maker/utils/testingutils"
78
"github.com/testcontainers/testcontainers-go"
@@ -153,7 +154,9 @@ Repository:
153154
}
154155

155156
cr := testcontainers.ContainerRequest{}
156-
cr.NetworkMode = "host"
157+
cr.HostConfigModifier = func(c *container.HostConfig) {
158+
c.NetworkMode = "host"
159+
}
157160
cr.Mounts = []testcontainers.ContainerMount{
158161
{
159162
Source: testcontainers.DockerBindMountSource{

pkg/generate/helm.go

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func (t *Templating) RenderChart(script string, gpgKeyContent string, schedule s
5353
"operationType": operation,
5454
"isGPGSealedSecret": isSealedSecret,
5555
"backupMakerImage": version.GetSelfContainerImage(),
56+
"operator": map[string]interface{}{
57+
"scheduledBackupName": "",
58+
"dynamicToken": "false",
59+
},
5660
"serviceAccount": map[string]interface{}{
5761
"name": "default",
5862
"create": false,

pkg/generate/templates/backup/kubectl.tmpl

+6
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ for ns in limit_namespaces:
123123

124124
print(" >> Packing and uploading")
125125
env = os.environ
126+
{{ if .Repository.token }}
126127
env["BM_AUTH_TOKEN"] = "{{ .Repository.token }}"
128+
{{ else }}
129+
env["BM_AUTH_LOGIN"] = "{{ .Repository.login }}"
130+
env["BM_AUTH_PASSWORD"] = "{{ .Repository.password }}"
131+
{{ end }}
132+
127133
env["BM_COLLECTION_ID"] = "{{ .Repository.collectionId }}"
128134
env["BM_PASSPHRASE"] = "{{ with .Repository.passphrase }}{{ . }}{{ end }}"
129135

pkg/generate/templates/backup/mysql-dump.tmpl

+6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ COMMAND="tar -zcvf - ${FILES_TO_PACK} /mnt/workspace/backup-db.sql.gz"
3737
{{ end }}
3838

3939

40+
{{ if .Repository.token }}
4041
export BM_AUTH_TOKEN="{{ .Repository.token }}";
42+
{{ else }}
43+
export BM_AUTH_LOGIN="{{ .Repository.login }}";
44+
export BM_AUTH_PASSWORD="{{ .Repository.password }}";
45+
{{ end }}
46+
4147
export BM_COLLECTION_ID="{{ .Repository.collectionId }}";
4248
export BM_PASSPHRASE="{{ with .Repository.passphrase }}{{ . }}{{ end }}";
4349

pkg/generate/templates/backup/postgres.tmpl

+5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ COMMAND="tar -zcvf - ${FILES_TO_PACK} /mnt/workspace/backup-db.sql.gz"
4848
{{ end }}
4949

5050

51+
{{ if .Repository.token }}
5152
export BM_AUTH_TOKEN="{{ .Repository.token }}";
53+
{{ else }}
54+
export BM_AUTH_LOGIN="{{ .Repository.login }}";
55+
export BM_AUTH_PASSWORD="{{ .Repository.password }}";
56+
{{ end }}
5257
export BM_COLLECTION_ID="{{ .Repository.collectionId }}";
5358
export BM_PASSPHRASE="{{ with .Repository.passphrase }}{{ . }}{{ end }}";
5459

pkg/generate/templates/backup/tar.tmpl

+5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ fi
1313

1414
COMMAND="(tar --exclude='../' -zcvf - ${tarArgs}) | cat -";
1515

16+
{{ if .Repository.token }}
1617
export BM_AUTH_TOKEN="{{ .Repository.token }}";
18+
{{ else }}
19+
export BM_AUTH_LOGIN="{{ .Repository.login }}";
20+
export BM_AUTH_PASSWORD="{{ .Repository.password }}";
21+
{{ end }}
1722
export BM_COLLECTION_ID="{{ .Repository.collectionId }}";
1823
export BM_PASSPHRASE="{{ with .Repository.passphrase }}{{ . }}{{ end }}";
1924

pkg/generate/templating_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package generate_test
22

33
import (
4-
generate2 "github.com/riotkit-org/br-backup-maker/pkg/generate"
4+
generate "github.com/riotkit-org/br-backup-maker/pkg/generate"
55
"github.com/stretchr/testify/assert"
66
"os"
77
"testing"
@@ -118,9 +118,9 @@ HelmValues:
118118
claimName: passbolt-pvc
119119
`))
120120

121-
tpl := generate2.Templating{}
121+
tpl := generate.Templating{}
122122
println(file.Name())
123-
cfg, err := tpl.LoadConfiguration("passbolt-pg15", file.Name(), generate2.Config{})
123+
cfg, err := tpl.LoadConfiguration("passbolt-pg15", file.Name(), generate.Config{})
124124

125125
assert.Nil(t, err)
126126

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# System-specific variables, in this case specific to PostgreSQL
2+
# ${...} and $(...) syntax will be evaluated in target environment e.g. Kubernetes POD
3+
Params:
4+
hostname: postgres.db.svc.cluster.local
5+
port: 5432
6+
db: rkc-test
7+
user: riotkit
8+
password: "${DB_PASSWORD}" # injects a shell-syntax, put your password in a `kind: Secret` and mount as environment variable. You can also use $(cat /mnt/secret) syntax, be aware of newlines!
9+
10+
# Generic repository access details. Everything here will land AS IS into the bash script.
11+
# This means that any ${...} and $(...) will be executed in target environment e.g. inside Kubernetes POD
12+
Repository:
13+
url: "https://example.org"
14+
token: "${BR_TOKEN}"
15+
encryptionKeyPath: "/var/lib/backup-repository/encryption.key"
16+
passphrase: "${GPG_PASSPHRASE}"
17+
recipient: "[email protected]"
18+
collectionId: "111-222-333-444"
19+
20+
# Generic values for Helm used to generate jobs/pods. Those values will overwrite others.
21+
# Notice: Environment variables with '${...}' and '$(...)' will be evaluated in LOCAL SHELL DURING BUILD
22+
HelmValues:
23+
name: "hello-world"
24+
operator:
25+
dynamicToken: "true"
26+
scheduledBackupName: "db-1"
27+
env:
28+
# if specified, then will be added to `kind: Secret` and injected into POD as environment
29+
# the value from ${GPG_PASSPHRASE} will be retrieved from the SHELL DURING THE BUILD
30+
GPG_PASSPHRASE: "${GPG_PASSPHRASE}"
31+
32+
# most secure way for Kubernetes is to not provide secrets there, but define them as environment variables
33+
# inside SealedSecrets - all encryptedData keys will be accessible as environment variables inside container
34+

test_generator.mk

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
bmg_test_postgres_backup:
22
${BM_BIN_PATH} procedure backup \
3-
--definition=generate/test_data/examples/postgres.yaml \
4-
--template postgres
3+
--definition=pkg/generate/test_data/examples/postgres.yaml \
4+
--template pg15
55

66
bmg_test_postgres_backup_k8s:
77
${BM_BIN_PATH} procedure backup \
8-
--definition=generate/test_data/examples/postgres.yaml \
9-
--template postgres \
8+
--definition=pkg/generate/test_data/examples/postgres.yaml \
9+
--template pg15 \
1010
--kubernetes \
11-
--gpg-key-path generate/test_data/examples/gpg.key
11+
--gpg-key-path pkg/generate/test_data/examples/gpg.key
12+
13+
bmg_test_postgres_backup_dynamic_jwt_k8s:
14+
${BM_BIN_PATH} procedure backup \
15+
--definition=pkg/generate/test_data/examples/postgres-with-operator-generated-jwt.yaml \
16+
--template pg15 \
17+
--kubernetes \
18+
--gpg-key-path pkg/generate/test_data/examples/gpg.key
1219

1320
bmg_test_postgres_backup_k8s_sealed_secret:
1421
${BM_BIN_PATH} procedure backup \
15-
--definition=generate/test_data/examples/postgres.yaml \
16-
--template postgres \
22+
--definition=pkg/generate/test_data/examples/postgres.yaml \
23+
--template pg15 \
1724
--kubernetes \
18-
--gpg-key-path generate/test_data/examples/valid-sealed-secret.yaml
25+
--gpg-key-path pkg/generate/test_data/examples/valid-sealed-secret.yaml

0 commit comments

Comments
 (0)