-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3-signed-url-burnafterreading.yaml
249 lines (222 loc) · 6.93 KB
/
s3-signed-url-burnafterreading.yaml
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
AWSTemplateFormatVersion: 2010-09-09
Description: S3 - Signed url burn after reading mechanism
Parameters:
BucketName:
Description: Bucket name with objects for which we want to create signed urls
Type: String
ObjectKey:
Description: Bucket objects key (path)
Type: String
Default: '/'
CreateBucket:
Description: If we want to use existing bucket or create a new one
Type: String
Default: no
AllowedValues: [ yes, no ]
BurnMethod:
Description: S3 link invalidation by deletion of object or moving to different path
Type: String
Default: delete
AllowedValues: [ delete, move ]
Conditions:
CreateBucket: !Equals [ !Ref CreateBucket, true ]
Resources:
LambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: S3DeleteMove
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- s3:DeleteObject
- s3:GetObject
- s3:PutObject
Resource:
- !Sub 'arn:aws:s3:::${BucketName}${ObjectKey}/*'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
LambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Role: !GetAtt LambdaRole.Arn
Handler: index.lambda_handler
Runtime: python3.7
Timeout: 25
Code:
ZipFile: |
import os
import json
import boto3
import logging
from botocore.exceptions import ClientError
bucket = os.environ['BUCKET']
object_key = os.environ['OBJECT_KEY']
burn_method = os.environ['BURN_METHOD']
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
session = boto3.session.Session()
client = session.client('s3')
def getObject(_bucket, _key):
try:
client.head_object(Bucket=_bucket, Key=_key)
except ClientError as e:
logging.error(e)
return False
return True
def invalidate(_burn_method, _bucket, _key):
if _burn_method == "delete":
try:
response = client.delete_object(
Bucket=_bucket,
Key=_key
)
logger.info('Deleting object %s' % _key)
except ClientError as e:
logging.error(e)
elif _burn_method == "move":
try:
response = client.copy_object(CopySource='%s/%s'% (_bucket, _key), Bucket=_bucket, Key='invalid/%s' % _key)
logger.info('Deleting object %s' % _key)
except ClientError as e:
logging.error(e)
try:
response = client.delete_object(
Bucket=_bucket,
Key=_key
)
logger.info('Deleting object %s' % _key)
except ClientError as e:
logging.error(e)
else:
logger.error('Method %s not supported' % _burn_method)
def lambda_handler(event, context):
logger.debug(event)
key = event['detail']['requestParameters']['key']
if event['detail']['eventName'] == "GetObject":
logger.debug('Deleting object %s' % key)
if not getObject(bucket, key):
return "Object does not exist on path %s" % key
invalidate(burn_method, bucket, key)
Environment:
Variables:
BUCKET: !Ref BucketName
OBJECT_KEY: !Ref ObjectKey
BURN_METHOD: !Ref BurnMethod
Bucket:
Condition: CreateBucket
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
VersioningConfiguration:
Status: Enabled
BucketPolicy:
Condition: CreateBucket
DependsOn: LambdaRole
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref Bucket
PolicyDocument:
Statement:
- Sid: Bucket policy
Effect: Allow
Principal:
AWS:
- !GetAtt LambdaRole.Arn
Action:
- s3:DeleteObject
- s3:GetObject
- s3:PutObject
Resource:
- !GetAtt Bucket.Arn
- !Join [ '', [ !GetAtt Bucket.Arn, '/*'] ]
CloudTrailBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub 'cloudtrail-${AWS::AccountId}'
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
VersioningConfiguration:
Status: Enabled
CloudTrailBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref CloudTrailBucket
PolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Action:
- s3:GetBucketAcl
Resource: !GetAtt CloudTrailBucket.Arn
- Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Action:
- s3:PutObject
Resource: !Sub ${CloudTrailBucket.Arn}/AWSLogs/${AWS::AccountId}/*
Condition:
StringEquals:
s3:x-amz-acl: bucket-owner-full-control
CloudTrail:
DependsOn: CloudTrailBucketPolicy
Type: AWS::CloudTrail::Trail
Properties:
EventSelectors:
- DataResources:
- Type: AWS::S3::Object
Values:
- !Sub ${Bucket.Arn}/
IncludeManagementEvents: false
ReadWriteType: ReadOnly
IncludeGlobalServiceEvents: true
IsLogging: true
IsMultiRegionTrail: false
S3BucketName: !Ref CloudTrailBucket
Event:
Type: AWS::Events::Rule
Properties:
Description: S3 event
EventPattern:
source:
- aws.s3
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- s3.amazonaws.com
eventName:
- GetObject
requestParameters:
bucketName:
- !Ref Bucket
Name: S3SignedUrlLinkInvalidation
State: ENABLED
Targets:
- Arn: !GetAtt LambdaFunction.Arn
Id: LamdaFunction
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref LambdaFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt Event.Arn