-
Notifications
You must be signed in to change notification settings - Fork 103
/
provider.go
293 lines (253 loc) · 7.01 KB
/
provider.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package provider
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"time"
"golang.org/x/exp/slog"
)
type Home interface {
Bootstrap(app, stage string) error
getData(key, app, stage string) (io.Reader, error)
putData(key, app, stage string, metadata map[string]string, data io.Reader) error
removeData(key, app, stage string) error
setPassphrase(app, stage string, passphrase string) error
getPassphrase(app, stage string) (string, error)
}
type DevTransport struct {
In chan string
Out chan string
}
type Provider interface {
Init(app string, stage string, args map[string]interface{}) error
Env() (map[string]string, error)
}
type DevEvent struct {
*io.PipeReader
}
func (dt *DevTransport) Publish(input interface{}) error {
jsonBytes, err := json.Marshal(input)
if err != nil {
return err
}
dt.Out <- string(jsonBytes)
return nil
}
type DevSession interface {
Cleanup() error
Publish(json string) error
}
const SSM_NAME_BOOTSTRAP = "/sst/bootstrap"
var ErrLockExists = fmt.Errorf("Concurrent update detected, run `sst unlock` to delete lock file and retry.")
var passphraseCache = map[Home]map[string]string{}
func Passphrase(backend Home, app, stage string) (string, error) {
slog.Info("getting passphrase", "app", app, "stage", stage)
cache, ok := passphraseCache[backend]
if !ok {
cache = map[string]string{}
passphraseCache[backend] = cache
}
existingPassphrase, ok := cache[app+stage]
if ok {
return existingPassphrase, nil
}
passphrase, err := backend.getPassphrase(app, stage)
if err != nil {
return "", err
}
if passphrase == "" {
slog.Info("passphrase not found, setting passphrase", "app", app, "stage", stage)
bytes := make([]byte, 32)
_, err := rand.Read(bytes)
if err != nil {
return "", err
}
passphrase = base64.StdEncoding.EncodeToString(bytes)
err = backend.setPassphrase(app, stage, passphrase)
if err != nil {
return "", err
}
}
existingPassphrase, ok = cache[app+stage]
return passphrase, nil
}
func GetLinks(backend Home, app, stage string) (map[string]interface{}, error) {
data := map[string]interface{}{}
err := getData(backend, "link", app, stage, true, &data)
if err != nil {
return nil, err
}
return data, err
}
type Summary struct {
UpdateID string `json:"updateID"`
TimeStarted string `json:"timeStarted"`
TimeCompleted string `json:"timeCompleted"`
ResourceUpdated int `json:"resourceUpdated"`
ResourceCreated int `json:"resourceCreated"`
ResourceDeleted int `json:"resourceDeleted"`
ResourceSame int `json:"resourceSame"`
}
func PutLinks(backend Home, app, stage string, data map[string]interface{}) error {
slog.Info("putting links", "app", app, "stage", stage)
if data == nil || len(data) == 0 {
return nil
}
return putData(backend, "link", app, stage, true, map[string]string{}, data)
}
func PutSummary(backend Home, app, stage, updateID string, summary Summary) error {
slog.Info("putting summary", "app", app, "stage", stage)
return putData(backend, "summary", app, stage+"/"+updateID, false, map[string]string{}, summary)
}
func GetSecrets(backend Home, app, stage string) (map[string]string, error) {
data := map[string]string{}
err := getData(backend, "secret", app, stage, true, &data)
if err != nil {
return nil, err
}
return data, err
}
func PutSecrets(backend Home, app, stage string, data map[string]string) error {
slog.Info("putting secrets", "app", app, "stage", stage)
if data == nil {
return nil
}
return putData(backend, "secret", app, stage, true, map[string]string{}, data)
}
func PushState(backend Home, updateID string, app, stage string, from string) error {
slog.Info("pushing state", "app", app, "stage", stage, "from", from)
file, err := os.Open(from)
if err != nil {
return nil
}
return backend.putData("app", app, stage, map[string]string{
"updateID": updateID,
}, file)
}
var ErrStateNotFound = fmt.Errorf("state not found")
func PullState(backend Home, app, stage string, out string) error {
slog.Info("pulling state", "app", app, "stage", stage, "out", out)
reader, err := backend.getData("app", app, stage)
if err != nil {
return err
}
if reader == nil {
return ErrStateNotFound
}
file, err := os.Create(out)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, reader)
if err != nil {
return err
}
return nil
}
type lockData struct {
Created time.Time `json:"created"`
UpdateID string `json:"updateID"`
Command string `json:"command"`
}
func Lock(backend Home, updateID, command, app, stage string) error {
slog.Info("locking", "app", app, "stage", stage)
var lockData lockData
err := getData(backend, "lock", app, stage, false, &lockData)
if err != nil {
return err
}
if !lockData.Created.IsZero() {
return ErrLockExists
}
lockData.Created = time.Now()
lockData.UpdateID = updateID
lockData.Command = command
err = putData(backend, "lock", app, stage, false, map[string]string{}, lockData)
if err != nil {
return err
}
return nil
}
func Unlock(backend Home, app, stage string) error {
slog.Info("unlocking", "app", app, "stage", stage)
return removeData(backend, "lock", app, stage)
}
func putData(backend Home, key, app, stage string, encrypt bool, metadata map[string]string, data interface{}) error {
slog.Info("putting data", "key", key, "app", app, "stage", stage)
jsonBytes, err := json.Marshal(data)
if err != nil {
return err
}
if encrypt {
passphrase, err := Passphrase(backend, app, stage)
if err != nil {
return err
}
passphraseBytes, err := base64.StdEncoding.DecodeString(passphrase)
if err != nil {
return err
}
blockCipher, err := aes.NewCipher(passphraseBytes)
if err != nil {
return err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = rand.Read(nonce); err != nil {
return err
}
jsonBytes = gcm.Seal(nonce, nonce, jsonBytes, nil)
}
return backend.putData(key, app, stage, metadata, bytes.NewReader(jsonBytes))
}
func getData(backend Home, key, app, stage string, encrypted bool, out interface{}) error {
slog.Info("getting data", "key", key, "app", app, "stage", stage)
reader, err := backend.getData(key, app, stage)
if err != nil {
return err
}
if reader == nil {
return nil
}
data, err := io.ReadAll(reader)
if err != nil {
return err
}
if encrypted {
passphrase, err := Passphrase(backend, app, stage)
if err != nil {
return err
}
passphraseBytes, err := base64.StdEncoding.DecodeString(passphrase)
if err != nil {
return err
}
blockCipher, err := aes.NewCipher(passphraseBytes)
if err != nil {
return err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return err
}
nonce, ciphertext := data[:gcm.NonceSize()], data[gcm.NonceSize():]
data, err = gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return err
}
}
return json.Unmarshal(data, out)
}
func removeData(backend Home, key, app, stage string) error {
return backend.removeData(key, app, stage)
}