-
Notifications
You must be signed in to change notification settings - Fork 787
/
delete_vault.go
183 lines (152 loc) · 5.18 KB
/
delete_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
package cmd
import (
"fmt"
"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"
"github.com/jenkins-x/jx/pkg/kube/serviceaccount"
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/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// DeleteVaultOptions keeps the options of delete vault command
type DeleteVaultOptions struct {
*CommonOptions
Namespace string
RemoveCloudResources bool
GKEProjectID string
GKEZone string
}
var (
deleteVaultLong = templates.LongDesc(`
Deletes a Vault
`)
deleteVaultExample = templates.Examples(`
# Deletes a Vault from namespace my-namespace
jx delete vault --namespace my-namespace my-vault
`)
)
// NewCmdDeleteVault builds a new delete vault command
func NewCmdDeleteVault(commonOpts *CommonOptions) *cobra.Command {
options := &DeleteVaultOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "vault",
Short: "Deletes a Vault",
Long: deleteVaultLong,
Example: deleteVaultExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "Namespace from where to delete the vault")
cmd.Flags().BoolVarP(&options.RemoveCloudResources, "remove-cloud-resources", "r", false, "Remove all cloud resource allocated for the Vault")
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")
return cmd
}
// Run implements the delete vault command
func (o *DeleteVaultOptions) Run() error {
if len(o.Args) != 1 {
return fmt.Errorf("Missing vault name")
}
vaultName := o.Args[0]
client, ns, err := o.KubeClientAndNamespace()
if err != nil {
return errors.Wrap(err, "creating kubernetes client")
}
if o.Namespace == "" {
o.Namespace = ns
}
vaultOperatorClient, err := o.VaultOperatorClient()
if err != nil {
return errors.Wrap(err, "creating vault operator client")
}
v, err := kubevault.GetVault(vaultOperatorClient, vaultName, o.Namespace)
if err != nil {
return fmt.Errorf("vault '%s' not found in namespace '%s'", vaultName, o.Namespace)
}
err = kubevault.DeleteVault(vaultOperatorClient, vaultName, o.Namespace)
if err != nil {
return errors.Wrap(err, "deleting the vault resource")
}
err = kube.DeleteIngress(client, o.Namespace, vaultName)
if err != nil {
return errors.Wrapf(err, "deleting the vault ingress '%s'", vaultName)
}
authServiceAccountName := kubevault.GetAuthSaName(*v)
err = serviceaccount.DeleteServiceAccount(client, o.Namespace, authServiceAccountName)
if err != nil {
return errors.Wrapf(err, "deleting the vault auth service account '%s'", authServiceAccountName)
}
gcpServiceAccountSecretName := gkevault.GcpServiceAccountSecretName(vaultName)
err = client.CoreV1().Secrets(o.Namespace).Delete(gcpServiceAccountSecretName, &metav1.DeleteOptions{})
if err != nil {
return errors.Wrapf(err, "deleting secret '%s' where GCP service account is stored", gcpServiceAccountSecretName)
}
err = kube.DeleteClusterRoleBinding(client, vaultName)
if err != nil {
return errors.Wrapf(err, "deleting the cluster role binding '%s' for vault", vaultName)
}
log.Infof("Vault %s deleted\n", util.ColorInfo(vaultName))
if o.RemoveCloudResources {
teamSettings, err := o.TeamSettings()
if err != nil {
return errors.Wrap(err, "retrieving the team settings")
}
if teamSettings.KubeProvider == gkeKubeProvider {
log.Infof("Removing GCP resources allocated for Vault...\n")
err := o.removeGCPResources(vaultName)
if err != nil {
return errors.Wrap(err, "removing GCP resource")
}
log.Infof("Cloud resources allocated for vault %s deleted\n", util.ColorInfo(vaultName))
}
}
return nil
}
func (o *DeleteVaultOptions) removeGCPResources(vaultName string) error {
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.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
}
sa := gkevault.ServiceAccountName(vaultName)
err = gke.DeleteServiceAccount(sa, o.GKEProjectID, gkevault.ServiceAccountRoles)
if err != nil {
return errors.Wrapf(err, "deleting the GCP service account '%s'", sa)
}
log.Infof("GCP service account %s deleted\n", util.ColorInfo(sa))
bucket := gkevault.BucketName(vaultName)
err = gke.DeleteAllObjectsInBucket(bucket)
if err != nil {
return errors.Wrapf(err, "deleting all objects in GCS bucket '%s'", bucket)
}
log.Infof("GCS bucket %s deleted\n", util.ColorInfo(bucket))
return nil
}