Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Configuration validation #12

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Kongverge/DTOs/GlobalConfig.cs
Expand Up @@ -11,11 +11,21 @@ public class GlobalConfig : IKongvergeConfigObject, IKongPluginHost
[JsonProperty("plugins")]
public IReadOnlyList<KongPlugin> Plugins { get; set; } = Array.Empty<KongPlugin>();

public async Task Validate(ICollection<string> errorMessages)
public async Task Validate(IReadOnlyCollection<string> availablePlugins, ICollection<string> errorMessages)
{
await ValidatePlugins(availablePlugins, errorMessages);
}

private async Task ValidatePlugins(IReadOnlyCollection<string> availablePlugins, ICollection<string> errorMessages)
{
if (Plugins == null)
{
errorMessages.Add("Plugins cannot be null.");
return;
}
foreach (var plugin in Plugins)
{
await plugin.Validate(errorMessages);
await plugin.Validate(availablePlugins, errorMessages);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Kongverge/DTOs/IValidatableObject.cs
Expand Up @@ -5,6 +5,6 @@ namespace Kongverge.DTOs
{
public interface IValidatableObject
{
Task Validate(ICollection<string> errorMessages);
Task Validate(IReadOnlyCollection<string> availablePlugins, ICollection<string> errorMessages);
}
}
}
10 changes: 7 additions & 3 deletions src/Kongverge/DTOs/KongPlugin.cs
@@ -1,12 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Kongverge.Helpers;
using Newtonsoft.Json;

namespace Kongverge.DTOs
{
public sealed class KongPlugin : KongObject, IKongEquatable<KongPlugin>, IValidatableObject
public class KongPlugin : KongObject, IKongEquatable<KongPlugin>, IValidatableObject
{
public const string ObjectName = "plugin";

Expand Down Expand Up @@ -48,9 +49,12 @@ internal override void StripPersistedValues()
RouteId = null;
}

public Task Validate(ICollection<string> errorMessages)
public virtual Task Validate(IReadOnlyCollection<string> availablePlugins, ICollection<string> errorMessages)
{
// TODO: Add validation
if (!availablePlugins.Contains(Name))
{
errorMessages.Add($"Plugin '{Name}' is not available on Kong server.");
}
return Task.CompletedTask;
}

Expand Down
40 changes: 26 additions & 14 deletions src/Kongverge/DTOs/KongRoute.cs
Expand Up @@ -8,7 +8,7 @@

namespace Kongverge.DTOs
{
public sealed class KongRoute : KongObject, IKongPluginHost, IKongEquatable<KongRoute>, IValidatableObject
public class KongRoute : KongObject, IKongPluginHost, IKongEquatable<KongRoute>, IValidatableObject
{
public const string ObjectName = "route";

Expand All @@ -31,7 +31,7 @@ public sealed class KongRoute : KongObject, IKongPluginHost, IKongEquatable<Kong
public IEnumerable<string> Paths { get; set; } = Array.Empty<string>();

[JsonProperty("regex_priority")]
public int RegexPriority { get; set; }
public ushort RegexPriority { get; set; }

[JsonProperty("strip_path")]
public bool StripPath { get; set; } = true;
Expand Down Expand Up @@ -75,35 +75,47 @@ public void AssignParentId(KongPlugin plugin)
plugin.RouteId = Id;
}

public Task Validate(ICollection<string> errorMessages)
public virtual async Task Validate(IReadOnlyCollection<string> availablePlugins, ICollection<string> errorMessages)
{
if (IsNullOrEmpty(Protocols) || Protocols.Any(string.IsNullOrWhiteSpace))
if (IsNullOrEmpty(Protocols) || Protocols.Any(x => !new[] { "http", "https" }.Contains(x)))
{
errorMessages.Add("Route Protocols cannot be null or contain null or empty values");
errorMessages.Add("Route Protocols is invalid (must contain one or both of 'http' or 'https').");
}

if (IsNullOrEmpty(Hosts) && IsNullOrEmpty(Methods) && IsNullOrEmpty(Paths))
{
errorMessages.Add("At least one of 'hosts', 'methods', or 'paths' must be set");
return Task.CompletedTask;
errorMessages.Add("At least one of Route 'Hosts', 'Methods', or 'Paths' must be set.");
}

if (Hosts?.Any(string.IsNullOrWhiteSpace) == true)
if (Hosts == null || Hosts.Any(x => string.IsNullOrWhiteSpace(x) || Uri.CheckHostName(x) == UriHostNameType.Unknown))
{
errorMessages.Add("Route Hosts cannot contain null or empty values");
errorMessages.Add("Route Hosts is invalid (cannot be null, or contain null, empty or invalid values).");
}

if (Methods?.Any(string.IsNullOrWhiteSpace) == true)
if (Methods == null || Methods.Any(string.IsNullOrWhiteSpace))
{
errorMessages.Add("Route Methods cannot contain null or empty values");
errorMessages.Add("Route Methods is invalid (cannot be null, or contain null or empty values).");
}

if (Paths?.Any(string.IsNullOrWhiteSpace) == true)
if (Paths == null || Paths.Any(x => string.IsNullOrWhiteSpace(x) || !Uri.IsWellFormedUriString(x, UriKind.Relative)))
{
errorMessages.Add("Route Paths cannot contain null or empty values");
errorMessages.Add("Route Paths is invalid (cannot be null, or contain null, empty or invalid values).");
}

return Task.CompletedTask;
await ValidatePlugins(availablePlugins, errorMessages);
}

private async Task ValidatePlugins(IReadOnlyCollection<string> availablePlugins, ICollection<string> errorMessages)
{
if (Plugins == null)
{
errorMessages.Add("Plugins cannot be null.");
return;
}
foreach (var plugin in Plugins)
{
await plugin.Validate(availablePlugins, errorMessages);
}
}

private static bool IsNullOrEmpty(IEnumerable<string> values)
Expand Down
74 changes: 61 additions & 13 deletions src/Kongverge/DTOs/KongService.cs
Expand Up @@ -8,7 +8,7 @@

namespace Kongverge.DTOs
{
public sealed class KongService : KongObject, IKongPluginHost, IKongEquatable<KongService>, IKongvergeConfigObject
public class KongService : KongObject, IKongPluginHost, IKongEquatable<KongService>, IKongvergeConfigObject
{
public const string ObjectName = "service";

Expand All @@ -21,22 +21,22 @@ public sealed class KongService : KongObject, IKongPluginHost, IKongEquatable<Ko
public string Host { get; set; }

[JsonProperty("port")]
public int Port { get; set; } = 80;
public ushort Port { get; set; } = 80;

[JsonProperty("protocol")]
public string Protocol { get; set; } = "http";

[JsonProperty("retries")]
public int Retries { get; set; } = 5;
public byte Retries { get; set; } = 5;

[JsonProperty("connect_timeout")]
public int ConnectTimeout { get; set; } = DefaultTimeout;
public uint ConnectTimeout { get; set; } = DefaultTimeout;

[JsonProperty("write_timeout")]
public int WriteTimeout { get; set; } = DefaultTimeout;
public uint WriteTimeout { get; set; } = DefaultTimeout;

[JsonProperty("read_timeout")]
public int ReadTimeout { get; set; } = DefaultTimeout;
public uint ReadTimeout { get; set; } = DefaultTimeout;

[JsonProperty("path")]
public string Path { get; set; }
Expand Down Expand Up @@ -86,24 +86,72 @@ public void AssignParentId(KongPlugin plugin)
plugin.ServiceId = Id;
}

public async Task Validate(ICollection<string> errorMessages)
public async Task Validate(IReadOnlyCollection<string> availablePlugins, ICollection<string> errorMessages)
{
await ValidateRoutesAreValid(errorMessages);
if (!new[] { "http", "https" }.Contains(Protocol))
{
errorMessages.Add("Protocol is invalid (must be either 'http' or 'https').");
}

if (Uri.CheckHostName(Host) == UriHostNameType.Unknown)
{
errorMessages.Add("Host is invalid.");
}

if (!string.IsNullOrEmpty(Path) && !Uri.IsWellFormedUriString(Path, UriKind.Relative))
{
errorMessages.Add("Path is invalid.");
}

if (Retries > 25)
{
errorMessages.Add("Retries is invalid (must be between 0 and 25).");
}

if (ConnectTimeout > 300000)
{
errorMessages.Add("ConnectTimeout is invalid (must be between 0 and 300000).");
}

if (WriteTimeout > 300000)
{
errorMessages.Add("WriteTimeout is invalid (must be between 0 and 300000).");
}

if (ReadTimeout > 300000)
{
errorMessages.Add("ReadTimeout is invalid (must be between 0 and 300000).");
}

await ValidatePlugins(availablePlugins, errorMessages);

await ValidateRoutes(availablePlugins, errorMessages);
}

private async Task ValidatePlugins(IReadOnlyCollection<string> availablePlugins, ICollection<string> errorMessages)
{
if (Plugins == null)
{
errorMessages.Add("Plugins cannot be null.");
return;
}
foreach (var plugin in Plugins)
{
await plugin.Validate(availablePlugins, errorMessages);
}
}

private async Task ValidateRoutesAreValid(ICollection<string> errorMessages)
private async Task ValidateRoutes(IReadOnlyCollection<string> availablePlugins, ICollection<string> errorMessages)
{
if (Routes == null || !Routes.Any())
{
errorMessages.Add("Routes cannot be null or empty");
errorMessages.Add("Routes cannot be null or empty.");
return;
}
foreach (var route in Routes)
{
await route.Validate(errorMessages);
await route.Validate(availablePlugins, errorMessages);
}

// TODO: Check if routes Clash
}

public string ToConfigJson()
Expand Down
12 changes: 6 additions & 6 deletions src/Kongverge/Services/ConfigFileReader.cs
Expand Up @@ -12,7 +12,7 @@ namespace Kongverge.Services
{
public class ConfigFileReader
{
public virtual async Task<KongvergeConfiguration> ReadConfiguration(string folderPath)
public virtual async Task<KongvergeConfiguration> ReadConfiguration(string folderPath, IReadOnlyCollection<string> availablePlugins)
{
Log.Information($"Reading files from {folderPath}");

Expand All @@ -27,16 +27,16 @@ public virtual async Task<KongvergeConfiguration> ReadConfiguration(string folde
foreach (var globalConfigFilePath in globalConfigFilePaths)
{
fileErrorMessages.AddErrors(globalConfigFilePath, $"Cannot have more than one {Settings.GlobalConfigFileName} file.");
await ParseFile<GlobalConfig>(globalConfigFilePath, fileErrorMessages);
await ParseFile<GlobalConfig>(globalConfigFilePath, availablePlugins, fileErrorMessages);
}
}
else if (globalConfigFilePaths.Any())
{
globalConfig = await ParseFile<GlobalConfig>(globalConfigFilePaths.Single(), fileErrorMessages);
globalConfig = await ParseFile<GlobalConfig>(globalConfigFilePaths.Single(), availablePlugins, fileErrorMessages);
}
foreach (var serviceConfigFilePath in filePaths.Except(globalConfigFilePaths))
{
services.Add(await ParseFile<KongService>(serviceConfigFilePath, fileErrorMessages));
services.Add(await ParseFile<KongService>(serviceConfigFilePath, availablePlugins, fileErrorMessages));
}

if (fileErrorMessages.Any())
Expand All @@ -51,7 +51,7 @@ public virtual async Task<KongvergeConfiguration> ReadConfiguration(string folde
};
}

private static async Task<T> ParseFile<T>(string path, FileErrorMessages fileErrorMessages) where T : class, IKongvergeConfigObject
private static async Task<T> ParseFile<T>(string path, IReadOnlyCollection<string> availablePlugins, FileErrorMessages fileErrorMessages) where T : class, IKongvergeConfigObject
{
Log.Verbose($"Reading {path}");
string text;
Expand All @@ -64,7 +64,7 @@ public virtual async Task<KongvergeConfiguration> ReadConfiguration(string folde
try
{
var data = JsonConvert.DeserializeObject<T>(text);
await data.Validate(errorMessages);
await data.Validate(availablePlugins, errorMessages);
if (errorMessages.Any())
{
fileErrorMessages.AddErrors(path, errorMessages.ToArray());
Expand Down
3 changes: 2 additions & 1 deletion src/Kongverge/Workflow/KongvergeWorkflow.cs
Expand Up @@ -35,10 +35,11 @@ public class KongvergeWorkflow : Workflow

public override async Task<int> DoExecute()
{
var availablePlugins = KongConfiguration.Plugins.Available.Where(x => x.Value).Select(x => x.Key).ToArray();
KongvergeConfiguration targetConfiguration;
try
{
targetConfiguration = await _configReader.ReadConfiguration(Configuration.InputFolder);
targetConfiguration = await _configReader.ReadConfiguration(Configuration.InputFolder, availablePlugins);
}
catch (DirectoryNotFoundException ex)
{
Expand Down
Expand Up @@ -7,6 +7,10 @@
"generator": "uuid#counter",
"header_name": "x-correlation-id"
}
},
{
"name": "unavailable",
"config": { }
}
]
}
@@ -1,12 +1,13 @@
{
"host": "www.service2.com",
"host": ":",
"name": "service2",
"port": 80,
"protocol": "http",
"retries": 5,
"connect_timeout": 60000,
"read_timeout": 60000,
"write_timeout": 60000,
"path": "path:invalid",
"protocol": "junk",
"retries": 26,
"connect_timeout": 300001,
"read_timeout": 300001,
"write_timeout": 300001,
"plugins": [
{
"name": "request-termination",
Expand Down
Expand Up @@ -28,6 +28,12 @@
"config": { }
}
]
},
{
"hosts": null,
"paths": null,
"protocols": null,
"methods": null
}
]
}
13 changes: 11 additions & 2 deletions test/Kongverge.IntegrationTests/ProgramSteps.cs
Expand Up @@ -5,8 +5,10 @@
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Kongverge.DTOs;
using Kongverge.Helpers;
using Kongverge.Services;
using Microsoft.Extensions.Options;

namespace Kongverge.IntegrationTests
{
Expand Down Expand Up @@ -103,9 +105,16 @@ protected async Task OutputFolderContentsMatchInputFolderContents()
{
Debug.WriteLine(Directory.GetCurrentDirectory());

var settings = new Settings
{
Admin = new Admin { Host = Host, Port = Port }
};
var kongReader = new KongAdminReader(new KongAdminHttpClient(Options.Create(settings)));
var kongConfiguration = await kongReader.GetConfiguration();
var availablePlugins = kongConfiguration.Plugins.Available.Where(x => x.Value).Select(x => x.Key).ToArray();
var configReader = new ConfigFileReader();
var inputConfiguration = await configReader.ReadConfiguration(InputFolder);
var outputConfiguration = await configReader.ReadConfiguration(OutputFolder);
var inputConfiguration = await configReader.ReadConfiguration(InputFolder, availablePlugins);
var outputConfiguration = await configReader.ReadConfiguration(OutputFolder, availablePlugins);

inputConfiguration.GlobalConfig.Plugins.Should().NotBeEmpty();
inputConfiguration.Services.Count.Should().Be(3);
Expand Down