1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-08 21:18:00 +03:00

chore: Update default timeout value to 8 seconds an added cached validity to openstack auth to avoid multple auths if posssible

This commit is contained in:
Adolfo Gómez García 2024-07-18 16:39:09 +02:00
parent 4fea6e6a3e
commit cfdf622447
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
3 changed files with 29 additions and 3 deletions

View File

@ -35,6 +35,8 @@ import contextlib
import logging
import typing
from unittest import mock
from uds.core.services.generics import exceptions as gen_exceptions
from tests.utils import vars, helpers
@ -417,3 +419,16 @@ class TestOpenStackClient(UDSTransactionTestCase):
# wait for server to be running
self.wait_for_server(res)
self.oclient.delete_server(res.id)
def test_auth_cached(self) -> None:
# Get a new client, it should be cached
cached_value = self.oclient.cache.get('auth')
# Unauthorized
self.oclient._authenticated = False
with mock.patch.object(self.oclient.cache, 'get', return_value=cached_value) as mock_cache_get:
# Session should not be used
with mock.patch.object(self.oclient, '_session') as mock_session:
self.assertTrue(self.oclient.is_available())
mock_cache_get.assert_called_once()
mock_session.assert_not_called()

View File

@ -198,7 +198,7 @@ def get_certificates_from_field(
# Timeout
def timeout_field(
default: int = 5,
default: int = 8,
order: int = 90,
tab: 'types.ui.Tab|str|None' = types.ui.Tab.ADVANCED,
old_field_name: typing.Optional[str] = None,

View File

@ -36,6 +36,8 @@ import json
import typing
import collections.abc
import dateutil.parser
import requests
from django.utils.translation import gettext as _
@ -318,6 +320,11 @@ class OpenStackClient: # pylint: disable=too-many-public-methods
def authenticate(self) -> None:
# logger.debug('Authenticating...')
# If credential is cached, use it instead of requesting it again
if (cached_creds := self.cache.get('auth')) != None:
self._authenticated_projectid, self._tokenid, self._userid, self._catalog = cached_creds
return
data: dict[str, typing.Any]
if self._auth_method == openstack_types.AuthMethod.APPLICATION_CREDENTIAL:
data = {
@ -376,7 +383,10 @@ class OpenStackClient: # pylint: disable=too-many-public-methods
token = r.json()['token']
# logger.debug('Got token {}'.format(token))
self._userid = token['user']['id']
# validity = (dateutil.parser.parse(token['expires_at']).replace(tzinfo=None) - dateutil.parser.parse(token['issued_at']).replace(tzinfo=None)).seconds - 60
# 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.put('auth', (self._authenticated_projectid, self._tokenid, self._userid, self._catalog), validity)
# logger.debug('The token {} will be valid for {}'.format(self._tokenId, validity))
@ -771,6 +781,7 @@ class OpenStackClient: # pylint: disable=too-many-public-methods
# Low cache, simple to avoid non needed requests
@decorators.cached(prefix='ava', timeout=4, key_helper=cache_key_helper)
@auth_required()
def is_available(self) -> bool:
try:
# If we can connect, it is available