/
s3-bucket-scanner.go
411 lines (372 loc) · 12 KB
/
s3-bucket-scanner.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"golang.org/x/exp/slices"
)
// BucketInfo represents the information about an S3 bucket
type BucketInfo struct {
Name string `json:"name"`
AccelerateConfig *s3.GetBucketAccelerateConfigurationOutput
ACL *s3.GetBucketAclOutput
AnalyticsConfig []*s3.GetBucketAnalyticsConfigurationOutput
CORSConfig *s3.GetBucketCorsOutput
EncryptionConfig *s3.GetBucketEncryptionOutput
IntelligentTieringConfig []*s3.GetBucketIntelligentTieringConfigurationOutput
InventoryConfig []*s3.GetBucketInventoryConfigurationOutput
LifecycleConfig *s3.GetBucketLifecycleConfigurationOutput
Location *s3.GetBucketLocationOutput
LoggingConfig *s3.GetBucketLoggingOutput
MetricsConfig []*s3.GetBucketMetricsConfigurationOutput
NotificationConfig *s3.GetBucketNotificationConfigurationOutput
OwnershipControlsConfig *s3.GetBucketOwnershipControlsOutput
Policy *s3.GetBucketPolicyOutput
PolicyStatus *s3.GetBucketPolicyStatusOutput
ReplicationConfig *s3.GetBucketReplicationOutput
RequestPaymentConfig *s3.GetBucketRequestPaymentOutput
TaggingConfig *s3.GetBucketTaggingOutput
VersioningConfig *s3.GetBucketVersioningOutput
}
func main() {
// Load AWS SDK configuration
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("failed to load AWS SDK config: %v", err)
}
// Create an S3 client
client := s3.NewFromConfig(cfg)
// List S3 buckets
resp, err := client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
if err != nil {
log.Fatalf("failed to list S3 buckets: %v", err)
}
// Create a slice to hold bucket information
bucketInfos := []BucketInfo{}
// Iterate over buckets and retrieve information
for _, bucket := range resp.Buckets {
if slices.Contains(
strings.Split(os.Getenv("EXCLUDED_BUCKETS"), ","),
aws.ToString(bucket.Name),
) {
continue
}
bucketInfo := BucketInfo{
Name: aws.ToString(bucket.Name),
}
// Get bucket accelerate configuration
accelerateConfigResp, err := client.GetBucketAccelerateConfiguration(
context.TODO(),
&s3.GetBucketAccelerateConfigurationInput{
Bucket: bucket.Name,
},
)
if err != nil {
log.Fatalf("failed to get bucket accelerate configuration: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.AccelerateConfig = accelerateConfigResp
}
// Get bucket ACL
aclResp, err := client.GetBucketAcl(context.TODO(), &s3.GetBucketAclInput{
Bucket: bucket.Name,
})
if err != nil {
log.Fatalf("failed to get bucket ACL: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.ACL = aclResp
}
// Get bucket analytics configuration
analyticsConfigurationsResp, err := client.ListBucketAnalyticsConfigurations(
context.TODO(),
&s3.ListBucketAnalyticsConfigurationsInput{
Bucket: bucket.Name,
},
)
if err != nil {
log.Fatalf("failed to list bucket analytics configurations: %+v, %s", err, *bucket.Name)
continue
}
for _, analyticsConfiguration := range analyticsConfigurationsResp.AnalyticsConfigurationList {
analyticsConfigResp, err := client.GetBucketAnalyticsConfiguration(
context.TODO(),
&s3.GetBucketAnalyticsConfigurationInput{
Bucket: bucket.Name,
Id: analyticsConfiguration.Id,
},
)
if err != nil {
log.Fatalf(
"failed to get bucket analytics configuration: %v, %s",
err,
*bucket.Name,
)
continue
}
bucketInfo.AnalyticsConfig = append(bucketInfo.AnalyticsConfig, analyticsConfigResp)
}
// Get bucket CORS configuration
corsConfigResp, err := client.GetBucketCors(context.TODO(), &s3.GetBucketCorsInput{
Bucket: bucket.Name,
})
if err != nil && !strings.Contains(err.Error(), "NoSuchCORSConfiguration") {
log.Fatalf("failed to get bucket CORS configuration: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.CORSConfig = corsConfigResp
}
// Get bucket encryption configuration
encryptionConfigResp, err := client.GetBucketEncryption(
context.TODO(),
&s3.GetBucketEncryptionInput{
Bucket: bucket.Name,
},
)
if err != nil {
log.Fatalf("failed to get bucket encryption configuration: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.EncryptionConfig = encryptionConfigResp
}
// Get bucket intelligent tiering configuration
intelligentTieringConfigurationsResp, err := client.ListBucketIntelligentTieringConfigurations(
context.TODO(),
&s3.ListBucketIntelligentTieringConfigurationsInput{
Bucket: bucket.Name,
},
)
if err != nil {
log.Fatalf(
"failed to list bucket intelligent tiering configurations: %v, %s",
err,
*bucket.Name,
)
continue
}
for _, intelligentTieringConfiguration := range intelligentTieringConfigurationsResp.IntelligentTieringConfigurationList {
intelligentTieringConfigResp, err := client.GetBucketIntelligentTieringConfiguration(
context.TODO(),
&s3.GetBucketIntelligentTieringConfigurationInput{
Bucket: bucket.Name,
Id: intelligentTieringConfiguration.Id,
},
)
if err != nil {
log.Fatalf(
"failed to get bucket intelligent tiering configuration: %v, %s",
err,
*bucket.Name,
)
continue
}
bucketInfo.IntelligentTieringConfig = append(
bucketInfo.IntelligentTieringConfig,
intelligentTieringConfigResp,
)
}
// Get bucket inventory configuration
inventoryConfigurationsResp, err := client.ListBucketInventoryConfigurations(
context.TODO(),
&s3.ListBucketInventoryConfigurationsInput{
Bucket: bucket.Name,
},
)
if err != nil {
log.Fatalf("failed to list bucket inventory configurations: %+v, %s", err, *bucket.Name)
continue
}
for _, inventoryConfiguration := range inventoryConfigurationsResp.InventoryConfigurationList {
inventoryConfigResp, err := client.GetBucketInventoryConfiguration(
context.TODO(),
&s3.GetBucketInventoryConfigurationInput{
Bucket: bucket.Name,
Id: inventoryConfiguration.Id,
},
)
if err != nil {
log.Fatalf(
"failed to get bucket inventory configuration: %v, %s",
err,
*bucket.Name,
)
continue
}
bucketInfo.InventoryConfig = append(bucketInfo.InventoryConfig, inventoryConfigResp)
}
// Get bucket lifecycle configuration
lifecycleConfigResp, err := client.GetBucketLifecycleConfiguration(
context.TODO(),
&s3.GetBucketLifecycleConfigurationInput{
Bucket: bucket.Name,
},
)
if err != nil && !strings.Contains(err.Error(), "NoSuchLifecycleConfiguration") {
log.Fatalf("failed to get bucket lifecycle configuration: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.LifecycleConfig = lifecycleConfigResp
}
// Get bucket location
locationResp, err := client.GetBucketLocation(context.TODO(), &s3.GetBucketLocationInput{
Bucket: bucket.Name,
})
if err != nil {
log.Fatalf("failed to get bucket location: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.Location = locationResp
}
// Get bucket logging configuration
loggingConfigResp, err := client.GetBucketLogging(context.TODO(), &s3.GetBucketLoggingInput{
Bucket: bucket.Name,
})
if err != nil {
log.Fatalf("failed to get bucket logging configuration: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.LoggingConfig = loggingConfigResp
}
// Get bucket metrics configuration
metricsConfigurationsResp, err := client.ListBucketMetricsConfigurations(
context.TODO(),
&s3.ListBucketMetricsConfigurationsInput{
Bucket: bucket.Name,
},
)
if err != nil {
log.Fatalf("failed to list bucket metrics configurations: %+v, %s", err, *bucket.Name)
continue
}
for _, metricsConfiguration := range metricsConfigurationsResp.MetricsConfigurationList {
metricsConfigResp, err := client.GetBucketMetricsConfiguration(
context.TODO(),
&s3.GetBucketMetricsConfigurationInput{
Bucket: bucket.Name,
Id: metricsConfiguration.Id,
},
)
if err != nil {
log.Fatalf("failed to get bucket metrics configuration: %+v, %s", err, *bucket.Name)
continue
}
bucketInfo.MetricsConfig = append(bucketInfo.MetricsConfig, metricsConfigResp)
}
// Get bucket notification configuration
notificationConfigResp, err := client.GetBucketNotificationConfiguration(
context.TODO(),
&s3.GetBucketNotificationConfigurationInput{
Bucket: bucket.Name,
},
)
if err != nil {
log.Fatalf(
"failed to get bucket notification configuration: %+v, %s",
err,
*bucket.Name,
)
} else {
bucketInfo.NotificationConfig = notificationConfigResp
}
// Get bucket ownership controls configuration
ownershipControlsConfigResp, err := client.GetBucketOwnershipControls(
context.TODO(),
&s3.GetBucketOwnershipControlsInput{
Bucket: bucket.Name,
},
)
if err != nil && !strings.Contains(err.Error(), "OwnershipControlsNotFoundError") {
log.Fatalf(
"failed to get bucket ownership controls configuration: %v, %s",
err,
*bucket.Name,
)
} else {
bucketInfo.OwnershipControlsConfig = ownershipControlsConfigResp
}
// Get bucket policy
policyResp, err := client.GetBucketPolicy(context.TODO(), &s3.GetBucketPolicyInput{
Bucket: bucket.Name,
})
if err != nil && !strings.Contains(err.Error(), "NoSuchBucketPolicy") {
log.Fatalf("failed to get bucket policy: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.Policy = policyResp
}
// Get bucket policy status
policyStatusResp, err := client.GetBucketPolicyStatus(
context.TODO(),
&s3.GetBucketPolicyStatusInput{
Bucket: bucket.Name,
},
)
if err != nil && !strings.Contains(err.Error(), "NoSuchBucketPolicy") {
log.Fatalf("failed to get bucket policy status: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.PolicyStatus = policyStatusResp
}
// Get bucket replication configuration
replicationConfigResp, err := client.GetBucketReplication(
context.TODO(),
&s3.GetBucketReplicationInput{
Bucket: bucket.Name,
},
)
if err != nil && !strings.Contains(err.Error(), "ReplicationConfigurationNotFoundError") {
log.Fatalf("failed to get bucket replication configuration: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.ReplicationConfig = replicationConfigResp
}
// Get bucket request payment configuration
requestPaymentConfigResp, err := client.GetBucketRequestPayment(
context.TODO(),
&s3.GetBucketRequestPaymentInput{
Bucket: bucket.Name,
},
)
if err != nil {
log.Fatalf(
"failed to get bucket request payment configuration: %v, %s",
err,
*bucket.Name,
)
} else {
bucketInfo.RequestPaymentConfig = requestPaymentConfigResp
}
// Get bucket tagging configuration
taggingConfigResp, err := client.GetBucketTagging(context.TODO(), &s3.GetBucketTaggingInput{
Bucket: bucket.Name,
})
if err != nil && !strings.Contains(err.Error(), "NoSuchTagSet") {
log.Fatalf("failed to get bucket tagging configuration: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.TaggingConfig = taggingConfigResp
}
// Get bucket versioning configuration
versioningConfigResp, err := client.GetBucketVersioning(
context.TODO(),
&s3.GetBucketVersioningInput{
Bucket: bucket.Name,
},
)
if err != nil {
log.Fatalf("failed to get bucket versioning configuration: %+v, %s", err, *bucket.Name)
} else {
bucketInfo.VersioningConfig = versioningConfigResp
}
bucketInfos = append(bucketInfos, bucketInfo)
}
// Create the output file
file, err := os.Create("bucket_info.json")
if err != nil {
log.Fatalf("failed to create output file: %v", err)
}
defer file.Close()
// Write bucket information to JSON file
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
err = encoder.Encode(bucketInfos)
if err != nil {
log.Fatalf("failed to encode bucket information: %v", err)
}
fmt.Println("Bucket information written to bucket_info.json")
}