-
Notifications
You must be signed in to change notification settings - Fork 784
/
create_vault.go
366 lines (320 loc) · 10.9 KB
/
create_vault.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package cmd
import (
"fmt"
"github.com/jenkins-x/jx/pkg/kube/serviceaccount"
"github.com/jenkins-x/jx/pkg/kube/services"
"io"
"io/ioutil"
"os"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/cloud/gke"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/jenkins-x/jx/pkg/vault"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
gkeKubeProvider = "gke"
gkeServiceAccountSecretKey = "service-account.json"
exposedVaultPort = "8200"
)
var (
createVaultLong = templates.LongDesc(`
Creates a Vault using the vault-operator
`)
createVaultExample = templates.Examples(`
# Create a new vault with name my-vault
jx create vault my-vault
# Create a new vault with name my-vault in namespace my-vault-namespace
jx create vault my-vault -n my-vault-namespace
`)
)
// CreateVaultOptions the options for the create vault command
type CreateVaultOptions struct {
CreateOptions
UpgradeIngressOptions UpgradeIngressOptions
GKEProjectID string
GKEZone string
Namespace string
SecretsPathPrefix string
}
// NewCmdCreateVault creates a command object for the "create" command
func NewCmdCreateVault(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
commonOptions := CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
}
options := &CreateVaultOptions{
CreateOptions: CreateOptions{
CommonOptions: commonOptions,
},
UpgradeIngressOptions: UpgradeIngressOptions{
CreateOptions: CreateOptions{
CommonOptions: commonOptions,
},
},
}
cmd := &cobra.Command{
Use: "vault",
Short: "Create a new Vault using the vault-operator",
Long: createVaultLong,
Example: createVaultExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.GKEProjectID, "gke-project-id", "", "", "Google Project ID to use for Vault backend")
cmd.Flags().StringVarP(&options.GKEZone, "gke-zone", "", "", "The zone (e.g. us-central1-a) where Vault will store the encrypted data")
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "Namespace where the Vault is created")
cmd.Flags().StringVarP(&options.SecretsPathPrefix, "secrets-path-prefix", "p", vault.DefaultSecretsPathPrefix, "Path prefix for secrets used for access control config")
options.addCommonFlags(cmd)
options.UpgradeIngressOptions.addFlags(cmd)
return cmd
}
// Run implements the command
func (o *CreateVaultOptions) Run() error {
var vaultName string
if len(o.Args) == 1 {
vaultName = o.Args[0]
} else if o.BatchMode {
return fmt.Errorf("Missing vault name")
} else {
// Prompt the user for the vault name
vaultName, _ = util.PickValue(
"Vault name:", "", true,
"The name of the vault that will be created", o.GetIn(), o.GetOut(), o.GetErr())
}
teamSettings, err := o.TeamSettings()
if err != nil {
return errors.Wrap(err, "retrieving the team settings")
}
if teamSettings.KubeProvider != gkeKubeProvider {
return errors.Wrapf(err, "this command only supports the '%s' kubernetes provider", gkeKubeProvider)
}
return o.createVaultGKE(vaultName)
}
func (o *CreateVaultOptions) createVaultGKE(vaultName string) error {
client, team, err := o.KubeClient()
if err != nil {
return errors.Wrap(err, "creating kubernetes client")
}
if o.Namespace == "" {
o.Namespace = team
}
err = kube.EnsureNamespaceCreated(client, o.Namespace, nil, nil)
if err != nil {
return errors.Wrapf(err, "failed to ensure that provided namespace '%s' is created", o.Namespace)
}
vaultOperatorClient, err := o.VaultOperatorClient()
if err != nil {
return errors.Wrap(err, "creating vault operator client")
}
// Checks if the vault alrady exists
found := vault.FindVault(vaultOperatorClient, vaultName, o.Namespace)
if found {
return fmt.Errorf("Vault with name '%s' already exists in namespace '%s'", vaultName, o.Namespace)
}
err = gke.Login("", true)
if err != nil {
return errors.Wrap(err, "login into GCP")
}
if o.GKEProjectID == "" {
projectId, err := o.getGoogleProjectId()
if err != nil {
return err
}
o.GKEProjectID = projectId
}
err = o.CreateOptions.CommonOptions.runCommandVerbose(
"gcloud", "config", "set", "project", o.GKEProjectID)
if err != nil {
return err
}
if o.GKEZone == "" {
zone, err := o.getGoogleZone(o.GKEProjectID)
if err != nil {
return err
}
o.GKEZone = zone
}
log.Infof("Creating GCP service account for Vault backend\n")
gcpServiceAccountSecretName, err := o.createVaultGCPServiceAccount(vaultName)
if err != nil {
return errors.Wrap(err, "creating GCP service account")
}
log.Infof("%s service account created\n", util.ColorInfo(gcpServiceAccountSecretName))
log.Infof("Setting up GCP KMS configuration\n")
kmsConfig, err := o.createKmsConfig(vaultName)
if err != nil {
return errors.Wrap(err, "creating KMS configuration")
}
log.Infof("KMS Key %s created in keying %s\n", util.ColorInfo(kmsConfig.key), util.ColorInfo(kmsConfig.keyring))
vaultBucket, err := o.createVaultBucket(vaultName)
if err != nil {
return errors.Wrap(err, "creating Vault GCS data bucket")
}
log.Infof("GCS bucket %s was created for Vault backend\n", util.ColorInfo(vaultBucket))
vaultAuthServiceAccount, err := o.createVaultAuthServiceAccount(vaultName)
if err != nil {
return errors.Wrap(err, "creating Vault authentication service account")
}
log.Infof("Created service account %s for Vault authentication\n", util.ColorInfo(vaultAuthServiceAccount))
log.Infof("Creating Vault...\n")
gcpConfig := &vault.GCPConfig{
ProjectId: o.GKEProjectID,
KmsKeyring: kmsConfig.keyring,
KmsKey: kmsConfig.key,
KmsLocation: kmsConfig.location,
GcsBucket: vaultBucket,
}
err = vault.CreateVault(vaultOperatorClient, vaultName, o.Namespace, gcpServiceAccountSecretName,
gcpConfig, vaultAuthServiceAccount, o.Namespace, o.SecretsPathPrefix)
if err != nil {
return errors.Wrap(err, "creating vault")
}
log.Infof("Vault %s created\n", util.ColorInfo(vaultName))
log.Infof("Exposing Vault...\n")
err = o.exposeVault(vaultName)
if err != nil {
return errors.Wrap(err, "exposing vault")
}
log.Infof("Vault %s exposed\n", util.ColorInfo(vaultName))
return nil
}
func (o *CreateVaultOptions) createVaultGCPServiceAccount(vaultName string) (string, error) {
serviceAccountDir, err := ioutil.TempDir("/tmp", gkeKubeProvider)
if err != nil {
return "", errors.Wrap(err, "creating a temporary folder where the service account will be stored")
}
defer os.RemoveAll(serviceAccountDir)
serviceAccountName := gke.VaultServiceAccountName(vaultName)
if err != nil {
return "", err
}
serviceAccountPath, err := gke.GetOrCreateServiceAccount(serviceAccountName, o.GKEProjectID, serviceAccountDir, gke.VaultServiceAccountRoles)
if err != nil {
return "", errors.Wrap(err, "creating the service account")
}
secretName, err := o.storeGCPServiceAccountIntoSecret(serviceAccountPath, vaultName)
if err != nil {
return "", errors.Wrap(err, "storing the service account into a secret")
}
return secretName, nil
}
func (o *CreateVaultOptions) storeGCPServiceAccountIntoSecret(serviceAccountPath string, vaultName string) (string, error) {
client, _, err := o.KubeClient()
if err != nil {
return "", errors.Wrap(err, "creating kubernetes client")
}
serviceAccount, err := ioutil.ReadFile(serviceAccountPath)
if err != nil {
return "", errors.Wrapf(err, "reading the service account from file '%s'", serviceAccountPath)
}
secretName := vault.VaultGcpServiceAccountSecretName(vaultName)
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
},
Data: map[string][]byte{
gkeServiceAccountSecretKey: serviceAccount,
},
}
secrets := client.CoreV1().Secrets(o.Namespace)
_, err = secrets.Get(secretName, metav1.GetOptions{})
if err != nil {
_, err = secrets.Create(secret)
} else {
_, err = secrets.Update(secret)
}
return secretName, nil
}
type kmsConfig struct {
keyring string
key string
location string
project string
}
func (o *CreateVaultOptions) createKmsConfig(vaultName string) (*kmsConfig, error) {
config := &kmsConfig{
keyring: fmt.Sprintf("%s-keyring", vaultName),
key: fmt.Sprintf("%s-key", vaultName),
location: gke.KmsLocation,
project: o.GKEProjectID,
}
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
}
func (o *CreateVaultOptions) createVaultBucket(vaultName string) (string, error) {
bucketName := gke.VaultBucketName(vaultName)
exists, err := gke.BucketExists(o.GKEProjectID, bucketName)
if err != nil {
return "", errors.Wrap(err, "checking if Vault GCS bucket exists")
}
if exists {
return bucketName, nil
}
if o.GKEZone == "" {
return "", errors.New("GKE zone must be provided in 'gke-zone' option")
}
region := gke.GetRegionFromZone(o.GKEZone)
err = gke.CreateBucket(o.GKEProjectID, bucketName, region)
if err != nil {
return "", errors.Wrap(err, "creating Vault GCS bucket")
}
return bucketName, nil
}
func (o *CreateVaultOptions) createVaultAuthServiceAccount(vaultName string) (string, error) {
client, _, err := o.KubeClient()
if err != nil {
return "", errors.Wrap(err, "creating kubernetes client")
}
serviceAccountName := fmt.Sprintf("%s-auth-sa", vaultName)
_, err = serviceaccount.CreateServiceAccount(client, o.Namespace, serviceAccountName)
if err != nil {
return "", errors.Wrap(err, "creating vault auth service account")
}
return serviceAccountName, nil
}
func (o *CreateVaultOptions) exposeVault(vaultService string) error {
err := services.WaitForService(o.KubeClientCached, vaultService, o.Namespace, 1*time.Minute)
if err != nil {
return errors.Wrap(err, "waiting for vault service")
}
svc, err := o.KubeClientCached.CoreV1().Services(o.Namespace).Get(vaultService, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "getting the vault service: %s", vaultService)
}
if svc.Annotations == nil {
svc.Annotations = map[string]string{}
}
if svc.Annotations[kube.AnnotationExpose] == "" {
svc.Annotations[kube.AnnotationExpose] = "true"
svc.Annotations[kube.AnnotationExposePort] = exposedVaultPort
svc, err = o.KubeClientCached.CoreV1().Services(o.Namespace).Update(svc)
if err != nil {
return errors.Wrapf(err, "updating %s service annotations", vaultService)
}
}
options := &o.UpgradeIngressOptions
options.Namespaces = []string{o.Namespace}
options.Services = []string{vaultService}
return options.Run()
}