forked from hazelops/ize
/
secrets_rm.go
140 lines (107 loc) · 2.83 KB
/
secrets_rm.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
package secrets
import (
"fmt"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hazelops/ize/internal/config"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
type SecretsRemoveOptions struct {
Config *config.Config
AppName string
Backend string
SecretsPath string
}
func NewSecretsRemoveFlags() *SecretsRemoveOptions {
return &SecretsRemoveOptions{}
}
func NewCmdSecretsRemove() *cobra.Command {
o := NewSecretsRemoveFlags()
cmd := &cobra.Command{
Use: "rm",
Short: "remove secrets from storage",
Long: "This command removes secrets from storage",
TraverseChildren: true,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := o.Complete(cmd, args)
if err != nil {
return err
}
err = o.Run()
if err != nil {
return err
}
return nil
},
}
cmd.Flags().StringVar(&o.Backend, "backend", "ssm", "backend type")
cmd.Flags().StringVar(&o.SecretsPath, "path", "", "path to secrets")
return cmd
}
func (o *SecretsRemoveOptions) Complete(cmd *cobra.Command, args []string) error {
cfg, err := config.InitializeConfig()
if err != nil {
return err
}
o.Config = cfg
o.AppName = cmd.Flags().Args()[0]
if o.SecretsPath == "" {
o.SecretsPath = fmt.Sprintf("/%s/%s", o.Config.Env, o.AppName)
}
return nil
}
func (o *SecretsRemoveOptions) Validate() error {
if len(o.Config.Env) == 0 {
return fmt.Errorf("env must be specified")
}
return nil
}
func (o *SecretsRemoveOptions) Run() error {
pterm.DefaultSection.Printfln("Removing Secrets for %s", o.AppName)
if o.Backend == "ssm" {
err := rm(o)
if err != nil {
pterm.DefaultSection.Sprintfln("Secrets have been removed from %s", o.SecretsPath)
return err
}
} else {
pterm.DefaultSection.Println("Secrets removal unsuccessful")
return fmt.Errorf("backend %s is not found or not supported", o.Backend)
}
//pterm.DefaultSection.Printfln("Done: removing secrets")
return nil
}
func rm(o *SecretsRemoveOptions) error {
if o.SecretsPath == "" {
pterm.Info.Printfln("Path was not set")
return nil
}
pterm.Info.Printfln("Removing secrets from %s://%s", o.Backend, o.SecretsPath)
ssmSvc := ssm.New(o.Config.Session)
out, err := ssmSvc.GetParametersByPath(&ssm.GetParametersByPathInput{
Path: &o.SecretsPath,
})
if err != nil {
return err
}
pterm.Success.Printfln("Getting secrets")
if len(out.Parameters) == 0 {
pterm.Info.Printfln("No values found")
pterm.Success.Printfln("Removing secrets")
return nil
}
var names []*string
for _, p := range out.Parameters {
names = append(names, p.Name)
}
_, err = ssmSvc.DeleteParameters(&ssm.DeleteParametersInput{
Names: names,
})
ssmSvc.RemoveTagsFromResource(&ssm.RemoveTagsFromResourceInput{})
if err != nil {
return err
}
pterm.Success.Printfln("Removed secrets")
return nil
}