-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan-s3obj.go
183 lines (150 loc) · 4.49 KB
/
scan-s3obj.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
package main
/*
This is an example of a test program that can execute a scan on a HTTP URL.
It will return an array of AMaaS scan results as part of its JSON output.
*/
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
amaasclient "github.com/trendmicro/tm-v1-fs-golang-sdk"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)
func main() {
var bucketregion string
var bucket string
var key string
var grpcAddr string
var apiKey string
var tls bool
var caCert string
var region string
var pml bool
var feedback bool
var verbose bool
var tag string
var digest bool
flag.StringVar(&bucketregion, "bucketregion", "us-west-2", "region for S3 bucket")
flag.StringVar(&bucket, "bucket", "", "S3 bucket name")
flag.StringVar(&key, "key", "", "S3 object key")
flag.StringVar(&grpcAddr, "addr", "", "the address to connect to for GRPC")
flag.StringVar(&apiKey, "apikey", "", "API key for service authentication")
flag.BoolVar(&tls, "tls", false, "enable server TLS by client for GRPC.")
flag.StringVar(®ion, "region", "", "the region to connect to")
flag.BoolVar(&pml, "pml", false, "enable predictive machine learning detection")
flag.BoolVar(&feedback, "feedback", false, "enable SPN feedback")
flag.BoolVar(&verbose, "verbose", false, "enable verbose scan result")
flag.StringVar(&tag, "tag", "", "tags to be used for scanning. separated by comma.")
flag.StringVar(&caCert, "ca_cert", "", "CA certificate for self hosted AMaaS server")
flag.BoolVar(&digest, "digest", false, "enable digest calculation. it might increase network traffic for cloud file.")
flag.Parse()
var ac *amaasclient.AmaasClient
var err error
if region != "" && grpcAddr != "" {
log.Fatal("Both region and addr are specified. Please specify only one.")
} else if region != "" {
ac, err = amaasclient.NewClient(apiKey, region)
if err != nil {
log.Fatalf("Unable to create AMaaS scan client object. error: %v", err)
}
} else if grpcAddr != "" {
ac, err = amaasclient.NewClientInternal(apiKey, grpcAddr, tls, caCert)
if err != nil {
log.Fatalf("Unable to create AMaaS scan client object. error: %v", err)
}
} else {
log.Fatal("Neither region nor addr is specified. Please specify one.")
}
if pml {
ac.SetPMLEnable()
}
if feedback {
ac.SetFeedbackEnable()
}
if verbose {
ac.SetVerboseEnable()
}
if !digest {
// disable digest calculation to reduce network traffic if file is on cloud
ac.SetDigestDisable()
}
var tagsArray []string
if tag != "" {
tagsArray = strings.Split(tag, ",")
}
reader, err := NewS3ClientReader(context.Background(), bucketregion, bucket, key)
if err != nil {
log.Fatalf("Unable to create S3 client reader. error: %v", err)
}
result, err := ac.ScanReader(reader, tagsArray)
if err != nil {
log.Fatalf("Unable to scan reader. error: %v", err)
}
fmt.Printf("%v", result)
os.Exit(0)
}
type S3ClientReader struct {
client *s3.Client
bucket string
key string
size int64
}
func NewS3ClientReader(ctx context.Context, bucketregion, bucket, key string) (*S3ClientReader, error) {
// load default config from environment with specified region
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(bucketregion))
if err != nil {
return nil, err
}
defer ctx.Done()
// create S3 client with given config
client := s3.NewFromConfig(cfg)
attr, err := client.GetObjectAttributes(ctx, &s3.GetObjectAttributesInput{
Bucket: &bucket,
Key: &key,
ObjectAttributes: []types.ObjectAttributes{
types.ObjectAttributesObjectSize,
},
})
if err != nil {
return nil, err
}
if attr.ObjectSize == nil {
return nil, fmt.Errorf("unable to get object size from S3")
}
return &S3ClientReader{
client: client,
bucket: bucket,
key: key,
size: *attr.ObjectSize,
}, nil
}
// S3ClientReader implements AmaasClientReader
func (r *S3ClientReader) Identifier() string {
return fmt.Sprintf("s3://%s/%s", r.bucket, r.key)
}
func (r *S3ClientReader) DataSize() (int64, error) {
return r.size, nil
}
func (r *S3ClientReader) ReadBytes(offset int64, length int32) ([]byte, error) {
var rng string = fmt.Sprintf("bytes=%d-%d", offset, offset+int64(length)-1)
output, err := r.client.GetObject(context.Background(), &s3.GetObjectInput{
Bucket: &r.bucket,
Key: &r.key,
Range: &rng,
})
if err != nil {
return nil, err
}
defer output.Body.Close()
bytes, err := io.ReadAll(output.Body)
if err != nil && err != io.EOF {
bytes = nil
}
return bytes, err
}