|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Xml.Linq; |
| 5 | +using Microsoft.AspNetCore.DataProtection.KeyManagement; |
| 6 | +using Microsoft.AspNetCore.DataProtection.Repositories; |
| 7 | +using Microsoft.AspNetCore.DataProtection.XmlEncryption; |
| 8 | +using Microsoft.Extensions.Configuration; |
| 9 | +using Microsoft.Extensions.Logging.Abstractions; |
| 10 | +using Microsoft.Extensions.Options; |
| 11 | + |
| 12 | +namespace Microsoft.AspNetCore.DataProtection.Internal; |
| 13 | + |
| 14 | +public class KeyManagementOptionsPostSetupTest |
| 15 | +{ |
| 16 | + private static readonly string keyDir = new DirectoryInfo("/testpath").FullName; |
| 17 | + private static readonly XElement xElement = new("element"); |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void ConfigureReadOnly() |
| 21 | + { |
| 22 | + var config = new ConfigurationBuilder().AddInMemoryCollection( |
| 23 | + [ |
| 24 | + new KeyValuePair<string, string>(KeyManagementOptionsPostSetup.ReadOnlyDataProtectionKeyDirectoryKey, keyDir), |
| 25 | + ]).Build(); |
| 26 | + |
| 27 | + IPostConfigureOptions<KeyManagementOptions> setup = new KeyManagementOptionsPostSetup(config, NullLoggerFactory.Instance); |
| 28 | + |
| 29 | + var options = new KeyManagementOptions(); |
| 30 | + |
| 31 | + setup.PostConfigure(Options.DefaultName, options); |
| 32 | + |
| 33 | + AssertReadOnly(options, keyDir); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void ConfigureReadOnly_NonDefaultInstance() |
| 38 | + { |
| 39 | + var config = new ConfigurationBuilder().AddInMemoryCollection( |
| 40 | + [ |
| 41 | + new KeyValuePair<string, string>(KeyManagementOptionsPostSetup.ReadOnlyDataProtectionKeyDirectoryKey, keyDir), |
| 42 | + ]).Build(); |
| 43 | + |
| 44 | + IPostConfigureOptions<KeyManagementOptions> setup = new KeyManagementOptionsPostSetup(config, NullLoggerFactory.Instance); |
| 45 | + |
| 46 | + var options = new KeyManagementOptions(); |
| 47 | + |
| 48 | + setup.PostConfigure(Options.DefaultName + 1, options); |
| 49 | + |
| 50 | + AssertNotReadOnly(options, keyDir); |
| 51 | + |
| 52 | + Assert.True(options.AutoGenerateKeys); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void ConfigureReadOnly_EmptyDirPath() |
| 57 | + { |
| 58 | + var config = new ConfigurationBuilder().AddInMemoryCollection( |
| 59 | + [ |
| 60 | + new KeyValuePair<string, string>(KeyManagementOptionsPostSetup.ReadOnlyDataProtectionKeyDirectoryKey, ""), |
| 61 | + ]).Build(); |
| 62 | + |
| 63 | + IPostConfigureOptions<KeyManagementOptions> setup = new KeyManagementOptionsPostSetup(config, NullLoggerFactory.Instance); |
| 64 | + |
| 65 | + var options = new KeyManagementOptions(); |
| 66 | + |
| 67 | + setup.PostConfigure(Options.DefaultName, options); |
| 68 | + |
| 69 | + AssertNotReadOnly(options, keyDir); |
| 70 | + |
| 71 | + Assert.True(options.AutoGenerateKeys); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void ConfigureReadOnly_ExplicitRepository() |
| 76 | + { |
| 77 | + var config = new ConfigurationBuilder().AddInMemoryCollection( |
| 78 | + [ |
| 79 | + new KeyValuePair<string, string>(KeyManagementOptionsPostSetup.ReadOnlyDataProtectionKeyDirectoryKey, keyDir), |
| 80 | + ]).Build(); |
| 81 | + |
| 82 | + IPostConfigureOptions<KeyManagementOptions> setup = new KeyManagementOptionsPostSetup(config, NullLoggerFactory.Instance); |
| 83 | + |
| 84 | + var xmlDir = Directory.CreateTempSubdirectory(); |
| 85 | + try |
| 86 | + { |
| 87 | + var options = new KeyManagementOptions() |
| 88 | + { |
| 89 | + XmlRepository = new FileSystemXmlRepository(xmlDir, NullLoggerFactory.Instance), |
| 90 | + }; |
| 91 | + |
| 92 | + setup.PostConfigure(Options.DefaultName, options); |
| 93 | + |
| 94 | + AssertNotReadOnly(options, keyDir); |
| 95 | + |
| 96 | + Assert.True(options.AutoGenerateKeys); |
| 97 | + } |
| 98 | + finally |
| 99 | + { |
| 100 | + xmlDir.Delete(recursive: true); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void ConfigureReadOnly_ExplicitEncryptor() |
| 106 | + { |
| 107 | + var config = new ConfigurationBuilder().AddInMemoryCollection( |
| 108 | + [ |
| 109 | + new KeyValuePair<string, string>(KeyManagementOptionsPostSetup.ReadOnlyDataProtectionKeyDirectoryKey, keyDir), |
| 110 | + ]).Build(); |
| 111 | + |
| 112 | + IPostConfigureOptions<KeyManagementOptions> setup = new KeyManagementOptionsPostSetup(config, NullLoggerFactory.Instance); |
| 113 | + |
| 114 | + var options = new KeyManagementOptions() |
| 115 | + { |
| 116 | + XmlEncryptor = new NullXmlEncryptor(), |
| 117 | + }; |
| 118 | + |
| 119 | + setup.PostConfigure(Options.DefaultName, options); |
| 120 | + |
| 121 | + AssertNotReadOnly(options, keyDir); |
| 122 | + |
| 123 | + Assert.True(options.AutoGenerateKeys); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void NotConfigured_NoProperty() |
| 128 | + { |
| 129 | + var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); |
| 130 | + |
| 131 | + IPostConfigureOptions<KeyManagementOptions> setup = new KeyManagementOptionsPostSetup(config, NullLoggerFactory.Instance); |
| 132 | + |
| 133 | + var options = new KeyManagementOptions(); |
| 134 | + |
| 135 | + setup.PostConfigure(Options.DefaultName, options); |
| 136 | + |
| 137 | + AssertNotReadOnly(options, keyDir); |
| 138 | + |
| 139 | + Assert.True(options.AutoGenerateKeys); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void NotConfigured_NoIConfiguration() |
| 144 | + { |
| 145 | + IPostConfigureOptions<KeyManagementOptions> setup = new KeyManagementOptionsPostSetup(); |
| 146 | + |
| 147 | + var options = new KeyManagementOptions(); |
| 148 | + |
| 149 | + setup.PostConfigure(Options.DefaultName, options); |
| 150 | + |
| 151 | + AssertNotReadOnly(options, keyDir); |
| 152 | + |
| 153 | + Assert.True(options.AutoGenerateKeys); |
| 154 | + } |
| 155 | + |
| 156 | + private static void AssertReadOnly(KeyManagementOptions options, string keyDir) |
| 157 | + { |
| 158 | + // Effect 1: No key generation |
| 159 | + Assert.False(options.AutoGenerateKeys); |
| 160 | + |
| 161 | + var repository = options.XmlRepository as FileSystemXmlRepository; |
| 162 | + Assert.NotNull(repository); |
| 163 | + |
| 164 | + // Effect 2: Location from configuration |
| 165 | + Assert.Equal(keyDir, repository.Directory.FullName); |
| 166 | + |
| 167 | + // Effect 3: No writing |
| 168 | + Assert.Throws<InvalidOperationException>(() => repository.StoreElement(xElement, friendlyName: null)); |
| 169 | + |
| 170 | + // Effect 4: No key encryption |
| 171 | + Assert.NotNull(options.XmlEncryptor); |
| 172 | + Assert.Throws<InvalidOperationException>(() => options.XmlEncryptor.Encrypt(xElement)); |
| 173 | + } |
| 174 | + |
| 175 | + private static void AssertNotReadOnly(KeyManagementOptions options, string keyDir) |
| 176 | + { |
| 177 | + // Missing effect 1: No key generation |
| 178 | + Assert.True(options.AutoGenerateKeys); |
| 179 | + |
| 180 | + var repository = options.XmlRepository; |
| 181 | + if (repository is not null) |
| 182 | + { |
| 183 | + // Missing effect 2: Location from configuration |
| 184 | + Assert.NotEqual(keyDir, (repository as FileSystemXmlRepository)?.Directory.FullName); |
| 185 | + |
| 186 | + // Missing effect 3: No writing |
| 187 | + repository.StoreElement(xElement, friendlyName: null); |
| 188 | + } |
| 189 | + |
| 190 | + var encryptor = options.XmlEncryptor; |
| 191 | + if (encryptor is not null) |
| 192 | + { |
| 193 | + // Missing effect 4: No key encryption |
| 194 | + options.XmlEncryptor.Encrypt(xElement); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments