Skip to content

Commit 39e429f

Browse files
authored
Remove KeyManagementOptions copy constructor (#54516)
There was only one caller and that caller only wanted two members. Plus, it was subtly hiding the fact that it might be cloning a null object.
1 parent 268a2df commit 39e429f

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

src/DataProtection/DataProtection/src/KeyManagement/KeyManagementOptions.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,6 @@ public KeyManagementOptions()
2727
{
2828
}
2929

30-
// copy ctor
31-
internal KeyManagementOptions(KeyManagementOptions other)
32-
{
33-
if (other != null)
34-
{
35-
AutoGenerateKeys = other.AutoGenerateKeys;
36-
_newKeyLifetime = other._newKeyLifetime;
37-
XmlEncryptor = other.XmlEncryptor;
38-
XmlRepository = other.XmlRepository;
39-
AuthenticatedEncryptorConfiguration = other.AuthenticatedEncryptorConfiguration;
40-
41-
foreach (var keyEscrowSink in other.KeyEscrowSinks)
42-
{
43-
KeyEscrowSinks.Add(keyEscrowSink);
44-
}
45-
46-
foreach (var encryptorFactory in other.AuthenticatedEncryptorFactories)
47-
{
48-
AuthenticatedEncryptorFactories.Add(encryptorFactory);
49-
}
50-
}
51-
}
52-
5330
/// <summary>
5431
/// Specifies whether the data protection system should auto-generate keys.
5532
/// </summary>

src/DataProtection/DataProtection/src/KeyManagement/KeyRingProvider.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ internal sealed class KeyRingProvider : ICacheableKeyRingProvider, IKeyRingProvi
1919
private CacheableKeyRing? _cacheableKeyRing;
2020
private readonly object _cacheableKeyRingLockObj = new object();
2121
private readonly IDefaultKeyResolver _defaultKeyResolver;
22-
private readonly KeyManagementOptions _keyManagementOptions;
22+
private readonly bool _autoGenerateKeys;
23+
private readonly TimeSpan _newKeyLifetime;
2324
private readonly IKeyManager _keyManager;
2425
private readonly ILogger _logger;
2526

@@ -41,7 +42,9 @@ public KeyRingProvider(
4142
IDefaultKeyResolver defaultKeyResolver,
4243
ILoggerFactory loggerFactory)
4344
{
44-
_keyManagementOptions = new KeyManagementOptions(keyManagementOptions.Value); // clone so new instance is immutable
45+
var options = keyManagementOptions.Value ?? new();
46+
_autoGenerateKeys = options.AutoGenerateKeys;
47+
_newKeyLifetime = options.NewKeyLifetime;
4548
_keyManager = keyManager;
4649
CacheableKeyRingProvider = this;
4750
_defaultKeyResolver = defaultKeyResolver;
@@ -113,7 +116,7 @@ private CacheableKeyRing CreateCacheableKeyRingCore(DateTimeOffset now, IKey? ke
113116

114117
// We have been asked to generate a new key, but auto-generation of keys has been disabled.
115118
// We need to use the fallback key or fail.
116-
if (!_keyManagementOptions.AutoGenerateKeys)
119+
if (!_autoGenerateKeys)
117120
{
118121
var keyToUse = defaultKey ?? defaultKeyPolicy.FallbackKey;
119122
if (keyToUse == null)
@@ -135,15 +138,15 @@ private CacheableKeyRing CreateCacheableKeyRingCore(DateTimeOffset now, IKey? ke
135138
{
136139
// The case where there's no default key is the easiest scenario, since it
137140
// means that we need to create a new key with immediate activation.
138-
var newKey = _keyManager.CreateNewKey(activationDate: now, expirationDate: now + _keyManagementOptions.NewKeyLifetime);
141+
var newKey = _keyManager.CreateNewKey(activationDate: now, expirationDate: now + _newKeyLifetime);
139142
return CreateCacheableKeyRingCore(now, keyJustAdded: newKey); // recursively call
140143
}
141144
else
142145
{
143146
// If there is a default key, then the new key we generate should become active upon
144147
// expiration of the default key. The new key lifetime is measured from the creation
145148
// date (now), not the activation date.
146-
var newKey = _keyManager.CreateNewKey(activationDate: defaultKey.ExpirationDate, expirationDate: now + _keyManagementOptions.NewKeyLifetime);
149+
var newKey = _keyManager.CreateNewKey(activationDate: defaultKey.ExpirationDate, expirationDate: now + _newKeyLifetime);
147150
return CreateCacheableKeyRingCore(now, keyJustAdded: newKey); // recursively call
148151
}
149152
}

0 commit comments

Comments
 (0)