Skip to content

Commit

Permalink
Merge pull request #90 from ONLYOFFICE/feature/dynamo-table
Browse files Browse the repository at this point in the history
DynamoDb: tableName to config
  • Loading branch information
pavelbannov authored Nov 27, 2023
2 parents ed445bd + c859a2a commit 1f9e189
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions web/ASC.Web.Core/Helpers/ApiSystemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,30 @@
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;


namespace ASC.Web.Core.Helpers;

[Scope]
public class ApiSystemHelper
{
public string ApiSystemUrl { get; }
public bool ApiCacheEnable { get; }

public bool ApiCacheEnable
{
get => _dynamoDbSettings.ApiCacheEnable;
}

private readonly byte[] _skey;
private readonly CommonLinkUtility _commonLinkUtility;
private readonly IHttpClientFactory _clientFactory;
private readonly TenantDomainValidator _tenantDomainValidator;
private readonly CoreBaseSettings _coreBaseSettings;
private readonly IConfiguration _configuration;
private readonly DynamoDbSettings _dynamoDbSettings;
private const string TenantRegionKey = "tenant_region";
private const string TenantDomainKey = "tenant_domain";
private const string RegionTableName = "docspace-tenants_region";
private readonly string _regionTableName;

public ApiSystemHelper(IConfiguration configuration,
public ApiSystemHelper(
ConfigurationExtension configuration,
CoreBaseSettings coreBaseSettings,
CommonLinkUtility commonLinkUtility,
MachinePseudoKeys machinePseudoKeys,
Expand All @@ -59,16 +63,11 @@ public ApiSystemHelper(IConfiguration configuration,
ApiSystemUrl = configuration["web:api-system"];
_commonLinkUtility = commonLinkUtility;
_skey = machinePseudoKeys.GetMachineConstant();
_configuration = configuration;
_clientFactory = clientFactory;
_tenantDomainValidator = tenantDomainValidator;
_coreBaseSettings = coreBaseSettings;

if (!String.IsNullOrEmpty(_configuration["aws:dynamoDB:accessKeyId"]) &&
!String.IsNullOrEmpty(_configuration["aws:dynamoDB:secretAccessKey"]))
{
ApiCacheEnable = true;
}
_dynamoDbSettings = configuration.GetSetting<DynamoDbSettings>("aws:dynamoDB");
_regionTableName = !string.IsNullOrEmpty(_dynamoDbSettings.TableName) ? _dynamoDbSettings.TableName: "docspace-tenants_region";
}

public string CreateAuthToken(string pkey)
Expand Down Expand Up @@ -119,7 +118,7 @@ public async Task AddTenantToCacheAsync(string tenantDomain, string tenantRegion

var putItemRequest = new PutItemRequest
{
TableName = RegionTableName,
TableName = _regionTableName,
Item = new Dictionary<string, AttributeValue>
{
{ TenantDomainKey, new AttributeValue
Expand All @@ -144,7 +143,7 @@ public async Task UpdateTenantToCacheAsync(string oldTenantDomain, string newTen

var getItemRequest = new GetItemRequest
{
TableName = RegionTableName,
TableName = _regionTableName,
Key = new Dictionary<string, AttributeValue>
{
{ TenantDomainKey, new AttributeValue { S = oldTenantDomain } }
Expand All @@ -165,7 +164,7 @@ public async Task RemoveTenantFromCacheAsync(string tenantDomain)

var request = new DeleteItemRequest
{
TableName = RegionTableName,
TableName = _regionTableName,
Key = new Dictionary<string, AttributeValue>
{
{ TenantDomainKey, new AttributeValue { S = tenantDomain } }
Expand All @@ -183,7 +182,7 @@ public async Task<IEnumerable<string>> FindTenantsInCacheAsync(string portalName

var getItemRequest = new GetItemRequest
{
TableName = RegionTableName,
TableName = _regionTableName,
Key = new Dictionary<string, AttributeValue>
{
{ TenantDomainKey, new AttributeValue { S = tenantDomain } }
Expand Down Expand Up @@ -211,7 +210,7 @@ public async Task<IEnumerable<string>> FindTenantsInCacheAsync(string portalName

var scanRequest = new ScanRequest
{
TableName = RegionTableName,
TableName = _regionTableName,
FilterExpression = "begins_with(tenant_domain, :v_tenant_domain)",
ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
{":v_tenant_domain", new AttributeValue { S = portalName }} },
Expand All @@ -225,13 +224,10 @@ public async Task<IEnumerable<string>> FindTenantsInCacheAsync(string portalName
}

#endregion

private AmazonDynamoDBClient GetDynamoDBClient()
{
var awsAccessKeyId = _configuration["aws:dynamoDB:accessKeyId"];
var awsSecretAccessKey = _configuration["aws:dynamoDB:secretAccessKey"];
var region = _configuration["aws:dynamoDB:region"];

return new AmazonDynamoDBClient(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.GetBySystemName(region));
return new AmazonDynamoDBClient(_dynamoDbSettings.AccessKeyId, _dynamoDbSettings.SecretAccessKey, RegionEndpoint.GetBySystemName(_dynamoDbSettings.Region));
}

private async Task<string> SendToApiAsync(string absoluteApiUrl, string apiPath, string httpMethod, Guid userId, string data = null)
Expand Down Expand Up @@ -264,3 +260,14 @@ private async Task<string> SendToApiAsync(string absoluteApiUrl, string apiPath,
return await reader.ReadToEndAsync();
}
}

public class DynamoDbSettings
{
public string AccessKeyId { get; set; }
public string SecretAccessKey { get; set; }
public string Region { get; set; }
public string TableName { get; set; }

public bool ApiCacheEnable => !String.IsNullOrEmpty(AccessKeyId) &&
!String.IsNullOrEmpty(SecretAccessKey);
}

0 comments on commit 1f9e189

Please sign in to comment.