示例#1
0
 // copy ctor
 internal KeyManagementOptions(KeyManagementOptions other)
 {
     if (other != null)
     {
         this.AutoGenerateKeys = other.AutoGenerateKeys;
         this._newKeyLifetime  = other._newKeyLifetime;
     }
 }
示例#2
0
        private static ICacheableKeyRingProvider SetupCreateCacheableKeyRingTestAndCreateKeyManager(
            IList <string> callSequence,
            IEnumerable <CancellationToken> getCacheExpirationTokenReturnValues,
            IEnumerable <IReadOnlyCollection <IKey> > getAllKeysReturnValues,
            IEnumerable <Tuple <DateTimeOffset, DateTimeOffset, IKey> > createNewKeyCallbacks,
            IEnumerable <Tuple <DateTimeOffset, IEnumerable <IKey>, DefaultKeyResolution> > resolveDefaultKeyPolicyReturnValues,
            KeyManagementOptions keyManagementOptions = null)
        {
            var getCacheExpirationTokenReturnValuesEnumerator = getCacheExpirationTokenReturnValues.GetEnumerator();
            var mockKeyManager = new Mock <IKeyManager>(MockBehavior.Strict);

            mockKeyManager.Setup(o => o.GetCacheExpirationToken())
            .Returns(() =>
            {
                callSequence.Add("GetCacheExpirationToken");
                getCacheExpirationTokenReturnValuesEnumerator.MoveNext();
                return(getCacheExpirationTokenReturnValuesEnumerator.Current);
            });

            var getAllKeysReturnValuesEnumerator = getAllKeysReturnValues.GetEnumerator();

            mockKeyManager.Setup(o => o.GetAllKeys())
            .Returns(() =>
            {
                callSequence.Add("GetAllKeys");
                getAllKeysReturnValuesEnumerator.MoveNext();
                return(getAllKeysReturnValuesEnumerator.Current);
            });

            if (createNewKeyCallbacks != null)
            {
                var createNewKeyCallbacksEnumerator = createNewKeyCallbacks.GetEnumerator();
                mockKeyManager.Setup(o => o.CreateNewKey(It.IsAny <DateTimeOffset>(), It.IsAny <DateTimeOffset>()))
                .Returns <DateTimeOffset, DateTimeOffset>((activationDate, expirationDate) =>
                {
                    callSequence.Add("CreateNewKey");
                    createNewKeyCallbacksEnumerator.MoveNext();
                    Assert.Equal(createNewKeyCallbacksEnumerator.Current.Item1, activationDate);
                    Assert.Equal(createNewKeyCallbacksEnumerator.Current.Item2, expirationDate);
                    return(createNewKeyCallbacksEnumerator.Current.Item3);
                });
            }

            var resolveDefaultKeyPolicyReturnValuesEnumerator = resolveDefaultKeyPolicyReturnValues.GetEnumerator();
            var mockDefaultKeyResolver = new Mock <IDefaultKeyResolver>(MockBehavior.Strict);

            mockDefaultKeyResolver.Setup(o => o.ResolveDefaultKeyPolicy(It.IsAny <DateTimeOffset>(), It.IsAny <IEnumerable <IKey> >()))
            .Returns <DateTimeOffset, IEnumerable <IKey> >((now, allKeys) =>
            {
                callSequence.Add("ResolveDefaultKeyPolicy");
                resolveDefaultKeyPolicyReturnValuesEnumerator.MoveNext();
                Assert.Equal(resolveDefaultKeyPolicyReturnValuesEnumerator.Current.Item1, now);
                Assert.Equal(resolveDefaultKeyPolicyReturnValuesEnumerator.Current.Item2, allKeys);
                return(resolveDefaultKeyPolicyReturnValuesEnumerator.Current.Item3);
            });

            return(CreateKeyRingProvider(mockKeyManager.Object, mockDefaultKeyResolver.Object, keyManagementOptions));
        }
 public KeyRingProvider(IKeyManager keyManager, KeyManagementOptions keyManagementOptions, IServiceProvider services)
 {
     _keyManagementOptions = new KeyManagementOptions(keyManagementOptions); // clone so new instance is immutable
     _keyManager = keyManager;
     _cacheableKeyRingProvider = services?.GetService<ICacheableKeyRingProvider>() ?? this;
     _logger = services?.GetLogger<KeyRingProvider>();
     _defaultKeyResolver = services?.GetService<IDefaultKeyResolver>()
         ?? new DefaultKeyResolver(_keyManagementOptions.KeyPropagationWindow, _keyManagementOptions.MaxServerClockSkew, services);
 }
示例#4
0
 public KeyRingProvider(IKeyManager keyManager, KeyManagementOptions keyManagementOptions, IServiceProvider services)
 {
     _keyManagementOptions     = new KeyManagementOptions(keyManagementOptions); // clone so new instance is immutable
     _keyManager               = keyManager;
     _cacheableKeyRingProvider = services?.GetService <ICacheableKeyRingProvider>() ?? this;
     _logger             = services?.GetLogger <KeyRingProvider>();
     _defaultKeyResolver = services?.GetService <IDefaultKeyResolver>()
                           ?? new DefaultKeyResolver(_keyManagementOptions.KeyPropagationWindow, _keyManagementOptions.MaxServerClockSkew, services);
 }
示例#5
0
 public KeyRingProvider(
     IKeyManager keyManager,
     IOptions <KeyManagementOptions> keyManagementOptions,
     IDefaultKeyResolver defaultKeyResolver,
     ILoggerFactory loggerFactory)
 {
     _keyManagementOptions    = new KeyManagementOptions(keyManagementOptions.Value); // clone so new instance is immutable
     _keyManager              = keyManager;
     CacheableKeyRingProvider = this;
     _defaultKeyResolver      = defaultKeyResolver;
     _logger = loggerFactory.CreateLogger <KeyRingProvider>();
 }
示例#6
0
        public KeyRingProvider(
            IKeyManager keyManager,
            IOptions <KeyManagementOptions> keyManagementOptions,
            IDefaultKeyResolver defaultKeyResolver,
            ILoggerFactory loggerFactory)
        {
            _keyManagementOptions    = new KeyManagementOptions(keyManagementOptions.Value); // clone so new instance is immutable
            _keyManager              = keyManager;
            CacheableKeyRingProvider = this;
            _defaultKeyResolver      = defaultKeyResolver;
            _logger = loggerFactory.CreateLogger <KeyRingProvider>();

            // We will automatically refresh any unknown keys for 2 minutes see https://github.com/dotnet/aspnetcore/issues/3975
            AutoRefreshWindowEnd = DateTime.UtcNow.AddMinutes(2);
        }
        private static KeyRingProvider CreateKeyRingProvider(ICacheableKeyRingProvider cacheableKeyRingProvider)
        {
            var mockEncryptorFactory = new Mock <IAuthenticatedEncryptorFactory>();

            mockEncryptorFactory.Setup(m => m.CreateEncryptorInstance(It.IsAny <IKey>())).Returns(new Mock <IAuthenticatedEncryptor>().Object);
            var options = new KeyManagementOptions();

            options.AuthenticatedEncryptorFactories.Add(mockEncryptorFactory.Object);

            return(new KeyRingProvider(
                       keyManager: null,
                       keyManagementOptions: Options.Create(options),
                       defaultKeyResolver: null,
                       loggerFactory: NullLoggerFactory.Instance)
            {
                CacheableKeyRingProvider = cacheableKeyRingProvider
            });
        }
示例#8
0
        // copy ctor
        internal KeyManagementOptions(KeyManagementOptions other)
        {
            if (other != null)
            {
                AutoGenerateKeys = other.AutoGenerateKeys;
                _newKeyLifetime  = other._newKeyLifetime;
                XmlEncryptor     = other.XmlEncryptor;
                XmlRepository    = other.XmlRepository;
                AuthenticatedEncryptorConfiguration = other.AuthenticatedEncryptorConfiguration;

                foreach (var keyEscrowSink in other.KeyEscrowSinks)
                {
                    KeyEscrowSinks.Add(keyEscrowSink);
                }

                foreach (var encryptorFactory in other.AuthenticatedEncryptorFactories)
                {
                    AuthenticatedEncryptorFactories.Add(encryptorFactory);
                }
            }
        }
示例#9
0
        private static ICacheableKeyRingProvider CreateKeyRingProvider(IKeyManager keyManager, IDefaultKeyResolver defaultKeyResolver, KeyManagementOptions keyManagementOptions = null)
        {
            var mockEncryptorFactory = new Mock <IAuthenticatedEncryptorFactory>();

            mockEncryptorFactory.Setup(m => m.CreateEncryptorInstance(It.IsAny <IKey>())).Returns(new Mock <IAuthenticatedEncryptor>().Object);
            keyManagementOptions = keyManagementOptions ?? new KeyManagementOptions();
            keyManagementOptions.AuthenticatedEncryptorFactories.Add(mockEncryptorFactory.Object);

            return(new KeyRingProvider(
                       keyManager: keyManager,
                       keyManagementOptions: Options.Create(keyManagementOptions),
                       defaultKeyResolver: defaultKeyResolver,
                       loggerFactory: NullLoggerFactory.Instance));
        }
示例#10
0
        private static ICacheableKeyRingProvider CreateKeyRingProvider(IKeyManager keyManager, IDefaultKeyResolver defaultKeyResolver, KeyManagementOptions keyManagementOptions = null)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IDefaultKeyResolver>(defaultKeyResolver);
            return(new KeyRingProvider(
                       keyManager: keyManager,
                       keyManagementOptions: keyManagementOptions,
                       services: serviceCollection.BuildServiceProvider()));
        }
 private static ICacheableKeyRingProvider CreateKeyRingProvider(IKeyManager keyManager, IDefaultKeyResolver defaultKeyResolver, KeyManagementOptions keyManagementOptions= null)
 {
     var serviceCollection = new ServiceCollection();
     serviceCollection.AddSingleton<IDefaultKeyResolver>(defaultKeyResolver);
     return new KeyRingProvider(
         keyManager: keyManager,
         keyManagementOptions: keyManagementOptions,
         services: serviceCollection.BuildServiceProvider());
 }
        private static ICacheableKeyRingProvider SetupCreateCacheableKeyRingTestAndCreateKeyManager(
            IList<string> callSequence,
            IEnumerable<CancellationToken> getCacheExpirationTokenReturnValues,
            IEnumerable<IReadOnlyCollection<IKey>> getAllKeysReturnValues,
            IEnumerable<Tuple<DateTimeOffset, DateTimeOffset, IKey>> createNewKeyCallbacks,
            IEnumerable<Tuple<DateTimeOffset, IEnumerable<IKey>, DefaultKeyResolution>> resolveDefaultKeyPolicyReturnValues,
            KeyManagementOptions keyManagementOptions = null)
        {
            var getCacheExpirationTokenReturnValuesEnumerator = getCacheExpirationTokenReturnValues.GetEnumerator();
            var mockKeyManager = new Mock<IKeyManager>(MockBehavior.Strict);
            mockKeyManager.Setup(o => o.GetCacheExpirationToken())
                .Returns(() =>
                {
                    callSequence.Add("GetCacheExpirationToken");
                    getCacheExpirationTokenReturnValuesEnumerator.MoveNext();
                    return getCacheExpirationTokenReturnValuesEnumerator.Current;
                });

            var getAllKeysReturnValuesEnumerator = getAllKeysReturnValues.GetEnumerator();
            mockKeyManager.Setup(o => o.GetAllKeys())
              .Returns(() =>
              {
                  callSequence.Add("GetAllKeys");
                  getAllKeysReturnValuesEnumerator.MoveNext();
                  return getAllKeysReturnValuesEnumerator.Current;
              });

            if (createNewKeyCallbacks != null)
            {
                var createNewKeyCallbacksEnumerator = createNewKeyCallbacks.GetEnumerator();
                mockKeyManager.Setup(o => o.CreateNewKey(It.IsAny<DateTimeOffset>(), It.IsAny<DateTimeOffset>()))
                    .Returns<DateTimeOffset, DateTimeOffset>((activationDate, expirationDate) =>
                    {
                        callSequence.Add("CreateNewKey");
                        createNewKeyCallbacksEnumerator.MoveNext();
                        Assert.Equal(createNewKeyCallbacksEnumerator.Current.Item1, activationDate);
                        Assert.Equal(createNewKeyCallbacksEnumerator.Current.Item2, expirationDate);
                        return createNewKeyCallbacksEnumerator.Current.Item3;
                    });
            }

            var resolveDefaultKeyPolicyReturnValuesEnumerator = resolveDefaultKeyPolicyReturnValues.GetEnumerator();
            var mockDefaultKeyResolver = new Mock<IDefaultKeyResolver>(MockBehavior.Strict);
            mockDefaultKeyResolver.Setup(o => o.ResolveDefaultKeyPolicy(It.IsAny<DateTimeOffset>(), It.IsAny<IEnumerable<IKey>>()))
                .Returns<DateTimeOffset, IEnumerable<IKey>>((now, allKeys) =>
                 {
                     callSequence.Add("ResolveDefaultKeyPolicy");
                     resolveDefaultKeyPolicyReturnValuesEnumerator.MoveNext();
                     Assert.Equal(resolveDefaultKeyPolicyReturnValuesEnumerator.Current.Item1, now);
                     Assert.Equal(resolveDefaultKeyPolicyReturnValuesEnumerator.Current.Item2, allKeys);
                     return resolveDefaultKeyPolicyReturnValuesEnumerator.Current.Item3;
                 });

            return CreateKeyRingProvider(mockKeyManager.Object, mockDefaultKeyResolver.Object, keyManagementOptions);
        }