예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataRepository"/> class
        /// </summary>
        /// <param name="cosmosettings">the configuration settings for azure cosmos database</param>
        /// <param name="storageConfiguration">the storage configuration for azure blob storage</param>
        public DataRepository(IOptions <AzureCosmosSettings> cosmosettings, IOptions <AzureStorageConfiguration> storageConfiguration)
        {
            var database = new CosmosDatabaseHandler(cosmosettings.Value);

            _client        = database.CreateDatabaseAndCollection(_collectionId);
            _collectionUri = database.CollectionUri;
            Uri databaseUri = database.DatabaseUri;

            _databaseId = database.DatabaseName;

            DocumentCollection documentCollection = database.CreateDocumentCollection(_collectionId, _partitionKey);

            _client.CreateDocumentCollectionIfNotExistsAsync(
                databaseUri,
                documentCollection).GetAwaiter().GetResult();

            _client.OpenAsync();

            _storageConfiguration = storageConfiguration.Value;

            // connect to azure blob storage
            StorageCredentials  storageCredentials = new StorageCredentials(_storageConfiguration.AccountName, _storageConfiguration.AccountKey);
            CloudStorageAccount storageAccount     = new CloudStorageAccount(storageCredentials, true);

            _blobClient = CreateBlobClient(storageCredentials, storageAccount);
            _container  = _blobClient.GetContainerReference(_storageConfiguration.StorageContainer);
        }
예제 #2
0
        protected override void Load(ContainerBuilder builder)
        {
            builder.Register(x =>
            {
                var context = x.Resolve <IComponentContext>();
                var config  = context.Resolve <IConfiguration>();
                return(new AzureCloudStorage(config[ConnectionString]));
            })
            .As <IContentStorage>();

            builder.Register(x =>
            {
                var context = x.Resolve <IComponentContext>();
                var config  = context.Resolve <IConfiguration>();
                var cfg     = new AzureStorageConfiguration(
                    config[ContentStorageHost],
                    config[MessageAttachmentsContainer],
                    config[MemberAvatarsContainer],
                    config[ChannelImagesContainer],
                    config[TempContainerName],
                    Convert.ToInt32(config[MessagePhotoSize]));
                return(cfg);
            }).AsSelf();

            builder.RegisterType <CloudImageProvider>().As <ICloudImageProvider>();
            builder.RegisterType <CloudTokenProvider>().As <ICloudTokenProvider>();
            builder.RegisterType <CloudAttachmentProvider>().As <ICloudAttachmentProvider>();
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataRepository"/> class
        /// </summary>
        /// <param name="sasTokenProvider">A provider that can be asked for SAS tokens.</param>
        /// <param name="cosmosSettings">the configuration settings for azure cosmos database</param>
        /// <param name="storageConfiguration">the storage configuration for azure blob storage</param>
        /// <param name="logger">The logger to use when writing to logs.</param>
        public DataRepository(
            ISasTokenProvider sasTokenProvider,
            IOptions <AzureCosmosSettings> cosmosSettings,
            IOptions <AzureStorageConfiguration> storageConfiguration,
            ILogger <DataRepository> logger)
        {
            _storageConfiguration = storageConfiguration.Value;
            _sasTokenProvider     = sasTokenProvider;
            _logger = logger;

            CosmosDatabaseHandler database = new CosmosDatabaseHandler(cosmosSettings.Value);

            _documentClient = database.CreateDatabaseAndCollection(_collectionId);
            _collectionUri  = database.CollectionUri;
            Uri databaseUri = database.DatabaseUri;

            _databaseId = database.DatabaseName;

            DocumentCollection documentCollection = database.CreateDocumentCollection(_collectionId, _partitionKey);

            _documentClient.CreateDocumentCollectionIfNotExistsAsync(
                databaseUri,
                documentCollection).GetAwaiter().GetResult();

            _documentClient.OpenAsync();
        }
        /// <summary>
        /// Get store from container identifier
        /// </summary>
        /// <param name="containerIdentifier">Container identifier</param>
        /// <returns>CT store</returns>
        public async Task <CTStore> GetStore(ContainerIdentifier containerIdentifier)
        {
            // refuse to provide the store if the version check fails, or if it has not been executed
            if (this.initialized == false)
            {
                return(null);
            }

            ContainerDescriptor containerDescriptor = ContainerTableDescriptorProvider.Containers[containerIdentifier];
            string azureStorageConnectionString     = await this.connectionStringProvider.GetTablesAzureStorageConnectionString(containerDescriptor.AzureStorageInstanceType);

            string redisConnectionString = await this.connectionStringProvider.GetRedisConnectionString(containerDescriptor.RedisInstanceType);

            string uniqueStoreIdentity = string.Join(":", azureStorageConnectionString, redisConnectionString);

            // cachedStoreObjects is a thread-safe dictionary (ConcurrentDictionary). If uniqueStoreIdentity is not present
            // in cachedStoreObects, try adding it. Since GetStore can be called concurrently by
            // different threads, it is possible for two (or more) threads to attempt inserting uniqueStoreIdentity
            // concurrently in the cachedStoreObjects. That's ok, because the call to TryAdd is guaranteed to be thread-safe.
            // One of the threads will not be able to insert (i.e., TryAdd will return false), but the code will happily execute
            // and fall through to the return statement.
            // This code makes no use of locking on the common path (i.e., reads of cachedStoreObjects).
            if (!this.cachedStoreObjects.ContainsKey(uniqueStoreIdentity))
            {
                AzureTableStorage azureTableStorage = new AzureTableStorage(azureStorageConnectionString);
                azureTableStorage.TableRequestOptions = AzureStorageConfiguration.GetTableRequestOptions();
                RedisCache redisCache = new RedisCache(redisConnectionString);

                CTStore store = new CTStore(azureTableStorage, redisCache);
                this.cachedStoreObjects.TryAdd(uniqueStoreIdentity, store);
            }

            return(this.cachedStoreObjects[uniqueStoreIdentity]);
        }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PolicyRepository"/> class
 /// </summary>
 /// <param name="storageConfig">The storage configuration for Azure Blob Storage.</param>
 /// <param name="logger">logger</param>
 public PolicyRepository(
     IOptions <AzureStorageConfiguration> storageConfig,
     ILogger <PolicyRepository> logger)
 {
     _logger        = logger;
     _storageConfig = storageConfig.Value;
 }
예제 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataRepository"/> class
        /// </summary>
        /// <param name="cosmosettings">the configuration settings for azure cosmos database</param>
        /// <param name="storageConfiguration">the storage configuration for azure blob storage</param>
        public DataRepository(IOptions <AzureCosmosSettings> cosmosettings, IOptions <AzureStorageConfiguration> storageConfiguration)
        {
            // Retrieve configuration values from appsettings.json
            _cosmosettings = cosmosettings.Value;
            _client        = new DocumentClient(new Uri(_cosmosettings.EndpointUri), _cosmosettings.PrimaryKey);
            _databaseUri   = UriFactory.CreateDatabaseUri(_cosmosettings.Database);
            _collectionUri = UriFactory.CreateDocumentCollectionUri(_cosmosettings.Database, _cosmosettings.Collection);
            databaseId     = _cosmosettings.Database;
            collectionId   = _cosmosettings.Collection;
            _client.CreateDatabaseIfNotExistsAsync(new Database {
                Id = _cosmosettings.Database
            }).GetAwaiter().GetResult();
            _client.CreateDocumentCollectionIfNotExistsAsync(
                _databaseUri,
                new DocumentCollection {
                Id = _cosmosettings.Collection
            }).GetAwaiter().GetResult();
            _storageConfiguration = storageConfiguration.Value;

            // connect to azure blob storage
            StorageCredentials  storageCredentials = new StorageCredentials(_storageConfiguration.AccountName, _storageConfiguration.AccountKey);
            CloudStorageAccount storageAccount     = new CloudStorageAccount(storageCredentials, true);

            blobClient = CreateBlobClient(storageCredentials, storageAccount);
            container  = blobClient.GetContainerReference(_storageConfiguration.StorageContainer);
        }
예제 #7
0
 public FacePersonController(ILogger <FacePersonController> logger, IFirebaseService firebase, ICognitiveVisionService cognitivevision, IOptions <AzureStorageConfiguration> azureStorage)
 {
     _logger          = logger;
     _cognitivevision = cognitivevision;
     _azureStorage    = azureStorage.Value ?? throw new ArgumentNullException(nameof(azureStorage));
     _firebase        = firebase;
 }
예제 #8
0
        protected BaseCloudProvider(IContentStorage storage, AzureStorageConfiguration configuration)
        {
            Ensure.That(storage).IsNotNull();
            Ensure.That(configuration).IsNotNull();

            Storage       = storage;
            Configuration = configuration;
        }
예제 #9
0
파일: Startup.cs 프로젝트: MatZil/Testing
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager <User> userManager, RoleManager <IdentityRole> roleManager)
        {
            AzureStorageConfiguration.Configure(app.ApplicationServices.GetRequiredService <IConfiguration>());
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseCorsExt();
            app.UseHttpsRedirection();

            app.SetUpStaticFiles(Configuration);
            app.SetUpAzureStorage();
            //app.AddCorsRuleForAzure();

            if (!env.IsDevelopment())
            {
                app.UseSpaStaticFiles();
            }

            app.ConfigureAndUseSwagger();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHealthChecks("/health");
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

            app.UseSpa(spa =>
            {
                // To learn more about options for serving an Angular SPA from ASP.NET Core,
                // see https://go.microsoft.com/fwlink/?linkid=864501

                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseProxyToSpaDevelopmentServer("http://localhost:4200/");
                    // spa.UseAngularCliServer(npmScript: "start");
                }
            });

            IdentityDataSeeder.SeedData(userManager, roleManager, Configuration);
        }
예제 #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PolicyRepository"/> class
        /// </summary>
        /// <param name="storageConfig">The storage configuration for Azure Blob Storage.</param>
        /// <param name="logger">logger</param>
        public PolicyRepository(
            IOptions <AzureStorageConfiguration> storageConfig,
            ILogger <PolicyRepository> logger)
        {
            this.logger = logger;

            _storageConfig = storageConfig.Value;
            SetUpBlobConnection();
        }
        public CustomVisionService(IOptions <AzureStorageConfiguration> azureStorageConfig, ISpectraDatabaseSettings settings, ILogger <CustomVisionService> logger)
        {
            _azureStorageConfig = azureStorageConfig.Value;
            _logger             = logger;

            var client = new MongoClient(settings.ConnectionString);

            _database = client.GetDatabase(settings.DatabaseName);
        }
예제 #12
0
 /// <summary>
 /// Initialize a new instance of the <see cref="SasTokenProvider"/> with the given <see cref="KeyVaultSettings"/>.
 /// </summary>
 /// <param name="keyVaultWrapper">
 /// An instance of <see cref="KeyVaultClientWrapper"/> with a principal with access to the application owner key vault(s).</param>
 /// <param name="storageConfiguration">
 /// The <see cref="AzureStorageConfiguration"/> with information about the key vault and storage account formats.
 /// The formats is used to generate the actual URI and storage account names based on organisation id.
 /// </param>
 /// <param name="logger">A logger that can be used to write to a log.</param>
 public SasTokenProvider(
     IKeyVaultClientWrapper keyVaultWrapper,
     IOptions <AzureStorageConfiguration> storageConfiguration,
     ILogger <SasTokenProvider> logger)
 {
     _keyVaultWrapper      = keyVaultWrapper;
     _storageConfiguration = storageConfiguration.Value;
     _logger = logger;
 }
예제 #13
0
        private BlobClient GetBlobClient(string containerName, string blobName)
        {
            var connectionString  = AzureStorageConfiguration.GetConnectionString();
            var blobServiceClient = new BlobServiceClient(connectionString);
            var containerClient   = blobServiceClient.GetBlobContainerClient(containerName);
            var blobClient        = containerClient.GetBlobClient(blobName);

            return(blobClient);
        }
예제 #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataRepository"/> class
 /// </summary>
 /// <param name="sasTokenProvider">A provider that can be asked for SAS tokens.</param>
 /// <param name="cosmosSettings">the configuration settings for azure cosmos database</param>
 /// <param name="storageConfiguration">the storage configuration for azure blob storage</param>
 /// <param name="logger">The logger to use when writing to logs.</param>
 public DataRepository(
     ISasTokenProvider sasTokenProvider,
     IOptions <AzureCosmosSettings> cosmosSettings,
     IOptions <AzureStorageConfiguration> storageConfiguration,
     ILogger <DataRepository> logger)
     : base(CollectionId, PartitionKey, cosmosSettings)
 {
     _storageConfiguration = storageConfiguration.Value;
     _sasTokenProvider     = sasTokenProvider;
     _logger = logger;
 }
        private async Task <IBlobStorageRepository> SetupRepositoryAsync()
        {
            var storageConfiguration = new AzureStorageConfiguration
            {
                ConnectionString = "UseDevelopmentStorage=true"
            };
            var storageOptions = Options.Create(storageConfiguration);
            var clientFactory  = new AzureStorageClientFactory(storageOptions);
            await clientFactory.CreateBlobContainerIfNotExistsAsync(containerName);

            return(new BlobStorageRepository(clientFactory));
        }
예제 #16
0
        public SasTokenProviderTests()
        {
            _mockLogger = new Mock <ILogger <SasTokenProvider> >();

            AzureStorageConfiguration storageSettings = new AzureStorageConfiguration
            {
                OrgKeyVaultURI    = KeyVaultURI,
                OrgStorageAccount = StorageAccount,
                OrgSasDefinition  = SasDefinition
            };

            _storageConfiguration = new Mock <IOptions <AzureStorageConfiguration> >();
            _storageConfiguration.SetupGet(x => x.Value).Returns(storageSettings);
        }
예제 #17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PolicyRepository"/> class
        /// </summary>
        /// <param name="storageConfig">The storage configuration for Azure Blob Storage.</param>
        /// <param name="logger">logger</param>
        public PolicyRepository(
            IOptions <AzureStorageConfiguration> storageConfig,
            ILogger <PolicyRepository> logger)
        {
            _logger        = logger;
            _storageConfig = storageConfig.Value;

            StorageSharedKeyCredential metadataCredentials   = new StorageSharedKeyCredential(_storageConfig.MetadataAccountName, _storageConfig.MetadataAccountKey);
            BlobServiceClient          metadataServiceClient = new BlobServiceClient(new Uri(_storageConfig.MetadataBlobEndpoint), metadataCredentials);

            _metadataContainerClient = metadataServiceClient.GetBlobContainerClient(_storageConfig.MetadataContainer);

            StorageSharedKeyCredential delegationsCredentials   = new StorageSharedKeyCredential(_storageConfig.DelegationsAccountName, _storageConfig.DelegationsAccountKey);
            BlobServiceClient          delegationsServiceClient = new BlobServiceClient(new Uri(_storageConfig.DelegationsBlobEndpoint), delegationsCredentials);

            _delegationsContainerClient = delegationsServiceClient.GetBlobContainerClient(_storageConfig.DelegationsContainer);
        }
예제 #18
0
        public string GetBlobUrl(string containerName, string blobName)
        {
            var connectionString = AzureStorageConfiguration.GetConnectionString();
            var account          = CloudStorageAccount.Parse(connectionString);
            var blobClient       = account.CreateCloudBlobClient();
            var container        = blobClient.GetContainerReference(containerName);
            var blob             = container.GetBlockBlobReference(blobName);
            var sasToken         = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy
            {
                Permissions            = SharedAccessBlobPermissions.Read,
                SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(5),
            }, new SharedAccessBlobHeaders
            {
                ContentDisposition = $"attachment; filename=\"{blobName}\"",
            });
            var downloadUrl = $"{blob.Uri.AbsoluteUri}{sasToken}";

            return(downloadUrl);
        }
예제 #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataRepository"/> class
 /// </summary>
 /// <param name="cosmosettings">the configuration settings for cosmos database</param>
 /// <param name="storageConfiguration">the storage configuration</param>
 public DataRepository(IOptions <AzureCosmosSettings> cosmosettings, IOptions <AzureStorageConfiguration> storageConfiguration)
 {
     // Retrieve configuration values from appsettings.json
     _cosmosettings = cosmosettings.Value;
     _client        = new DocumentClient(new Uri(_cosmosettings.EndpointUri), _cosmosettings.PrimaryKey);
     _databaseUri   = UriFactory.CreateDatabaseUri(_cosmosettings.Database);
     _collectionUri = UriFactory.CreateDocumentCollectionUri(_cosmosettings.Database, _cosmosettings.Collection);
     databaseId     = _cosmosettings.Database;
     collectionId   = _cosmosettings.Collection;
     _client.CreateDatabaseIfNotExistsAsync(new Database {
         Id = _cosmosettings.Database
     }).GetAwaiter().GetResult();
     _client.CreateDocumentCollectionIfNotExistsAsync(
         _databaseUri,
         new DocumentCollection {
         Id = _cosmosettings.Collection
     }).GetAwaiter().GetResult();
     _storageConfiguration = storageConfiguration.Value;
 }
        public async Task GetSasToken_TokenExpiresBetweenCalls_PerformsTwoCallsToKeyVault()
        {
            // Arrange
            string org = "ttd";
            string uri = string.Format(KeyVaultURI, org);

            string storageAccount = string.Format(StorageAccount, org);
            string sasDefinition  = string.Format(SasDefinition, org);
            string secretName     = $"{storageAccount}-{sasDefinition}";

            Mock <IKeyVaultClientWrapper> keyVaultClient = new Mock <IKeyVaultClientWrapper>();

            keyVaultClient.Setup(s => s.GetSecretAsync(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync("ttdsecret");

            AzureStorageConfiguration storageSettings = new AzureStorageConfiguration
            {
                OrgKeyVaultURI          = KeyVaultURI,
                OrgStorageAccount       = StorageAccount,
                OrgSasDefinition        = SasDefinition,
                AllowedSasTokenAgeHours = 0
            };

            Mock <IOptions <AzureStorageConfiguration> > storageConfiguration = new Mock <IOptions <AzureStorageConfiguration> >();

            storageConfiguration.SetupGet(x => x.Value).Returns(storageSettings);

            SasTokenProvider target = new SasTokenProvider(keyVaultClient.Object, storageConfiguration.Object, _mockLogger.Object);

            // Act
            await target.GetSasToken(org);

            string actual = await target.GetSasToken(org);

            // Assert
            Assert.Equal("ttdsecret", actual);

            keyVaultClient.Verify(s => s.GetSecretAsync(It.Is <string>(u => u == uri), It.Is <string>(i => i == secretName)), Times.Exactly(2));
        }
예제 #21
0
        public void Initialize()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkInMemoryDatabase()
                                  .BuildServiceProvider();

            var options = new DbContextOptionsBuilder <HolidayDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_Database")
                          .UseInternalServiceProvider(serviceProvider)
                          .Options;

            _configuration = GetConfiguration();
            _context       = new HolidayDbContext(options, _configuration);
            Seed(_context);

            var config = new AutoMapper.MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutoMapperConfiguration());
            });

            _mapper = config.CreateMapper();
            AzureStorageConfiguration.Configure(_configuration);
        }
예제 #22
0
 /// <summary>
 /// CTOR
 /// </summary>
 /// <param name="configuration"></param>
 public AzureStorageProvider(AzureStorageConfiguration configuration)
 {
     Configuration = configuration;
 }
예제 #23
0
 public AzureStorageDomainEventsRepository(AzureStorageConfiguration azureStorageConfiguration)
 {
     _azureStorageConfiguration = azureStorageConfiguration;
 }
예제 #24
0
        /// <summary>
        /// Configure data protection on the services collection.
        /// </summary>
        /// <param name="services">The service collections</param>
        /// <param name="isDevelopment">A boolean indicating if the environment is development</param>
        /// <param name="config">Configuration for Azure Storage</param>
        public static void ConfigureDataProtection(this IServiceCollection services, bool isDevelopment, AzureStorageConfiguration config)
        {
            if (isDevelopment)
            {
                services.AddDataProtection()
                .PersistKeysToFileSystem(FileSystemXmlRepository.DefaultKeyStorageDirectory);
            }
            else
            {
                StorageSharedKeyCredential keysCredentials = new StorageSharedKeyCredential(config.KeysAccountName, config.KeysAccountKey);
                Uri uri = new Uri($"{config.KeysBlobEndpoint}{config.KeysContainer}");
                BlobContainerClient container = new BlobContainerClient(uri, keysCredentials);
                BlobClient          client    = container.GetBlobClient(_blobName);

                services.AddDataProtection()
                .PersistKeysToAzureBlobStorage(client);
            }
        }
예제 #25
0
 public AzureFileStorage(IOptions <AzureStorageConfiguration> storageConfiguration)
 {
     _storageConfiguration = storageConfiguration.Value;
 }
 public AzureStorageService(AzureStorageConfiguration config)
 {
     _config = config;
 }
예제 #27
0
        private static BlobServiceClient GetBlobServiceClient()
        {
            var connectionString = AzureStorageConfiguration.GetConnectionString();

            return(new BlobServiceClient(connectionString));
        }
예제 #28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DelegationChangeEventQueue"/> class.
 /// </summary>
 /// <param name="eventMapperService">Mapper service responsible for mapping models</param>
 /// <param name="storageConfig">Azure storage queue config</param>
 public DelegationChangeEventQueue(IEventMapperService eventMapperService, IOptions <AzureStorageConfiguration> storageConfig)
 {
     _eventMapperService = eventMapperService;
     _storageConfig      = storageConfig.Value;
 }
예제 #29
0
 public CloudTokenProvider(IContentStorage contentStorage, AzureStorageConfiguration storageConfiguration)
     : base(contentStorage, storageConfiguration)
 {
 }
예제 #30
0
 public CloudAttachmentProvider(IContentStorage storage, AzureStorageConfiguration configuration)
     : base(storage, configuration)
 {
 }