public void Test_ServicePrincipleConfig_Validate()
        {
            // Arrange
            var spConfig = new ServicePrincipleConfig()
            {
                InstanceName   = null,
                SubscriptionId = null,
                TenantId       = null,
                AppId          = null,
                AppSecret      = null,
                Sender         = null,
                Receiver       = null
            };

            // Act/Assert
            Assert.Throws <ValidateException>(() => spConfig.ThrowIfInvalid());
            Assert.Null(spConfig.TenantId);
            Assert.Null(spConfig.SubscriptionId);
            Assert.Null(spConfig.AppId);
            Assert.Null(spConfig.AppSecret);
            Assert.Null(spConfig.Sender);
            Assert.Null(spConfig.Receiver);
            Assert.Null(spConfig.InstanceName);
            spConfig.ToString().Should().NotBeNullOrEmpty();
        }
        /// <summary>Initializes a new instance of the KeyVault class using Service Principle security.</summary>
        /// <param name="config">Service Principle configuration the instance.</param>
        public KeyVault([NotNull] ServicePrincipleConfig config)
        {
            // Ensure the required config values are all set.
            config.ThrowIfInvalid();

            ServicePrincipleConfig = config;
            InstanceUri            = config.Uri;
            Name   = config.KeyVaultInstanceName;
            Config = config;
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of ServiceBusMessenger with Service Principle authentication.
        /// </summary>
        /// <param name="config">The Service Principle configuration.</param>
        /// <param name="logger">The logger.</param>
        public ServiceBusMessenger([NotNull] ServicePrincipleConfig config, ILogger logger = null)
        {
            // Ensure all required configuration is as expected.
            config.Validate();
            Name = config.InstanceName;

            Logger    = logger;
            _spConfig = config;
            Config    = config;
        }
        public void Test_ServicePrincipleConfig_ToString()
        {
            // Arrange
            var spConfig = new ServicePrincipleConfig
            {
                KeyVaultInstanceName = "test"
            };

            // Act/Assert
            Assert.Contains("test", spConfig.ToString());
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TableStorageBase"/> class.
        /// </summary>
        /// <param name="config">The configuration.</param>
        /// <param name="logger">The logger.</param>
        protected TableStorageBase(ServicePrincipleConfig config, ILogger logger = null)
        {
            // Ensure all mandatory fields are set.
            config.ThrowIfInvalid();

            Logger = logger;
            ServicePrincipleConfig = config;
            Name = config.InstanceName;

            _instanceName   = config.InstanceName;
            _subscriptionId = config.SubscriptionId;
        }
        public TableStorageIntegrationTests()
        {
            var readConfig = new ConfigurationBuilder().AddJsonFile("appSettings.json").Build();
            var config     = new ServicePrincipleConfig
            {
                InstanceName   = readConfig.GetValue <string>("InstanceName"),
                TenantId       = readConfig.GetValue <string>("TenantId"),
                SubscriptionId = readConfig.GetValue <string>("SubscriptionId"),
                AppId          = readConfig.GetValue <string>("AppId"),
                AppSecret      = readConfig.GetValue <string>("AppSecret"),
            };

            _tableStorageClient = new TableStorage(config);
            _tableStorageClient.Name.Should().Be(config.InstanceName);
            _tableStorageClient.CreateTable(TestTableName).GetAwaiter().GetResult();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CosmosStorageBase"/> class.
        /// </summary>
        /// <param name="config">The configuration.</param>
        /// <param name="logger">The logger.</param>
        protected CosmosStorageBase(ServicePrincipleConfig config, ILogger logger = null)
        {
            // Validate the config.
            config.ThrowIfInvalid();

            Logger = logger;
            ServicePrincipleConfig = config;
            Name         = config.InstanceName;
            DatabaseName = config.DatabaseName;

            _instanceName                = config.InstanceName;
            _subscriptionId              = config.SubscriptionId;
            _createIfNotExists           = config.CreateDatabaseIfNotExists;
            _createTableNames            = config.CreateTables;
            _maxThroughput               = config.MaxThroughput;
            _autoscaleDatabaseThroughput = config.AutoscaleDatabaseThroughput;
        }
        public AuditLogIntegrationTests()
        {
            var readConfig = new ConfigurationBuilder().AddJsonFile("appSettings.json").Build();
            var config     = new ServicePrincipleConfig
            {
                InstanceName   = readConfig.GetValue <string>("InstanceName"),
                TenantId       = readConfig.GetValue <string>("TenantId"),
                SubscriptionId = readConfig.GetValue <string>("SubscriptionId"),
                AppId          = readConfig.GetValue <string>("AppId"),
                AppSecret      = readConfig.GetValue <string>("AppSecret"),
            };

            _tableStorage = new TableStorage(config);
            _tableStorage.CreateTable(_tableStorage.AuditTableName).GetAwaiter().GetResult();
            Thread.Sleep(5000);

            _auditLogger = _tableStorage;
        }
Esempio n. 9
0
        public void Test_ServicePrincipleConfig_Validation()
        {
            var spConfig = new ServicePrincipleConfig();

            // Check the service Principle config validation.
            Assert.Throws <ValidateException>(() => spConfig.ThrowIfInvalid());
            spConfig.InstanceName = "test";
            Assert.Throws <ValidateException>(() => spConfig.ThrowIfInvalid());
            spConfig.AppId = "test";
            Assert.Throws <ValidateException>(() => spConfig.ThrowIfInvalid());
            spConfig.AppSecret = "test";
            Assert.Throws <ValidateException>(() => spConfig.ThrowIfInvalid());
            spConfig.TenantId = "test";
            Assert.Throws <ValidateException>(() => spConfig.ThrowIfInvalid());
            spConfig.SubscriptionId = "test";
            AssertExtensions.DoesNotThrow(() => spConfig.ThrowIfInvalid());
            spConfig.ToString().Should().NotBeNullOrEmpty();
        }
Esempio n. 10
0
        public void Test_ServicePrincipleConfig_ToString()
        {
            // Arrange.
            var config = new ServicePrincipleConfig {
                LockInSeconds = 10
            };
            var configWithSecret = new ServicePrincipleConfig {
                AppSecret = "sample"
            };

            // Act.
            var configNotSet = config.ToString();
            var configSet    = configWithSecret.ToString();

            // Assert.
            configNotSet.Contains("Not Set").Should().BeTrue();
            configSet.Contains("Not Set").Should().BeFalse();
            config.LockInSeconds.Should().Be(10);
        }
Esempio n. 11
0
        public void Test_ServiceBusMessenger_GetAccessTokenUrlNotImplemented()
        {
            // Arrange/Act
            var msiConfig = new MsiConfig()
            {
                SharedAccessPolicyName = ""
            };
            var connectionConfig = new ConnectionConfig();
            var spConfig         = new ServicePrincipleConfig()
            {
                SharedAccessPolicyName = "TestPolicy", InstanceName = "testSBInstance", AppId = "TestAppId", AppSecret = "TestAppSecret", TenantId = "TestTenantId", SubscriptionId = "FakeSubscriptionId"
            };
            var serviceBus = new ServiceBusMessenger(spConfig);

            // Assert
            serviceBus.ToString().Should().NotBeNullOrEmpty();
            serviceBus.Name.Should().Be("testSBInstance");
            (serviceBus.Config as ServicePrincipleConfig).Should().NotBeNull();
        }
        public void Test_Configuration_ServicePrincipleValidation()
        {
            // Arrange
            var spConfig = new ServicePrincipleConfig();

            // Act/Assert
            var validationRes = spConfig.Validate();

            validationRes.IsValid.Should().BeFalse();
            validationRes.Errors.ToList().Count.Should().Be(6);

            spConfig.InstanceName = "test";
            validationRes         = spConfig.Validate();
            validationRes.IsValid.Should().BeFalse();
            validationRes.Errors.ToList().Count.Should().Be(5);

            spConfig.AppId = "test";
            validationRes  = spConfig.Validate();
            validationRes.IsValid.Should().BeFalse();
            validationRes.Errors.ToList().Count.Should().Be(4);

            spConfig.AppSecret = "test";
            validationRes      = spConfig.Validate();
            validationRes.IsValid.Should().BeFalse();
            validationRes.Errors.ToList().Count.Should().Be(3);

            spConfig.TenantId = "test";
            validationRes     = spConfig.Validate();
            validationRes.IsValid.Should().BeFalse();
            validationRes.Errors.ToList().Count.Should().Be(2);

            spConfig.DatabaseName = "test";
            validationRes         = spConfig.Validate();
            validationRes.IsValid.Should().BeFalse();
            validationRes.Errors.ToList().Count.Should().Be(1);

            spConfig.SubscriptionId = "test";
            validationRes           = spConfig.Validate();
            validationRes.IsValid.Should().BeTrue();
            validationRes.Errors.ToList().Count.Should().Be(0);
            spConfig.ToString().Length.Should().BeGreaterThan(0);
        }
        public BlobStorageIntegrationTests()
        {
            var config = new ConfigurationBuilder().AddJsonFile("appSettings.json").Build();

            _config = new ServicePrincipleConfig {
                InstanceName            = config.GetValue <string>("InstanceName"),
                TenantId                = config.GetValue <string>("TenantId"),
                SubscriptionId          = config.GetValue <string>("SubscriptionId"),
                AppId                   = config.GetValue <string>("AppId"),
                AppSecret               = config.GetValue <string>("AppSecret"),
                LockInSeconds           = 60,
                CreateFolderIfNotExists = true
            };

            _logger = new ServiceCollection().AddLogging(builder => builder.AddConsole())
                      .BuildServiceProvider().GetService <ILogger <BlobStorageIntegrationTests> >();

            _client = new BlobStorage(_config, _logger);
            RemoveTestFile(_fullPath);
        }
        public void Test_KeyVault_ServicePrincipleConfigValidate()
        {
            // Arrange
            var spConfigGood = new ServicePrincipleConfig {
                KeyVaultInstanceName = "test", AppSecret = "test", TenantId = "test", AppId = "test"
            };
            var spConfigBad    = new ServicePrincipleConfig();
            var spConfigString = spConfigGood.ToString();

            // Act/Assert
            AssertExtensions.DoesNotThrow(() => spConfigGood.Validate());

            var validationResult = spConfigBad.Validate();

            validationResult.IsValid.Should().BeFalse();
            validationResult.Errors.Count().Should().Be(4);

            spConfigBad.KeyVaultInstanceName = "test";
            validationResult = spConfigBad.Validate();
            validationResult.IsValid.Should().BeFalse();
            validationResult.Errors.Count().Should().Be(3);

            spConfigBad.AppId = "test";
            validationResult  = spConfigBad.Validate();
            validationResult.IsValid.Should().BeFalse();
            validationResult.Errors.Count().Should().Be(2);

            spConfigBad.AppSecret = "test";
            validationResult      = spConfigBad.Validate();
            validationResult.IsValid.Should().BeFalse();
            validationResult.Errors.Count().Should().Be(1);

            spConfigBad.TenantId = "test";
            validationResult     = spConfigBad.Validate();
            validationResult.IsValid.Should().BeTrue();
            validationResult.Errors.Count().Should().Be(0);
        }
Esempio n. 15
0
        public void Test_ervicePrincipleConfig_Validate()
        {
            // Arrange
            var spConfig = new ServicePrincipleConfig()
            {
                SharedAccessPolicyName = ""
            };

            // Act/Assert - Check the service Principle config validation.
            Assert.Throws <ArgumentException>(() => spConfig.Validate());
            spConfig.InstanceName = "test";
            Assert.Throws <ArgumentException>(() => spConfig.Validate());
            spConfig.AppId = "test";
            Assert.Throws <ArgumentException>(() => spConfig.Validate());
            spConfig.AppSecret = "test";
            Assert.Throws <ArgumentException>(() => spConfig.Validate());
            spConfig.TenantId = "test";
            Assert.Throws <ArgumentException>(() => spConfig.Validate());
            spConfig.SubscriptionId = "test";
            Assert.Throws <ArgumentException>(() => spConfig.Validate());
            spConfig.SharedAccessPolicyName = "test";
            AssertExtensions.DoesNotThrow(() => spConfig.Validate());
            spConfig.ToString().Should().NotBeNullOrEmpty();
        }
        /// <summary>
        /// Adds key vault secrets to the configuration builder.
        /// Uses Service Principle configuration for security.
        /// </summary>
        /// <param name="builder">The builder to extend.</param>
        /// <param name="config">The service principle configuration information.</param>
        /// <param name="keys">The keys to lookup.</param>
        /// <returns>IConfigurationBuilder.</returns>
        /// <exception cref="InvalidOperationException">Problem occurred retrieving secrets from KeyVault</exception>
        public static IConfigurationBuilder AddKeyVaultSecrets(this IConfigurationBuilder builder, ServicePrincipleConfig config, params string[] keys)
        {
            try
            {
                var vault   = new KeyVault(config);
                var secrets = new List <KeyValuePair <string, string> >();

                // Gather secrets from Key Vault
                foreach (var key in keys)
                {
                    try
                    {
                        var value = vault.GetSecret(key).GetAwaiter().GetResult();
                        secrets.Add(new KeyValuePair <string, string>(key, value));
                    }
                    catch (KeyVaultErrorException e)
                    {
                        // Do nothing if it fails to find the value.
                        Console.WriteLine($"Failed to find keyvault setting: {key}, exception: {e.Message}");
                    }
                }

                // Add them to config.
                if (secrets.Any())
                {
                    builder.AddInMemoryCollection(secrets);
                }

                // Keep track of instance.
                KeyVaultInstance = vault;

                // Return updated builder.
                return(builder);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Problem occurred retrieving secrets from KeyVault using Service Principle", ex);
            }
        }
        /// <summary>
        /// Adds the key vault singleton named with service principle authentication.
        /// </summary>
        /// <param name="services">The services.</param>
        /// <param name="key">The key.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>IServiceCollection.</returns>
        public static IServiceCollection AddKeyVaultSingletonNamed(this IServiceCollection services, string key, ServicePrincipleConfig config)
        {
            var instance = new KeyVault(config);

            if (!key.IsNullOrEmpty())
            {
                instance.Name = key;
            }

            services.AddSingleton <ISecureVault>(instance);
            AddFactoryIfNotAdded(services);
            return(services);
        }
 /// <summary>
 /// Initializes a new instance of <see cref="BlobStorage" /> with Service Principle authentication.
 /// </summary>
 /// <param name="config">The Service Principle configuration settings for connecting to storage.</param>
 /// <param name="logger">The logger to log information to.</param>
 /// <inheritdoc />
 public BlobStorage([NotNull] ServicePrincipleConfig config, [MaybeNull] ILogger logger = null)
     : base(config, logger)
 {
 }
 /// <summary>
 /// Adds an instance of Azure cosmos storage as a singleton, using service principle config to setup.
 /// </summary>
 /// <param name="services">The services to extend.</param>
 /// <param name="config">The configuration to initialise with.</param>
 /// <returns>IServiceCollection.</returns>
 public static IServiceCollection AddCosmosStorageSingleton(this IServiceCollection services, ServicePrincipleConfig config)
 {
     services.AddSingleton <ITableStorage>(new CosmosStorage(config));
     services.AddFactoryIfNotAdded <ITableStorage>();
     return(services);
 }
 /// <summary>
 /// Adds the key vault singleton instance using service principle config.
 /// </summary>
 /// <param name="services">The services.</param>
 /// <param name="config">The configuration.</param>
 /// <returns>IServiceCollection.</returns>
 public static IServiceCollection AddKeyVaultSingleton(this IServiceCollection services, ServicePrincipleConfig config)
 {
     services.AddSingleton <ISecureVault>(new KeyVault(config));
     AddFactoryIfNotAdded(services);
     return(services);
 }
Esempio n. 21
0
 /// <summary>
 /// Adds the service bus singleton instance and NamedInstanceFactory{ServiceBusInstance} configured.
 /// Uses ServicePrincipleConfig to setup configuration.
 /// </summary>
 /// <param name="services">Service collection to extend.</param>
 /// <param name="config">The configuration.</param>
 /// <returns>Modified service collection with the ServiceBusMessenger and NamedInstanceFactory{ServiceBusMessenger} configured.</returns>
 public static IServiceCollection AddServiceBusSingleton(this IServiceCollection services, ServicePrincipleConfig config)
 {
     return(AddNamedInstance <ServiceBusMessenger>(services, null, new ServiceBusMessenger(config)));
 }
Esempio n. 22
0
 /// <summary>
 /// Adds the service bus singleton instance and NamedInstanceFactory{ServiceBusInstance} configured.
 /// Uses ServicePrincipleConfig to setup configuration.
 /// </summary>
 /// <param name="services">Service collection to extend.</param>
 /// <param name="config">The configuration.</param>
 /// <returns>Modified service collection with the IReactiveMessenger or IMessenger and NamedInstanceFactory{T} configured.</returns>
 public static IServiceCollection AddServiceBusSingleton <T>(this IServiceCollection services, ServicePrincipleConfig config)
     where T : IMessageOperations
 {
     return(AddNamedInstance <T>(services, null, new ServiceBusMessenger(config)));
 }
Esempio n. 23
0
        /// <summary>
        /// Adds the service bus singleton.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="services">The services.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>IServiceCollection.</returns>
        public static IServiceCollection AddServiceBusSingleton <T>(this IServiceCollection services, ServicePrincipleConfig config)
            where T : IMessageOperations
        {
            var serviceBusInstance = new ServiceBusMessenger(config);

            services.AddSingleton(typeof(T), serviceBusInstance);
            services.AddFactoryIfNotAdded <IMessenger>();
            services.AddFactoryIfNotAdded <IReactiveMessenger>();
            return(services);
        }
        /// <summary>
        /// Adds the state storage singleton.
        /// </summary>
        /// <param name="services">The services.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>IServiceCollection.</returns>
        public static IServiceCollection AddStateStorageSingleton(this IServiceCollection services, ServicePrincipleConfig config)
        {
            var instance = new Cloud.Core.Storage.AzureTableStorage.TableStorage(config)
            {
                Name = $"{config.InstanceName}-StateStorage"
            };

            services.AddSingleton <IStateStorage>(instance);
            return(services);
        }
Esempio n. 25
0
 /// <summary>
 /// Initializes a new instance of ServiceBusMessenger with Service Principle authentication.
 /// </summary>
 /// <param name="config">The Service Principle configuration.</param>
 /// <param name="logger">The logger.</param>
 public StorageQueueMessenger([NotNull] ServicePrincipleConfig config, ILogger logger = null)
     : base(config, logger)
 {
 }