This repository has been archived by the owner on May 19, 2022. It is now read-only.
forked from carbonblack/cb-event-forwarder
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paths3_behavior.go
156 lines (126 loc) · 4.12 KB
/
s3_behavior.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
package main
import (
"bufio"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
log "github.com/sirupsen/logrus"
)
type S3Behavior struct {
bucketName string
out *s3.S3
region string
}
type S3Statistics struct {
BucketName string `json:"bucket_name"`
Region string `json:"region"`
EncryptionEnabled bool `json:"encryption_enabled"`
}
func (o *S3Behavior) Upload(fileName string, fp *os.File) UploadStatus {
var baseName string
//
// If a prefix is specified then concatenate it with the Base of the filename
//
if config.S3ObjectPrefix != nil {
prefix := *config.S3ObjectPrefix
// cust_name=abc/ingest_dt=2017-05-11/format=cb_response/bucket=the-bucket.2017-05-11T23:59:58
if config.S3VerboseKey == true {
current_time := time.Now().UTC()
baseName = fmt.Sprintf("%s/ingest_dt=%s/format=cb_response/%s,ingest_ts=%s,format=cb_response.json", prefix, current_time.Format("2006-01-02"), prefix, current_time.Format("2006-01-02T15:04:05.000Z"))
} else {
s := []string{prefix, filepath.Base(fileName)}
baseName = strings.Join(s, "/")
}
} else {
baseName = filepath.Base(fileName)
}
var byteReader io.ReadSeeker
if config.S3CompressData != false {
baseName += ".gz"
fileReader := bufio.NewReader(fp)
var gzBytes bytes.Buffer
gzWriter := gzip.NewWriter(&gzBytes)
fileContents, ferr := ioutil.ReadAll(fileReader)
if ferr != nil {
return UploadStatus{fileName: fileName, result: ferr}
}
gzWriter.Write(fileContents)
gzWriter.Close()
byteReader = bytes.NewReader(gzBytes.Bytes())
} else {
byteReader = fp
}
_, err := o.out.PutObject(&s3.PutObjectInput{
Body: byteReader,
Bucket: &o.bucketName,
Key: &baseName,
ServerSideEncryption: config.S3ServerSideEncryption,
ACL: config.S3ACLPolicy,
StorageClass: config.S3StorageClass,
})
fp.Close()
log.WithFields(log.Fields{"Filename": fileName, "Bucket": &o.bucketName}).Debug("Uploading File to Bucket")
return UploadStatus{fileName: fileName, result: err}
}
func (o *S3Behavior) Initialize(connString string) error {
// bucketName can either be a single value (just the bucket name itself, defaulting to "/var/cb/data/event-forwarder" as the
// temporary file directory and "us-east-1" for the AWS region), or:
//
// if bucketName contains two colons, treat it as follows: (temp-file-directory):(region):(bucket-name)
parts := strings.SplitN(connString, ":", 2)
if len(parts) == 1 {
o.bucketName = connString
o.region = "us-east-1"
} else if len(parts) == 2 {
o.bucketName = parts[1]
o.region = parts[0]
} else {
return errors.New(fmt.Sprintf("Invalid connection string: '%s' should look like (temp-file-directory):(region):bucket-name",
connString))
}
awsConfig := &aws.Config{Region: aws.String(o.region)}
if config.S3CredentialProfileName != nil {
parts = strings.SplitN(*config.S3CredentialProfileName, ":", 2)
credentialProvider := credentials.SharedCredentialsProvider{}
if len(parts) == 2 {
credentialProvider.Filename = parts[0]
credentialProvider.Profile = parts[1]
} else {
credentialProvider.Profile = parts[0]
}
creds := credentials.NewCredentials(&credentialProvider)
awsConfig.Credentials = creds
}
sess := session.New(awsConfig)
o.out = s3.New(sess)
_, err := o.out.HeadBucket(&s3.HeadBucketInput{Bucket: &o.bucketName})
if err != nil {
// converting this to a warning, as you could have buckets with PutObject rights but not ListBucket
log.Infof("Could not open bucket %s: %s", o.bucketName, err)
}
return nil
}
func (o *S3Behavior) Key() string {
return fmt.Sprintf("%s:%s", o.region, o.bucketName)
}
func (o *S3Behavior) String() string {
return "AWS S3 " + o.Key()
}
func (o *S3Behavior) Statistics() interface{} {
return S3Statistics{
BucketName: o.bucketName,
Region: o.region,
EncryptionEnabled: config.S3ServerSideEncryption != nil,
}
}