Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Projects for direct Lambda Functions & Custom Authorizer #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
18 changes: 6 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
.vs/
UltimatePDF_ExternalLogic/bin/
UltimatePDF_ExternalLogic/obj/
UltimatePDF_ExternalLogic.Tests/bin/
UltimatePDF_ExternalLogic.Tests/obj/

TestLambda_UltimatePDF_ExternalLogic/.vs/
TestLambda_UltimatePDF_ExternalLogic/src/TestLambda_UltimatePDF_ExternalLogic/.vs/
TestLambda_UltimatePDF_ExternalLogic/src/TestLambda_UltimatePDF_ExternalLogic/bin/
TestLambda_UltimatePDF_ExternalLogic/src/TestLambda_UltimatePDF_ExternalLogic/obj/
TestLambda_UltimatePDF_ExternalLogic/src/TestLambda_UltimatePDF_ExternalLogic/Properties/launchSettings.json
.vs
bin
obj

*.zip
*.cache
*.pdf
*.dll
*.br
*.so
*.pdb
*.pdb

launchSettings.json
61 changes: 61 additions & 0 deletions UltimatePDFLambdaAuthorizer/Function.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace UltimatePDFLambdaAuthorizer
{
public class Function
{
public APIGatewayCustomAuthorizerResponse FunctionHandler(APIGatewayCustomAuthorizerRequest request, ILambdaContext context)
{
var authorized = false;

request.Headers.TryGetValue("Authorization", out string? authHeader);

if (!string.IsNullOrWhiteSpace(authHeader)) {
var encodedCredentials = authHeader.Split(' ')[1];

byte[] data = Convert.FromBase64String(encodedCredentials);
var decodedCredentials = System.Text.Encoding.UTF8.GetString(data).Split(":");
var username = decodedCredentials[0];
var password = decodedCredentials[1];

// Sample credentials below for demostration purposes
// These details should be fetched from somewhere secure and configurable
if (username == "test" && password == "test")
{
authorized = true;
}
}

APIGatewayCustomAuthorizerPolicy policy = new APIGatewayCustomAuthorizerPolicy
{
Version = "2012-10-17",
Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>()
};

policy.Statement.Add(new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement
{
Action = new HashSet<string>(new string[] { "execute-api:Invoke" }),
Effect = authorized ? "Allow" : "Deny",
Resource = new HashSet<string>(new string[] { request.MethodArn })

});

APIGatewayCustomAuthorizerContextOutput contextOutput = new APIGatewayCustomAuthorizerContextOutput()
{
["User"] = "User",
["Path"] = request.MethodArn
};

return new APIGatewayCustomAuthorizerResponse
{
PrincipalID = "User",
Context = contextOutput,
PolicyDocument = policy
};
}
}
}
15 changes: 15 additions & 0 deletions UltimatePDFLambdaAuthorizer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
This project is for illustration purposes only and provides a sample Custom Authorizer for use with AWS API Gateway endpoints using Basic Auth

Step to execute the code:
1. Run .\generate_upload_package.ps1
1. Create a AWS Lambda .net 6.0 runtime
1. Upload the zip UltimatePDFLambdaAuthorizer.zip as the lambda function code
1. Add the lambda function as a new request based custom authorizer on API gateway


To test the custom authorizer call an API Gateway endpoint that has the authorizer enabled using Basic Auth headers

```text
// This header equates to Basic Auth with username: test | password: test
Authorization: Basic dGVzdDp0ZXM=
```
16 changes: 16 additions & 0 deletions UltimatePDFLambdaAuthorizer/UltimatePDFLambdaAuthorizer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.7.1" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.3" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions UltimatePDFLambdaAuthorizer/generate_upload_package.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
dotnet publish -c Release -r linux-x64 --self-contained false
Compress-Archive -Path .\bin\Release\net6.0\linux-x64\publish\* -Update -DestinationPath UltimatePDFLambdaAuthorizer.zip -CompressionLevel Optimal
117 changes: 117 additions & 0 deletions UltimatePDFLambdaFunctions/Functions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Amazon.Lambda.Core;
using UltimatePDFLambdaFunctions.Inputs;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace UltimatePDFLambdaFunctions
{
/// <summary>
/// A class that will hold our AWS Function methods.
/// </summary>
public class Functions
{
/// <summary>
/// This function returns a base64 encoded string representation of the generated PDF
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns>Base64 encoded string representation of the generated PDF</returns>
public string PrintPDFFunctionHandler(PrintPDFInput input, ILambdaContext context)
{
var uri = new Uri(input.Url);
var ultimatePdfLib = new OutSystems.UltimatePDF_ExternalLogic.UltimatePDF_ExternalLogic();
var environment = new OutSystems.UltimatePDF_ExternalLogic.Structures.Environment
{
BaseURL = uri.Host,
Locale = input.Environment.Locale,
Timezone = input.Environment.Timezone
};

var pdf = ultimatePdfLib.PrintPDF(
input.Url,
input.Viewport,
environment,
input.Cookies,
input.Paper,
input.TimeoutSeconds,
input.CollectLogs,
input.AttachFilesLogs,
out byte[] logsZipFile);

return Convert.ToBase64String(pdf);
}

public void PrintPDFToRestFunctionHandler(PrintPDFToRestInput input, ILambdaContext context)
{
var uri = new Uri(input.Url);
var ultimatePdfLib = new OutSystems.UltimatePDF_ExternalLogic.UltimatePDF_ExternalLogic();
var environment = new OutSystems.UltimatePDF_ExternalLogic.Structures.Environment
{
BaseURL = uri.Host,
Locale = input.Environment.Locale,
Timezone = input.Environment.Timezone
};

ultimatePdfLib.PrintPDF_ToRest(
input.Url,
input.Viewport,
environment,
input.Cookies,
input.Paper,
input.TimeoutSeconds,
input.CollectLogs,
input.AttachFilesLogs,
input.RestCaller);
}

public void PrintPDFToS3FunctionHandler(PrintPDFToS3Input input, ILambdaContext context)
{
var uri = new Uri(input.Url);
var ultimatePdfLib = new OutSystems.UltimatePDF_ExternalLogic.UltimatePDF_ExternalLogic();
var environment = new OutSystems.UltimatePDF_ExternalLogic.Structures.Environment
{
BaseURL = uri.Host,
Locale = input.Environment.Locale,
Timezone = input.Environment.Timezone
};

ultimatePdfLib.PrintPDF_ToS3(
input.Url,
input.Viewport,
environment,
input.Cookies,
input.Paper,
input.TimeoutSeconds,
input.CollectLogs,
input.AttachFilesLogs,
input.S3Endpoints);
}

public string ScreenshotPNGFunctionHandler(ScreenshotPNGInput input, ILambdaContext context)
{
var uri = new Uri(input.Url);
var ultimatePdfLib = new OutSystems.UltimatePDF_ExternalLogic.UltimatePDF_ExternalLogic();
var environment = new OutSystems.UltimatePDF_ExternalLogic.Structures.Environment
{
BaseURL = uri.Host,
Locale = input.Environment.Locale,
Timezone = input.Environment.Timezone
};

var screenshot = ultimatePdfLib.ScreenshotPNG(
input.Url,
input.Viewport,
environment,
input.Cookies,
input.Paper,
input.ScreenshotOptions,
input.TimeoutSeconds,
input.CollectLogs,
input.AttachFilesLogs,
out byte[] logsZipFile);

return Convert.ToBase64String(screenshot);
}
}
}
33 changes: 33 additions & 0 deletions UltimatePDFLambdaFunctions/Inputs/PrintPDFInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using OutSystems.UltimatePDF_ExternalLogic.Structures;
using Environment = OutSystems.UltimatePDF_ExternalLogic.Structures.Environment;

namespace UltimatePDFLambdaFunctions.Inputs
{
public class PrintPDFInput
{
public string Url { get; init; } = String.Empty;
public Viewport Viewport { get; init; } = new Viewport { Width = 1366, Height = 768 };
public Environment Environment { get; init; } = new Environment
{
BaseURL = "",
Locale = "en",
Timezone = "Europe/Lisbon"
};
public IEnumerable<Cookie> Cookies { get; init; } = Array.Empty<Cookie>();
public Paper Paper { get; init; } = new Paper
{
UseCustomPaper = false,
Width = 21,
Height = 29.7M,
UseCustomMargins = false,
MarginTop = 2.54M,
MarginRight = 2.54M,
MarginBottom = 2.54M,
MarginLeft = 2.54M
};
public int TimeoutSeconds { get; init; } = 120;
public bool CollectLogs { get; init; } = false;
public bool AttachFilesLogs { get; init; } = false;
public byte[]? LogsZipFile { get; init; } = null;
}
}
33 changes: 33 additions & 0 deletions UltimatePDFLambdaFunctions/Inputs/PrintPDFToRestInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using OutSystems.UltimatePDF_ExternalLogic.Structures;
using Environment = OutSystems.UltimatePDF_ExternalLogic.Structures.Environment;

namespace UltimatePDFLambdaFunctions.Inputs
{
public class PrintPDFToRestInput
{
public string Url { get; init; } = String.Empty;
public Viewport Viewport { get; init; } = new Viewport { Width = 1366, Height = 768 };
public Environment Environment { get; init; } = new Environment
{
BaseURL = "",
Locale = "en",
Timezone = "Europe/Lisbon"
};
public IEnumerable<Cookie> Cookies { get; init; } = Array.Empty<Cookie>();
public Paper Paper { get; init; } = new Paper
{
UseCustomPaper = false,
Width = 21,
Height = 29.7M,
UseCustomMargins = false,
MarginTop = 2.54M,
MarginRight = 2.54M,
MarginBottom = 2.54M,
MarginLeft = 2.54M
};
public int TimeoutSeconds { get; init; } = 120;
public bool CollectLogs { get; init; } = false;
public bool AttachFilesLogs { get; init; } = false;
public RestCaller RestCaller { get; init; } = default;
}
}
33 changes: 33 additions & 0 deletions UltimatePDFLambdaFunctions/Inputs/PrintPDFToS3Input.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using OutSystems.UltimatePDF_ExternalLogic.Structures;
using Environment = OutSystems.UltimatePDF_ExternalLogic.Structures.Environment;

namespace UltimatePDFLambdaFunctions.Inputs
{
public class PrintPDFToS3Input
{
public string Url { get; init; } = String.Empty;
public Viewport Viewport { get; init; } = new Viewport { Width = 1366, Height = 768 };
public Environment Environment { get; init; } = new Environment
{
BaseURL = "",
Locale = "en",
Timezone = "Europe/Lisbon"
};
public IEnumerable<Cookie> Cookies { get; init; } = Array.Empty<Cookie>();
public Paper Paper { get; init; } = new Paper
{
UseCustomPaper = false,
Width = 21,
Height = 29.7M,
UseCustomMargins = false,
MarginTop = 2.54M,
MarginRight = 2.54M,
MarginBottom = 2.54M,
MarginLeft = 2.54M
};
public int TimeoutSeconds { get; init; } = 120;
public bool CollectLogs { get; init; } = false;
public bool AttachFilesLogs { get; init; } = false;
public S3Endpoints S3Endpoints { get; init; } = default;
}
}
35 changes: 35 additions & 0 deletions UltimatePDFLambdaFunctions/Inputs/ScreenshotPNGInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using OutSystems.UltimatePDF_ExternalLogic.Structures;
using Environment = OutSystems.UltimatePDF_ExternalLogic.Structures.Environment;

namespace UltimatePDFLambdaFunctions.Inputs
{
public class ScreenshotPNGInput
{
public string Url { get; init; } = String.Empty;
public Viewport Viewport { get; init; } = new Viewport { Width = 1366, Height = 768 };
public Environment Environment { get; init; } = new Environment
{
BaseURL = "",
Locale = "en",
Timezone = "Europe/Lisbon"
};
public IEnumerable<Cookie> Cookies { get; init; } = Array.Empty<Cookie>();
public Paper Paper { get; init; } = new Paper
{
UseCustomPaper = false,
Width = 21,
Height = 29.7M,
UseCustomMargins = false,
MarginTop = 2.54M,
MarginRight = 2.54M,
MarginBottom = 2.54M,
MarginLeft = 2.54M
};
public ScreenshotOptions ScreenshotOptions { get; init; } = new ScreenshotOptions { FullPage = true, TransparentBackground = true };
public int TimeoutSeconds { get; init; } = 120;
public bool CollectLogs { get; init; } = false;
public bool AttachFilesLogs { get; init; } = false;
public byte[]? LogsZipFile { get; init; } = null;

}
}
6 changes: 6 additions & 0 deletions UltimatePDFLambdaFunctions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This project is for illustration purposes only and provides sample Lambda Function wrappers around UltimatePDF

Step to execute the code:
1. Run .\generate_upload_package.ps1
1. Create a AWS Lambda .net 6.0 runtime for each public method
1. Upload the zip UltimatePDFLambda.zip as the lambda function code
Loading