Skip to content

Commit af855f2

Browse files
committed
fix: make s3 key, requestParameters mutually exclusive when applicable
1 parent f3c8f52 commit af855f2

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ custom:
189189
action: PutObject
190190
bucket:
191191
Ref: S3Bucket
192-
key: static-key.json
193192
cors: true
194193
195194
requestParameters:
196-
# use auto generated object key (will override the static key)
195+
# if requestParameters has a 'integration.request.path.object' property you should remove the key setting
197196
'integration.request.path.object': 'context.requestId'
197+
"integration.request.header.cache-control": "'public, max-age=31536000, immutable'"
198198
```
199199

200200
### SNS

lib/apiGateway/schema.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ const proxiesSchemas = {
157157
.valid('GetObject', 'PutObject', 'DeleteObject')
158158
.required(),
159159
bucket: stringOrRef.required(),
160-
key: key.required(),
160+
// don't accept a key when requestParameters has a 'integration.request.path.object' property
161+
key: Joi.when('requestParameters', {
162+
is: requestParameters
163+
.keys({ 'integration.request.path.object': Joi.string().required() })
164+
.required(),
165+
then: Joi.forbidden(),
166+
otherwise: key.required()
167+
}),
161168
requestParameters
162169
})
163170
}),

lib/apiGateway/validate.test.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,11 +895,11 @@ describe('#validateServiceProxies()', () => {
895895
)
896896
}
897897

898-
const shouldSucceed = (key, value) => {
898+
const shouldSucceed = (key, value, ...missing) => {
899899
serverlessApigatewayServiceProxy.serverless.service.custom = {
900900
apiGatewayServiceProxies: [
901901
{
902-
s3: getProxy(key, value)
902+
s3: getProxy(key, value, ...missing)
903903
}
904904
]
905905
}
@@ -1026,6 +1026,34 @@ describe('#validateServiceProxies()', () => {
10261026
it('should not throw if requestParameters is a string to string mapping', () => {
10271027
shouldSucceed('requestParameters', { key1: 'value1', key2: 'value2' })
10281028
})
1029+
1030+
it('should throw if requestParameters has "integration.request.path.object" and key is defined', () => {
1031+
shouldError(
1032+
'child "s3" fails because [child "key" fails because ["key" is not allowed]]',
1033+
'requestParameters',
1034+
{
1035+
'integration.request.path.object': 'context.requestId',
1036+
'integration.request.header.cache-control': "'public, max-age=31536000, immutable'"
1037+
}
1038+
)
1039+
})
1040+
1041+
it('should not throw if requestParameters has "integration.request.path.object" and key is not defined', () => {
1042+
shouldSucceed(
1043+
'requestParameters',
1044+
{
1045+
'integration.request.path.object': 'context.requestId',
1046+
'integration.request.header.cache-control': "'public, max-age=31536000, immutable'"
1047+
},
1048+
'key'
1049+
)
1050+
})
1051+
1052+
it(`should not throw if requestParameters doesn't have "integration.request.path.object" and key is defined`, () => {
1053+
shouldSucceed('requestParameters', {
1054+
'integration.request.header.cache-control': "'public, max-age=31536000, immutable'"
1055+
})
1056+
})
10291057
})
10301058

10311059
describe('sns', () => {

lib/package/s3/compileMethodsToS3.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,20 @@ module.exports = {
122122
getS3MethodIntegration(http) {
123123
const bucket = http.bucket
124124
const httpMethod = this.getIntegrationHttpMethod(http)
125-
const objectRequestParam = this.getObjectRequestParameter(http)
126-
const requestParams = _.merge(this.getIntegrationRequestParameters(http), {
127-
'integration.request.path.object': objectRequestParam,
125+
126+
let requestParams = _.merge(this.getIntegrationRequestParameters(http), {
128127
'integration.request.path.bucket': {
129128
'Fn::Sub': ["'${bucket}'", { bucket }]
130129
}
131130
})
131+
132+
if (_.has(http, 'key')) {
133+
const objectRequestParam = this.getObjectRequestParameter(http)
134+
requestParams = _.merge(requestParams, {
135+
'integration.request.path.object': objectRequestParam
136+
})
137+
}
138+
132139
const responseParams = this.getIntegrationResponseParameters(http)
133140

134141
const integration = {

lib/package/s3/compileMethodsToS3.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ describe('#compileMethodsToS3()', () => {
576576
Ref: 'MyBucket'
577577
},
578578
action: 'PutObject',
579-
key: 'static',
580579
auth: { authorizationType: 'NONE' },
581580
requestParameters: requestParametersOverride
582581
}
@@ -586,7 +585,6 @@ describe('#compileMethodsToS3()', () => {
586585
}
587586

588587
const intRequestParams = {
589-
'integration.request.path.object': 'method.request.path.key',
590588
'integration.request.path.bucket': {
591589
'Fn::Sub': [
592590
"'${bucket}'",

0 commit comments

Comments
 (0)