Skip to content

Commit

Permalink
Automatically create https proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Shimmings committed Dec 4, 2024
1 parent 6b84504 commit 7ece879
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 38 deletions.
32 changes: 13 additions & 19 deletions Btms.Backend.Test/Config/ApiOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,37 @@ namespace Btms.Backend.Test.Config;

public class ApiOptionsTest
{
private IValidateOptions<ApiOptions> validator = new ApiOptions.Validator();
private readonly IValidateOptions<ApiOptions> validator = new ApiOptions.Validator();

[Fact]
public void ShouldSucceedIfNoCdsProxy()
{
var c = new ApiOptions() { };
var c = new ApiOptions();

validator.Validate("", c).Should().Be(ValidateOptionsResult.Success);
}

[Fact]
public void ShouldSucceedIfCdsProxyAndHttpsProxy()
{
var c = new ApiOptions() { CdpHttpsProxy = "https://aaa", HttpsProxy = "aaa", };

validator.Validate("", c).Should().Be(ValidateOptionsResult.Success);
}

[Fact]
public void ShouldFailIfCdsProxyAndNotHttpsProxy()
public void ShouldFailIfCdsProxyDoesntHaveProtocol()
{
var c = new ApiOptions() { CdpHttpsProxy = "https://aaa" };

var c = new ApiOptions() { CdpHttpsProxy = "aaa" };
validator.Validate("", c).Failed.Should().BeTrue();
}

[Fact]
public void ShouldFailIfCdsProxyDoesntHaveProtocol()
public void ShouldSetHttpsProxyIfCdsProxyPresent()
{
var c = new ApiOptions() { CdpHttpsProxy = "aaa", HttpsProxy = "aaa", };
validator.Validate("", c).Failed.Should().BeTrue();
var c = new ApiOptions { CdpHttpsProxy = "https://aaa" };

c.HttpsProxy.Should().Be("aaa");
}

[Fact]
public void ShouldFailIfHttpsProxyHasProtocol()
public void ShouldNotSetHttpsProxyIfCdsProxyPresent()
{
var c = new ApiOptions() { CdpHttpsProxy = "https://aaa", HttpsProxy = "https://aaa", };
validator.Validate("", c).Failed.Should().BeTrue();
var c = new ApiOptions();

c.HttpsProxy.Should().BeNull();
}
}
22 changes: 3 additions & 19 deletions Btms.Backend/Config/ApiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,29 @@ public class ApiOptions
// This is used by the azure library when connecting to auth related services
// when connecting to blob storage
[ConfigurationKeyName("HTTPS_PROXY")]
public string? HttpsProxy { get; set; }
public string? HttpsProxy => CdpHttpsProxy?.Contains("://") == true ? CdpHttpsProxy[(CdpHttpsProxy.IndexOf("://", StringComparison.Ordinal) + 3)..] : null;

public Dictionary<string, string?> Credentials { get; set; } = [];

public class Validator() : IValidateOptions<ApiOptions>
{
/// <summary>
/// Validates that if we have CDP_HTTPS_PROXY we also have HTTPS_PROXY
/// Validates that CDP_HTTPS_PROXY is shaped correctly if present
/// I'm sure this can be written more concisely, there are tests
/// </summary>
/// <returns></returns>
public ValidateOptionsResult Validate(string? name, ApiOptions options)
{
var valid = !options.CdpHttpsProxy.HasValue() || options.HttpsProxy.HasValue();

if (!valid)
{
return ValidateOptionsResult.Fail("If CDP_HTTPS_PROXY is set HTTPS_PROXY must also be set.");
}
if (options.CdpHttpsProxy.HasValue())
{
valid = options.CdpHttpsProxy!.StartsWith("http");
var valid = options.CdpHttpsProxy!.StartsWith("http");

if (!valid)
{
return ValidateOptionsResult.Fail("If CDP_HTTPS_PROXY is set, it must start with protocol");
}
}

if (options.HttpsProxy.HasValue())
{
valid = !options.HttpsProxy!.StartsWith("http");

if (!valid)
{
return ValidateOptionsResult.Fail("If HTTPS_PROXY is set HTTPS_PROXY it must not start with protocol");
}
}

return ValidateOptionsResult.Success;
}
}
Expand Down

0 comments on commit 7ece879

Please sign in to comment.