-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
volumes.go
113 lines (93 loc) · 3.09 KB
/
volumes.go
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
/*
Copyright 2020 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kms
import (
"github.com/hashicorp/vault/api"
"github.com/libopenstorage/secrets"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
v1 "k8s.io/api/core/v1"
)
const (
// Key name of the Secret containing the cert and client key
vaultCACertSecretKeyName = "cert"
vaultKeySecretKeyName = "key"
// File names of the Secret value when mapping on the filesystem
VaultCAFileName = "vault.ca"
VaultCertFileName = "vault.crt"
VaultKeyFileName = "vault.key"
// File name for token file
VaultFileName = "vault.token"
)
// TLSSecretVolumeAndMount return the volume and matching volume mount for mounting the secrets into /etc/vault
func TLSSecretVolumeAndMount(config map[string]string) []v1.VolumeProjection {
// Projection list
secretVolumeProjections := []v1.VolumeProjection{}
// File mode
mode := int32(0400)
// Vault TLS Secrets
for _, tlsOption := range cephv1.VaultTLSConnectionDetails {
tlsSecretName := GetParam(config, tlsOption)
if tlsSecretName != "" {
projectionSecret := &v1.SecretProjection{Items: []v1.KeyToPath{{Key: tlsSecretKeyToCheck(tlsOption), Path: tlsSecretPath(tlsOption), Mode: &mode}}}
projectionSecret.Name = tlsSecretName
secretProjection := v1.VolumeProjection{Secret: projectionSecret}
secretVolumeProjections = append(secretVolumeProjections, secretProjection)
}
}
return secretVolumeProjections
}
// VaultVolumeAndMount returns Vault volume and volume mount
func VaultVolumeAndMount(config map[string]string) (v1.Volume, v1.VolumeMount) {
v := v1.Volume{
Name: secrets.TypeVault,
VolumeSource: v1.VolumeSource{
Projected: &v1.ProjectedVolumeSource{
Sources: TLSSecretVolumeAndMount(config),
},
},
}
m := v1.VolumeMount{
Name: secrets.TypeVault,
ReadOnly: true,
MountPath: EtcVaultDir,
}
return v, m
}
func tlsSecretPath(tlsOption string) string {
switch tlsOption {
case api.EnvVaultCACert:
return VaultCAFileName
case api.EnvVaultClientCert:
return VaultCertFileName
case api.EnvVaultClientKey:
return VaultKeyFileName
}
return ""
}
// VaultTokenFileVolume save token from secret as volume mount
func VaultTokenFileVolume(tokenSecretName string) (v1.Volume, v1.VolumeMount) {
volume := v1.Volume{
Name: "vault-file-vol",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: tokenSecretName,
Items: []v1.KeyToPath{
{Key: KMSTokenSecretNameKey, Path: VaultFileName},
}}}}
mount := v1.VolumeMount{
Name: volume.Name,
ReadOnly: true,
MountPath: EtcVaultDir,
}
return volume, mount
}