diff --git a/cloudformation.yml b/cloudformation.yml new file mode 100644 index 0000000..bbcbe2d --- /dev/null +++ b/cloudformation.yml @@ -0,0 +1,54 @@ +AWSTemplateFormatVersion: 2010-09-09 +Description: This CloudFormation template for deploying edge re-write lambda function. + +Parameters: + S3SourceBucket: + Type: String + Description: S3 bucket containing lambda code zip file + Default: 'aws-landing-zone-s3-shared-sse-469685743753-ca-central-1' + LambdaCodeZipFile: + Type: 'String' + Description: 'Name of the zip file of the lambda code' + Default: 'edge-rewrite-master.zip' + +Resources: + edgeRewriteRole: + Type: 'AWS::IAM::Role' + Properties: + RoleName: edge-rewrite-role + AssumeRolePolicyDocument: + Version: 2012-10-17 + Statement: + - Effect: Allow + Principal: + Service: + - lambda.amazonaws.com + - edgelambda.amazonaws.com + Action: + - 'sts:AssumeRole' + Policies: + - PolicyName: edgeRewritePolicy + PolicyDocument: + Statement: + - Effect: Allow + Action: + - 'logs:CreateLogGroup' + - 'logs:CreateLogStream' + - 'logs:PutLogEvents' + Resource: '*' + EdgeRewriteFunction: + Type: AWS::Lambda::Function + DependsOn: edgeRewriteRole + Properties: + Code: + S3Bucket: !Ref S3SourceBucket + S3Key: !Ref LambdaCodeZipFile + FunctionName : 'Edge-Rewrite' + Handler: 'handler.handler' + Role: + Fn::GetAtt: + - 'edgeRewriteRole' + - 'Arn' + Runtime: nodejs12.x + Timeout: 900 + \ No newline at end of file diff --git a/edge-rewrite.cmd b/edge-rewrite.cmd new file mode 100644 index 0000000..a161d5b --- /dev/null +++ b/edge-rewrite.cmd @@ -0,0 +1,42 @@ +@echo off + +rem go to directory where cmd script is +cd %~dp0 + +rem check for 7z.exe +rem todo : allow for an environment variable that defines location of 7zip +if not exist "c:\program files\7-zip\7z.exe" goto need7zip + +rem cut up date and time to use for backup filename +set CUR_YYYY=%date:~0,4% +set CUR_MM=%date:~5,2% +set CUR_DD=%date:~8,2% +set CUR_HH=%time:~0,2% +if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%) + +set CUR_MI=%time:~3,2% +set CUR_SS=%time:~6,2% +set CUR_MS=%time:~9,2% + +rem rename zip file for safe keeping +rename Edge-Rewrite.zip Edge-Rewrite_%CUR_YYYY%%CUR_MM%%CUR_DD%_%CUR_HH%%CUR_MI%%CUR_SS%-bak.zip +cd Edge-Rewrite +"c:\program files\7-zip\7z.exe" a -r -x!*.bak ..\Edge-Rewrite.zip * +if errorlevel 1 goto zipError + +goto end + +:need7zip +echo 7zip is either not installed or not found at c:\program files\7-zip +rem pause +goto end + +:zipError +echo. +echo ERROR zipping config file +echo See messages above +rem pause +goto end + +:end +pause diff --git a/rules.sample.json b/rules.sample.json deleted file mode 100644 index c06c864..0000000 --- a/rules.sample.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - "^/topsecret.*$ [F,L]", - "^/oldpath/(\\d*)/(.*)$ /newpath/$2/$1 [L]", - "!^/oldpath.*$ http://www.example.com [R=302,L,NC]", - "^/deadlink.*$ [G]", - "^/foo$ /bar [H=^baz.com$]" -] \ No newline at end of file diff --git a/rules_.json b/rules_.json new file mode 100644 index 0000000..4d703c9 --- /dev/null +++ b/rules_.json @@ -0,0 +1,35 @@ +[ + "^/hosted/rest/services/EGS_SGU/EGS_Flood_Product_Active_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Flood_Inondation/EGS_Flood_Product_Active_en/$1 [R]", + "^/hosted/rest/services/EGS_SGU/EGS_Flood_Product_Active_fr/(.+) /egs_sgu/rest/services/Flood_Inondation/EGS_Flood_Product_Active_fr/$1", + "^/hosted/services/EGS_SGU/EGS_Flood_Product_Active_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/services/Flood_Inondation/EGS_Flood_Product_Active_en/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_Flood_Product_Active_fr/(.+) https://maps-stage.services.geo.ca/egs_sgu/services/Flood_Inondation/EGS_Flood_Product_Active_fr/$1 [R] ", + "^/hosted/rest/services/EGS_SGU/EGS_Flood_Product_Current_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Flood_Inondation/EGS_Flood_Product_Current_en/$1 [R]", + "^/hosted/rest/services/EGS_SGU/EGS_Flood_Product_Current_fr/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Flood_Inondation/EGS_Flood_Product_Current_fr/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_Flood_Product_Current_en/(.+) https://maps-stage.services.geo.ca/egs-sgu/services/Flood_Inondation/EGS_Flood_Product_Current_en/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_Flood_Product_Current_fr/(.+) https://maps-stage.services.geo.ca/egs_sgu/services/Flood_Inondation/EGS_Flood_Product_Current_fr/$1 [R]", + "^/hosted/rest/services/EGS_SGU/EGS_Flood_Product_Archive_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Flood_Inondation/EGS_Flood_Product_Archive_en/$1 [R]", + "^/hosted/rest/services/EGS_SGU/EGS_Flood_Product_Archive_fr/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Flood_Inondation/EGS_Flood_Product_Archive_fr/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_Flood_Product_Archive_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/services/Flood_Inondation/EGS_Flood_Product_Archive_en/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_Flood_Product_Archive_fr/(.+) https://maps-stage.services.geo.ca/egs_sgu/services/Flood_Inondation/EGS_Flood_Product_Archive_fr/$1 [R]", + "^/hosted/rest/services/EGS_SGU/EGS_RiverIce_Product_Active_FP_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Ice_Glace/EGS_RiverIce_Product_Active_FP_en/$1 [R]", + "^/hosted/rest/services/EGS_SGU/EGS_RiverIce_Product_Active_FP_fr/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Ice_Glace/EGS_RiverIce_Product_Active_FP_fr/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_RiverIce_Product_Active_FP_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/services/Ice_Glace/EGS_RiverIce_Product_Active_FP_en/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_RiverIce_Product_Active_FP_fr/(.+) https://maps-stage.services.geo.ca/egs_sgu/services/Ice_Glace/EGS_RiverIce_Product_Active_FP_fr/$1 [R]", + "^/hosted/rest/services/Hosted/EGS_RiverIce_Product_Active/(.+) https://maps-stage.services.geo.ca/hosted/rest/services/Hosted/EGS_RiverIce_Product_Active/$1 [R]", + "^/hosted/rest/services/EGS_SGU/EGS_RiverIce_Product_Current_FP_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Ice_Glace/EGS_RiverIce_Product_Current_FP_en/$1 [R]", + "^/hosted/rest/services/EGS_SGU/EGS_RiverIce_Product_Current_FP_fr/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Ice_Glace/EGS_RiverIce_Product_Current_FP_fr/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_RiverIce_Product_Current_FP_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/services/Ice_Glace/EGS_RiverIce_Product_Current_FP_en/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_RiverIce_Product_Current_FP_fr/(.+) https://maps-stage.services.geo.ca/egs_sgu/services/Ice_Glace/EGS_RiverIce_Product_Current_FP_fr/$1 [R]", + "^/hosted/rest/services/Hosted/EGS_RiverIce_Product_Current/(.+) https://maps-stage.services.geo.ca/hosted/rest/services/Hosted/EGS_RiverIce_Product_Current/$1 [R]", + "^/hosted/rest/services/EGS_SGU/EGS_RiverIce_Product_Archive_FP_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Ice_Glace/EGS_RiverIce_Product_Archive_FP_en/$1 [R]", + "^/hosted/rest/services/EGS_SGU/EGS_RiverIce_Product_Archive_FP_fr/(.+) https://maps-stage.services.geo.ca/egs_sgu/rest/services/Ice_Glace/EGS_RiverIce_Product_Archive_FP_fr/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_RiverIce_Product_Archive_FP_en/(.+) https://maps-stage.services.geo.ca/egs_sgu/services/Ice_Glace/EGS_RiverIce_Product_Archive_FP_en/$1 [R]", + "^/hosted/services/EGS_SGU/EGS_RiverIce_Product_Archive_FP_fr/(.+) /egs_sgu/services/Ice_Glace/EGS_RiverIce_Product_Archive_FP_fr/$1", + "^/hosted/rest/services/Hosted/EGS_RiverIce_Product_Archive/(.+) /hosted/rest/services/Hosted/EGS_RiverIce_Product_Archive/$1", + "^/hosted/rest/services/EGS_SGU/EGS_AcquisitionPlan_Collection_Active_en/(.+) /egs_sgu/rest/services/Acquisition_Plan/EGS_AcquisitionPlan_Collection_Active_en/$1", + "^/hosted/rest/services/EGS_SGU/EGS_AcquisitionPlan_Collection_Active_fr/(.+) /egs_sgu/rest/services/Acquisition_Plan/EGS_AcquisitionPlan_Collection_Active_fr/$1", + "^/hosted/services/EGS_SGU/EGS_AcquisitionPlan_Collection_Active_en/(.+) /egs_sgu/services/Acquisition_Plan/EGS_AcquisitionPlan_Collection_Active_en/$1", + "^/hosted/services/EGS_SGU/EGS_AcquisitionPlan_Collection_Active_fr/(.+) /egs_sgu/services/Acquisition_Plan/EGS_AcquisitionPlan_Collection_Active_fr/$1", + "^/alb /portal/home [H=maps-stage.egs-redir.services.geo.ca]", + "^/oldpath /newpath" +] diff --git a/serverlesscftemplate.yml b/serverlesscftemplate.yml new file mode 100644 index 0000000..ab24301 --- /dev/null +++ b/serverlesscftemplate.yml @@ -0,0 +1,88 @@ +AWSTemplateFormatVersion: 2010-09-09 +Description: The AWS CloudFormation template for this Serverless application +Resources: + ServerlessDeploymentBucket: + Type: 'AWS::S3::Bucket' + Properties: + BucketEncryption: + ServerSideEncryptionConfiguration: + - ServerSideEncryptionByDefault: + SSEAlgorithm: AES256 + ServerlessDeploymentBucketPolicy: + Type: 'AWS::S3::BucketPolicy' + Properties: + Bucket: !Ref ServerlessDeploymentBucket + PolicyDocument: + Statement: + - Action: 's3:*' + Effect: Deny + Principal: '*' + Resource: + - !Join + - '' + - - 'arn:' + - !Ref 'AWS::Partition' + - ':s3:::' + - !Ref ServerlessDeploymentBucket + - /* + Condition: + Bool: + 'aws:SecureTransport': false + RedirectsLogGroup: + Type: 'AWS::Logs::LogGroup' + Properties: + LogGroupName: /aws/lambda/edge-rewrite-prod-redirects + RedirectsLambdaFunction: + Type: 'AWS::Lambda::Function' + Properties: + Code: + S3Bucket: !Ref ServerlessDeploymentBucket + S3Key: >- + serverless/edge-rewrite/prod/1597686890418-2020-08-17T17:54:50.418Z/redirects.zip + FunctionName: edge-rewrite-prod-redirects + Handler: _optimize/edge-rewrite-prod-redirects/handler.handler + MemorySize: 128 + Role: !GetAtt + - edgeRewriteRole0 + - Arn + Runtime: nodejs10.x + Timeout: 1 + DependsOn: + - RedirectsLogGroup + - edgeRewriteRole0 + RedirectsLambdaVersionZR48yggZG3QaSsHAr4ehhOlyd5PmZT30l87x6tpPc8: + Type: 'AWS::Lambda::Version' + DeletionPolicy: Retain + Properties: + FunctionName: !Ref RedirectsLambdaFunction + CodeSha256: hMUDG4BG8RoDD6P5uh3ItjO6xfUgg/PAZ7f0u5WxASo= + edgeRewriteRole0: + Type: 'AWS::IAM::Role' + Properties: + RoleName: edgeRewriteRole-prod + AssumeRolePolicyDocument: + Version: 2012-10-17 + Statement: + - Effect: Allow + Principal: + Service: + - lambda.amazonaws.com + - edgelambda.amazonaws.com + Action: 'sts:AssumeRole' + Policies: + - PolicyName: edgeRewriteRolePolicy-prod + PolicyDocument: + Version: 2012-10-17 + Statement: + - Effect: Allow + Action: + - 'logs:CreateLogGroup' + - 'logs:CreateLogStream' + - 'logs:PutLogEvents' + Resource: 'arn:aws:logs:us-east-1::log-group:/aws/lambda/*:*:*' +Outputs: + ServerlessDeploymentBucketName: + Value: !Ref ServerlessDeploymentBucket + RedirectsLambdaFunctionQualifiedArn: + Description: Current Lambda function version + Value: !Ref RedirectsLambdaVersionZR48yggZG3QaSsHAr4ehhOlyd5PmZT30l87x6tpPc8