forked from getsops/sops
-
Notifications
You must be signed in to change notification settings - Fork 1
/
publish.go
193 lines (171 loc) · 4.44 KB
/
publish.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
190
191
192
193
package publish
import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"go.mozilla.org/sops/v3"
"go.mozilla.org/sops/v3/cmd/sops/codes"
"go.mozilla.org/sops/v3/cmd/sops/common"
"go.mozilla.org/sops/v3/config"
"go.mozilla.org/sops/v3/keyservice"
"go.mozilla.org/sops/v3/logging"
"go.mozilla.org/sops/v3/publish"
"go.mozilla.org/sops/v3/version"
"github.com/sirupsen/logrus"
)
var log *logrus.Logger
func init() {
log = logging.NewLogger("PUBLISH")
}
// Opts represents publish options and config
type Opts struct {
Interactive bool
Cipher sops.Cipher
ConfigPath string
InputPath string
KeyServices []keyservice.KeyServiceClient
InputStore sops.Store
OmitExtensions bool
Recursive bool
RootPath string
}
// Run publish operation
func Run(opts Opts) error {
var fileContents []byte
path, err := filepath.Abs(opts.InputPath)
if err != nil {
return err
}
conf, err := config.LoadDestinationRuleForFile(opts.ConfigPath, opts.InputPath, make(map[string]*string))
if err != nil {
return err
}
if conf.Destination == nil {
return errors.New("no destination configured for this file")
}
var destinationPath string
if opts.Recursive {
destinationPath, err = filepath.Rel(opts.RootPath, opts.InputPath)
if err != nil {
return err
}
} else {
_, destinationPath = filepath.Split(path)
}
if opts.OmitExtensions || conf.OmitExtensions {
destinationPath = strings.TrimSuffix(destinationPath, filepath.Ext(path))
}
// Check that this is a sops-encrypted file
tree, err := common.LoadEncryptedFile(opts.InputStore, opts.InputPath)
if err != nil {
return err
}
data := map[string]interface{}{}
switch conf.Destination.(type) {
case *publish.S3Destination, *publish.GCSDestination:
// Re-encrypt if settings exist to do so
if len(conf.KeyGroups[0]) != 0 {
log.Debug("Re-encrypting tree before publishing")
_, err = common.DecryptTree(common.DecryptTreeOpts{
Cipher: opts.Cipher,
IgnoreMac: false,
Tree: tree,
KeyServices: opts.KeyServices,
})
if err != nil {
return err
}
diffs := common.DiffKeyGroups(tree.Metadata.KeyGroups, conf.KeyGroups)
keysWillChange := false
for _, diff := range diffs {
if len(diff.Added) > 0 || len(diff.Removed) > 0 {
keysWillChange = true
}
}
if keysWillChange {
fmt.Printf("The following changes will be made to the file's key groups:\n")
common.PrettyPrintDiffs(diffs)
}
tree.Metadata = sops.Metadata{
KeyGroups: conf.KeyGroups,
UnencryptedSuffix: conf.UnencryptedSuffix,
EncryptedSuffix: conf.EncryptedSuffix,
Version: version.Version,
ShamirThreshold: conf.ShamirThreshold,
}
dataKey, errs := tree.GenerateDataKeyWithKeyServices(opts.KeyServices)
if len(errs) > 0 {
err = fmt.Errorf("Could not generate data key: %s", errs)
return err
}
err = common.EncryptTree(common.EncryptTreeOpts{
DataKey: dataKey,
Tree: tree,
Cipher: opts.Cipher,
})
if err != nil {
return err
}
fileContents, err = opts.InputStore.EmitEncryptedFile(*tree)
if err != nil {
return common.NewExitError(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
}
} else {
fileContents, err = ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("could not read file: %s", err)
}
}
case *publish.VaultDestination:
_, err = common.DecryptTree(common.DecryptTreeOpts{
Cipher: opts.Cipher,
IgnoreMac: false,
Tree: tree,
KeyServices: opts.KeyServices,
})
if err != nil {
return err
}
data, err = sops.EmitAsMap(tree.Branches)
if err != nil {
return err
}
}
if opts.Interactive {
var response string
for response != "y" && response != "n" {
fmt.Printf("uploading %s to %s ? (y/n): ", path, conf.Destination.Path(destinationPath))
_, err := fmt.Scanln(&response)
if err != nil {
return err
}
}
if response == "n" {
msg := fmt.Sprintf("Publication of %s canceled", path)
if opts.Recursive {
fmt.Println(msg)
return nil
} else {
return errors.New(msg)
}
}
}
switch dest := conf.Destination.(type) {
case *publish.S3Destination, *publish.GCSDestination:
err = dest.Upload(fileContents, destinationPath)
case *publish.VaultDestination:
err = dest.UploadUnencrypted(data, destinationPath)
}
if err != nil {
return err
}
return nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}