partition_info.py 1.21 KB
Newer Older
1
import logging
Luc Yriarte's avatar
Luc Yriarte committed
2
3
from typing import Optional

4

Luc Yriarte's avatar
Luc Yriarte committed
5
6
7
8
9
10
from azure.keyvault.secrets import SecretClient

from osdu_az import conf
from osdu_az.identity.az_identity import AzureIdentity


11
12
_LOGGER = logging.getLogger(__name__)

Luc Yriarte's avatar
Luc Yriarte committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class PartitionInfo():
    def __init__(self, partition_properties: dict = None):
        self._partition_properties = partition_properties
        self._secret_client = None

    def get_value(self, property_name: str) -> Optional[str]:
        partition_property = self._partition_properties.get(property_name)
        if not partition_property:
            return None

        if not partition_property.get('sensitive'):
            return partition_property['value']

        secret = self._get_secret(partition_property['value'])
        return secret.value

    def _get_secret(self, key: str):
30
31
32
33
        ts = datetime.utcnow()
        secret = self._get_secret_client().get_secret(key)
        _LOGGER.info(f'PartitionInfo get secret took {(datetime.utcnow() - ts).total_seconds()} ms')
        return secret
Luc Yriarte's avatar
Luc Yriarte committed
34
35
36
37
38
39

    def _get_secret_client(self):
        if not self._secret_client:
            self._secret_client = SecretClient(conf.keyvault_url, AzureIdentity.get_default_credential())
        return self._secret_client