Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# package directories
../node_modules
../jspm_packages
../.idea

../package-lock.json

.env
# Serverless directories
.serverless

.gitignore

19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,21 @@ Endpoint backend: https://o3lc79zr1i.execute-api.us-east-1.amazonaws.com/dev
#### Description: Get a product by ID
- productId is required
- productId is String
- curl --location --request GET 'https://o3lc79zr1i.execute-api.us-east-1.amazonaws.com/dev/products/ABCDE102030'
- curl --location --request GET 'https://o3lc79zr1i.execute-api.us-east-1.amazonaws.com/dev/products/ABC001'

### POST - Create product - /product
#### Description: Create a product

- title: String - is required
- description: String - is requires
- price: Number - is required
- count": Number - is required

## Import

### Get - Image
#### Description: Get image from bucket
- curl --location --request GET 'https://guouzuadzd.execute-api.us-east-1.amazonaws.com/dev/import/Alexa'

## Frontend
### URL: http://epam-shopcart.s3-website-us-east-1.amazonaws.com/
31 changes: 31 additions & 0 deletions authorization-service/authorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const path = require('path');
require('dotenv').config({path: path.resolve(__dirname, './.env')});

const utils = require('./utils/util.function');

module.exports.basicAuthorizer = async (event, context, callback) => {
console.log(`Event: ${JSON.stringify(event)}`)
let effect;
let token = '';

if (event[`type`] !== 'TOKEN') {
effect = 'Unauthorized'
} else {
token = event.authorizationToken;
console.log(`Token ${token}`);
effect = await utils.getEffect(token);
console.log(`Effect: ${effect}`);
}

console.log(`Token for Policy: ${token}`);
console.log(`Method for Policy: ${event.methodArn}`);
console.log(`Effect for Policy ${effect}`);

const policy = utils.generatePolicy(token, event.methodArn, effect);

console.log(`Policy: ${JSON.stringify(policy)}`);

callback(null, policy);
};
11 changes: 11 additions & 0 deletions authorization-service/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const dotenv = require('dotenv');

module.exports = async ({ options, resolveConfigurationProperty }) => {
// Load env vars into Serverless environment
// You can do more complicated env var resolution with dotenv here
const envVars = dotenv.config({ path: '.env' }).parsed;
return Object.assign(
{},
envVars, // `dotenv` environment variables
);
};
19 changes: 19 additions & 0 deletions authorization-service/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
},
null,
2
),
};

// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};

26 changes: 26 additions & 0 deletions authorization-service/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
service: authorization-service

frameworkVersion: '3'

provider:
name: aws
runtime: nodejs12.x
environment:
manguianoepam: ${self:custom.dotenvVars.manguianoepam, env:manguianoepam, ''}

plugins:
- serverless-webpack
- serverless-dotenv-plugin

custom:
webpack:
webpackConfig: 'webpack.config.js' # Name of webpack configuration file
includeModules: false
packager: 'npm'
dotenvVars: ${./configs.js)}

functions:
hello:
handler: handler.hello
basicAuthorizer:
handler: authorization.basicAuthorizer
46 changes: 46 additions & 0 deletions authorization-service/test/lambda.authorization.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const expect = require('chai').expect;
const lambda = require('lambda-tester');

const basic = require('../authorization').basicAuthorizer;

describe('Authorization Test', () => {
describe('lambda', () => {
describe('authorization', () => {
it('Should get Allow Effect', async () => {
const result = await lambda(basic)
.event({
"type": "TOKEN",
"methodArn": "arn:aws:execute-api:us-east-1:436988374415:guouzuadzd/dev/GET/import/Alexa",
"authorizationToken": "Basic bWFuZ3VpYW5vZXBhbT1URVNUX1BBU1NXT1JE"
})
.expectResult((data) => data);

expect(result.policyDocument.Statement[0].Effect).equals('Allow');
});

it('Should get Allow Unauthorized', async () => {
const result = await lambda(basic)
.event({
})
.expectResult((data) => data);

expect(result.policyDocument.Statement[0].Effect).equals('Unauthorized');
});

it('Should get Deny Effect', async () => {
const result = await lambda(basic)
.event({
"type": "TOKEN",
"methodArn": "arn:aws:execute-api:us-east-1:436988374415:guouzuadzd/dev/GET/import/Alexa",
"authorizationToken": "Basic d2RIM2ZQREwwdkVDVW1xQ3ZDdGk="
})
.expectResult((data) => data);

expect(result.policyDocument.Statement[0].Effect).equals('Deny');
});

});
})
});
45 changes: 45 additions & 0 deletions authorization-service/utils/util.function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const path = require('path');
require('dotenv').config({path: path.resolve(__dirname, '../.env')});

const getEffect = (token) => {
console.log(`Executing getEffect...`);
try {
const credentials = token.split(' ')[1];
console.log(`Credentials BS64: ${credentials}`);
const buffer = Buffer.from(credentials, 'base64');
const creds = buffer.toString('utf-8').split('=');
console.log(`Credentials: ${creds}`);
const username = creds[0];
const password = creds[1];
const userPass = process.env[username];

console.log(`User: ${userPass}`);

return !userPass || userPass !== password ? 'Deny' : 'Allow';
} catch (error) {
return 'Deny';
}
}

const generatePolicy = (principalId, resource, effect = 'Allow') => {
return {
principalId: principalId,
policyDocument: {
Version: '2012-10-17',
Statement:[
{
Action: 'execute-api:Invoke',
Effect: effect,
Resource: resource
}
]
}
}
};

module.exports = {
getEffect,
generatePolicy
}
11 changes: 11 additions & 0 deletions authorization-service/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
module.exports = {
context: __dirname,
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
target: 'node',
/*target: 'node',
mode: 'none',*/
externals: [nodeExternals(), 'pg-native'],
};
11 changes: 11 additions & 0 deletions import-service/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const dotenv = require('dotenv');

module.exports = async ({ options, resolveConfigurationProperty }) => {
// Load env vars into Serverless environment
// You can do more complicated env var resolution with dotenv here
const envVars = dotenv.config({ path: '.env' }).parsed;
return Object.assign(
{},
envVars, // `dotenv` environment variables
);
};
18 changes: 18 additions & 0 deletions import-service/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
},
null,
2
),
};

// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
15 changes: 15 additions & 0 deletions import-service/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const utils = require('./utils/utils.aws.functions');

module.exports.importFileParser = async (event) => {
console.log(`Event: ${JSON.stringify(event)}`);
for (const record of event.Records) {
await utils.moveImage(record.s3.object.key);
}

return {
statusCode: 200,
body: `importFileParser executed`
};
}
27 changes: 27 additions & 0 deletions import-service/images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const utils = require('./utils/utils.aws.functions');

module.exports.importProductsFile = async (event) => {
const body = {};
let status = 200

if (!event[`pathParameters`].nameImage) {
status = 500;
body.message = 'Name image not provided'
} else {
const image = event[`pathParameters`].nameImage;
const result = await utils.getSignedImage(image).then((data) => data).catch(error => error);

body.message = result.message;
body.url = result.status === 200 ? result.url : '';
}

return {
statusCode: status,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify(body)
};
}
Loading