-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpg.rego
149 lines (126 loc) · 3.31 KB
/
pg.rego
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
package pginject
import rego.v1
vault_policy := "db-access"
add_vault_ops contains operation if {
input.TaskGroups[g].Tasks[t].Meta.postgres
object.get(input.TaskGroups[g].Tasks[t], "Vault", null) == null
operation := {
"op": "add",
"path": sprintf("/TaskGroups/%d/Tasks/%d/Vault", [g, t]),
"value": {
"ChangeMode": "restart",
"ChangeSignal": "SIGHUP",
"Env": true,
"Namespace": "",
"Policies": [],
},
}
}
add_vault_policy_ops contains operation if {
input.TaskGroups[g].Tasks[t].Meta.postgres
operation := {
"op": "add",
"path": sprintf("/TaskGroups/%d/Tasks/%d/Vault/Policies/-", [g, t]),
"value": vault_policy,
}
}
add_env_template_block_ops contains operation if {
input.TaskGroups[g].Tasks[t].Meta.postgres
object.get(input.TaskGroups[g].Tasks[t], "Templates", null) == null
operation := {
"op": "add",
"path": sprintf("/TaskGroups/%d/Tasks/%d/Templates", [g, t]),
"value": [],
}
}
add_env_template_ops contains operation if {
input.TaskGroups[g].Tasks[t].Meta.postgres
operation := {
"op": "add",
"path": sprintf("/TaskGroups/%d/Tasks/%d/Templates/-", [g, t]),
"value": {
"ChangeMode": "restart",
"ChangeScript": null,
"ChangeSignal": "",
"DestPath": "${NOMAD_SECRETS_DIR}/postgres.env",
"EmbeddedTmpl": env_tmpl,
"Envvars": true,
"ErrMissingKey": false,
"Gid": null,
"LeftDelim": "{{",
"Perms": "0644",
"RightDelim": "}}",
"SourcePath": "",
"Splay": 5000000000,
"Uid": null,
"VaultGrace": 0,
"Wait": null,
},
}
}
env_tmpl := native_tmpl if {
input.TaskGroups[g].Tasks[t].Meta.postgres == "native"
native_tmpl := concat("\n", [
"{{ range nomadService \"postgres\" }}",
"PGHOSTADDR={{ .Address }}",
"PGPORT={{ .Port }}",
"{{ end }}",
"PGDATABASE=postgres",
"{{ with secret \"postgres/creds/dev\" }}",
"PGUSER={{ .Data.username }}",
"PGPASSWORD={{ .Data.password }}",
"{{ end }}",
])
}
env_tmpl := spring_boot_tmpl if {
input.TaskGroups[g].Tasks[t].Meta.postgres == "springboot"
spring_boot_tmpl := concat("\n", [
"{{ range nomadService \"postgres\" }}",
"SPRING_DATASOURCE_URL=jdbc:postgresql://{{ .Address }}:{{ .Port }}/postgres",
"{{ end }}",
"{{ with secret \"postgres/creds/dev\" }}",
"SPRING_DATASOURCE_USERNAME={{ .Data.username }}",
"SPRING_DATASOURCE_PASSWORD={{ .Data.password }}",
"{{ end }}",
])
}
add_constaint_block_ops contains operation if {
input.TaskGroups[g].Tasks[t].Meta.postgres
object.get(input.TaskGroups[g].Tasks[t], "Constraints", null) == null
operation := {
"op": "add",
"path": sprintf("/TaskGroups/%d/Constraints", [g]),
"value": [],
}
}
add_vault_constraint_block_ops contains operation if {
input.TaskGroups[g].Tasks[t].Meta.postgres
constraints := object.get(input.TaskGroups[g].Tasks[t], "Constraints", [])
every constraint in constraints {
constraint != {
"LTarget": "${attr.vault.version}",
"Operand": "semver",
"RTarget": ">= 0.6.1",
}
}
operation := {
"op": "add",
"path": sprintf("/TaskGroups/%d/Constraints/-", [g]),
"value": {
"LTarget": "${attr.vault.version}",
"Operand": "semver",
"RTarget": ">= 0.6.1",
},
}
}
patch := [operation |
some ops in [
add_vault_ops,
add_vault_policy_ops,
add_env_template_block_ops,
add_env_template_ops,
add_constaint_block_ops,
add_vault_constraint_block_ops,
]
operation := ops[_]
]