-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3Report.go
123 lines (113 loc) · 2.87 KB
/
s3Report.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
/*
s3report-go is a s3 bucket report generator.
It uses default AWS credentials to authenticate with AWS API's and establish a client.
Once the script authenticates it lists all buckets, and gathers information about each bucket.
Usage:
s3report-go [flags] [path ...]
The flags are:
-f
The filename of the generated csv file.
*/package main
import (
"context"
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"strconv"
"github.com/Smiddie31/s3Tools"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type s3Bucket struct {
name string
region string
versioning string
encStatus string
encType string
logStatus string
logBucket string
polStatus bool
}
func main() {
var fName string
flag.StringVar(&fName, "f", "bucket-data", "Specify filename. Default is 'bucket-data'")
flag.Parse()
var bucketData []*s3Bucket
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("failed to load configuration, %v", err)
}
s3Client := s3.NewFromConfig(cfg, func(o *s3.Options) {})
buckets, awsErr := s3Tools.ListBuckets(context.Background(), s3Client)
if awsErr != nil {
log.Fatalf("Couldn't list buckets: %v", err)
return
}
for _, bucket := range buckets.Buckets {
bL, blErr := s3Tools.GetBucketLocation(context.Background(), s3Client, *bucket.Name)
if blErr != nil {
log.Fatalf("Couldn't locate bucket: %v", blErr)
}
bLocation := string(bL.LocationConstraint)
if bLocation == "" {
bLocation = "us-east-1"
}
s3Client := s3.NewFromConfig(cfg, func(o *s3.Options) {
o.Region = bLocation
})
vStatus := s3Tools.GetBucketVersioning(context.Background(), s3Client, *bucket.Name)
eStatus, eType := s3Tools.GetBucketEncryption(context.Background(), s3Client, *bucket.Name)
lStatus, lBucket := s3Tools.GetBucketLogging(context.Background(), s3Client, *bucket.Name)
pStatus := s3Tools.GetBucketPolicyStatus(context.Background(), s3Client, *bucket.Name)
bucketData = append(
bucketData,
&s3Bucket{*bucket.Name, bLocation, vStatus, eStatus, eType, lStatus, lBucket, pStatus},
)
}
s := fmt.Sprintf("%v.csv", fName)
file, err := os.Create(s)
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatalln("failed to close file", err)
}
}(file)
if err != nil {
log.Fatalln("failed to open file", err)
}
w := csv.NewWriter(file)
defer w.Flush()
var data [][]string
data = append(
data,
[]string{
"Name",
"Region",
"Versioning",
"Encryption Status",
"Encryption Type",
"Logging",
"Logging Bucket",
"Public",
},
)
for _, record := range bucketData {
row := []string{
record.name,
record.region,
record.versioning,
record.encStatus,
record.encType,
record.logStatus,
record.logBucket,
strconv.FormatBool(record.polStatus),
}
data = append(data, row)
}
errData := w.WriteAll(data)
if errData != nil {
return
}
}