| | | 1 | | namespace Envilder; |
| | | 2 | | |
| | | 3 | | using global::Envilder.Domain.Ports; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | internal static class ContractValidation |
| | | 10 | | { |
| | | 11 | | private const string NoResultReason = "The provider returned no result for this secret."; |
| | | 12 | | |
| | | 13 | | /// <summary>The result for a map file that failed validation: no provider was called.</summary> |
| | | 14 | | public static ContractValidationResult InvalidMapFile(IReadOnlyList<MapFileError> mapFileErrors) |
| | | 15 | | { |
| | 1 | 16 | | return new(mapFileErrors, []); |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Checks that every secret path declared in a valid map file exists, |
| | | 21 | | /// without reading any value. A path shared by several variables is checked |
| | | 22 | | /// once. Returns one check per variable, in map-file order. |
| | | 23 | | /// </summary> |
| | | 24 | | public static async Task<ContractValidationResult> CheckRemoteKeysAsync( |
| | | 25 | | ParsedMapFile mapFile, |
| | | 26 | | ISecretExistenceChecker checker, |
| | | 27 | | SecretProviderType provider, |
| | | 28 | | CancellationToken cancellationToken) |
| | | 29 | | { |
| | 1 | 30 | | var mappings = mapFile.Mappings.ToList(); |
| | 1 | 31 | | var existence = mappings.Count == 0 |
| | 1 | 32 | | ? new Dictionary<string, SecretExistence>() |
| | 1 | 33 | | : await checker.CheckSecretsExistAsync( |
| | 1 | 34 | | mappings.Select(mapping => mapping.Value).Distinct().ToList(), |
| | 1 | 35 | | cancellationToken).ConfigureAwait(false); |
| | | 36 | | |
| | 1 | 37 | | var keys = mappings |
| | 1 | 38 | | .Select(mapping => ToKeyCheck(mapping.Key, mapping.Value, provider, existence)) |
| | 1 | 39 | | .ToList(); |
| | 1 | 40 | | return new([], keys); |
| | 1 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// The result for a provider that cannot check existence (a custom one |
| | | 45 | | /// that does not implement <see cref="ISecretExistenceChecker"/>): every |
| | | 46 | | /// key is a provider error. |
| | | 47 | | /// </summary> |
| | | 48 | | public static ContractValidationResult NotCheckable(ParsedMapFile mapFile, SecretProviderType provider) |
| | | 49 | | { |
| | 1 | 50 | | return EveryKey( |
| | 1 | 51 | | mapFile, |
| | 1 | 52 | | provider, |
| | 1 | 53 | | SecretExistenceStatus.ProviderError, |
| | 1 | 54 | | "The provider cannot check whether a secret exists."); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// The result when the provider cannot be built because the AWS profile is |
| | | 59 | | /// not in the credential store: no key can be checked with those credentials. |
| | | 60 | | /// </summary> |
| | | 61 | | public static ContractValidationResult ProfileNotFound( |
| | | 62 | | ParsedMapFile mapFile, |
| | | 63 | | SecretProviderType provider, |
| | | 64 | | string reason) |
| | | 65 | | { |
| | 1 | 66 | | return EveryKey(mapFile, provider, SecretExistenceStatus.AuthenticationFailed, reason); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | private static ContractValidationResult EveryKey( |
| | | 70 | | ParsedMapFile mapFile, |
| | | 71 | | SecretProviderType provider, |
| | | 72 | | SecretExistenceStatus status, |
| | | 73 | | string reason) |
| | | 74 | | { |
| | 1 | 75 | | var keys = mapFile.Mappings |
| | 1 | 76 | | .Select(mapping => new KeyCheck(mapping.Key, mapping.Value, provider, status, reason)) |
| | 1 | 77 | | .ToList(); |
| | 1 | 78 | | return new([], keys); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static KeyCheck ToKeyCheck( |
| | | 82 | | string variable, |
| | | 83 | | string path, |
| | | 84 | | SecretProviderType provider, |
| | | 85 | | IReadOnlyDictionary<string, SecretExistence> existence) |
| | | 86 | | { |
| | 1 | 87 | | return existence.TryGetValue(path, out var found) |
| | 1 | 88 | | ? new(variable, path, provider, found.Status, found.Reason) |
| | 1 | 89 | | : new(variable, path, provider, SecretExistenceStatus.ProviderError, NoResultReason); |
| | | 90 | | } |
| | | 91 | | } |