Skip to content

Commit

Permalink
ADD test lambda function version
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroslavPshenichnikov committed Dec 19, 2023
1 parent 438f391 commit 68b8c0d
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 196 deletions.
1 change: 1 addition & 0 deletions .github/workflows/sam-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
branches:
- develop
- main
- test

jobs:
build-deploy:
Expand Down
217 changes: 21 additions & 196 deletions docspace-reverse-proxy/index.mjs
Original file line number Diff line number Diff line change
@@ -1,198 +1,23 @@
'use strict';

const cachedItem = {};
const regionsMap = {
"default": "default_region_elb_placeholder",
"eu-central-1": "eu_central_1_region_elb_placeholder",
"us-east-2": "us_east_2_region_elb_placeholder"
};

const ddbRegionsMap = {

"default": "eu-central-1",

"us-east-1": "us-east-1",
"us-east-2": "us-east-2",
"us-west-1": "us-east-2",
"us-west-2": "us-east-2",

"eu-central-1": "eu-central-1",
"eu-west-1": "eu-central-1",
"eu-west-2": "eu-central-1",
"eu-west-3": "eu-central-1",
"eu-north-1": "eu-central-1",
"me-central-1": "eu-central-1",

"ap-south-1": "eu-central-1",
"ap-northeast-3": "eu-central-1",
"ap-northeast-2": "eu-central-1",
"ap-southeast-1": "eu-central-1",
"ap-southeast-2": "eu-central-1",
"ap-northeast-1": "eu-central-1"

};

const dynamodbTableName = "dynamodb_table_name_placeholder";

const execRegionCode = process.env.AWS_REGION;

var ddbClientRegion = ddbRegionsMap["default"];

if (ddbRegionsMap[execRegionCode]) {
ddbClientRegion = ddbRegionsMap[execRegionCode];

console.log("change ddbClient region from %s to %s", ddbRegionsMap["default"], ddbClientRegion);
}

import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb";

async function getTenantRegion(ddbRegion, tenantDomain) {

console.log("getTenantRegion params ddbRegion: %s, tenantDomain: %s", ddbRegion, tenantDomain);

const ddbClient = new DynamoDBClient({ region: ddbRegion });

const getItemParams = {
Key: {
'tenant_domain': { S: tenantDomain }
},
ProjectionExpression: "tenant_region",
TableName: dynamodbTableName
};

console.log("[DynamoDb] before send command get item %s with tenant domain %s", getItemParams, tenantDomain);

const response = await ddbClient.send(new GetItemCommand(getItemParams));

console.log("[DynamoDb] responce send command get item %s with tenant domain %s", response, tenantDomain);

if (response && response.Item) {

const tenantRegion = regionsMap[response.Item["tenant_region"]["S"]];

console.log("Added item %s to cache with key %s", tenantRegion, tenantDomain);

return tenantRegion;

} else {

console.log("Error recieve data from DynamoDB with tenant domain %s in region %s", tenantDomain, ddbRegion);

return null;
}
}

export const handler = async (event, context, callback) => {
console.log(JSON.stringify(event));

// Extract the request from the CloudFront event that is sent to Lambda@Edge
var request = event.Records[0].cf.request;

const headers = request.headers;
let tenantDomain = headers.host[0].value;

console.log("current tenant_domain from host header: %s", tenantDomain);



// Redirect from DS to WS if match
const workspaceRegex = /(\/products\/|\/addons\/|.aspx)/i;

if (workspaceRegex.test(request.uri))
{
const newurl = `https://${tenantDomain.replace('onlyoffice.io', 'teamlab.info')}${request.uri}?${request.querystring}`;
console.log("redirect to: %s", newurl);
const response = {
status: '302',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location',
value: newurl,
}],
},
};

return callback(null, response);

}

let originDomain;

if (request.uri == "/apisystem/portal/register" && request.method == "POST") {
console.log("this is body %s", request.body);

let body = JSON.parse(Buffer.from(request.body.data, 'base64').toString('utf8'));
let regionFromRequest = body["awsRegion"];
let portalName = body["portalName"];

originDomain = regionsMap[regionFromRequest];

console.log("Register portal request: Origin Domain is %s, awsRegion is %s", originDomain, regionFromRequest);

if (regionFromRequest == null) {
console.log("Register portal request: awsRegion param is null. Trying set to default");

body["awsRegion"] = "eu-central-1";
request.body.data = Buffer.from(JSON.stringify(body), 'utf8').toString('base64');
request.body.action = "replace";

console.log(request.body.data);

}

if (originDomain == null) {
originDomain = regionsMap["default"];
}

console.log("Register portal request: portalName: %s, regionFromRequest: %s, originDomain: %s", portalName, regionFromRequest, originDomain);

if (originDomain != regionsMap["default"]) {
request.origin.custom["domainName"] = originDomain;
console.log("Register portal request: Change request origin to %s", originDomain);
}

console.log("request after changed %s", JSON.stringify(request));

// Return to CloudFront
return callback(null, request);
}


if (cachedItem[tenantDomain] && cachedItem[tenantDomain].expiresOn > Date.now()) {
originDomain = cachedItem[tenantDomain].value;

console.log("[CACHE] Recieved item %s from cache with key %s", originDomain, tenantDomain);

}
else {
originDomain = await getTenantRegion(ddbClientRegion, tenantDomain);

if (originDomain == null) {

console.log("originDomain is null. Attempt 2. Trying get value from default region %s", ddbRegionsMap["default"]);

originDomain = await getTenantRegion(ddbRegionsMap["default"], tenantDomain);

if (originDomain == null) {
console.log("originDomain is null. Using default");

originDomain = regionsMap["default"];
}
}
}

cachedItem[tenantDomain] = {
expiresOn: Date.now() + 15 * 60 * 1000, // Set expiry time of 15 minutes
value: originDomain
};

if (originDomain != regionsMap["default"]) {
request.origin.custom["domainName"] = originDomain;

console.log("Change request origin to %s", originDomain);
}

// Return to CloudFront
return callback(null, request);
};
// Redirect from DS to WS if match
const workspaceRegex = /(\/products\/|\/addons\/|.aspx)/i;

if (workspaceRegex.test(request.uri))
{
const newurl = `https://${tenantDomain.replace('onlyoffice.io', 'teamlab.info')}${request.uri}?${request.querystring}`;
console.log("redirect to: %s", newurl);
const response = {
status: '302',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location',
value: newurl,
}],
},
};

return callback(null, response);

}
35 changes: 35 additions & 0 deletions test-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# More information about the configuration file can be found here:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
version = 0.1

[default]
[default.global.parameters]
stack_name = "docspace-reverse-proxy-test"

[default.build.parameters]
cached = true
parallel = true

[default.validate.parameters]
lint = true

[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = false
fail_on_empty_changeset = false
resolve_s3 = true
s3_prefix = "docspace-reverse-proxy-test"
region = "us-east-1"
image_repositories = []

[default.package.parameters]
resolve_s3 = true

[default.sync.parameters]
watch = true

[default.local_start_api.parameters]
warm_containers = "EAGER"

[default.local_start_lambda.parameters]
warm_containers = "EAGER"
Loading

0 comments on commit 68b8c0d

Please sign in to comment.