/
run.go
189 lines (165 loc) · 4.59 KB
/
run.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
184
185
186
187
188
189
package secrets
import (
"fmt"
"os"
"go.mozilla.org/sops/v3"
"go.mozilla.org/sops/v3/aes"
_ "go.mozilla.org/sops/v3/audit"
"go.mozilla.org/sops/v3/azkv"
"go.mozilla.org/sops/v3/cmd/sops/common"
"go.mozilla.org/sops/v3/gcpkms"
"go.mozilla.org/sops/v3/keys"
"go.mozilla.org/sops/v3/keyservice"
"go.mozilla.org/sops/v3/kms"
"go.mozilla.org/sops/v3/pgp"
"go.uber.org/zap"
)
type SecretArgs struct {
KMS, KMSC, AWS, GCP, Azure, PGP string //nolint:golint,unused
}
func SecretsCmd(cmdName, fileName string, sa SecretArgs, args []string, log *zap.SugaredLogger) error {
var err error
if _, err := os.Stat(fileName); os.IsNotExist(err) {
if (cmdName == "add-master-key" || cmdName == "remove-master-key") &&
(sa.PGP != "" || sa.GCP != "" || sa.Azure != "" || sa.AWS != "") {
return fmt.Errorf("cannot add or remove keys on non-existent files")
}
if cmdName == "encrypt" || cmdName == "decrypt" || cmdName == "rotate" {
return fmt.Errorf("cannot operate on non-existent file")
}
}
ks := []keyservice.KeyServiceClient{keyservice.NewLocalClient()}
inputStore := common.DefaultStoreForPath(fileName)
outputStore := common.DefaultStoreForPath(fileName)
var output []byte
switch cmdName {
case "rotate", "add-master-keys", "remove-master-keys":
var allKeys []keys.MasterKey
kmsContext := kms.ParseKMSContext(sa.KMSC)
for _, k := range kms.MasterKeysFromArnString(sa.KMS, kmsContext, sa.AWS) {
allKeys = append(allKeys, k)
}
for _, k := range pgp.MasterKeysFromFingerprintString(sa.PGP) {
allKeys = append(allKeys, k)
}
for _, k := range gcpkms.MasterKeysFromResourceIDString(sa.GCP) {
allKeys = append(allKeys, k)
}
azKeys, err := azkv.MasterKeysFromURLs(sa.Azure)
if err != nil {
return err
}
for _, k := range azKeys {
allKeys = append(allKeys, k)
}
var addKeys, rmKeys []keys.MasterKey
switch cmdName {
case "add-master-keys":
addKeys = allKeys
case "remove-master-keys":
rmKeys = allKeys
}
output, err = rotate(rotateOpts{
log: log,
OutputStore: outputStore,
InputStore: inputStore,
InputPath: fileName,
Cipher: aes.NewCipher(),
KeyServices: ks,
IgnoreMAC: false,
AddMasterKeys: addKeys,
RemoveMasterKeys: rmKeys,
})
if err != nil {
return err
}
return os.WriteFile(fileName, output, 0o600)
}
if err != nil {
return err
}
_, statErr := os.Stat(fileName)
fileExists := statErr == nil
opts := editOpts{
log: log,
OutputStore: outputStore,
InputStore: inputStore,
InputPath: fileName,
Cipher: aes.NewCipher(),
KeyServices: ks,
IgnoreMAC: false,
ShowMasterKeys: false,
}
if fileExists {
output, err = edit(opts)
} else {
// File doesn't exist, edit the example file instead
var groups []sops.KeyGroup
if groups, err = keyGroups(sa, fileName); err != nil {
return err
}
if len(args) == 0 {
return fmt.Errorf("no key management provider defined")
}
output, err = editExample(editExampleOpts{
editOpts: opts,
UnencryptedSuffix: "",
EncryptedSuffix: "",
EncryptedRegex: "",
KeyGroups: groups,
GroupThreshold: 0,
})
}
if err != nil {
return err
}
// We open the file *after* the operations on the tree have been
// executed to avoid truncating it when there's errors
file, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("could not open in-place file for writing: %s", err)
}
defer file.Close()
if _, err = file.Write(output); err != nil {
return err
}
log.Info("File written successfully")
return nil
}
func keyGroups(sa SecretArgs, file string) ([]sops.KeyGroup, error) {
var kmsKeys []keys.MasterKey
var pgpKeys []keys.MasterKey
var cloudKmsKeys []keys.MasterKey
var azkvKeys []keys.MasterKey
if sa.KMS != "" {
kmsContext := kms.ParseKMSContext(sa.KMSC)
for _, k := range kms.MasterKeysFromArnString(sa.KMS, kmsContext, sa.AWS) {
kmsKeys = append(kmsKeys, k)
}
}
if sa.GCP != "" {
for _, k := range gcpkms.MasterKeysFromResourceIDString(sa.GCP) {
cloudKmsKeys = append(cloudKmsKeys, k)
}
}
if sa.Azure != "" {
azureKeys, err := azkv.MasterKeysFromURLs(sa.Azure)
if err != nil {
return nil, err
}
for _, k := range azureKeys {
azkvKeys = append(azkvKeys, k)
}
}
if sa.PGP != "" {
for _, k := range pgp.MasterKeysFromFingerprintString(sa.PGP) {
pgpKeys = append(pgpKeys, k)
}
}
var group sops.KeyGroup
group = append(group, kmsKeys...)
group = append(group, cloudKmsKeys...)
group = append(group, azkvKeys...)
group = append(group, pgpKeys...)
return []sops.KeyGroup{group}, nil
}