diff --git a/framework/src/Volo.Abp.BlobStoring.Aws/Volo.Abp.BlobStoring.Aws.csproj b/framework/src/Volo.Abp.BlobStoring.Aws/Volo.Abp.BlobStoring.Aws.csproj index d30601d1e8..814158b064 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aws/Volo.Abp.BlobStoring.Aws.csproj +++ b/framework/src/Volo.Abp.BlobStoring.Aws/Volo.Abp.BlobStoring.Aws.csproj @@ -19,6 +19,8 @@ + + diff --git a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsModule.cs b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsModule.cs index 84a18a5ca9..6f2e5dc330 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsModule.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsModule.cs @@ -1,4 +1,5 @@ -using Volo.Abp.Caching; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Caching; using Volo.Abp.Modularity; namespace Volo.Abp.BlobStoring.Aws @@ -7,6 +8,9 @@ namespace Volo.Abp.BlobStoring.Aws typeof(AbpCachingModule))] public class AbpBlobStoringAwsModule : AbpModule { - + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddDataProtection(); + } } } diff --git a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfiguration.cs b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfiguration.cs index 62695610ff..c2d3297f4e 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfiguration.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfiguration.cs @@ -1,7 +1,4 @@ -using Amazon; -using Amazon.Runtime; -using Amazon.Runtime.CredentialManagement; -using Amazon.S3; +using System; namespace Volo.Abp.BlobStoring.Aws { @@ -97,11 +94,19 @@ namespace Volo.Abp.BlobStoring.Aws set => _containerConfiguration.SetConfiguration(AwsBlobProviderConfigurationNames.CreateContainerIfNotExists, value); } + private readonly string _temporaryCredentialsCacheKey; + public string TemporaryCredentialsCacheKey + { + get => _containerConfiguration.GetConfigurationOrDefault(AwsBlobProviderConfigurationNames.TemporaryCredentialsCacheKey, _temporaryCredentialsCacheKey); + set => _containerConfiguration.SetConfiguration(AwsBlobProviderConfigurationNames.TemporaryCredentialsCacheKey, value); + } + private readonly BlobContainerConfiguration _containerConfiguration; public AwsBlobProviderConfiguration(BlobContainerConfiguration containerConfiguration) { _containerConfiguration = containerConfiguration; + _temporaryCredentialsCacheKey = Guid.NewGuid().ToString("N"); } } } diff --git a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfigurationNames.cs b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfigurationNames.cs index 600aab5bd7..24dfca53d2 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfigurationNames.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProviderConfigurationNames.cs @@ -10,6 +10,7 @@ public const string ProfileName = "Aws.ProfileName"; public const string ProfilesLocation = "Aws.ProfilesLocation"; public const string DurationSeconds = "Aws.DurationSeconds"; + public const string TemporaryCredentialsCacheKey = "Aws.TemporaryCredentialsCacheKey"; public const string Name = "Aws.Name"; public const string Policy = "Aws.Policy"; public const string Region = "Aws.Region"; diff --git a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsTemporaryCredentialsCacheItem.cs b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsTemporaryCredentialsCacheItem.cs index d45cb2d958..887acddc5d 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsTemporaryCredentialsCacheItem.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsTemporaryCredentialsCacheItem.cs @@ -5,8 +5,6 @@ namespace Volo.Abp.BlobStoring.Aws [Serializable] public class AwsTemporaryCredentialsCacheItem { - public const string Key = "AwsBlobTemporaryCredentialsCache"; - public string AccessKeyId { get; set; } public string SecretAccessKey { get; set; } diff --git a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/DefaultAmazonS3ClientFactory.cs b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/DefaultAmazonS3ClientFactory.cs index 7ae6e4c004..717ef2e05d 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/DefaultAmazonS3ClientFactory.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/DefaultAmazonS3ClientFactory.cs @@ -6,6 +6,7 @@ using Amazon.Runtime.CredentialManagement; using Amazon.S3; using Amazon.SecurityToken; using Amazon.SecurityToken.Model; +using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; @@ -16,9 +17,13 @@ namespace Volo.Abp.BlobStoring.Aws { protected IDistributedCache Cache { get; } - public DefaultAmazonS3ClientFactory(IDistributedCache cache) + protected IDataProtector DataProtector { get; } + + public DefaultAmazonS3ClientFactory(IDistributedCache cache, + IDataProtectionProvider dataProtectionProvider) { Cache = cache; + DataProtector = dataProtectionProvider.CreateProtector(nameof(AwsTemporaryCredentialsCacheItem)); } public virtual async Task GetAmazonS3Client( @@ -72,7 +77,7 @@ namespace Volo.Abp.BlobStoring.Aws protected virtual async Task GetTemporaryCredentialsAsync( AwsBlobProviderConfiguration configuration) { - var temporaryCredentialsCache = await Cache.GetAsync(AwsTemporaryCredentialsCacheItem.Key); + var temporaryCredentialsCache = await Cache.GetAsync(configuration.TemporaryCredentialsCacheKey); if (temporaryCredentialsCache == null) { @@ -104,14 +109,14 @@ namespace Volo.Abp.BlobStoring.Aws var credentials = sessionTokenResponse.Credentials; temporaryCredentialsCache = - await SetTemporaryCredentialsCache(credentials, configuration.DurationSeconds); + await SetTemporaryCredentialsCache(configuration, credentials); } } var sessionCredentials = new SessionAWSCredentials( - temporaryCredentialsCache.AccessKeyId, - temporaryCredentialsCache.SecretAccessKey, - temporaryCredentialsCache.SessionToken); + DataProtector.Unprotect(temporaryCredentialsCache.AccessKeyId), + DataProtector.Unprotect(temporaryCredentialsCache.SecretAccessKey), + DataProtector.Unprotect(temporaryCredentialsCache.SessionToken)); return sessionCredentials; } @@ -121,7 +126,7 @@ namespace Volo.Abp.BlobStoring.Aws Check.NotNullOrWhiteSpace(configuration.Name, nameof(configuration.Name)); Check.NotNullOrWhiteSpace(configuration.Policy, nameof(configuration.Policy)); - var temporaryCredentialsCache = await Cache.GetAsync(AwsTemporaryCredentialsCacheItem.Key); + var temporaryCredentialsCache = await Cache.GetAsync(configuration.TemporaryCredentialsCacheKey); if (temporaryCredentialsCache == null) { @@ -155,29 +160,30 @@ namespace Volo.Abp.BlobStoring.Aws var credentials = federationTokenResponse.Credentials; temporaryCredentialsCache = - await SetTemporaryCredentialsCache(credentials, configuration.DurationSeconds); + await SetTemporaryCredentialsCache(configuration, credentials); } } var sessionCredentials = new SessionAWSCredentials( - temporaryCredentialsCache.AccessKeyId, - temporaryCredentialsCache.SecretAccessKey, - temporaryCredentialsCache.SessionToken); + DataProtector.Unprotect(temporaryCredentialsCache.AccessKeyId), + DataProtector.Unprotect(temporaryCredentialsCache.SecretAccessKey), + DataProtector.Unprotect(temporaryCredentialsCache.SessionToken)); return sessionCredentials; } private async Task SetTemporaryCredentialsCache( - Credentials credentials, - int durationSeconds) + AwsBlobProviderConfiguration configuration, + Credentials credentials) { - var temporaryCredentialsCache = new AwsTemporaryCredentialsCacheItem(credentials.AccessKeyId, - credentials.SecretAccessKey, - credentials.SessionToken); + var temporaryCredentialsCache = new AwsTemporaryCredentialsCacheItem( + DataProtector.Protect(credentials.AccessKeyId), + DataProtector.Protect(credentials.SecretAccessKey), + DataProtector.Protect(credentials.SessionToken)); - await Cache.SetAsync(AwsTemporaryCredentialsCacheItem.Key, temporaryCredentialsCache, + await Cache.SetAsync(configuration.TemporaryCredentialsCacheKey, temporaryCredentialsCache, new DistributedCacheEntryOptions { - AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(durationSeconds - 10) + AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(configuration.DurationSeconds - 10) }); return temporaryCredentialsCache; diff --git a/framework/test/Volo.Abp.BlobStoring.Aws.Tests/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsTestModule.cs b/framework/test/Volo.Abp.BlobStoring.Aws.Tests/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsTestModule.cs index dc9dda1b76..d176a864e7 100644 --- a/framework/test/Volo.Abp.BlobStoring.Aws.Tests/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsTestModule.cs +++ b/framework/test/Volo.Abp.BlobStoring.Aws.Tests/Volo/Abp/BlobStoring/Aws/AbpBlobStoringAwsTestModule.cs @@ -54,6 +54,7 @@ namespace Volo.Abp.BlobStoring.Aws aws.SecretAccessKey = secretAccessKey; aws.Region = region; aws.CreateContainerIfNotExists = true; + aws.ContainerName = _randomContainerName; _configuration = aws; });