-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvault.go
More file actions
222 lines (188 loc) · 5.91 KB
/
vault.go
File metadata and controls
222 lines (188 loc) · 5.91 KB
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
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/uswitch/vault-webhook/pkg/apis/vaultwebhook.uswitch.com/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
func createPatch(pod *corev1.Pod, namespace string, databases []database) ([]byte, error) {
patch := []patchOperation{}
patch = append(patch, addVolume(pod)...)
pod.Spec.Containers = addVolumeMount(pod.Spec.Containers, databases)
if len(pod.Spec.InitContainers) != 0 {
pod.Spec.InitContainers = addVolumeMount(pod.Spec.InitContainers, databases)
}
patch = append(patch, addVault(pod, namespace, databases)...)
return json.Marshal(patch)
}
func addVault(pod *corev1.Pod, namespace string, databases []database) (patch []patchOperation) {
initContainers := []corev1.Container{}
for _, databaseInfo := range databases {
vaultContainerSpec := databaseInfo.vaultContainer
database := databaseInfo.database
role := databaseInfo.role
serviceAccount := pod.Spec.ServiceAccountName
authRole := fmt.Sprintf("%s_%s_%s", database, namespace, serviceAccount)
containerName := fmt.Sprintf("vault-creds-%s-%s", strings.Replace(database, "_", "-", -1), role)
secretPath := fmt.Sprintf(secretPathFormat, database, role)
templatePath := fmt.Sprintf("/creds/template/%s-%s", database, role)
var outputPath string
if databaseInfo.outputFile == "" {
outputPath = fmt.Sprintf("/creds/output/%s-%s", database, role)
} else {
outputPath = fmt.Sprintf("/creds/output/%s", databaseInfo.outputFile)
}
requests := corev1.ResourceList{
"cpu": resource.MustParse("10m"),
"memory": resource.MustParse("20Mi"),
}
limits := corev1.ResourceList{
"cpu": resource.MustParse("30m"),
"memory": resource.MustParse("50Mi"),
}
vaultContainer := corev1.Container{
Image: sidecarImage,
ImagePullPolicy: "IfNotPresent",
Resources: corev1.ResourceRequirements{
Requests: requests,
Limits: limits,
},
Name: containerName,
Args: []string{
"--vault-addr=" + vaultAddr,
"--gateway-addr=" + gatewayAddr,
"--ca-cert=" + vaultCaPath,
"--secret-path=" + secretPath,
"--login-path=" + loginPath,
"--auth-role=" + authRole,
"--template=" + templatePath,
"--out=" + outputPath,
"--completed-path=/creds/output/completed",
"--renew-interval=1h",
"--lease-duration=12h",
"--json-log",
},
Env: []corev1.EnvVar{
corev1.EnvVar{
Name: "POD_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
corev1.EnvVar{
Name: "NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
},
VolumeMounts: []corev1.VolumeMount{
corev1.VolumeMount{
Name: "vault-template",
MountPath: "/creds/template",
},
corev1.VolumeMount{
Name: "vault-creds",
MountPath: "/creds/output",
},
},
}
initContainer := vaultContainer
// Configure Lifecycle Hooks if spec exists
vaultContainer = addLifecycleHook(vaultContainer, vaultContainerSpec)
jobLikeOwnerReferencesKinds := map[string]bool{"Job": true, "Workflow": true}
if len(pod.ObjectMeta.OwnerReferences) != 0 {
ownerKind := pod.ObjectMeta.OwnerReferences[0].Kind
if _, ok := jobLikeOwnerReferencesKinds[ownerKind]; ok {
vaultContainer.Args = append(vaultContainer.Args, "--job")
}
}
// Append the new Vault container spec into the Pod Spec generated by the client Deployment/Daemonset/etc
pod.Spec.Containers = append(pod.Spec.Containers, vaultContainer)
initContainer.Args = append(initContainer.Args, "--init")
initContainer.Name = initContainer.Name + "-init"
initContainers = append(initContainers, initContainer)
}
var initOp string
if len(pod.Spec.InitContainers) != 0 {
initContainers = append(initContainers, pod.Spec.InitContainers...)
initOp = "replace"
} else {
initOp = "add"
}
patch = append(patch, []patchOperation{
patchOperation{
Op: "replace",
Path: "/spec/containers",
Value: pod.Spec.Containers,
},
patchOperation{
Op: initOp,
Path: "/spec/initContainers",
Value: initContainers,
}}...)
return patch
}
func addVolume(pod *corev1.Pod) (patch []patchOperation) {
volume := corev1.Volume{
Name: "vault-creds",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
}
path := "/spec/volumes"
var value interface{}
if len(pod.Spec.Volumes) != 0 {
path = path + "/-"
value = volume
} else {
value = []corev1.Volume{volume}
}
patch = append(patch, patchOperation{
Op: "add",
Path: path,
Value: value,
})
return patch
}
func addVolumeMount(containers []corev1.Container, databases []database) []corev1.Container {
modifiedContainers := []corev1.Container{}
for _, container := range containers {
for _, database := range databases {
volumeMount := corev1.VolumeMount{
Name: "vault-creds",
MountPath: database.outputPath,
}
//we don't want to mount the same path twice
container.VolumeMounts = appendVolumeMountIfMissing(container.VolumeMounts, volumeMount)
}
modifiedContainers = append(modifiedContainers, container)
}
return modifiedContainers
}
func appendVolumeMountIfMissing(slice []corev1.VolumeMount, v corev1.VolumeMount) []corev1.VolumeMount {
for _, ele := range slice {
if ele == v {
return slice
}
}
return append(slice, v)
}
// Conditionally set Lifecycle if it exists in containerSpec
func addLifecycleHook(container corev1.Container, containerSpec v1alpha1.Container) corev1.Container {
// Check DatabaseCredentialBindingSpec.Container.Lifecycle is not empty
emptyLifecycle := corev1.Lifecycle{}
if containerSpec.Lifecycle != emptyLifecycle {
// Check for a complete PreStop hook
if containerSpec.HasValidPreStop() {
container.Lifecycle = &containerSpec.Lifecycle
}
}
return container
}