예제 #1
0
        public async Task NewSecretVersion_TriggersKeyVaultJob_AutoInvalidatesSecret()
        {
            // Arrange
            var applicationId        = _config.GetValue <string>("Arcus:ServicePrincipal:ApplicationId");
            var clientKey            = _config.GetValue <string>("Arcus:ServicePrincipal:AccessKey");
            var keyVaultUri          = _config.GetValue <string>("Arcus:KeyVault:Uri");
            var authentication       = new ServicePrincipalAuthentication(applicationId, clientKey);
            var cachedSecretProvider = _host.Services.GetService <ICachedSecretProvider>();
            var secretValue          = Guid.NewGuid().ToString("N");

            using (IKeyVaultClient client = await authentication.AuthenticateAsync())
                // Act
                await using (var tempSecret = await TemporaryAzureKeyVaultSecret.CreateNewAsync(client, keyVaultUri))
                {
                    await tempSecret.UpdateSecretAsync(secretValue);

                    // Assert
                    RetryAssertion(
                        // ReSharper disable once AccessToDisposedClosure - disposal happens after retry.
                        () => Mock.Get(cachedSecretProvider)
                        .Verify(p => p.InvalidateSecretAsync(It.Is <string>(n => n == tempSecret.Name)), Times.Once),
                        timeout: TimeSpan.FromMinutes(5),
                        interval: TimeSpan.FromMilliseconds(500));
                }
        }
        private static async Task <IKeyVaultClient> CreateKeyVaultClientAsync(KeyRotationConfig rotationConfig)
        {
            ServicePrincipalAuthentication authentication = rotationConfig.ServicePrincipal.CreateAuthentication();
            IKeyVaultClient keyVaultClient = await authentication.AuthenticateAsync();

            return(keyVaultClient);
        }
예제 #3
0
        public async Task ServiceBusMessagePump_RotateServiceBusConnectionKeys_MessagePumpRestartsThenMessageSuccessfullyProcessed()
        {
            // Arrange
            var config = TestConfig.Create();
            KeyRotationConfig keyRotationConfig = config.GetKeyRotationConfig();

            _logger.LogInformation("Using Service Principal [ClientID: '{ClientId}']", keyRotationConfig.ServicePrincipal.ClientId);

            var    client = new ServiceBusConfiguration(keyRotationConfig, _logger);
            string freshConnectionString = await client.RotateConnectionStringKeysForQueueAsync(KeyType.PrimaryKey);

            ServicePrincipalAuthentication authentication = keyRotationConfig.ServicePrincipal.CreateAuthentication();
            IKeyVaultClient keyVaultClient = await authentication.AuthenticateAsync();

            await SetConnectionStringInKeyVaultAsync(keyVaultClient, keyRotationConfig, freshConnectionString);

            var options = new WorkerOptions();

            options.AddEventGridPublisher(config)
            .AddSingleton <ISecretProvider>(serviceProvider =>
            {
                return(new KeyVaultSecretProvider(
                           new ServicePrincipalAuthentication(keyRotationConfig.ServicePrincipal.ClientId,
                                                              keyRotationConfig.ServicePrincipal.ClientSecret),
                           new KeyVaultConfiguration(keyRotationConfig.KeyVault.VaultUri)));
            })
            .AddServiceBusQueueMessagePump(keyRotationConfig.KeyVault.SecretName, opt => opt.AutoComplete = true)
            .WithServiceBusMessageHandler <OrdersAzureServiceBusMessageHandler, Order>();

            await using (var worker = await Worker.StartNewAsync(options))
            {
                string newSecondaryConnectionString = await client.RotateConnectionStringKeysForQueueAsync(KeyType.SecondaryKey);
                await SetConnectionStringInKeyVaultAsync(keyVaultClient, keyRotationConfig, newSecondaryConnectionString);

                await using (var service = await TestMessagePumpService.StartNewAsync(config, _logger))
                {
                    // Act
                    string newPrimaryConnectionString = await client.RotateConnectionStringKeysForQueueAsync(KeyType.PrimaryKey);

                    // Assert
                    await service.SimulateMessageProcessingAsync(newPrimaryConnectionString);
                }
            }
        }