mirror of
https://github.com/dkmstr/openuds.git
synced 2025-01-10 01:17:59 +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:
parent
4fea6e6a3e
commit
cfdf622447
@ -35,6 +35,8 @@ import contextlib
|
|||||||
import logging
|
import logging
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
from uds.core.services.generics import exceptions as gen_exceptions
|
from uds.core.services.generics import exceptions as gen_exceptions
|
||||||
|
|
||||||
from tests.utils import vars, helpers
|
from tests.utils import vars, helpers
|
||||||
@ -417,3 +419,16 @@ class TestOpenStackClient(UDSTransactionTestCase):
|
|||||||
# wait for server to be running
|
# wait for server to be running
|
||||||
self.wait_for_server(res)
|
self.wait_for_server(res)
|
||||||
self.oclient.delete_server(res.id)
|
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()
|
||||||
|
@ -198,7 +198,7 @@ def get_certificates_from_field(
|
|||||||
|
|
||||||
# Timeout
|
# Timeout
|
||||||
def timeout_field(
|
def timeout_field(
|
||||||
default: int = 5,
|
default: int = 8,
|
||||||
order: int = 90,
|
order: int = 90,
|
||||||
tab: 'types.ui.Tab|str|None' = types.ui.Tab.ADVANCED,
|
tab: 'types.ui.Tab|str|None' = types.ui.Tab.ADVANCED,
|
||||||
old_field_name: typing.Optional[str] = None,
|
old_field_name: typing.Optional[str] = None,
|
||||||
|
@ -36,6 +36,8 @@ import json
|
|||||||
import typing
|
import typing
|
||||||
import collections.abc
|
import collections.abc
|
||||||
|
|
||||||
|
import dateutil.parser
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
@ -318,6 +320,11 @@ class OpenStackClient: # pylint: disable=too-many-public-methods
|
|||||||
|
|
||||||
def authenticate(self) -> None:
|
def authenticate(self) -> None:
|
||||||
# logger.debug('Authenticating...')
|
# 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]
|
data: dict[str, typing.Any]
|
||||||
if self._auth_method == openstack_types.AuthMethod.APPLICATION_CREDENTIAL:
|
if self._auth_method == openstack_types.AuthMethod.APPLICATION_CREDENTIAL:
|
||||||
data = {
|
data = {
|
||||||
@ -376,7 +383,10 @@ class OpenStackClient: # pylint: disable=too-many-public-methods
|
|||||||
token = r.json()['token']
|
token = r.json()['token']
|
||||||
# logger.debug('Got token {}'.format(token))
|
# logger.debug('Got token {}'.format(token))
|
||||||
self._userid = token['user']['id']
|
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))
|
# 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
|
# Low cache, simple to avoid non needed requests
|
||||||
@decorators.cached(prefix='ava', timeout=4, key_helper=cache_key_helper)
|
@decorators.cached(prefix='ava', timeout=4, key_helper=cache_key_helper)
|
||||||
|
@auth_required()
|
||||||
def is_available(self) -> bool:
|
def is_available(self) -> bool:
|
||||||
try:
|
try:
|
||||||
# If we can connect, it is available
|
# If we can connect, it is available
|
||||||
|
Loading…
Reference in New Issue
Block a user