Skip to content

Commit

Permalink
Refactor OpenStackClient cache key generation and storage
Browse files Browse the repository at this point in the history
This commit refactors the cache key generation and storage in the OpenStackClient class. The cache key is now generated using a combination of the authentication method, identity endpoint, port, domain, username, project ID, and region. The cache key is used to store authentication information in the cache. Additionally, the token validity is calculated and stored in the cache. This improves the caching mechanism and ensures that the token remains valid for the appropriate duration.

This miscalculation, in conjuntion with storing cached credentials WITHOUT a catalog, leads to failure
  • Loading branch information
dkmstr committed Oct 9, 2024
1 parent ed94992 commit 0e3120f
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions server/src/uds/services/OpenStack/openstack/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def __init__(
self._identity_endpoint += '/'

self.cache = cache.Cache(
f'openstack_{identity_endpoint}_{port}_{domain}_{username}_{projectid}_{region}'
f'openstack_{self._auth_method}{identity_endpoint}{port}{domain}{username}{projectid}{region}'
)

def _get_endpoints_for(self, *endpoint_types: str) -> collections.abc.Generator[str, None, None]:
Expand Down Expand Up @@ -395,19 +395,6 @@ def authenticate(self) -> None:
self._authenticated_projectid = self._projectid = token['project']['id']
self._project_name = token['project'].get('name', self._projectid)

# For cache, we store the token validity, minus 60 seconds t
validity = (
dateutil.parser.parse(token['expires_at']).replace(tzinfo=None)
- dateutil.parser.parse(token['issued_at']).replace(tzinfo=None)
).seconds - 60
self.cache.set(
'auth',
(self._authenticated_projectid, self._projectid, self._tokenid, self._userid, self._catalog),
validity,
)

# logger.debug('The token {} will be valid for {}'.format(self._tokenId, validity))

# Now, if endpoints are present (only if tenant was specified), store them
if self._projectid is not None:
self._catalog = token['catalog']
Expand All @@ -421,6 +408,20 @@ def authenticate(self) -> None:
# else:
# 'volumev3', 'volumev2' = 'volumev2'

# For cache, we store the token validity, minus 60 seconds t
validity = (
dateutil.parser.parse(token['expires_at']).replace(tzinfo=None)
- dateutil.parser.parse(token['issued_at']).replace(tzinfo=None)
).seconds - 60
self.cache.set(
'auth',
(self._authenticated_projectid, self._projectid, self._tokenid, self._userid, self._catalog),
validity,
)

# logger.debug('The token {} will be valid for {}'.format(self._tokenId, validity))


def ensure_authenticated(self) -> None:
if self._authenticated is False or self._projectid != self._authenticated_projectid:
self.authenticate()
Expand Down Expand Up @@ -461,7 +462,7 @@ def list_regions(self) -> list[openstack_types.RegionInfo]:
)
]

@decorators.cached(prefix='svrs', timeout=consts.cache.DEFAULT_CACHE_TIMEOUT, key_helper=cache_key_helper)
@decorators.cached(prefix='svrs', timeout=consts.cache.SHORT_CACHE_TIMEOUT, key_helper=cache_key_helper)
def list_servers(
self,
detail: bool = False,
Expand Down

0 comments on commit 0e3120f

Please sign in to comment.