Commit 27c042be authored by Spencer Sutton's avatar Spencer Sutton
Browse files

Adding caching and using ssm prefix instead of tenant id

parent 31897e54
Pipeline #70790 passed with stage
in 37 seconds
......@@ -22,6 +22,7 @@ from osdu.core.api.storage.tenant import Tenant
import boto3
from datetime import datetime, timedelta, timezone
from botocore.exceptions import ClientError, WaiterError
from functools import lru_cache
from osdu_api.clients.partition.partition_client import PartitionClient
from osdu_api.providers.aws.partition_info_aws import PartitionInfoAws
......@@ -123,10 +124,12 @@ class AwsStorage(BlobStorageBase):
]}
return UploadPolicy
def getTenantId(self, data_partition_id: str) -> str:
# https://docs.python.org/3.4/library/functools.html#functools.lru_cache
@lru_cache(maxsize=256)
def getTenantSSMPrefix(self, data_partition_id: str) -> str:
partition_client = PartitionClient(data_partition_id=data_partition_id)
partition_info_aws = partition_client.get_partition(data_partition_id)
return partition_info_aws.tenant_id
return partition_info_aws.tenant_ssm_prefix
async def getCredentials(self, tenant: Tenant):
if len(self._session_token) > 2 and datetime.now(timezone.utc) < self._timestamp:
......@@ -134,13 +137,13 @@ class AwsStorage(BlobStorageBase):
""" get credentials to access s3 bucket for tenant's folder """
# all tenants in one bucket(self._bucket_name). Each tenant.bucket_name is in fact, a folder in self._bucket_name
folder = tenant.bucket_name
tenant_id = self.getTenantId(tenant.data_partition_id)
tenant_ssm_prefix = self.getTenantSSMPrefix(tenant.data_partition_id)
ssm = boto3.client('ssm', region_name=self._region)
if(self._bucket_name == ""):
# get bucket name from ssm
response = ssm.get_parameter(
Name=f'/osdu/{self._aws_env}/{tenant_id}/wellbore-ddms/WellboreDDMSBucket/name',
Name=f'{tenant_ssm_prefix}/wellbore-ddms/WellboreDDMSBucket/name',
WithDecryption=True)
self._bucket_name = response['Parameter']['Value']
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment