This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
get_secret.go
118 lines (101 loc) · 3.28 KB
/
get_secret.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
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package cmd
import (
"encoding/json"
"fmt"
"io"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/vmware/dispatch/pkg/api/v1"
"github.com/vmware/dispatch/pkg/dispatchcli/cmd/utils"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
secret "github.com/vmware/dispatch/pkg/secret-store/gen/client/secret"
"golang.org/x/net/context"
)
var (
getSecretsLong = i18n.T(`Get secrets.`)
// TODO: add examples
getSecretsExample = i18n.T(``)
getSecretContent = false
)
// NewCmdGetSecret creates command responsible for getting secrets.
func NewCmdGetSecret(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "secret [SECRET_NAME ...]",
Short: i18n.T("Get secrets"),
Long: getSecretsLong,
Example: getSecretsExample,
Args: cobra.MaximumNArgs(1),
Aliases: []string{"secrets"},
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) == 1 {
err = getSecret(out, errOut, cmd, args)
} else {
err = getSecrets(out, errOut, cmd)
}
CheckErr(err)
},
}
cmd.Flags().StringVarP(&cmdFlagApplication, "application", "a", "", "filter by application")
cmd.Flags().BoolVarP(&getSecretContent, "all", "", false, "also get secret content (in json format)")
return cmd
}
func getSecret(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
client := secretStoreClient()
params := &secret.GetSecretParams{
Context: context.Background(),
SecretName: args[0],
Tags: []string{},
}
utils.AppendApplication(¶ms.Tags, cmdFlagApplication)
resp, err := client.Secret.GetSecret(params, GetAuthInfoWriter())
if err != nil {
return formatAPIError(err, params)
}
if resp.Payload.Name == nil {
err := secret.NewGetSecretNotFound()
err.Payload = &v1.Error{
Code: 404,
Message: &args[0],
}
return formatAPIError(err, params)
}
return formatSecretOutput(out, false, []*v1.Secret{resp.Payload})
}
func getSecrets(out, errOut io.Writer, cmd *cobra.Command) error {
client := secretStoreClient()
params := &secret.GetSecretsParams{
Context: context.Background(),
Tags: []string{},
}
utils.AppendApplication(¶ms.Tags, cmdFlagApplication)
resp, err := client.Secret.GetSecrets(params, GetAuthInfoWriter())
if err != nil {
return formatAPIError(err, params)
}
return formatSecretOutput(out, true, resp.Payload)
}
func formatSecretOutput(out io.Writer, list bool, secrets []*v1.Secret) error {
if getSecretContent || dispatchConfig.JSON {
encoder := json.NewEncoder(out)
encoder.SetIndent("", " ")
if list {
return encoder.Encode(secrets)
}
return encoder.Encode(secrets[0])
}
fmt.Fprintf(out, "Note: secret values are hidden, please use --all flag to get them\n\n")
table := tablewriter.NewWriter(out)
table.SetHeader([]string{"ID", "Name", "Content"})
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetCenterSeparator("")
for _, secret := range secrets {
table.Append([]string{secret.ID.String(), *secret.Name, "<hidden>"})
}
table.Render()
return nil
}