-
Notifications
You must be signed in to change notification settings - Fork 203
/
decrypt.go
92 lines (76 loc) · 1.72 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
package secret
import (
"bytes"
"errors"
"fmt"
"os"
"golang.org/x/crypto/ssh/terminal"
"github.com/flant/werf/pkg/deploy/secret"
)
func SecretFileDecrypt(m secret.Manager, filePath, outputFilePath string) error {
options := &GenerateOptions{
FilePath: filePath,
OutputFilePath: outputFilePath,
Values: false,
}
return secretDecrypt(m, options)
}
func SecretValuesDecrypt(m secret.Manager, filePath, outputFilePath string) error {
options := &GenerateOptions{
FilePath: filePath,
OutputFilePath: outputFilePath,
Values: true,
}
return secretDecrypt(m, options)
}
func secretDecrypt(m secret.Manager, options *GenerateOptions) error {
var encodedData []byte
var data []byte
var err error
if options.FilePath != "" {
encodedData, err = ReadFileData(options.FilePath)
if err != nil {
return err
}
} else {
if !terminal.IsTerminal(int(os.Stdin.Fd())) {
encodedData, err = InputFromStdin()
if err != nil {
return err
}
} else {
return ExpectedFilePathOrPipeError()
}
if len(encodedData) == 0 {
return nil
}
}
encodedData = bytes.TrimSpace(encodedData)
if options.Values {
data, err = m.DecryptYamlData(encodedData)
if err != nil {
return err
}
} else {
data, err = m.Decrypt(encodedData)
if err != nil {
return err
}
}
if options.OutputFilePath != "" {
if err := SaveGeneratedData(options.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(string(data))
}
return nil
}
func ExpectedFilePathOrPipeError() error {
return errors.New("expected FILE_PATH or pipe")
}