/
gcs.go
317 lines (267 loc) · 8.79 KB
/
gcs.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package gcs
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"cloud.google.com/go/storage"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/shalb/cluster.dev/pkg/hcltools"
"github.com/shalb/cluster.dev/pkg/project"
"github.com/shalb/cluster.dev/pkg/utils"
"github.com/zclconf/go-cty/cty"
"golang.org/x/oauth2"
"google.golang.org/api/impersonate"
"google.golang.org/api/option"
"gopkg.in/yaml.v3"
)
// Backend - describe GCS backend for interface package.backend.
type Backend struct {
storageClient *storage.Client `yaml:"-"`
storageContext context.Context
name string `yaml:"-"`
Bucket string `yaml:"bucket"`
Credentials string `yaml:"credentials,omitempty"`
ImpersonateSA string `yaml:"impersonate_service_account,omitempty"`
ImpersonateSADelegates []string `yaml:"impersonate_service_account_delegates,omitempty"`
AccessToken string `yaml:"access_token,omitempty"`
Prefix string `yaml:"prefix"`
encryptionKey []byte `yaml:"encryption_key,omitempty"`
StorageCustomEndpoint string `yaml:"storage_custom_endpoint,omitempty"`
state map[string]interface{} `yaml:"-"`
ProjectPtr *project.Project `yaml:"-"`
}
func (b *Backend) Configure() error {
if b.storageClient != nil {
return nil
}
ctx := context.Background()
b.storageContext = ctx
b.Prefix = strings.TrimLeft(b.Prefix, "/")
if b.Prefix != "" && !strings.HasSuffix(b.Prefix, "/") {
b.Prefix = b.Prefix + "/"
}
var opts []option.ClientOption
var credOptions []option.ClientOption
// Add credential source
var creds string
var tokenSource oauth2.TokenSource
if b.AccessToken != "" {
tokenSource = oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: b.AccessToken,
})
} else if b.Credentials != "" {
creds = b.Credentials
} else if v := os.Getenv("GOOGLE_BACKEND_CREDENTIALS"); v != "" {
creds = v
} else {
creds = os.Getenv("GOOGLE_CREDENTIALS")
}
if tokenSource != nil {
credOptions = append(credOptions, option.WithTokenSource(tokenSource))
} else if creds != "" {
contents, err := ReadPathOrContents(creds)
if err != nil {
return fmt.Errorf("error loading credentials: %s", err.Error())
}
if !json.Valid([]byte(contents)) {
return fmt.Errorf("the string provided in credentials is neither valid json nor a valid file path")
}
credOptions = append(credOptions, option.WithCredentialsJSON([]byte(contents)))
}
if b.ImpersonateSA != "" {
ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
TargetPrincipal: b.ImpersonateSA,
Scopes: []string{storage.ScopeReadWrite},
Delegates: b.ImpersonateSADelegates,
}, credOptions...)
if err != nil {
return err
}
opts = append(opts, option.WithTokenSource(ts))
} else {
opts = append(opts, credOptions...)
}
if b.StorageCustomEndpoint != "" {
endpoint := option.WithEndpoint(b.StorageCustomEndpoint)
opts = append(opts, endpoint)
}
client, err := storage.NewClient(b.storageContext, opts...)
if err != nil {
return fmt.Errorf("storage.NewClient() failed: %v", err.Error())
}
b.storageClient = client
key := b.encryptionKey
if len(key) > 0 {
kc, err := ReadPathOrContents(string(key))
if err != nil {
return fmt.Errorf("error loading encryption key: %s", err.Error())
}
k, err := base64.StdEncoding.DecodeString(kc)
if err != nil {
return fmt.Errorf("error decoding encryption key: %s", err.Error())
}
b.encryptionKey = k
}
return nil
}
func (b *Backend) State() map[string]interface{} {
return b.state
}
// Name return name.
func (b *Backend) Name() string {
return b.name
}
// Provider return name.
func (b *Backend) Provider() string {
return "gcs"
}
// GetBackendBytes generate terraform backend config.
func (b *Backend) GetBackendBytes(stackName, unitName string) ([]byte, error) {
f, err := b.GetBackendHCL(stackName, unitName)
if err != nil {
return nil, err
}
return f.Bytes(), nil
}
// GetBackendHCL generate terraform backend config.
func (b *Backend) GetBackendHCL(stackName, unitName string) (*hclwrite.File, error) {
bConfigTmpl, err := getStateMap(*b)
if err != nil {
return nil, err
}
bConfigTmpl["prefix"] = fmt.Sprintf("%s%s_%s", b.Prefix, stackName, unitName)
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
terraformBlock := rootBody.AppendNewBlock("terraform", []string{})
backendBlock := terraformBlock.Body().AppendNewBlock("backend", []string{"gcs"})
backendBody := backendBlock.Body()
for key, value := range bConfigTmpl {
backendBody.SetAttributeValue(key, cty.StringVal(value.(string)))
}
return f, nil
}
// GetRemoteStateHCL generate terraform remote state for this backend.
func (b *Backend) GetRemoteStateHCL(stackName, unitName string) ([]byte, error) {
bConfigTmpl, err := getStateMap(*b)
if err != nil {
return nil, err
}
bConfigTmpl["prefix"] = fmt.Sprintf("%s%s_%s", b.Prefix, stackName, unitName)
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
dataBlock := rootBody.AppendNewBlock("data", []string{"terraform_remote_state", fmt.Sprintf("%s-%s", stackName, unitName)})
dataBody := dataBlock.Body()
dataBody.SetAttributeValue("backend", cty.StringVal("gcs"))
config, err := hcltools.InterfaceToCty(bConfigTmpl)
if err != nil {
return nil, err
}
dataBody.SetAttributeValue("config", config)
return f.Bytes(), nil
}
func getStateMap(in Backend) (res map[string]interface{}, err error) {
tmpData, err := yaml.Marshal(in)
if err != nil {
return
}
res = map[string]interface{}{}
err = yaml.Unmarshal(tmpData, &res)
err = utils.ResolveYamlError(tmpData, err)
return
}
func (b *Backend) LockState() error {
lockKey := fmt.Sprintf("cdev.%s.lock", b.ProjectPtr.Name())
// Create a context.
ctx := context.Background()
// Check if the lock object exists.
_, err := b.storageClient.Bucket(b.Bucket).Object(lockKey).Attrs(ctx)
if err == nil {
return fmt.Errorf("lock state file found, the state is locked")
}
sessionID := utils.RandString(10)
// Create the lock object with the sessionID.
lockObject := b.storageClient.Bucket(b.Bucket).Object(lockKey)
w := lockObject.NewWriter(ctx)
defer w.Close()
if _, err := w.Write([]byte(sessionID)); err != nil {
return fmt.Errorf("can't save lock state file: %v", err.Error())
}
// Sleep and read the sessionID from the lock object.
// Compare it with the generated sessionID.
return nil
}
func (b *Backend) UnlockState() error {
lockKey := fmt.Sprintf("cdev.%s.lock", b.ProjectPtr.Name())
// Create a context.
ctx := context.Background()
// Delete the lock object.
return b.storageClient.Bucket(b.Bucket).Object(lockKey).Delete(ctx)
}
func (b *Backend) WriteState(stateData string) error {
stateKey := fmt.Sprintf("cdev.%s.state", b.ProjectPtr.Name())
// Create a context.
ctx := context.Background()
// Create or overwrite the state object with stateData.
stateObject := b.storageClient.Bucket(b.Bucket).Object(stateKey)
w := stateObject.NewWriter(ctx)
defer w.Close()
if _, err := w.Write([]byte(stateData)); err != nil {
return fmt.Errorf("can't save state file: %v", err.Error())
}
return nil
}
func (b *Backend) ReadState() (string, error) {
stateKey := fmt.Sprintf("cdev.%s.state", b.ProjectPtr.Name())
// Create a context.
ctx := context.Background()
// Check if the object exists.
_, err := b.storageClient.Bucket(b.Bucket).Object(stateKey).Attrs(ctx)
if err != nil {
if err == storage.ErrObjectNotExist {
// fmt.Printf("Object '%s' does not exist in bucket '%s'\n", stateKey, b.Bucket)
return "", nil
}
fmt.Printf("Error checking object existence: %v\n", err)
return "", err
}
// Read the state object.
stateObject := b.storageClient.Bucket(b.Bucket).Object(stateKey)
r, err := stateObject.NewReader(ctx)
if err != nil {
return "", err
}
defer r.Close()
stateData, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}
return string(stateData), nil
}
// ReadPathOrContents reads the contents of a file if the input is a file path,
// or returns the input as is if it's not a file path.
func ReadPathOrContents(input string) (string, error) {
// Check if the input contains a path separator (e.g., '/').
if strings.Contains(input, "/") {
// Treat input as a file path and read its contents.
contents, err := readFileContents(input)
if err != nil {
return "", err
}
return contents, nil
}
// Input is not a file path, return it as is.
return input, nil
}
// readFileContents reads the contents of a file given its path.
func readFileContents(filePath string) (string, error) {
// Read the file contents.
contents, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
return string(contents), nil
}