-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
s3.go
157 lines (138 loc) · 3.5 KB
/
s3.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
package reporter
import (
"bytes"
"encoding/json"
"fmt"
"path"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"golang.org/x/xerrors"
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/models"
)
// S3Writer writes results to S3
type S3Writer struct {
FormatJSON bool
FormatFullText bool
FormatOneLineText bool
FormatList bool
Gzip bool
config.AWSConf
}
func (w S3Writer) getS3() (*s3.S3, error) {
ses, err := session.NewSession()
if err != nil {
return nil, err
}
config := &aws.Config{
Region: aws.String(w.Region),
Credentials: credentials.NewChainCredentials([]credentials.Provider{
&credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{Filename: "", Profile: w.Profile},
&ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(ses)},
}),
}
s, err := session.NewSession(config)
if err != nil {
return nil, err
}
return s3.New(s), nil
}
// Write results to S3
// http://docs.aws.amazon.com/sdk-for-go/latest/v1/developerguide/common-examples.title.html
func (w S3Writer) Write(rs ...models.ScanResult) (err error) {
if len(rs) == 0 {
return nil
}
svc, err := w.getS3()
if err != nil {
return err
}
if w.FormatOneLineText {
timestr := rs[0].ScannedAt.Format(time.RFC3339)
k := fmt.Sprintf(timestr + "/summary.txt")
text := formatOneLineSummary(rs...)
if err := w.putObject(svc, k, []byte(text), w.Gzip); err != nil {
return err
}
}
for _, r := range rs {
key := r.ReportKeyName()
if w.FormatJSON {
k := key + ".json"
var b []byte
if b, err = json.Marshal(r); err != nil {
return xerrors.Errorf("Failed to Marshal to JSON: %w", err)
}
if err := w.putObject(svc, k, b, w.Gzip); err != nil {
return err
}
}
if w.FormatList {
k := key + "_short.txt"
text := formatList(r)
if err := w.putObject(svc, k, []byte(text), w.Gzip); err != nil {
return err
}
}
if w.FormatFullText {
k := key + "_full.txt"
text := formatFullPlainText(r)
if err := w.putObject(svc, k, []byte(text), w.Gzip); err != nil {
return err
}
}
}
return nil
}
// Validate check the existence of S3 bucket
func (w S3Writer) Validate() error {
svc, err := w.getS3()
if err != nil {
return err
}
result, err := svc.ListBuckets(&s3.ListBucketsInput{})
if err != nil {
return xerrors.Errorf("Failed to list buckets. err: %w, profile: %s, region: %s",
err, w.Profile, w.Region)
}
found := false
for _, bucket := range result.Buckets {
if *bucket.Name == w.S3Bucket {
found = true
break
}
}
if !found {
return xerrors.Errorf("Failed to find the buckets. profile: %s, region: %s, bucket: %s",
w.Profile, w.Region, w.S3Bucket)
}
return nil
}
func (w S3Writer) putObject(svc *s3.S3, k string, b []byte, gzip bool) error {
var err error
if gzip {
if b, err = gz(b); err != nil {
return err
}
k += ".gz"
}
putObjectInput := &s3.PutObjectInput{
Bucket: aws.String(w.S3Bucket),
Key: aws.String(path.Join(w.S3ResultsDir, k)),
Body: bytes.NewReader(b),
}
if w.S3ServerSideEncryption != "" {
putObjectInput.ServerSideEncryption = aws.String(w.S3ServerSideEncryption)
}
if _, err := svc.PutObject(putObjectInput); err != nil {
return xerrors.Errorf("Failed to upload data to %s/%s, err: %w",
w.S3Bucket, k, err)
}
return nil
}