-
Notifications
You must be signed in to change notification settings - Fork 783
/
create_vault.go
311 lines (267 loc) · 8.49 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
package cmd
import (
"fmt"
"io"
"io/ioutil"
"os"
"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"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
gkeKubeProvider = "gke"
gkeServiceAccountSecretKey = "service-account.json"
)
var (
vaultServiceAccountRoles = []string{"roles/storage.objectAdmin",
"roles/cloudkms.admin",
"roles/cloudkms.cryptoKeyEncrypterDecrypter",
}
)
var (
createVaultLong = templates.LongDesc(`
Creates a Vault using the vault-operator
`)
createVaultExample = templates.Examples(`
# Create a new vault
jx create vault
"
`)
)
// CreateVaultOptions the options for the create vault command
type CreateVaultOptions struct {
CreateOptions
GKEProjectID string
GKEZone string
Namespace 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 {
options := &CreateVaultOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "vault",
Short: "Create a new Vault using the vault-opeator",
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")
options.addCommonFlags(cmd)
return cmd
}
// Run implements the command
func (o *CreateVaultOptions) Run() error {
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()
}
func (o *CreateVaultOptions) createVaultGKE() error {
_, team, err := o.KubeClient()
if err != nil {
return errors.Wrap(err, "creating kubernetes client")
}
if o.Namespace == "" {
o.Namespace = team
}
err = gke.Login("", false)
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()
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(team)
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(team)
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()
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")
vaultOperatorClient, err := o.VaultOperatorClient()
if err != nil {
return errors.Wrap(err, "creating vault opeator client")
}
vaultName := fmt.Sprintf("%s-vault", team)
gcpConfig := &kube.GCPConfig{
ProjectId: o.GKEProjectID,
KmsKeyring: kmsConfig.keyring,
KmsKey: kmsConfig.key,
KmsLocation: kmsConfig.location,
GcsBucket: vaultBucket,
}
err = kube.CreateVault(vaultOperatorClient, vaultName, o.Namespace, gcpServiceAccountSecretName,
gcpConfig, vaultAuthServiceAccount, o.Namespace)
if err != nil {
return errors.Wrap(err, "creating vault")
}
log.Infof("Vault %s created\n", util.ColorInfo(vaultName))
return nil
}
func (o *CreateVaultOptions) createVaultGCPServiceAccount() (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, err := o.serviceAccountName()
if err != nil {
return "", err
}
serviceAccountPath, err := gke.GetOrCreateServiceAccount(serviceAccountName, o.GKEProjectID, serviceAccountDir, vaultServiceAccountRoles)
if err != nil {
return "", errors.Wrap(err, "creating the service account")
}
secretName, err := o.storeGCPServiceAccountIntoSecret(serviceAccountPath)
if err != nil {
return "", errors.Wrap(err, "storing the service account into a secret")
}
return secretName, nil
}
func (o *CreateVaultOptions) serviceAccountName() (string, error) {
_, currentTeam, err := o.KubeClient()
if err != nil {
return "", errors.Wrap(err, "retrieving the current team name")
}
return fmt.Sprintf("%s-vault", currentTeam), nil
}
func (o *CreateVaultOptions) storeGCPServiceAccountIntoSecret(serviceAccountPath string) (string, error) {
client, currentTeam, 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 := fmt.Sprintf("%s-vault-gcp-sa", currentTeam)
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(team string) (*kmsConfig, error) {
config := &kmsConfig{
keyring: fmt.Sprintf("%s-vault-keyring", team),
key: fmt.Sprintf("%s-vault-key", team),
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(team string) (string, error) {
bucketName := fmt.Sprintf("%s-vault-bucket", team)
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() (string, error) {
client, team, err := o.KubeClient()
if err != nil {
return "", errors.Wrap(err, "creating kubernetes client")
}
serviceAccountName := fmt.Sprintf("%s-vault-auth-sa", team)
_, err = kube.CreateServiceAccount(client, o.Namespace, serviceAccountName)
if err != nil {
return "", errors.Wrap(err, "creating vault auth service account")
}
return serviceAccountName, nil
}