Skip to content

Commit

Permalink
add cluster destination address validation (#2184)
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev authored Jul 11, 2023
1 parent 2c30e4d commit 156d8cc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/ReverseProxy/Configuration/ConfigValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public ValueTask<IList<Exception>> ValidateClusterAsync(ClusterConfig cluster)
}

errors.AddRange(_transformBuilder.ValidateCluster(cluster));
ValidateDestinations(errors, cluster);
ValidateLoadBalancing(errors, cluster);
ValidateSessionAffinity(errors, cluster);
ValidateProxyHttpClient(errors, cluster);
Expand Down Expand Up @@ -366,6 +367,21 @@ private async ValueTask ValidateCorsPolicyAsync(IList<Exception> errors, string?
}
}

private void ValidateDestinations(IList<Exception> errors, ClusterConfig cluster)
{
if (cluster.Destinations is null)
{
return;
}
foreach (var (name, destination) in cluster.Destinations)
{
if (string.IsNullOrEmpty(destination.Address))
{
errors.Add(new ArgumentException($"No address found for destination '{name}' on cluster '{cluster.ClusterId}'."));
}
}
}

private void ValidateLoadBalancing(IList<Exception> errors, ClusterConfig cluster)
{
var loadBalancingPolicy = cluster.LoadBalancingPolicy;
Expand Down
40 changes: 40 additions & 0 deletions test/ReverseProxy.Tests/Configuration/ConfigValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -725,6 +726,45 @@ public async Task EmptyCluster_Works()
Assert.Empty(errors);
}

[Fact]
public async Task DestinationAddress_Works()
{
var services = CreateServices();
var validator = services.GetRequiredService<IConfigValidator>();

var cluster = new ClusterConfig {
ClusterId = "cluster1",
Destinations = new Dictionary<string, DestinationConfig>(StringComparer.OrdinalIgnoreCase) {
{ "destination1", new DestinationConfig { Address = "https://localhost:1234" } }
}
};

var errors = await validator.ValidateClusterAsync(cluster);

Assert.Empty(errors);
}

[Theory]
[InlineData(null)]
[InlineData("")]
public async Task DestinationAddressInvalid_Fails(string address)
{
var services = CreateServices();
var validator = services.GetRequiredService<IConfigValidator>();

var cluster = new ClusterConfig {
ClusterId = "cluster1",
Destinations = new Dictionary<string, DestinationConfig>(StringComparer.OrdinalIgnoreCase) {
{ "destination1", new DestinationConfig { Address = address } }
}
};

var errors = await validator.ValidateClusterAsync(cluster);

var ex = Assert.Single(errors);
Assert.Equal("No address found for destination 'destination1' on cluster 'cluster1'.", ex.Message);
}

[Fact]
public async Task LoadBalancingPolicy_KnownPolicy_Works()
{
Expand Down

0 comments on commit 156d8cc

Please sign in to comment.