-
Notifications
You must be signed in to change notification settings - Fork 48
/
s3.go
177 lines (153 loc) · 4.68 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package accessor
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
sparta "github.com/mweagle/Sparta"
spartaAWS "github.com/mweagle/Sparta/aws"
spartaCF "github.com/mweagle/Sparta/aws/cloudformation"
gocf "github.com/mweagle/go-cloudformation"
"github.com/sirupsen/logrus"
)
// S3Accessor to make it a bit easier to work with S3
// as the backing store
type S3Accessor struct {
testingBucketName string
S3BucketResourceName string
}
// BucketPrivilege returns a privilege that targets the Bucket
func (svc *S3Accessor) BucketPrivilege(bucketPrivs ...string) sparta.IAMRolePrivilege {
return sparta.IAMRolePrivilege{
Actions: bucketPrivs,
Resource: spartaCF.S3ArnForBucket(gocf.Ref(svc.S3BucketResourceName)),
}
}
// KeysPrivilege returns a privilege that targets the Bucket objects
func (svc *S3Accessor) KeysPrivilege(keyPrivileges ...string) sparta.IAMRolePrivilege {
return sparta.IAMRolePrivilege{
Actions: keyPrivileges,
Resource: spartaCF.S3AllKeysArnForBucket(gocf.Ref(svc.S3BucketResourceName)),
}
}
func (svc *S3Accessor) s3Svc(ctx context.Context) *s3.S3 {
logger, _ := ctx.Value(sparta.ContextKeyLogger).(*logrus.Logger)
sess := spartaAWS.NewSession(logger)
s3Client := s3.New(sess)
xrayInit(s3Client.Client)
return s3Client
}
func (svc *S3Accessor) s3BucketName() string {
if svc.testingBucketName != "" {
return svc.testingBucketName
}
discover, discoveryInfoErr := sparta.Discover()
if discoveryInfoErr != nil {
return ""
}
s3BucketRes, s3BucketResExists := discover.Resources[svc.S3BucketResourceName]
if !s3BucketResExists {
return ""
}
return s3BucketRes.ResourceRef
}
// Delete handles deleting the resource
func (svc *S3Accessor) Delete(ctx context.Context, keyPath string) error {
deleteObjectInput := &s3.DeleteObjectInput{
Bucket: aws.String(svc.s3BucketName()),
Key: aws.String(keyPath),
}
_, deleteResultErr := svc.
s3Svc(ctx).
DeleteObjectWithContext(ctx, deleteObjectInput)
return deleteResultErr
}
// DeleteAll handles deleting all the items
func (svc *S3Accessor) DeleteAll(ctx context.Context) error {
// List each one, delete it
listObjectInput := &s3.ListObjectsInput{
Bucket: aws.String(svc.s3BucketName()),
}
listObjectResult, listObjectResultErr := svc.
s3Svc(ctx).
ListObjectsWithContext(ctx, listObjectInput)
if listObjectResultErr != nil {
return nil
}
for _, eachObject := range listObjectResult.Contents {
deleteErr := svc.Delete(ctx, *eachObject.Key)
if deleteErr != nil {
return deleteErr
}
}
return nil
}
// Put handles saving the item
func (svc *S3Accessor) Put(ctx context.Context, keyPath string, object interface{}) error {
jsonBytes, jsonBytesErr := json.Marshal(object)
if jsonBytesErr != nil {
return jsonBytesErr
}
logger, _ := ctx.Value(sparta.ContextKeyLogger).(*logrus.Logger)
logger.WithFields(logrus.Fields{
"Bytes": string(jsonBytes),
"KeyPath": keyPath}).Debug("Saving S3 object")
bytesReader := bytes.NewReader(jsonBytes)
putObjectInput := &s3.PutObjectInput{
Bucket: aws.String(svc.s3BucketName()),
Key: aws.String(keyPath),
Body: bytesReader,
}
putObjectResponse, putObjectRespErr := svc.
s3Svc(ctx).
PutObjectWithContext(ctx, putObjectInput)
logger.WithFields(logrus.Fields{
"Error": putObjectRespErr,
"Results": putObjectResponse}).Debug("Save object results")
return putObjectRespErr
}
// Get handles getting the item
func (svc *S3Accessor) Get(ctx context.Context,
keyPath string,
destObject interface{}) error {
getObjectInput := &s3.GetObjectInput{
Bucket: aws.String(svc.s3BucketName()),
Key: aws.String(keyPath),
}
getObjectResult, getObjectResultErr := svc.
s3Svc(ctx).
GetObjectWithContext(ctx, getObjectInput)
if getObjectResultErr != nil {
return getObjectResultErr
}
jsonBytes, jsonBytesErr := ioutil.ReadAll(getObjectResult.Body)
if jsonBytesErr != nil {
return jsonBytesErr
}
return json.Unmarshal(jsonBytes, destObject)
}
// GetAll handles returning all of the items
func (svc *S3Accessor) GetAll(ctx context.Context,
ctor NewObjectConstructor) ([]interface{}, error) {
listObjectInput := &s3.ListObjectsInput{
Bucket: aws.String(svc.s3BucketName()),
}
listObjectResult, listObjectResultErr := svc.
s3Svc(ctx).
ListObjectsWithContext(ctx, listObjectInput)
if listObjectResultErr != nil {
return nil, listObjectResultErr
}
allObjects := make([]interface{}, 0)
for _, eachObject := range listObjectResult.Contents {
objectInstance := ctor()
entryEntryErr := svc.Get(ctx, *eachObject.Key, objectInstance)
if entryEntryErr != nil {
return nil, entryEntryErr
}
allObjects = append(allObjects, objectInstance)
}
return allObjects, nil
}