This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require("aws-sdk"); | |
const retry = require("async-retry"); | |
const lambda = new AWS.Lambda(); | |
const bailIfErrorNotRetryable = (bail) => (error) => { | |
if (!error.retryable) { | |
bail(error); | |
} else { | |
throw error; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo apt-get update | |
echo 'installing curl' | |
sudo apt install curl -y | |
echo 'installing git' | |
sudo apt install git -y | |
echo "What name do you want to use in GIT user.name?" | |
echo "For example, mine will be \"Fabiano Furlan\"" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Resources: | |
ProductsFunction: | |
Type: AWS::Serverless::Function | |
Properties: | |
CodeUri: products/ | |
Handler: app.handler | |
Layers: | |
- !Ref CommonLayer | |
Runtime: nodejs8.10 | |
Events: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("when send an invalid user", () => { | |
it("returns unauthorized", async () => { | |
const handler = middy(async (event, context) => {}); | |
handler.use(vegetableCompany()); | |
const event = { requestContext: { user: { company: "fruit-company" } } }; | |
const { statusCode } = await handler(event, {}); | |
expect(statusCode).toEqual(401); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createError = require('/opt/node_modules/http-errors'); | |
const middyWrapper = require('/opt/middy-wrapper'); | |
const vegetableCompany= require('/opt/vegetable-company-allowed'); | |
// common-layer/vegetable-company-allowed.js | |
const products = require('/opt/data/vegetable-products.json'); | |
const handler = async (event, context) => { | |
try { | |
return { | |
'statusCode': 200, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createError = require("http-errors"); | |
module.exports = () => { | |
return { | |
before: async handler => { | |
const { event } = handler; | |
const user = event.requestContext && event.requestContext.user; | |
if (!user || user.company !== "vegetable-company") | |
throw new createError.Unauthorized(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const middy = require('/opt/middy-wrapper'); | |
const products = require('/opt/data/vegetable-products.json'); | |
// common-layer/data/vegetable-products.json | |
const handler = async (event, context) => { | |
try { | |
return { | |
'statusCode': 200, | |
'body': JSON.stringify({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{ | |
"id": 1, | |
"title": "Asparagus", | |
"price": 28.1 | |
}, { | |
"id": 2, | |
"title": "Cabbage", | |
"price": 29.45 | |
}, { | |
"id": 3, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const middy = require('middy'); | |
const { httpSecurityHeaders, httpErrorHandler } = require('middy/middlewares'); | |
module.exports = (handler) => { | |
return middy(handler).use(httpSecurityHeaders()) | |
.use(httpErrorHandler()); | |
}; |