-
Notifications
You must be signed in to change notification settings - Fork 786
/
create_vault.go
294 lines (258 loc) · 8.89 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
package cmd
import (
"fmt"
"io"
"time"
"github.com/banzaicloud/bank-vaults/operator/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/kube/services"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/cloud/gke"
gkevault "github.com/jenkins-x/jx/pkg/cloud/gke/vault"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
kubevault "github.com/jenkins-x/jx/pkg/kube/vault"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/jenkins-x/jx/pkg/vault"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
gkeKubeProvider = "gke"
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 {
kubeClient, team, err := o.KubeClientAndNamespace()
if err != nil {
return errors.Wrap(err, "creating kubernetes client")
}
if o.Namespace == "" {
o.Namespace = team
}
err = kube.EnsureNamespaceCreated(kubeClient, 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")
}
return o.createVault(vaultOperatorClient, vaultName)
}
// DoCreateVault creates a vault in the existing namespace.
// If the vault already exists, it will error
func (o *CreateVaultOptions) createVault(vaultOperatorClient versioned.Interface, vaultName string) error {
kubeClient, _, err := o.KubeClientAndNamespace()
if err != nil {
return err
}
// Checks if the vault already exists
found := kubevault.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 == "" {
kubeClient, ns, err := o.KubeClientAndDevNamespace()
if err != nil {
log.Warnf("Failed create KubeClient: %s\n", err)
} else {
data, err := kube.ReadInstallValues(kubeClient, ns)
if err != nil {
log.Warnf("Failed to load install values %s\n", err)
} else if data != nil {
o.GKEProjectID = data[kube.ProjectID]
if o.GKEZone == "" {
o.GKEZone = data[kube.Zone]
}
}
}
}
if o.GKEProjectID == "" {
o.GKEProjectID, err = o.getGoogleProjectId()
if err != nil {
return err
}
}
err = o.CreateOptions.CommonOptions.runCommandVerbose(
"gcloud", "config", "set", "project", o.GKEProjectID)
if err != nil {
return err
}
if o.GKEZone == "" {
defaultZone := ""
if cluster, err := gke.ClusterName(o.Kube()); err == nil && cluster != "" {
if clusterZone, err := gke.ClusterZone(cluster); err == nil {
defaultZone = clusterZone
}
}
zone, err := o.getGoogleZoneWithDefault(o.GKEProjectID, defaultZone)
if err != nil {
return err
}
o.GKEZone = zone
}
clusterName, err := gke.ShortClusterName(o.Kube())
if err != nil {
return err
}
log.Infof("Current Cluster: %s\n", util.ColorInfo(clusterName))
log.Infof("Creating GCP service account for Vault backend\n")
gcpServiceAccountSecretName, err := gkevault.CreateGCPServiceAccount(kubeClient, vaultName, o.Namespace, clusterName, o.GKEProjectID)
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 := gkevault.CreateKmsConfig(vaultName, clusterName, o.GKEProjectID)
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 := gkevault.CreateBucket(vaultName, clusterName, o.GKEProjectID, o.GKEZone)
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 := gkevault.CreateAuthServiceAccount(kubeClient, vaultName, o.Namespace, clusterName)
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 := &kubevault.GCPConfig{
ProjectId: o.GKEProjectID,
KmsKeyring: kmsConfig.Keyring,
KmsKey: kmsConfig.Key,
KmsLocation: kmsConfig.Location,
GcsBucket: vaultBucket,
}
err = kubevault.CreateVault(kubeClient, 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 in cluster %s\n", util.ColorInfo(vaultName), util.ColorInfo(clusterName))
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) exposeVault(vaultService string) error {
client, err := o.KubeClient()
if err != nil {
return err
}
err = services.WaitForService(client, vaultService, o.Namespace, 1*time.Minute)
if err != nil {
return errors.Wrap(err, "waiting for vault service")
}
svc, err := client.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 = client.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}
options.SkipResourcesUpdate = true
options.WaitForCerts = true
return options.Run()
}