Skip to content

Commit 3a997e4

Browse files
authored
Merge pull request #20 from erezrokah/fix/limit_permissions
Fix: limit sqs,kinesis permissions
2 parents 84a2421 + a82e7bf commit 3a997e4

File tree

14 files changed

+399
-143
lines changed

14 files changed

+399
-143
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
service: multiple-kinesis-proxy
2+
3+
provider:
4+
name: aws
5+
runtime: nodejs10.x
6+
7+
plugins:
8+
localPath: './../../../../../../'
9+
modules:
10+
- serverless-apigateway-service-proxy
11+
12+
custom:
13+
apiGatewayServiceProxies:
14+
- kinesis:
15+
path: /kinesis1
16+
method: post
17+
streamName: { Ref: 'YourStream1' }
18+
cors: true
19+
20+
- kinesis:
21+
path: /kinesis2
22+
method: post
23+
streamName: { Ref: 'YourStream2' }
24+
cors: true
25+
26+
- kinesis:
27+
path: /kinesis3
28+
method: post
29+
streamName: { Ref: 'YourStream3' }
30+
cors: true
31+
32+
resources:
33+
Resources:
34+
YourStream1:
35+
Type: AWS::Kinesis::Stream
36+
Properties:
37+
ShardCount: 1
38+
YourStream2:
39+
Type: AWS::Kinesis::Stream
40+
Properties:
41+
ShardCount: 1
42+
YourStream3:
43+
Type: AWS::Kinesis::Stream
44+
Properties:
45+
ShardCount: 1
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
const expect = require('chai').expect
4+
const fetch = require('node-fetch')
5+
const { deployWithRandomStage, removeService } = require('../../../utils')
6+
7+
describe('Multiple Kinesis Proxy Integrations Test', () => {
8+
let endpoint
9+
let stage
10+
const config = '__tests__/integration/kinesis/multiple-integrations/service/serverless.yml'
11+
12+
beforeAll(async () => {
13+
const result = await deployWithRandomStage(config)
14+
stage = result.stage
15+
endpoint = result.endpoint
16+
})
17+
18+
afterAll(() => {
19+
removeService(stage, config)
20+
})
21+
22+
it('should get correct response from multiple kinesis proxy endpoints', async () => {
23+
const streams = ['kinesis1', 'kinesis2', 'kinesis3']
24+
25+
for (const stream of streams) {
26+
const testEndpoint = `${endpoint}/${stream}`
27+
28+
const response = await fetch(testEndpoint, {
29+
method: 'POST',
30+
headers: { 'Content-Type': 'application/json' },
31+
body: JSON.stringify({ Data: `data for stream ${stream}`, PartitionKey: 'some key' })
32+
})
33+
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*')
34+
expect(response.status).to.be.equal(200)
35+
const body = await response.json()
36+
expect(body).to.have.own.property('ShardId')
37+
expect(body).to.have.own.property('SequenceNumber')
38+
}
39+
})
40+
})

__tests__/integration/kinesis/service/serverless.yml renamed to __tests__/integration/kinesis/single-integration/service/serverless.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ provider:
55
runtime: nodejs10.x
66

77
plugins:
8-
localPath: './../'
8+
localPath: './../../../../../../'
99
modules:
1010
- serverless-apigateway-service-proxy
1111

__tests__/integration/kinesis/tests.js renamed to __tests__/integration/kinesis/single-integration/tests.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
const expect = require('chai').expect
44
const fetch = require('node-fetch')
5-
const { deployService, removeService, getApiGatewayEndpoint } = require('./../../utils')
5+
const { deployWithRandomStage, removeService } = require('../../../utils')
66

7-
describe('Kinesis Proxy Integration Test', () => {
7+
describe('Single Kinesis Proxy Integration Test', () => {
88
let endpoint
9-
let stackName
109
let stage
11-
const config = '__tests__/integration/kinesis/service/serverless.yml'
10+
const config = '__tests__/integration/kinesis/single-integration/service/serverless.yml'
1211

1312
beforeAll(async () => {
14-
stage = Math.random()
15-
.toString(32)
16-
.substring(2)
17-
stackName = 'kinesis-proxy-' + stage
18-
deployService(stage, config)
19-
endpoint = await getApiGatewayEndpoint(stackName)
13+
const result = await deployWithRandomStage(config)
14+
stage = result.stage
15+
endpoint = result.endpoint
2016
})
2117

2218
afterAll(() => {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
service: multiple-sqs-proxy
2+
3+
provider:
4+
name: aws
5+
runtime: nodejs10.x
6+
7+
plugins:
8+
localPath: './../../../../../../'
9+
modules:
10+
- serverless-apigateway-service-proxy
11+
12+
custom:
13+
apiGatewayServiceProxies:
14+
- sqs:
15+
path: /sqs1
16+
method: post
17+
queueName: { 'Fn::GetAtt': ['SQSQueue1', 'QueueName'] }
18+
cors: true
19+
20+
- sqs:
21+
path: /sqs2
22+
method: post
23+
queueName: { 'Fn::GetAtt': ['SQSQueue2', 'QueueName'] }
24+
cors: true
25+
26+
- sqs:
27+
path: /sqs3
28+
method: post
29+
queueName: { 'Fn::GetAtt': ['SQSQueue3', 'QueueName'] }
30+
cors: true
31+
32+
resources:
33+
Resources:
34+
SQSQueue1:
35+
Type: 'AWS::SQS::Queue'
36+
SQSQueue2:
37+
Type: 'AWS::SQS::Queue'
38+
SQSQueue3:
39+
Type: 'AWS::SQS::Queue'
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
const expect = require('chai').expect
4+
const fetch = require('node-fetch')
5+
const { deployWithRandomStage, removeService } = require('../../../utils')
6+
7+
describe('Multiple SQS Proxy Integrations Test', () => {
8+
let endpoint
9+
let stage
10+
const config = '__tests__/integration/sqs/multiple-integrations/service/serverless.yml'
11+
12+
beforeAll(async () => {
13+
const result = await deployWithRandomStage(config)
14+
stage = result.stage
15+
endpoint = result.endpoint
16+
})
17+
18+
afterAll(() => {
19+
removeService(stage, config)
20+
})
21+
22+
it('should get correct response from multiple sqs proxy endpoints', async () => {
23+
const queues = ['sqs1', 'sqs2', 'sqs3']
24+
25+
for (const queue of queues) {
26+
const testEndpoint = `${endpoint}/${queue}`
27+
const response = await fetch(testEndpoint, {
28+
method: 'POST',
29+
headers: { 'Content-Type': 'application/json' },
30+
body: JSON.stringify({ message: `message for ${queue}` })
31+
})
32+
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*')
33+
expect(response.status).to.be.equal(200)
34+
const body = await response.json()
35+
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property(
36+
'MD5OfMessageAttributes'
37+
)
38+
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('MD5OfMessageBody')
39+
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('MessageId')
40+
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('SequenceNumber')
41+
expect(body.SendMessageResponse.ResponseMetadata).to.have.own.property('RequestId')
42+
}
43+
})
44+
})

__tests__/integration/sqs/service/serverless.yml renamed to __tests__/integration/sqs/single-integration/service/serverless.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ provider:
55
runtime: nodejs10.x
66

77
plugins:
8-
localPath: './../'
8+
localPath: './../../../../../../'
99
modules:
1010
- serverless-apigateway-service-proxy
1111

@@ -14,10 +14,10 @@ custom:
1414
- sqs:
1515
path: /sqs
1616
method: post
17-
queueName: {"Fn::GetAtt":[ "SQSQueue", "QueueName" ]}
17+
queueName: { 'Fn::GetAtt': ['SQSQueue', 'QueueName'] }
1818
cors: true
1919

2020
resources:
2121
Resources:
2222
SQSQueue:
23-
Type: "AWS::SQS::Queue"
23+
Type: 'AWS::SQS::Queue'

__tests__/integration/sqs/tests.js renamed to __tests__/integration/sqs/single-integration/tests.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22

33
const expect = require('chai').expect
44
const fetch = require('node-fetch')
5-
const { deployService, removeService, getApiGatewayEndpoint } = require('./../../utils')
5+
const { deployWithRandomStage, removeService } = require('../../../utils')
66

7-
describe('SQS Proxy Integration Test', () => {
7+
describe('Single SQS Proxy Integration Test', () => {
88
let endpoint
9-
let stackName
109
let stage
11-
const config = '__tests__/integration/sqs/service/serverless.yml'
10+
const config = '__tests__/integration/sqs/single-integration/service/serverless.yml'
1211

1312
beforeAll(async () => {
14-
stage = Math.random()
15-
.toString(32)
16-
.substring(2)
17-
stackName = 'sqs-proxy-' + stage
18-
deployService(stage, config)
19-
endpoint = await getApiGatewayEndpoint(stackName)
13+
const result = await deployWithRandomStage(config)
14+
15+
stage = result.stage
16+
endpoint = result.endpoint
2017
})
2118

2219
afterAll(() => {

__tests__/utils.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
11
'use strict'
22

33
const _ = require('lodash')
4+
const yaml = require('js-yaml')
5+
const fs = require('fs')
6+
const path = require('path')
47
const execSync = require('child_process').execSync
58
const aws = require('aws-sdk')
69
const cloudformation = new aws.CloudFormation({ region: 'us-east-1' })
710

11+
async function getApiGatewayEndpoint(stackName) {
12+
const result = await cloudformation.describeStacks({ StackName: stackName }).promise()
13+
14+
const endpointOutput = _.find(result.Stacks[0].Outputs, { OutputKey: 'ServiceEndpoint' })
15+
.OutputValue
16+
return endpointOutput.match(/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/)[0]
17+
}
18+
19+
function deployService(stage, config) {
20+
execSync(`npx serverless deploy --stage ${stage} --config ${path.basename(config)}`, {
21+
stdio: 'inherit',
22+
cwd: path.dirname(config)
23+
})
24+
}
25+
26+
function removeService(stage, config) {
27+
execSync(`npx serverless remove --stage ${stage} --config ${path.basename(config)}`, {
28+
stdio: 'inherit',
29+
cwd: path.dirname(config)
30+
})
31+
}
32+
33+
async function deployWithRandomStage(config) {
34+
const serviceName = yaml.safeLoad(fs.readFileSync(config)).service
35+
const stage = Math.random()
36+
.toString(32)
37+
.substring(2)
38+
const stackName = `${serviceName}-${stage}`
39+
deployService(stage, config)
40+
const endpoint = await getApiGatewayEndpoint(stackName)
41+
42+
return { stage, endpoint }
43+
}
44+
845
module.exports = {
9-
async getApiGatewayEndpoint(stackName) {
10-
const result = await cloudformation.describeStacks({ StackName: stackName }).promise()
11-
12-
const endpointOutput = _.find(result.Stacks[0].Outputs, { OutputKey: 'ServiceEndpoint' })
13-
.OutputValue
14-
return endpointOutput.match(/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/)[0]
15-
},
16-
17-
deployService(stage, config) {
18-
execSync(`npx serverless deploy --stage ${stage} --config ${config}`, {
19-
stdio: 'inherit'
20-
})
21-
},
22-
23-
removeService(stage, config) {
24-
execSync(`npx serverless remove --stage ${stage} --config ${config}`, { stdio: 'inherit' })
25-
}
46+
getApiGatewayEndpoint,
47+
deployService,
48+
removeService,
49+
deployWithRandomStage
2650
}

0 commit comments

Comments
 (0)