Skip to content

Commit fea239d

Browse files
authored
Add options to configure HTTPS ports (#2282)
1 parent 1b46c29 commit fea239d

File tree

5 files changed

+94
-12
lines changed

5 files changed

+94
-12
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.TestTool",
5+
"Type": "Minor",
6+
"ChangelogMessages": [
7+
"Add support for configuring HTTPS endpoints for the Lambda UI emulator and API Gateway emulator"
8+
]
9+
}
10+
]
11+
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Commands/RunCommand.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public sealed class RunCommand(
2121
IToolInteractiveService toolInteractiveService, IEnvironmentManager environmentManager) : CancellableAsyncCommand<RunCommandSettings>
2222
{
2323
public const string LAMBDA_RUNTIME_API_PORT = "LAMBDA_RUNTIME_API_PORT";
24+
public const string LAMBDA_WEB_UI_HTTPS_PORT = "LAMBDA_WEB_UI_HTTPS_PORT";
2425
public const string API_GATEWAY_EMULATOR_PORT = "API_GATEWAY_EMULATOR_PORT";
26+
public const string API_GATEWAY_EMULATOR_HTTPS_PORT = "API_GATEWAY_EMULATOR_HTTPS_PORT";
2527

2628
/// <summary>
2729
/// Task for the Lambda Runtime API.
@@ -37,10 +39,10 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCommandS
3739
{
3840
EvaluateEnvironmentVariables(settings);
3941

40-
if (!settings.LambdaEmulatorPort.HasValue && !settings.ApiGatewayEmulatorPort.HasValue && string.IsNullOrEmpty(settings.SQSEventSourceConfig))
42+
if (!settings.LambdaEmulatorPort.HasValue && !settings.ApiGatewayEmulatorPort.HasValue && !settings.ApiGatewayEmulatorHttpsPort.HasValue && string.IsNullOrEmpty(settings.SQSEventSourceConfig))
4143
{
4244
throw new ArgumentException("At least one of the following parameters must be set: " +
43-
"--lambda-emulator-port, --api-gateway-emulator-port or --sqs-eventsource-config");
45+
"--lambda-emulator-port, --api-gateway-emulator-port, --api-gateway-emulator-https-port or --sqs-eventsource-config");
4446
}
4547

4648
var tasks = new List<Task>();
@@ -69,11 +71,11 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCommandS
6971
}
7072
}
7173

72-
if (settings.ApiGatewayEmulatorPort.HasValue)
74+
if (settings.ApiGatewayEmulatorPort.HasValue || settings.ApiGatewayEmulatorHttpsPort.HasValue)
7375
{
7476
if (settings.ApiGatewayEmulatorMode is null)
7577
{
76-
throw new ArgumentException("When --api-gateway-emulator-port is set the --api-gateway-emulator-mode must be set to configure the mode for the API Gateway emulator.");
78+
throw new ArgumentException("When --api-gateway-emulator-port or --api-gateway-emulator-https-port is set the --api-gateway-emulator-mode must be set to configure the mode for the API Gateway emulator.");
7779
}
7880

7981
var apiGatewayEmulatorProcess =
@@ -137,6 +139,18 @@ private void EvaluateEnvironmentVariables(RunCommandSettings settings)
137139
throw new ArgumentException($"Value for {LAMBDA_RUNTIME_API_PORT} environment variable was not a valid port number");
138140
}
139141
}
142+
if (environmentVariables.Contains(LAMBDA_WEB_UI_HTTPS_PORT))
143+
{
144+
var envValue = environmentVariables[LAMBDA_WEB_UI_HTTPS_PORT]?.ToString();
145+
if (int.TryParse(envValue, out var port))
146+
{
147+
settings.LambdaEmulatorHttpsPort = port;
148+
}
149+
else
150+
{
151+
throw new ArgumentException($"Value for {LAMBDA_WEB_UI_HTTPS_PORT} environment variable was not a valid port number");
152+
}
153+
}
140154
if (environmentVariables.Contains(API_GATEWAY_EMULATOR_PORT))
141155
{
142156
var envValue = environmentVariables[API_GATEWAY_EMULATOR_PORT]?.ToString();
@@ -149,6 +163,18 @@ private void EvaluateEnvironmentVariables(RunCommandSettings settings)
149163
throw new ArgumentException($"Value for {API_GATEWAY_EMULATOR_PORT} environment variable was not a valid port number");
150164
}
151165
}
166+
if (environmentVariables.Contains(API_GATEWAY_EMULATOR_HTTPS_PORT))
167+
{
168+
var envValue = environmentVariables[API_GATEWAY_EMULATOR_HTTPS_PORT]?.ToString();
169+
if (int.TryParse(envValue, out var port))
170+
{
171+
settings.ApiGatewayEmulatorHttpsPort = port;
172+
}
173+
else
174+
{
175+
throw new ArgumentException($"Value for {API_GATEWAY_EMULATOR_HTTPS_PORT} environment variable was not a valid port number");
176+
}
177+
}
152178

153179
if (settings.SQSEventSourceConfig != null && settings.SQSEventSourceConfig.StartsWith(Constants.ArgumentEnvironmentVariablePrefix, StringComparison.CurrentCultureIgnoreCase))
154180
{

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Commands/Settings/RunCommandSettings.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ public sealed class RunCommandSettings : CommandSettings
3030
[Description("The port number used for the test tool's web interface.")]
3131
public int? LambdaEmulatorPort { get; set; }
3232

33+
/// <summary>
34+
/// The https port number used for the test tool's web interface. This is only used for the web UI. Lambda functions making REST calls to the Lambda Runtime API
35+
/// always use http configured by the port specified in <see cref="LambdaEmulatorPort"/>. To use HTTPS the environment must be configured with certs
36+
/// for the host specified in <see cref="LambdaEmulatorHost"/>.
37+
/// </summary>
38+
[CommandOption("--lambda-emulator-https-port <PORT>")]
39+
[Description("The https port number used for the test tool's web interface.")]
40+
public int? LambdaEmulatorHttpsPort { get; set; }
41+
3342
/// <summary>
3443
/// Disable auto launching the test tool's web interface in a browser.
3544
/// </summary>
@@ -49,12 +58,22 @@ public sealed class RunCommandSettings : CommandSettings
4958
public ApiGatewayEmulatorMode? ApiGatewayEmulatorMode { get; set; }
5059

5160
/// <summary>
52-
/// The port number used for the test tool's API Gateway emulator. If a port is specified the API Gateway emulator will be started. The --api-gateway-mode muse also be set when setting the API Gateway emulator port.
61+
/// The port number used for the test tool's API Gateway emulator. If a port is specified the API Gateway emulator will be started. The --api-gateway-emulator-mode
62+
/// must also be set when setting the API Gateway emulator port.
5363
/// </summary>
5464
[CommandOption("--api-gateway-emulator-port <PORT>")]
5565
[Description("The port number used for the test tool's API Gateway emulator.")]
5666
public int? ApiGatewayEmulatorPort { get; set; }
5767

68+
/// <summary>
69+
/// The https port number used for the test tool's API Gateway emulator. If a port is specified the API Gateway emulator will be started. The --api-gateway--emulator-mode must
70+
/// also be set when setting the API Gateway emulator port. To use HTTPS the environment must be configured with certs
71+
/// for the host specified in <see cref="LambdaEmulatorHost"/>.
72+
/// </summary>
73+
[CommandOption("--api-gateway-emulator-https-port <PORT>")]
74+
[Description("The https port number used for the test tool's API Gateway emulator.")]
75+
public int? ApiGatewayEmulatorHttpsPort { get; set; }
76+
5877
/// <summary>
5978
/// The configuration for the SQS event source. The format of the config is a comma delimited key pairs. For example \"QueueUrl=queue-url,FunctionName=function-name,VisibilityTimeout=100\".
6079
/// Possible keys are: BatchSize, DisableMessageDelete, FunctionName, LambdaRuntimeApi, Profile, QueueUrl, Region, VisibilityTimeout

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Processes/ApiGatewayEmulatorProcess.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,22 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
5454
builder.Services.AddApiGatewayEmulatorServices();
5555
builder.Services.AddSingleton<ILambdaClient, LambdaClient>();
5656

57-
var serviceUrl = $"http://{settings.LambdaEmulatorHost}:{settings.ApiGatewayEmulatorPort}";
58-
builder.WebHost.UseUrls(serviceUrl);
57+
string? serviceHttpUrl = null;
58+
string? serviceHttpsUrl = null;
59+
var serviceUrls = new List<string>();
60+
61+
if (settings.ApiGatewayEmulatorPort.HasValue)
62+
{
63+
serviceHttpUrl = $"http://{settings.LambdaEmulatorHost}:{settings.ApiGatewayEmulatorPort}";
64+
serviceUrls.Add(serviceHttpUrl);
65+
}
66+
if (settings.ApiGatewayEmulatorHttpsPort.HasValue)
67+
{
68+
serviceHttpsUrl = $"https://{settings.LambdaEmulatorHost}:{settings.ApiGatewayEmulatorHttpsPort}";
69+
serviceUrls.Add(serviceHttpsUrl);
70+
}
71+
72+
builder.WebHost.UseUrls(serviceUrls.ToArray());
5973
builder.WebHost.SuppressStatusMessages(true);
6074

6175
builder.Services.AddHealthChecks();
@@ -66,7 +80,7 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
6680

6781
app.Lifetime.ApplicationStarted.Register(() =>
6882
{
69-
app.Logger.LogInformation("The API Gateway Emulator is available at: {ServiceUrl}", serviceUrl);
83+
app.Logger.LogInformation("The API Gateway Emulator is available at: {ServiceUrl}", serviceHttpsUrl ?? serviceHttpUrl);
7084
});
7185

7286
app.Map("/{**catchAll}", async (HttpContext context, IApiGatewayRouteConfigService routeConfigService, ILambdaClient lambdaClient) =>
@@ -160,7 +174,7 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
160174
{
161175
Services = app.Services,
162176
RunningTask = runTask,
163-
ServiceUrl = serviceUrl
177+
ServiceUrl = serviceHttpsUrl ?? serviceHttpUrl ?? throw new InvalidOperationException("No valid service URL was configured for the API Gateway emulator.")
164178
};
165179
}
166180
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Processes/TestToolProcess.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,20 @@ public static TestToolProcess Startup(RunCommandSettings settings, CancellationT
6666
}
6767
builder.Services.AddSingleton<IDirectoryManager, DirectoryManager>();
6868

69-
var serviceUrl = $"http://{settings.LambdaEmulatorHost}:{settings.LambdaEmulatorPort}";
70-
builder.WebHost.UseUrls(serviceUrl);
69+
var serviceHttp = $"http://{settings.LambdaEmulatorHost}:{settings.LambdaEmulatorPort}";
70+
71+
string? serviceHttps = null;
72+
73+
if (settings.LambdaEmulatorHttpsPort.HasValue)
74+
{
75+
serviceHttps = $"https://{settings.LambdaEmulatorHost}:{settings.LambdaEmulatorHttpsPort}";
76+
builder.WebHost.UseUrls(serviceHttp, serviceHttps);
77+
}
78+
else
79+
{
80+
builder.WebHost.UseUrls(serviceHttp);
81+
}
82+
7183
builder.WebHost.SuppressStatusMessages(true);
7284

7385
builder.Services.AddSingleton<IGlobalSettingsRepository, FileSettingsRepository>();
@@ -102,7 +114,7 @@ public static TestToolProcess Startup(RunCommandSettings settings, CancellationT
102114
{
103115
Services = app.Services,
104116
RunningTask = runTask,
105-
ServiceUrl = serviceUrl
117+
ServiceUrl = serviceHttps ?? serviceHttp
106118
};
107119

108120
return startup;

0 commit comments

Comments
 (0)