Skip to content

Commit

Permalink
Added ValidateAspNetCoreUrls (#2466)
Browse files Browse the repository at this point in the history
This pull request introduces a validation check for the
`ASPNETCORE_URLS` environment variable to ensure it is in the correct
format before starting the engine in the `Program` class. This change
helps in preventing runtime errors due to misconfigured URLs.

Key changes:

* Added a new method `ValidateAspNetCoreUrls` to check the format of the
`ASPNETCORE_URLS` environment variable. This method splits the
environment variable by commas or semicolons and ensures each part is a
valid absolute URI.
* Integrated the `ValidateAspNetCoreUrls` method into the `Main` method
to validate the URLs before proceeding with the engine start. If the
validation fails, an error message is printed, and the application exits
with an error code.

## Why make this change?

The primary motivation is to return a more clear error message to the
user.

Closes #2465

## What is this change?

Startup validation.

## How was this tested?

- [ ] Integration Tests
- [ ] Unit Tests

## Sample Request(s)

Valid value in END file:
`ASPNETCORE_URLS="http://localhost:5000;https://localhost:5001"`

Invalid value in END file:
`ASPNETCORE_URLS=http://localhost:5000;https://localhost:5001`

---------

Co-authored-by: Jerry Nixon <jerry.nixon@microsoft.com>
  • Loading branch information
JerryNixon and Jerry Nixon authored Jan 7, 2025
1 parent b04dfc1 commit d86e630
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Service.Tests/Unittests/EnvironmentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Azure.DataApiBuilder.Service.Tests.UnitTests;

/// <summary>
/// Contains test involving environment variables.
/// </summary>
[TestClass]
public class EnvironmentTests
{
/// <summary>
/// Tests the behavior of the <c>Main</c> method when the <c>ASPNETCORE_URLS</c> environment variable is set to an invalid value.
/// </summary>
/// <remarks>
/// This test sets the <c>ASPNETCORE_URLS</c> environment variable to an invalid value, invokes the <c>Main</c> method,
/// and verifies that the application exits with an error code of -1. Additionally, it checks if the error message
/// contains the name of the invalid environment variable.
/// </remarks>
[TestMethod]
public void Main_WhenAspNetCoreUrlsInvalid_ShouldExitWithError()
{
const string ASPNETCORE_URLS_NAME = "ASPNETCORE_URLS";
const string ASPNETCORE_URLS_INVALID_VALUE = nameof(Main_WhenAspNetCoreUrlsInvalid_ShouldExitWithError);
string originalEnvValue = Environment.GetEnvironmentVariable(ASPNETCORE_URLS_NAME);

// Arrange
Environment.SetEnvironmentVariable(ASPNETCORE_URLS_NAME, ASPNETCORE_URLS_INVALID_VALUE);
using StringWriter consoleOutput = new();
Console.SetError(consoleOutput);

// Act
Program.Main(Array.Empty<string>());

// Assert
Assert.AreEqual(-1, Environment.ExitCode);
StringAssert.Contains(consoleOutput.ToString(), ASPNETCORE_URLS_NAME, StringComparison.Ordinal);

// Cleanup
Environment.SetEnvironmentVariable(ASPNETCORE_URLS_NAME, originalEnvValue);
}
}
20 changes: 20 additions & 0 deletions src/Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Linq;
using System.Threading.Tasks;
using Azure.DataApiBuilder.Config;
using Azure.DataApiBuilder.Service.Exceptions;
Expand All @@ -26,6 +27,13 @@ public class Program

public static void Main(string[] args)
{
if (!ValidateAspNetCoreUrls())
{
Console.Error.WriteLine("Invalid ASPNETCORE_URLS format. e.g.: ASPNETCORE_URLS=\"http://localhost:5000;https://localhost:5001\"");
Environment.ExitCode = -1;
return;
}

if (!StartEngine(args))
{
Environment.ExitCode = -1;
Expand Down Expand Up @@ -224,5 +232,17 @@ private static void AddConfigurationProviders(
.AddEnvironmentVariables(prefix: FileSystemRuntimeConfigLoader.ENVIRONMENT_PREFIX)
.AddCommandLine(args);
}

private static bool ValidateAspNetCoreUrls()
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_URLS") is not string value)
{
return true; // If the environment variable is missing, then it cannot be invalid.
}

return value
.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
.All(x => Uri.TryCreate(x.Trim(), UriKind.Absolute, out _));
}
}
}

0 comments on commit d86e630

Please # to comment.