Skip to content

Commit d4bc4e1

Browse files
Merge pull request #29 from erezrokah/fix/s3_authorization
Fix: S3 integration ignores authorization settings
2 parents ddb11e9 + cba75d3 commit d4bc4e1

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

lib/package/s3/compileMethodsToS3.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module.exports = {
1414
Properties: {
1515
HttpMethod: event.http.method.toUpperCase(),
1616
RequestParameters: {},
17-
AuthorizationType: 'NONE',
17+
AuthorizationType: event.http.auth.authorizationType,
18+
AuthorizationScopes: event.http.auth.authorizationScopes,
19+
AuthorizerId: event.http.auth.authorizerId,
1820
ApiKeyRequired: Boolean(event.http.private),
1921
ResourceId: resourceId,
2022
RestApiId: this.provider.getApiGatewayRestApiId()

lib/package/s3/compileMethodsToS3.test.js

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const template = {
1111
Type: 'AWS::ApiGateway::Method',
1212
Properties: {
1313
RequestParameters: {},
14-
AuthorizationType: 'NONE',
1514
ApiKeyRequired: false,
1615
ResourceId: { Ref: 'ApiGatewayResourceS3' },
1716
RestApiId: { Ref: 'ApiGatewayRestApi' },
@@ -102,6 +101,9 @@ describe('#compileMethodsToS3()', () => {
102101
const diff = {
103102
Properties: {
104103
HttpMethod: method,
104+
AuthorizationType: http.auth.authorizationType,
105+
AuthorizationScopes: http.auth.authorizationScopes,
106+
AuthorizerId: http.auth.authorizerId,
105107
RequestParameters: requestParams,
106108
Integration: {
107109
IntegrationHttpMethod: intMethod,
@@ -146,7 +148,8 @@ describe('#compileMethodsToS3()', () => {
146148
Ref: 'MyBucket'
147149
},
148150
action: 'GetObject',
149-
key
151+
key,
152+
auth: { authorizationType: 'NONE' }
150153
}
151154

152155
const requestParams = {}
@@ -210,7 +213,8 @@ describe('#compileMethodsToS3()', () => {
210213
Ref: 'MyBucket'
211214
},
212215
action: 'PutObject',
213-
key
216+
key,
217+
auth: { authorizationType: 'NONE' }
214218
}
215219

216220
const requestParams = {
@@ -278,7 +282,8 @@ describe('#compileMethodsToS3()', () => {
278282
Ref: 'MyBucket'
279283
},
280284
action: 'DeleteObject',
281-
key
285+
key,
286+
auth: { authorizationType: 'NONE' }
282287
}
283288

284289
const requestParams = {}
@@ -362,7 +367,8 @@ describe('#compileMethodsToS3()', () => {
362367
'X-Amz-User-Agent'
363368
],
364369
allowCredentials: false
365-
}
370+
},
371+
auth: { authorizationType: 'NONE' }
366372
}
367373
}
368374
]
@@ -386,6 +392,8 @@ describe('#compileMethodsToS3()', () => {
386392
'method.request.path.key': true
387393
},
388394
AuthorizationType: 'NONE',
395+
AuthorizationScopes: undefined,
396+
AuthorizerId: undefined,
389397
ApiKeyRequired: false,
390398
ResourceId: { Ref: 'ApiGatewayResourceS3' },
391399
RestApiId: { Ref: 'ApiGatewayRestApi' },
@@ -463,4 +471,70 @@ describe('#compileMethodsToS3()', () => {
463471
}
464472
})
465473
})
474+
475+
const testAuthorization = (auth) => {
476+
const http = {
477+
path: 's3',
478+
method: 'get',
479+
bucket: {
480+
Ref: 'MyBucket'
481+
},
482+
action: 'GetObject',
483+
key: { pathParam: 'key' },
484+
auth
485+
}
486+
487+
const requestParams = { 'method.request.path.key': true }
488+
489+
const intRequestParams = {
490+
'integration.request.path.object': 'method.request.path.key',
491+
'integration.request.path.bucket': {
492+
'Fn::Sub': [
493+
"'${bucket}'",
494+
{
495+
bucket: {
496+
Ref: 'MyBucket'
497+
}
498+
}
499+
]
500+
}
501+
}
502+
503+
const responseParams = {
504+
'method.response.header.content-type': true,
505+
'method.response.header.Content-Type': true
506+
}
507+
508+
const intResponseParams = {
509+
'method.response.header.content-type': 'integration.response.header.content-type',
510+
'method.response.header.Content-Type': 'integration.response.header.Content-Type'
511+
}
512+
513+
testSingleProxy({
514+
http,
515+
logicalId: 'ApiGatewayMethods3Get',
516+
method: 'GET',
517+
intMethod: 'GET',
518+
requestParams,
519+
intRequestParams,
520+
responseParams,
521+
intResponseParams
522+
})
523+
}
524+
525+
it('should create corresponding resources with "NONE" authorization type', () => {
526+
testAuthorization({ authorizationType: 'NONE' })
527+
})
528+
529+
it('should create corresponding resources with "CUSTOM" authorization type', () => {
530+
testAuthorization({ authorizationType: 'CUSTOM', authorizerId: { Ref: 'AuthorizerLogicalId' } })
531+
})
532+
533+
it('should create corresponding resources with "AWS_IAM" authorization type', () => {
534+
testAuthorization({ authorizationType: 'AWS_IAM' })
535+
})
536+
537+
it('should create corresponding resources with "AWS_IAM" authorization type', () => {
538+
testAuthorization({ authorizationType: 'COGNITO_USER_POOLS', authorizationScopes: ['admin'] })
539+
})
466540
})

0 commit comments

Comments
 (0)