-
Notifications
You must be signed in to change notification settings - Fork 198
/
decrypt.go
122 lines (99 loc) · 2.71 KB
/
decrypt.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
package secret
import (
"bytes"
"fmt"
"os"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"github.com/werf/werf/cmd/werf/common"
secret_common "github.com/werf/werf/cmd/werf/helm/secret/common"
"github.com/werf/werf/pkg/deploy/secret"
"github.com/werf/werf/pkg/werf"
)
var cmdData struct {
OutputFilePath string
}
var commonCmdData common.CmdData
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "decrypt",
DisableFlagsInUseLine: true,
Short: "Decrypt data",
Long: common.GetLongCommandDescription(`Decrypt data from standard input.
Encryption key should be in $WERF_SECRET_KEY or .werf_secret_key file`),
Example: ` # Decrypt data in interactive mode
$ werf helm secret decrypt
Enter secret:
test
# Decrypt from a pipe
$ cat .helm/secret/date | werf helm secret decrypt
Tue Jun 26 09:58:10 PDT 1990`,
Annotations: map[string]string{
common.CmdEnvAnno: common.EnvsDescription(common.WerfSecretKey),
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
return err
}
return runSecretDecrypt()
},
}
common.SetupDir(&commonCmdData, cmd)
common.SetupTmpDir(&commonCmdData, cmd)
common.SetupHomeDir(&commonCmdData, cmd)
common.SetupLogOptions(&commonCmdData, cmd)
cmd.Flags().StringVarP(&cmdData.OutputFilePath, "output-file-path", "o", "", "Write to file instead of stdout")
return cmd
}
func runSecretDecrypt() error {
if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
}
projectDir, err := common.GetProjectDir(&commonCmdData)
if err != nil {
return fmt.Errorf("getting project dir failed: %s", err)
}
m, err := secret.GetManager(projectDir)
if err != nil {
return err
}
return secretDecrypt(m)
}
func secretDecrypt(m secret.Manager) error {
var encodedData []byte
var data []byte
var err error
if terminal.IsTerminal(int(os.Stdin.Fd())) {
encodedData, err = secret_common.InputFromInteractiveStdin()
if err != nil {
return err
}
} else {
encodedData, err = secret_common.InputFromStdin()
if err != nil {
return err
}
}
if len(encodedData) == 0 {
return nil
}
encodedData = bytes.TrimSpace(encodedData)
data, err = m.Decrypt(encodedData)
if err != nil {
return err
}
if cmdData.OutputFilePath != "" {
if err := secret_common.SaveGeneratedData(cmdData.OutputFilePath, data); err != nil {
return err
}
} else {
if terminal.IsTerminal(int(os.Stdout.Fd())) {
if !bytes.HasSuffix(data, []byte("\n")) {
data = append(data, []byte("\n")...)
}
}
fmt.Printf("%s", string(data))
}
return nil
}