| | | 1 | | namespace Envilder; |
| | | 2 | | |
| | | 3 | | using Envilder.Domain; |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Text.Json; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Checks a map file against <c>spec/map-file.v1.json</c> and the provider |
| | | 11 | | /// configuration it resolves to, with the same rules and wording as |
| | | 12 | | /// <c>envilder validate --offline</c>. The SDK supports fewer providers than |
| | | 13 | | /// the schema declares, so <c>gcp</c>, <c>hashicorp</c> and <c>file</c> are |
| | | 14 | | /// reported as unsupported. Builder overrides take precedence over |
| | | 15 | | /// <c>$config</c>, as they do on a load. No mapped value ever reaches an error. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class MapFileValidator |
| | | 18 | | { |
| | | 19 | | private const string DefaultProvider = "aws"; |
| | | 20 | | |
| | 1 | 21 | | private static readonly string[] SupportedProviders = ["aws", "azure"]; |
| | | 22 | | |
| | 1 | 23 | | private static readonly string[] ConfigFields = |
| | 1 | 24 | | [ |
| | 1 | 25 | | "provider", |
| | 1 | 26 | | "profile", |
| | 1 | 27 | | "vaultUrl", |
| | 1 | 28 | | "projectId", |
| | 1 | 29 | | "namespace", |
| | 1 | 30 | | "path", |
| | 1 | 31 | | "name", |
| | 1 | 32 | | "description", |
| | 1 | 33 | | "owner", |
| | 1 | 34 | | "environment", |
| | 1 | 35 | | ]; |
| | | 36 | | |
| | 1 | 37 | | private static readonly string[] UriFields = ["vaultUrl"]; |
| | | 38 | | |
| | 1 | 39 | | private static readonly (string Field, string[] Providers)[] ProviderSpecificFields = |
| | 1 | 40 | | [ |
| | 1 | 41 | | ("profile", ["aws"]), |
| | 1 | 42 | | ("vaultUrl", ["azure", "hashicorp"]), |
| | 1 | 43 | | ("projectId", ["gcp"]), |
| | 1 | 44 | | ("namespace", ["hashicorp"]), |
| | 1 | 45 | | ("path", ["file"]), |
| | 1 | 46 | | ]; |
| | | 47 | | |
| | 1 | 48 | | private static readonly Dictionary<string, string> RequiredProviderFields = new() |
| | 1 | 49 | | { |
| | 1 | 50 | | ["azure"] = "vaultUrl", |
| | 1 | 51 | | }; |
| | | 52 | | |
| | 1 | 53 | | private static readonly Dictionary<string, string> ProviderLabels = new() |
| | 1 | 54 | | { |
| | 1 | 55 | | ["aws"] = "AWS", |
| | 1 | 56 | | ["azure"] = "Azure", |
| | 1 | 57 | | }; |
| | | 58 | | |
| | 1 | 59 | | private static readonly Dictionary<string, string> OverrideMethods = new() |
| | 1 | 60 | | { |
| | 1 | 61 | | ["provider"] = "WithProvider()", |
| | 1 | 62 | | ["profile"] = "WithProfile()", |
| | 1 | 63 | | ["vaultUrl"] = "WithVaultUrl()", |
| | 1 | 64 | | }; |
| | | 65 | | |
| | | 66 | | /// <summary>Parses and validates the raw content of a map file.</summary> |
| | | 67 | | public static IReadOnlyList<MapFileError> ValidateContent(string json, EnvilderOptions overrides) |
| | | 68 | | { |
| | | 69 | | try |
| | | 70 | | { |
| | 1 | 71 | | using var document = JsonDocument.Parse(json); |
| | 1 | 72 | | return Validate(document.RootElement, overrides); |
| | | 73 | | } |
| | 1 | 74 | | catch (JsonException) |
| | | 75 | | { |
| | | 76 | | // The parser's message quotes the surrounding text, which can be a |
| | | 77 | | // secret pasted into the file, so it is not repeated. |
| | 1 | 78 | | return [new("(root)", "The map file is not valid JSON.")]; |
| | | 79 | | } |
| | 1 | 80 | | } |
| | | 81 | | |
| | | 82 | | private static List<MapFileError> Validate(JsonElement root, EnvilderOptions overrides) |
| | | 83 | | { |
| | 1 | 84 | | if (root.ValueKind != JsonValueKind.Object) |
| | | 85 | | { |
| | 1 | 86 | | return [new("(root)", "The map file must be a JSON object.")]; |
| | | 87 | | } |
| | | 88 | | |
| | 1 | 89 | | var errors = new List<MapFileError>(); |
| | 1 | 90 | | var fileConfig = new Dictionary<string, JsonElement>(); |
| | 1 | 91 | | foreach (var property in root.EnumerateObject()) |
| | | 92 | | { |
| | 1 | 93 | | var key = property.Name; |
| | 1 | 94 | | if (key == "$schema") |
| | | 95 | | { |
| | 1 | 96 | | errors.AddRange(ValidateUri("$schema", property.Value)); |
| | | 97 | | } |
| | 1 | 98 | | else if (key == "$config") |
| | | 99 | | { |
| | 1 | 100 | | if (property.Value.ValueKind == JsonValueKind.Object) |
| | | 101 | | { |
| | 1 | 102 | | fileConfig = property.Value.EnumerateObject() |
| | 1 | 103 | | .GroupBy(field => field.Name) |
| | 1 | 104 | | .ToDictionary(group => group.Key, group => group.Last().Value); |
| | 1 | 105 | | errors.AddRange(ValidateConfigFields(property.Value)); |
| | | 106 | | } |
| | | 107 | | else |
| | | 108 | | { |
| | 1 | 109 | | errors.Add(new("$config", "$config must be an object.")); |
| | | 110 | | } |
| | | 111 | | } |
| | 1 | 112 | | else if (key.StartsWith("$", StringComparison.Ordinal)) |
| | | 113 | | { |
| | 1 | 114 | | errors.Add(new( |
| | 1 | 115 | | key, |
| | 1 | 116 | | $"{FormatKey(key)} is reserved: only $schema and $config may start with \"$\".")); |
| | | 117 | | } |
| | | 118 | | else |
| | | 119 | | { |
| | 1 | 120 | | errors.AddRange(ValidateMapping(key, property.Value)); |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | 1 | 124 | | errors.AddRange(ValidateProviderConfiguration(fileConfig, overrides)); |
| | 1 | 125 | | return errors; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | private static List<MapFileError> ValidateMapping(string key, JsonElement value) |
| | | 129 | | { |
| | 1 | 130 | | var errors = new List<MapFileError>(); |
| | 1 | 131 | | if (!EnvironmentVariableName.IsValid(key)) |
| | | 132 | | { |
| | 1 | 133 | | errors.Add(new(key, $"{EnvironmentVariableName.InvalidMessage(key)}.")); |
| | | 134 | | } |
| | | 135 | | |
| | 1 | 136 | | if (value.ValueKind != JsonValueKind.String) |
| | | 137 | | { |
| | | 138 | | // The type only: the value itself may be a secret pasted by mistake. |
| | 1 | 139 | | errors.Add(new( |
| | 1 | 140 | | key, |
| | 1 | 141 | | $"{FormatKey(key)} must map to a string secret path, not {DescribeType(value)}.")); |
| | | 142 | | } |
| | | 143 | | |
| | 1 | 144 | | return errors; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | private static List<MapFileError> ValidateConfigFields(JsonElement config) |
| | | 148 | | { |
| | 1 | 149 | | var errors = new List<MapFileError>(); |
| | 1 | 150 | | foreach (var property in config.EnumerateObject()) |
| | | 151 | | { |
| | 1 | 152 | | var field = property.Name; |
| | 1 | 153 | | var key = $"$config.{field}"; |
| | 1 | 154 | | if (!ConfigFields.Contains(field)) |
| | | 155 | | { |
| | 1 | 156 | | errors.Add(new( |
| | 1 | 157 | | key, |
| | 1 | 158 | | $"{FormatKey(key)} is not a known field. Allowed fields: {string.Join(", ", ConfigFields)}.")); |
| | | 159 | | } |
| | 1 | 160 | | else if (property.Value.ValueKind != JsonValueKind.String) |
| | | 161 | | { |
| | 1 | 162 | | errors.Add(new(key, $"{key} must be a string, not {DescribeType(property.Value)}.")); |
| | | 163 | | } |
| | 1 | 164 | | else if (UriFields.Contains(field)) |
| | | 165 | | { |
| | 1 | 166 | | errors.AddRange(ValidateUri(key, property.Value)); |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | |
| | 1 | 170 | | return errors; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | private static List<MapFileError> ValidateUri(string key, JsonElement value) |
| | | 174 | | { |
| | 1 | 175 | | if (value.ValueKind != JsonValueKind.String) |
| | | 176 | | { |
| | 0 | 177 | | return [new(key, $"{key} must be a string, not {DescribeType(value)}.")]; |
| | | 178 | | } |
| | | 179 | | |
| | 1 | 180 | | return IsUri(value.GetString()!) |
| | 1 | 181 | | ? [] |
| | 1 | 182 | | : [new(key, $"{key} must be an absolute URI.")]; |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | private static List<MapFileError> ValidateProviderConfiguration( |
| | | 186 | | Dictionary<string, JsonElement> fileConfig, |
| | | 187 | | EnvilderOptions overrides) |
| | | 188 | | { |
| | 1 | 189 | | var effective = ResolveEffectiveConfig(fileConfig, overrides); |
| | 1 | 190 | | var provider = effective.TryGetValue("provider", out var configured) |
| | 1 | 191 | | ? configured |
| | 1 | 192 | | : new(DefaultProvider, "$config.provider"); |
| | | 193 | | |
| | 1 | 194 | | if (!SupportedProviders.Contains(provider.Value)) |
| | | 195 | | { |
| | 1 | 196 | | return |
| | 1 | 197 | | [ |
| | 1 | 198 | | new( |
| | 1 | 199 | | provider.Source, |
| | 1 | 200 | | $"Unsupported provider: {FormatKey(provider.Value)}." |
| | 1 | 201 | | + $" Supported providers: {string.Join(", ", SupportedProviders)}."), |
| | 1 | 202 | | ]; |
| | | 203 | | } |
| | | 204 | | |
| | 1 | 205 | | var errors = new List<MapFileError>(); |
| | 1 | 206 | | foreach (var (field, validProviders) in ProviderSpecificFields) |
| | | 207 | | { |
| | 1 | 208 | | if (!effective.TryGetValue(field, out var setting) || validProviders.Contains(provider.Value)) |
| | | 209 | | { |
| | | 210 | | continue; |
| | | 211 | | } |
| | | 212 | | |
| | 1 | 213 | | var usable = validProviders.Where(name => SupportedProviders.Contains(name)).ToList(); |
| | 1 | 214 | | var names = string.Join(" or ", usable.Count > 0 ? usable : validProviders.ToList()); |
| | 1 | 215 | | errors.Add(new( |
| | 1 | 216 | | setting.Source, |
| | 1 | 217 | | $"{setting.Source} is only supported with the {names} provider" |
| | 1 | 218 | | + $" (current provider: {provider.Value}).")); |
| | | 219 | | } |
| | | 220 | | |
| | 1 | 221 | | if (RequiredProviderFields.TryGetValue(provider.Value, out var required) |
| | 1 | 222 | | && !effective.ContainsKey(required)) |
| | | 223 | | { |
| | 1 | 224 | | errors.Add(new( |
| | 1 | 225 | | $"$config.{required}", |
| | 1 | 226 | | $"{required} is required when using {ProviderLabels[provider.Value]} provider." |
| | 1 | 227 | | + $" Set it in $config.{required} in your map file" |
| | 1 | 228 | | + $" or via {OverrideMethods[required]}.")); |
| | | 229 | | } |
| | | 230 | | |
| | 1 | 231 | | return errors; |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | private static Dictionary<string, SourcedValue> ResolveEffectiveConfig( |
| | | 235 | | Dictionary<string, JsonElement> fileConfig, |
| | | 236 | | EnvilderOptions overrides) |
| | | 237 | | { |
| | 1 | 238 | | var effective = new Dictionary<string, SourcedValue>(); |
| | 1 | 239 | | foreach (var field in ConfigFields) |
| | | 240 | | { |
| | 1 | 241 | | if (fileConfig.TryGetValue(field, out var value) && value.ValueKind == JsonValueKind.String) |
| | | 242 | | { |
| | 1 | 243 | | effective[field] = new(value.GetString()!, $"$config.{field}"); |
| | | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | // Blank overrides are ignored, as they are on a load. |
| | 1 | 248 | | var overridden = new Dictionary<string, string?> |
| | 1 | 249 | | { |
| | 1 | 250 | | ["provider"] = overrides.Provider?.ToString(), |
| | 1 | 251 | | ["profile"] = overrides.Profile, |
| | 1 | 252 | | ["vaultUrl"] = overrides.VaultUrl, |
| | 1 | 253 | | }; |
| | 1 | 254 | | foreach (var entry in OverrideMethods) |
| | | 255 | | { |
| | 1 | 256 | | var field = entry.Key; |
| | 1 | 257 | | var value = overridden[field]?.Trim(); |
| | 1 | 258 | | if (!string.IsNullOrEmpty(value)) |
| | | 259 | | { |
| | 1 | 260 | | effective[field] = new( |
| | 1 | 261 | | field == "provider" ? value!.ToLowerInvariant() : value!, |
| | 1 | 262 | | entry.Value); |
| | | 263 | | } |
| | | 264 | | } |
| | | 265 | | |
| | 1 | 266 | | return effective; |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | private static bool IsUri(string value) |
| | | 270 | | { |
| | 1 | 271 | | var colon = value.IndexOf(':'); |
| | 1 | 272 | | return colon > 0 |
| | 1 | 273 | | && IsScheme(value.Substring(0, colon)) |
| | 1 | 274 | | && !value.Any(char.IsWhiteSpace) |
| | 1 | 275 | | && Uri.TryCreate(value, UriKind.Absolute, out _); |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | private static bool IsScheme(string scheme) |
| | | 279 | | { |
| | 1 | 280 | | return IsAsciiLetter(scheme[0]) |
| | 1 | 281 | | && scheme.All(c => IsAsciiLetter(c) || (c is >= '0' and <= '9') || c is '+' or '.' or '-'); |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | private static bool IsAsciiLetter(char c) |
| | | 285 | | { |
| | 1 | 286 | | return c is (>= 'A' and <= 'Z') or (>= 'a' and <= 'z'); |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | private static string DescribeType(JsonElement value) |
| | | 290 | | { |
| | 1 | 291 | | return value.ValueKind switch |
| | 1 | 292 | | { |
| | 1 | 293 | | JsonValueKind.Null => "null", |
| | 1 | 294 | | JsonValueKind.Array => "an array", |
| | 1 | 295 | | JsonValueKind.Object => "an object", |
| | 0 | 296 | | JsonValueKind.String => "a string", |
| | 1 | 297 | | JsonValueKind.Number => "a number", |
| | 1 | 298 | | _ => "a boolean", |
| | 1 | 299 | | }; |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | /// <summary> |
| | | 303 | | /// Keys are untrusted input: anything outside a plain identifier is quoted, |
| | | 304 | | /// so a key carrying a line break cannot split a message. |
| | | 305 | | /// </summary> |
| | | 306 | | private static string FormatKey(string key) |
| | | 307 | | { |
| | 1 | 308 | | var plain = key.Length > 0 && key.All(c => |
| | 1 | 309 | | IsAsciiLetter(c) || (c is >= '0' and <= '9') || c is '_' or '$' or '.' or '/' or '-'); |
| | 1 | 310 | | return plain ? key : EnvironmentVariableName.Quote(key); |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | /// <summary>A configuration field with the key that named it, for the report.</summary> |
| | | 314 | | private sealed class SourcedValue |
| | | 315 | | { |
| | 1 | 316 | | public SourcedValue(string value, string source) |
| | | 317 | | { |
| | 1 | 318 | | Value = value; |
| | 1 | 319 | | Source = source; |
| | 1 | 320 | | } |
| | | 321 | | |
| | | 322 | | public string Value { get; } |
| | | 323 | | |
| | | 324 | | public string Source { get; } |
| | | 325 | | } |
| | | 326 | | } |