/
aws.go
362 lines (312 loc) · 11.4 KB
/
aws.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package cmd
import (
"context"
"fmt"
"os"
"strconv"
"unicode"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var (
bucketName string
tableName string
billingMode string
)
type S3Clientable interface {
CreateBucket(ctx context.Context,
params *s3.CreateBucketInput,
optFns ...func(*s3.Options)) (*s3.CreateBucketOutput, error)
PutPublicAccessBlock(ctx context.Context,
params *s3.PutPublicAccessBlockInput,
optFns ...func(*s3.Options)) (*s3.PutPublicAccessBlockOutput, error)
PutBucketEncryption(ctx context.Context,
params *s3.PutBucketEncryptionInput,
optFns ...func(*s3.Options)) (*s3.PutBucketEncryptionOutput, error)
PutBucketVersioning(ctx context.Context,
params *s3.PutBucketVersioningInput,
optFns ...func(*s3.Options)) (*s3.PutBucketVersioningOutput, error)
GetBucketLocation(ctx context.Context,
params *s3.GetBucketLocationInput,
optFns ...func(*s3.Options)) (*s3.GetBucketLocationOutput, error)
GetPublicAccessBlock(ctx context.Context,
params *s3.GetPublicAccessBlockInput,
optFns ...func(*s3.Options)) (*s3.GetPublicAccessBlockOutput, error)
GetBucketEncryption(ctx context.Context,
params *s3.GetBucketEncryptionInput,
optFns ...func(*s3.Options)) (*s3.GetBucketEncryptionOutput, error)
GetBucketVersioning(ctx context.Context,
params *s3.GetBucketVersioningInput,
optFns ...func(*s3.Options)) (*s3.GetBucketVersioningOutput, error)
}
type DynamoDBClientable interface {
CreateTable(ctx context.Context,
params *dynamodb.CreateTableInput,
optFns ...func(*dynamodb.Options)) (*dynamodb.CreateTableOutput, error)
DescribeTable(ctx context.Context,
params *dynamodb.DescribeTableInput,
optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error)
}
type initS3Result struct {
BucketName string
Region string
BlockPublicAccess string
Encryption string
Versioning string
}
type initDynamoDBResult struct {
TableName string
BillingMode string
WriteCapacity string
ReadCapacity string
}
func NewCmdAws() *cobra.Command {
cmd := &cobra.Command{
Use: "aws",
Short: "Create S3 bucket and DynamoDB table for terraform backend and lock table.",
Long: `Create S3 bucket and DynamoDB table for terraform backend and lock table.
By default, the bucket configuration is below.
- Enabled versioning
- Enabled block public access
- Enabled default encryption: SSE-S3(AES-256)
By default, the table configuration is below.
- Billing mode: PROVISIONED
`,
SilenceUsage: true,
RunE: runCmdAws,
}
cobra.OnInitialize(func() {
initBuildInDefault()
})
// flag
cmd.Flags().StringVarP(&bucketName, "s3", "", "", "Name of S3 bucket to create.")
cmd.MarkFlagRequired("s3")
cmd.Flags().StringVarP(&tableName, "dynamodb", "", "", "Name of DynamoDB table to create.")
cmd.Flags().StringVarP(&billingMode, "billing-mode", "", "", "DynamoDB billing mode. Only 'PAY_PER_REQUEST' or 'PROVISIONED' can be accepted. Default is PROVISIONED.")
return cmd
}
func initBuildInDefault() {
if billingMode == "" {
billingMode = "PROVISIONED"
}
}
func runCmdAws(cmd *cobra.Command, args []string) error {
// Validation
if !validateBucketName(bucketName) {
return fmt.Errorf("bucket name contains capital letter: %v", bucketName)
}
// Load config
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return fmt.Errorf("configuration error: %w", err)
}
// Initialize S3 bucket.
s3 := s3.NewFromConfig(cfg)
s3Res, err := initS3(s3, bucketName, cfg.Region)
if err != nil {
return fmt.Errorf("failed to initialize s3 bucket: %w", err)
}
printCyan(fmt.Sprintf("Successfully create terraform backend - s3 bucket: %v\n", bucketName))
fmt.Printf("Detail ... \n\n")
sTable := tablewriter.NewWriter(os.Stdout)
h, b := s3Res.createTableInput()
sTable.SetHeader(h)
for _, v := range b {
sTable.Append(v)
}
sTable.SetAlignment(tablewriter.ALIGN_LEFT)
sTable.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
sTable.SetCenterSeparator("|")
sTable.Render()
// Initialize DynamoDB table.
if tableName != "" {
dynamodb := dynamodb.NewFromConfig(cfg)
dynamoRes, err := initDynamoDB(dynamodb, tableName, billingMode)
if err != nil {
return fmt.Errorf("failed to initialize dynamodb table: %w", err)
}
printCyan(fmt.Sprintf("Successfully create terraform lock table - dynamodb table: %v\n", tableName))
fmt.Printf("Detail ... \n\n")
dTable := tablewriter.NewWriter(os.Stdout)
h, b := dynamoRes.createTableInput()
dTable.SetHeader(h)
for _, v := range b {
dTable.Append(v)
}
dTable.SetAlignment(tablewriter.ALIGN_LEFT)
dTable.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
dTable.SetCenterSeparator("|")
dTable.Render()
}
return nil
}
// initS3 setup terraform backend with messages.
func initS3(c S3Clientable, bucketName string, region string) (*initS3Result, error) {
fmt.Printf("\n")
fmt.Printf("---------------------------------------------------------\n")
fmt.Printf("🚀 Start to create terraform backend: s3 bucket ... \n")
fmt.Printf("---------------------------------------------------------\n")
fmt.Printf("\n")
// Create bucket
fmt.Printf("Step1: Creating bucket ... ")
if _, err := createS3Bucket(context.TODO(), c, bucketName, region); err != nil {
printRed("FAILURE\n\n")
return nil, fmt.Errorf("failed to create s3 bucket: %w", err)
}
fmt.Printf("SUCCESS\n")
// Activate block all public access
fmt.Printf("Step2: Activate block public access ... ")
if _, err := enableAllPublicAccessBlock(context.TODO(), c, bucketName); err != nil {
printRed("FAILURE\n\n")
return nil, fmt.Errorf("failed to activate block public access of s3 bucket: %w", err)
}
fmt.Printf("SUCCESS\n")
// Activate default encryption
fmt.Printf("Step3: Activate default encryption (AES256) ... ")
if _, err := enableBucketEncryptionAES256(context.TODO(), c, bucketName); err != nil {
printRed("FAILURE\n\n")
return nil, fmt.Errorf("failed to activate default encryption of s3 bucket: %w", err)
}
fmt.Printf("SUCCESS\n")
// Activate versioning
fmt.Printf("Step4: Activate bucket versioning ... ")
if _, err := enableBucketVersioning(context.TODO(), c, bucketName); err != nil {
printRed("FAILURE\n\n")
return nil, fmt.Errorf("failed to activate versioning: %w", err)
}
fmt.Printf("SUCCESS\n")
// Describe bucket
res := initS3Result{
BucketName: bucketName,
}
fmt.Printf("Step5: Confirmation - Get bucket location ... ")
locationRes, err := getBucketLocation(context.TODO(), c, bucketName)
if err != nil {
printRed("FAILURE\n\n")
return nil, fmt.Errorf("successfully created s3 bucket, but failed to describe s3 bucket: %w", err)
}
res.Region = string(locationRes.LocationConstraint)
fmt.Printf("SUCCESS\n")
fmt.Printf("Step6: Confirmation - Get block public access status ... ")
blockRes, err := getPublicAccessBlock(context.TODO(), c, bucketName)
if err != nil {
printRed("FAILURE\n\n")
return nil, fmt.Errorf("successfully created s3 bucket, but failed to describe s3 bucket: %w", err)
}
if blockRes.PublicAccessBlockConfiguration.BlockPublicAcls &&
blockRes.PublicAccessBlockConfiguration.BlockPublicPolicy &&
blockRes.PublicAccessBlockConfiguration.IgnorePublicAcls &&
blockRes.PublicAccessBlockConfiguration.RestrictPublicBuckets {
res.BlockPublicAccess = "Enabled"
} else if !blockRes.PublicAccessBlockConfiguration.BlockPublicAcls &&
!blockRes.PublicAccessBlockConfiguration.BlockPublicPolicy &&
!blockRes.PublicAccessBlockConfiguration.IgnorePublicAcls &&
!blockRes.PublicAccessBlockConfiguration.RestrictPublicBuckets {
res.BlockPublicAccess = "Not fully enabled"
}
fmt.Printf("SUCCESS\n")
fmt.Printf("Step7: Confirmation - Get bucket encryption status ... ")
encryptionRes, err := getBucketEncryption(context.TODO(), c, bucketName)
if err != nil {
printRed("FAILURE\n\n")
return nil, fmt.Errorf("successfully created s3 bucket, but failed to describe s3 bucket: %w", err)
}
if encryptionRes.ServerSideEncryptionConfiguration.Rules[0].ApplyServerSideEncryptionByDefault != nil {
res.Encryption = string(encryptionRes.ServerSideEncryptionConfiguration.Rules[0].ApplyServerSideEncryptionByDefault.SSEAlgorithm)
}
fmt.Printf("SUCCESS\n")
fmt.Printf("Step8: Confirmation - Get bucket versioning status ... ")
versioningRes, err := getBucketVersioning(context.TODO(), c, bucketName)
if err != nil {
printRed("FAILURE\n\n")
return nil, fmt.Errorf("successfully created s3 bucket, but failed to describe s3 bucket: %w", err)
}
res.Versioning = string(versioningRes.Status)
fmt.Printf("SUCCESS\n")
return &res, nil
}
// initDynamoDB setup terraform lock table with messages.
func initDynamoDB(c DynamoDBClientable, tableName string, billingMode string) (*initDynamoDBResult, error) {
fmt.Printf("\n")
fmt.Printf("---------------------------------------------------------\n")
fmt.Printf("🚀 Start to create terraform lock table: DynamoDB ... \n")
fmt.Printf("---------------------------------------------------------\n")
fmt.Printf("\n")
// Create table
fmt.Printf("Step1: Creating table ... ")
if _, err := createDynamoDBTable(context.TODO(), c, tableName, billingMode); err != nil {
printRed("FAILURE\n\n")
return nil, fmt.Errorf("failed to create dynamodb table: %w", err)
}
fmt.Printf("SUCCESS\n")
// Describe table
fmt.Printf("Step2: Confirmation - Describe table ... ")
desc, err := describeDynamoDBTable(context.TODO(), c, tableName)
if err != nil {
printRed("FAILURE\n\n")
return nil, fmt.Errorf("successfully created dynamodb table, but failed to describe dynamodb table: %w", err)
}
fmt.Printf("SUCCESS\n")
res := initDynamoDBResult{}
if desc.Table.TableName != nil {
res.TableName = *desc.Table.TableName
}
if desc.Table.BillingModeSummary != nil {
res.BillingMode = string(desc.Table.BillingModeSummary.BillingMode)
} else if desc.Table.ProvisionedThroughput != nil {
res.BillingMode = "PROVISIONED"
}
if desc.Table.ProvisionedThroughput != nil {
res.WriteCapacity = strconv.FormatInt(*desc.Table.ProvisionedThroughput.WriteCapacityUnits, 10)
res.ReadCapacity = strconv.FormatInt(*desc.Table.ProvisionedThroughput.ReadCapacityUnits, 10)
}
return &res, nil
}
func (i *initS3Result) createTableInput() (header []string, body [][]string) {
h := []string{"PARAMETER", "VALUE"}
b := [][]string{
{"Bucket name", i.BucketName},
{"Region", i.Region},
{"Block Public Access", i.BlockPublicAccess},
{"Encryption", i.Encryption},
{"Versioning", i.Versioning},
}
return h, b
}
func (i *initDynamoDBResult) createTableInput() (header []string, body [][]string) {
h := []string{"PARAMETER", "VALUE"}
if i.BillingMode == "PAY_PER_REQUEST" {
b := [][]string{
{"Table name", i.TableName},
{"Billing mode", i.BillingMode},
}
return h, b
} else {
b := [][]string{
{"Table name", i.TableName},
{"Billing mode", i.BillingMode},
{"Write capacity", i.WriteCapacity},
{"Read capacity", i.ReadCapacity},
}
return h, b
}
}
// validateBucketName checks if bucket name contains capitals.
func validateBucketName(b string) bool {
for _, r := range b {
if !unicode.IsLower(r) && unicode.IsLetter(r) {
return false
}
}
return true
}
func validateBillingMode(m string) bool {
if m != string(types.BillingModePayPerRequest) && m != string(types.BillingModeProvisioned) {
return false
}
return true
}