-
Notifications
You must be signed in to change notification settings - Fork 787
/
vault_backend.go
144 lines (125 loc) · 4.57 KB
/
vault_backend.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
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
package vault
import (
"fmt"
"io/ioutil"
"os"
"github.com/jenkins-x/jx/pkg/cloud/gke"
"github.com/jenkins-x/jx/pkg/kube/serviceaccount"
"github.com/pkg/errors"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
gkeServiceAccountSecretKey = "service-account.json"
)
var (
ServiceAccountRoles = []string{"roles/storage.objectAdmin",
"roles/cloudkms.admin",
"roles/cloudkms.cryptoKeyEncrypterDecrypter",
}
)
// KmsConfig keeps the configuration for Google KMS service
type KmsConfig struct {
Keyring string
Key string
Location string
project string
}
// This is a loose collection of methods needed to set up a vault in GKE.
// If they are generic enough and needed elsewhere, we can move them up one level to more generic GCP methods.
// CreateKmsConfig creates a KMS config for the GKE Vault
func CreateKmsConfig(vaultName, clusterName, projectId string) (*KmsConfig, error) {
config := &KmsConfig{
Keyring: KeyringName(vaultName, clusterName),
Key: KeyName(vaultName, clusterName),
Location: gke.KmsLocation,
project: projectId,
}
err := gke.CreateKmsKeyring(config.Keyring, config.project)
if err != nil {
return nil, errors.Wrapf(err, "creating kms keyring '%s'", config.Keyring)
}
err = gke.CreateKmsKey(config.Key, config.Keyring, config.project)
if err != nil {
return nil, errors.Wrapf(err, "crating the kms key '%s'", config.Key)
}
return config, nil
}
// CreateGCPServiceAccount creates a service account in GCP for the vault service
func CreateGCPServiceAccount(kubeClient kubernetes.Interface, vaultName, namespace, clusterName, projectId string) (string, error) {
serviceAccountDir, err := ioutil.TempDir("", "gke")
if err != nil {
return "", errors.Wrap(err, "creating a temporary folder where the service account will be stored")
}
defer os.RemoveAll(serviceAccountDir)
serviceAccountName := ServiceAccountName(vaultName, clusterName)
if err != nil {
return "", err
}
serviceAccountPath, err := gke.GetOrCreateServiceAccount(serviceAccountName, projectId, serviceAccountDir, ServiceAccountRoles)
if err != nil {
return "", errors.Wrap(err, "creating the service account")
}
secretName, err := storeGCPServiceAccountIntoSecret(kubeClient, serviceAccountPath, vaultName, namespace, clusterName)
if err != nil {
return "", errors.Wrap(err, "storing the service account into a secret")
}
return secretName, nil
}
// CreateBucket Creates a bucket in GKE to store the backend (encrypted) data for vault
func CreateBucket(vaultName, clusterName, projectId, zone string) (string, error) {
bucketName := BucketName(vaultName, clusterName)
exists, err := gke.BucketExists(projectId, bucketName)
if err != nil {
return "", errors.Wrap(err, "checking if Vault GCS bucket exists")
}
if exists {
return bucketName, nil
}
if zone == "" {
return "", errors.New("GKE zone must be provided in 'gke-zone' option")
}
region := gke.GetRegionFromZone(zone)
err = gke.CreateBucket(projectId, bucketName, region)
if err != nil {
return "", errors.Wrap(err, "creating Vault GCS bucket")
}
return bucketName, nil
}
// CreateAuthServiceAccount creates a Serivce Account for the Auth service for vault
func CreateAuthServiceAccount(client kubernetes.Interface, vaultName, namespace, clusterName string) (string, error) {
serviceAccountName := AuthServiceAccountName(vaultName, clusterName)
_, err := serviceaccount.CreateServiceAccount(client, namespace, serviceAccountName)
if err != nil {
return "", errors.Wrap(err, "creating vault auth service account")
}
return serviceAccountName, nil
}
// GcpServiceAccountSecretName builds the secret name where the GCP service account is stored
func GcpServiceAccountSecretName(vaultName string, clusterName string) string {
return fmt.Sprintf("%s-%s-gcp-sa", clusterName, vaultName)
}
func storeGCPServiceAccountIntoSecret(client kubernetes.Interface, serviceAccountPath, vaultName, namespace, clusterName string) (string, error) {
serviceAccount, err := ioutil.ReadFile(serviceAccountPath)
if err != nil {
return "", errors.Wrapf(err, "reading the service account from file '%s'", serviceAccountPath)
}
secretName := GcpServiceAccountSecretName(vaultName, clusterName)
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
},
Data: map[string][]byte{
gkeServiceAccountSecretKey: serviceAccount,
},
}
secrets := client.CoreV1().Secrets(namespace)
_, err = secrets.Get(secretName, metav1.GetOptions{})
if err != nil {
_, err = secrets.Create(secret)
} else {
_, err = secrets.Update(secret)
}
return secretName, nil
}