Skip to content

Commit

Permalink
Change credential cache directory
Browse files Browse the repository at this point in the history
  • Loading branch information
snigdhasjg committed Nov 28, 2023
1 parent d049983 commit d7bfe94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
20 changes: 13 additions & 7 deletions aws_fusion/aws/assume_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import logging
import json
import os

from botocore.exceptions import ClientError
from botocore.utils import JSONFileCache
Expand All @@ -12,7 +13,8 @@


class AssumeRoleWithSamlCache:
__jsonFileCache = JSONFileCache()
__CACHE_DIR = os.path.expanduser(os.path.join('~', '.aws', 'saml', 'cache'))
__jsonFileCache = JSONFileCache(__CACHE_DIR)

def __init__(self, role) -> None:
LOG.debug('Initialize AssumeRoleWithSamlCache')
Expand All @@ -23,10 +25,13 @@ def __init__(self, role) -> None:
def does_valid_token_cache_exists(self):
if self.__cache_key in self.__jsonFileCache:
response = self.__jsonFileCache[self.__cache_key]
expiration = datetime.datetime.strptime(response['Credentials']['Expiration'], '%Y-%m-%dT%H:%M:%S%Z')
current_utc_time = datetime.datetime.utcnow()
expiration = (datetime.datetime
.strptime(response['Credentials']['Expiration'], '%Y-%m-%dT%H:%M:%S%Z')
.replace(tzinfo=datetime.timezone.utc))
current_utc_time = datetime.datetime.now(tz=datetime.timezone.utc)

if expiration - current_utc_time >= datetime.timedelta(minutes=1):
response['Credentials']['Expiration'] = expiration.replace(tzinfo=datetime.timezone.utc)
response['Credentials']['Expiration'] = expiration
self.__response = response
LOG.debug('Valid token exists. Can use cache')
return True
Expand All @@ -42,9 +47,10 @@ def credential_process(self):
"Version": 1,
"AccessKeyId": credentials['AccessKeyId'],
"SecretAccessKey": credentials['SecretAccessKey'],
"SessionToken": credentials['SessionToken']
"SessionToken": credentials['SessionToken'],
"Expiration": credentials['Expiration'].isoformat()
})

def environment_variable(self):
credentials = self.__response['Credentials']
LOG.debug(f'Giving credential as environment variable format')
Expand All @@ -59,6 +65,7 @@ def environment_variable(self):

def assume_role_with_saml(self, saml_response, roles, session_duration):
LOG.debug(f'Started assuming role with SAML')
# Just need a dummy sts session client object to call assume role with saml
client = boto3.Session(aws_access_key_id='dummy', aws_secret_access_key='dummy').client('sts')
selected_role = self.__role
try:
Expand All @@ -82,4 +89,3 @@ def assume_role_with_saml(self, saml_response, roles, session_duration):
self.__jsonFileCache[self.__cache_key] = response

self.__response = response

5 changes: 4 additions & 1 deletion aws_fusion/aws/session.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import boto3
import logging
import os

from botocore.utils import JSONFileCache

LOG = logging.getLogger(__name__)


class TokenGenerationException(Exception):
"""Exception for credential not having token"""
pass
Expand All @@ -26,9 +28,10 @@ def credentials(profile_name, region_name):

def __update_credential_provider_cache(session):
"""Setting up a custom cache implementation like aws cli"""
cache_dir = os.path.expanduser(os.path.join('~', '.aws', 'cli', 'cache'))

cred_chain = session._session.get_component('credential_provider')
json_file_cache = JSONFileCache()
json_file_cache = JSONFileCache(cache_dir)

def _update(provider_name):
cred_chain.get_provider(provider_name).cache = json_file_cache
Expand Down

0 comments on commit d7bfe94

Please sign in to comment.