maleo-foundation 0.2.15__py3-none-any.whl → 0.2.17__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- maleo_foundation/client/services/encryption/aes.py +41 -11
- maleo_foundation/client/services/encryption/rsa.py +59 -16
- maleo_foundation/client/services/hash/bcrypt.py +24 -9
- maleo_foundation/client/services/hash/hmac.py +26 -8
- maleo_foundation/client/services/hash/sha256.py +16 -6
- maleo_foundation/client/services/key.py +34 -12
- maleo_foundation/client/services/signature.py +45 -12
- maleo_foundation/client/services/token.py +68 -16
- maleo_foundation/expanded_types/client.py +6 -3
- maleo_foundation/expanded_types/encryption/aes.py +2 -1
- maleo_foundation/expanded_types/encryption/rsa.py +2 -1
- maleo_foundation/expanded_types/general.py +2 -1
- maleo_foundation/expanded_types/hash.py +2 -1
- maleo_foundation/expanded_types/key.py +2 -1
- maleo_foundation/expanded_types/query.py +6 -3
- maleo_foundation/expanded_types/service.py +6 -3
- maleo_foundation/expanded_types/signature.py +2 -1
- maleo_foundation/expanded_types/token.py +2 -1
- maleo_foundation/managers/client/base.py +5 -1
- maleo_foundation/managers/client/google/storage.py +5 -1
- maleo_foundation/managers/client/maleo.py +20 -4
- maleo_foundation/managers/db.py +21 -6
- maleo_foundation/managers/service.py +108 -23
- maleo_foundation/middlewares/authentication.py +35 -8
- maleo_foundation/middlewares/base.py +53 -15
- maleo_foundation/models/schemas/general.py +3 -0
- maleo_foundation/models/table.py +21 -4
- maleo_foundation/models/transfers/parameters/service.py +30 -4
- maleo_foundation/utils/controller.py +23 -6
- maleo_foundation/utils/dependencies/auth.py +14 -4
- maleo_foundation/utils/exceptions.py +21 -6
- maleo_foundation/utils/loaders/credential/google.py +3 -1
- maleo_foundation/utils/query.py +38 -7
- {maleo_foundation-0.2.15.dist-info → maleo_foundation-0.2.17.dist-info}/METADATA +1 -1
- {maleo_foundation-0.2.15.dist-info → maleo_foundation-0.2.17.dist-info}/RECORD +37 -37
- {maleo_foundation-0.2.15.dist-info → maleo_foundation-0.2.17.dist-info}/WHEEL +0 -0
- {maleo_foundation-0.2.15.dist-info → maleo_foundation-0.2.17.dist-info}/top_level.txt +0 -0
@@ -1,16 +1,24 @@
|
|
1
1
|
import jwt
|
2
2
|
from maleo_foundation.enums import BaseEnums
|
3
|
-
from maleo_foundation.expanded_types.token
|
3
|
+
from maleo_foundation.expanded_types.token \
|
4
|
+
import MaleoFoundationTokenResultsTypes
|
4
5
|
from maleo_foundation.managers.client.base import ClientService
|
5
|
-
from maleo_foundation.models.schemas.token
|
6
|
-
|
7
|
-
from maleo_foundation.models.transfers.
|
8
|
-
|
6
|
+
from maleo_foundation.models.schemas.token \
|
7
|
+
import MaleoFoundationTokenSchemas
|
8
|
+
from maleo_foundation.models.transfers.general.token \
|
9
|
+
import MaleoFoundationTokenGeneralTransfers
|
10
|
+
from maleo_foundation.models.transfers.parameters.token \
|
11
|
+
import MaleoFoundationTokenParametersTransfers
|
12
|
+
from maleo_foundation.models.transfers.results.token \
|
13
|
+
import MaleoFoundationTokenResultsTransfers
|
9
14
|
from maleo_foundation.utils.exceptions import BaseExceptions
|
10
15
|
from maleo_foundation.utils.loaders.key.rsa import RSAKeyLoader
|
11
16
|
|
12
17
|
class MaleoFoundationTokenClientService(ClientService):
|
13
|
-
def encode(
|
18
|
+
def encode(
|
19
|
+
self,
|
20
|
+
parameters:MaleoFoundationTokenParametersTransfers.Encode
|
21
|
+
) -> MaleoFoundationTokenResultsTypes.Encode:
|
14
22
|
@BaseExceptions.service_exception_handler(
|
15
23
|
operation="encoding a payload into a token",
|
16
24
|
logger=self._logger,
|
@@ -18,25 +26,54 @@ class MaleoFoundationTokenClientService(ClientService):
|
|
18
26
|
)
|
19
27
|
def _impl():
|
20
28
|
try:
|
21
|
-
private_key = RSAKeyLoader.load_with_pycryptodome(
|
29
|
+
private_key = RSAKeyLoader.load_with_pycryptodome(
|
30
|
+
type=BaseEnums.KeyType.PRIVATE,
|
31
|
+
extern_key=parameters.key,
|
32
|
+
passphrase=parameters.password
|
33
|
+
)
|
22
34
|
except TypeError:
|
23
35
|
message = "Invalid key type"
|
24
36
|
description = "A private key must be used for payload encoding"
|
25
37
|
other = "Ensure the given key is of type private key"
|
26
|
-
return MaleoFoundationTokenResultsTransfers.Fail(
|
38
|
+
return MaleoFoundationTokenResultsTransfers.Fail(
|
39
|
+
message=message,
|
40
|
+
description=description,
|
41
|
+
other=other
|
42
|
+
)
|
27
43
|
except Exception as e:
|
28
44
|
self._logger.error("Unexpected error occured while trying to import key:\n'%s'", str(e), exc_info=True)
|
29
45
|
message = "Invalid key"
|
30
46
|
description = "Unexpected error occured while trying to import key"
|
31
47
|
other = "Ensure given key is valid"
|
32
|
-
return MaleoFoundationTokenResultsTransfers.Fail(
|
33
|
-
|
34
|
-
|
48
|
+
return MaleoFoundationTokenResultsTransfers.Fail(
|
49
|
+
message=message,
|
50
|
+
description=description,
|
51
|
+
other=other
|
52
|
+
)
|
53
|
+
payload = (
|
54
|
+
MaleoFoundationTokenGeneralTransfers
|
55
|
+
.EncodePayload
|
56
|
+
.model_validate(
|
57
|
+
parameters.payload.model_dump()
|
58
|
+
)
|
59
|
+
.model_dump(
|
60
|
+
mode="json",
|
61
|
+
exclude_none=True
|
62
|
+
)
|
63
|
+
)
|
64
|
+
token = jwt.encode(
|
65
|
+
payload=payload,
|
66
|
+
key=private_key.export_key(),
|
67
|
+
algorithm="RS256"
|
68
|
+
)
|
35
69
|
data = MaleoFoundationTokenSchemas.Token(token=token)
|
36
70
|
return MaleoFoundationTokenResultsTransfers.Encode(data=data)
|
37
71
|
return _impl()
|
38
72
|
|
39
|
-
def decode(
|
73
|
+
def decode(
|
74
|
+
self,
|
75
|
+
parameters:MaleoFoundationTokenParametersTransfers.Decode
|
76
|
+
) -> MaleoFoundationTokenResultsTypes.Decode:
|
40
77
|
@BaseExceptions.service_exception_handler(
|
41
78
|
operation="decoding a token into a payload",
|
42
79
|
logger=self._logger,
|
@@ -44,19 +81,34 @@ class MaleoFoundationTokenClientService(ClientService):
|
|
44
81
|
)
|
45
82
|
def _impl():
|
46
83
|
try:
|
47
|
-
public_key = RSAKeyLoader.load_with_pycryptodome(
|
84
|
+
public_key = RSAKeyLoader.load_with_pycryptodome(
|
85
|
+
type=BaseEnums.KeyType.PUBLIC,
|
86
|
+
extern_key=parameters.key
|
87
|
+
)
|
48
88
|
except TypeError:
|
49
89
|
message = "Invalid key type"
|
50
90
|
description = "A public key must be used for token decoding"
|
51
91
|
other = "Ensure the given key is of type public key"
|
52
|
-
return MaleoFoundationTokenResultsTransfers.Fail(
|
92
|
+
return MaleoFoundationTokenResultsTransfers.Fail(
|
93
|
+
message=message,
|
94
|
+
description=description,
|
95
|
+
other=other
|
96
|
+
)
|
53
97
|
except Exception as e:
|
54
98
|
self._logger.error("Unexpected error occured while trying to import key:\n'%s'", str(e), exc_info=True)
|
55
99
|
message = "Invalid key"
|
56
100
|
description = "Unexpected error occured while trying to import key"
|
57
101
|
other = "Ensure given key is valid"
|
58
|
-
return MaleoFoundationTokenResultsTransfers.Fail(
|
59
|
-
|
102
|
+
return MaleoFoundationTokenResultsTransfers.Fail(
|
103
|
+
message=message,
|
104
|
+
description=description,
|
105
|
+
other=other
|
106
|
+
)
|
107
|
+
payload = jwt.decode(
|
108
|
+
jwt=parameters.token,
|
109
|
+
key=public_key.export_key(),
|
110
|
+
algorithms=["RS256"]
|
111
|
+
)
|
60
112
|
data = MaleoFoundationTokenGeneralTransfers.DecodePayload.model_validate(payload)
|
61
113
|
return MaleoFoundationTokenResultsTransfers.Decode(data=data)
|
62
114
|
return _impl()
|
@@ -1,7 +1,10 @@
|
|
1
1
|
from typing import Awaitable, Callable, Union
|
2
|
-
from maleo_foundation.models.transfers.parameters.general
|
3
|
-
|
4
|
-
from maleo_foundation.models.transfers.
|
2
|
+
from maleo_foundation.models.transfers.parameters.general \
|
3
|
+
import BaseGeneralParametersTransfers
|
4
|
+
from maleo_foundation.models.transfers.parameters.client \
|
5
|
+
import BaseClientParametersTransfers
|
6
|
+
from maleo_foundation.models.transfers.results.client.service \
|
7
|
+
import BaseClientServiceResultsTransfers
|
5
8
|
|
6
9
|
class ExpandedClientTypes:
|
7
10
|
#* Unpaginated multiple data
|
@@ -1,5 +1,6 @@
|
|
1
1
|
from typing import Union
|
2
|
-
from maleo_foundation.models.transfers.results.encryption.aes
|
2
|
+
from maleo_foundation.models.transfers.results.encryption.aes \
|
3
|
+
import MaleoFoundationAESEncryptionResultsTransfers
|
3
4
|
|
4
5
|
class MaleoFoundationAESEncryptionResultsTypes:
|
5
6
|
Encrypt = Union[
|
@@ -1,5 +1,6 @@
|
|
1
1
|
from typing import Union
|
2
|
-
from maleo_foundation.models.transfers.results.encryption.rsa
|
2
|
+
from maleo_foundation.models.transfers.results.encryption.rsa \
|
3
|
+
import MaleoFoundationRSAEncryptionResultsTransfers
|
3
4
|
|
4
5
|
class MaleoFoundationRSAEncryptionResultsTypes:
|
5
6
|
Encrypt = Union[
|
@@ -1,6 +1,7 @@
|
|
1
1
|
from typing import Callable, List, Optional
|
2
2
|
from maleo_foundation.types import BaseTypes
|
3
|
-
from maleo_foundation.models.transfers.parameters.general
|
3
|
+
from maleo_foundation.models.transfers.parameters.general \
|
4
|
+
import BaseGeneralParametersTransfers
|
4
5
|
|
5
6
|
class BaseGeneralExpandedTypes:
|
6
7
|
#* Expansion processor related types
|
@@ -1,5 +1,6 @@
|
|
1
1
|
from typing import Union
|
2
|
-
from maleo_foundation.models.transfers.results.hash
|
2
|
+
from maleo_foundation.models.transfers.results.hash \
|
3
|
+
import MaleoFoundationHashResultsTransfers
|
3
4
|
|
4
5
|
class MaleoFoundationHashResultsTypes:
|
5
6
|
Hash = Union[
|
@@ -1,5 +1,6 @@
|
|
1
1
|
from typing import Union
|
2
|
-
from maleo_foundation.models.transfers.results.key
|
2
|
+
from maleo_foundation.models.transfers.results.key \
|
3
|
+
import MaleoFoundationKeyResultsTransfers
|
3
4
|
|
4
5
|
class MaleoFoundationKeyResultsTypes:
|
5
6
|
CreatePrivate = Union[
|
@@ -1,7 +1,10 @@
|
|
1
1
|
from typing import Awaitable, Callable, Union
|
2
|
-
from maleo_foundation.models.transfers.parameters.general
|
3
|
-
|
4
|
-
from maleo_foundation.models.transfers.
|
2
|
+
from maleo_foundation.models.transfers.parameters.general \
|
3
|
+
import BaseGeneralParametersTransfers
|
4
|
+
from maleo_foundation.models.transfers.parameters.service \
|
5
|
+
import BaseServiceParametersTransfers
|
6
|
+
from maleo_foundation.models.transfers.results.service.query \
|
7
|
+
import BaseServiceQueryResultsTransfers
|
5
8
|
|
6
9
|
class ExpandedQueryTypes:
|
7
10
|
#* Unpaginated multiple data
|
@@ -1,7 +1,10 @@
|
|
1
1
|
from typing import Awaitable, Callable, Union
|
2
|
-
from maleo_foundation.models.transfers.parameters.general
|
3
|
-
|
4
|
-
from maleo_foundation.models.transfers.
|
2
|
+
from maleo_foundation.models.transfers.parameters.general \
|
3
|
+
import BaseGeneralParametersTransfers
|
4
|
+
from maleo_foundation.models.transfers.parameters.service \
|
5
|
+
import BaseServiceParametersTransfers
|
6
|
+
from maleo_foundation.models.transfers.results.service.general \
|
7
|
+
import BaseServiceGeneralResultsTransfers
|
5
8
|
|
6
9
|
class ExpandedServiceTypes:
|
7
10
|
#* Unpaginated multiple data
|
@@ -1,5 +1,6 @@
|
|
1
1
|
from typing import Union
|
2
|
-
from maleo_foundation.models.transfers.results.signature
|
2
|
+
from maleo_foundation.models.transfers.results.signature \
|
3
|
+
import MaleoFoundationSignatureResultsTransfers
|
3
4
|
|
4
5
|
class MaleoFoundationSignatureResultsTypes:
|
5
6
|
Sign = Union[
|
@@ -1,5 +1,6 @@
|
|
1
1
|
from typing import Union
|
2
|
-
from maleo_foundation.models.transfers.results.token
|
2
|
+
from maleo_foundation.models.transfers.results.token \
|
3
|
+
import MaleoFoundationTokenResultsTransfers
|
3
4
|
|
4
5
|
class MaleoFoundationTokenResultsTypes:
|
5
6
|
Encode = Union[
|
@@ -115,7 +115,11 @@ class ClientManager:
|
|
115
115
|
self._logger.info("Initializing client manager")
|
116
116
|
|
117
117
|
def _initialize_logger(self) -> None:
|
118
|
-
self._logger = ClientLogger(
|
118
|
+
self._logger = ClientLogger(
|
119
|
+
client_key=self._key,
|
120
|
+
service_key=self._service_key,
|
121
|
+
**self._log_config.model_dump()
|
122
|
+
)
|
119
123
|
|
120
124
|
@property
|
121
125
|
def key(self) -> str:
|
@@ -64,5 +64,9 @@ class GoogleCloudStorage(GoogleClientManager):
|
|
64
64
|
if not blob.exists():
|
65
65
|
raise ValueError(f"File '{location}' did not exists.")
|
66
66
|
|
67
|
-
url = blob.generate_signed_url(
|
67
|
+
url = blob.generate_signed_url(
|
68
|
+
version="v4",
|
69
|
+
expiration=timedelta(minutes=15),
|
70
|
+
method="GET"
|
71
|
+
)
|
68
72
|
return url
|
@@ -1,9 +1,20 @@
|
|
1
|
-
from pydantic import
|
2
|
-
from maleo_foundation.managers.client.base import
|
1
|
+
from pydantic import Field
|
2
|
+
from maleo_foundation.managers.client.base import (
|
3
|
+
ClientManager,
|
4
|
+
ClientHTTPControllerManager,
|
5
|
+
ClientControllerManagers,
|
6
|
+
ClientHTTPController,
|
7
|
+
ClientServiceControllers,
|
8
|
+
ClientControllers
|
9
|
+
)
|
3
10
|
from maleo_foundation.managers.service import ServiceManager
|
4
11
|
|
5
12
|
class MaleoClientHTTPController(ClientHTTPController):
|
6
|
-
def __init__(
|
13
|
+
def __init__(
|
14
|
+
self,
|
15
|
+
service_manager:ServiceManager,
|
16
|
+
manager:ClientHTTPControllerManager
|
17
|
+
):
|
7
18
|
self._service_manager = service_manager
|
8
19
|
super().__init__(manager)
|
9
20
|
|
@@ -27,7 +38,12 @@ class MaleoClientManager(ClientManager):
|
|
27
38
|
):
|
28
39
|
self._url = url
|
29
40
|
self._service_manager = service_manager
|
30
|
-
super().__init__(
|
41
|
+
super().__init__(
|
42
|
+
key,
|
43
|
+
name,
|
44
|
+
service_manager.log_config,
|
45
|
+
service_manager.configs.service.key
|
46
|
+
)
|
31
47
|
|
32
48
|
@property
|
33
49
|
def service_manager(self) -> ServiceManager:
|
maleo_foundation/managers/db.py
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
import os
|
2
2
|
from contextlib import contextmanager
|
3
|
-
from pydantic import BaseModel, Field
|
3
|
+
from pydantic import BaseModel, Field
|
4
4
|
from sqlalchemy import MetaData
|
5
5
|
from sqlalchemy.engine import Engine, create_engine
|
6
6
|
from sqlalchemy.exc import SQLAlchemyError
|
7
7
|
from sqlalchemy.ext.declarative import DeclarativeMeta
|
8
8
|
from sqlalchemy.orm import sessionmaker, Session, declarative_base
|
9
|
-
from typing import
|
9
|
+
from typing import Generator
|
10
10
|
from maleo_foundation.types import BaseTypes
|
11
11
|
from maleo_foundation.utils.logging import ServiceLogger
|
12
12
|
|
@@ -15,10 +15,17 @@ class MetadataManager:
|
|
15
15
|
metadata:MetaData = Base.metadata
|
16
16
|
|
17
17
|
class SessionManager:
|
18
|
-
def __init__(
|
18
|
+
def __init__(
|
19
|
+
self,
|
20
|
+
logger:ServiceLogger,
|
21
|
+
engine:Engine
|
22
|
+
):
|
19
23
|
self._logger = logger
|
20
24
|
self._logger.info("Initializing SessionMaker")
|
21
|
-
self._sessionmaker:sessionmaker[Session] = sessionmaker(
|
25
|
+
self._sessionmaker:sessionmaker[Session] = sessionmaker(
|
26
|
+
bind=engine,
|
27
|
+
expire_on_commit=False
|
28
|
+
)
|
22
29
|
self._logger.info("SessionMaker initialized successfully")
|
23
30
|
|
24
31
|
def _session_handler(self) -> Generator[Session, None, None]:
|
@@ -84,7 +91,11 @@ class DatabaseManager:
|
|
84
91
|
if url is None:
|
85
92
|
raise ValueError("DB_URL environment variable must be set if url is not provided")
|
86
93
|
self._logger.info("Creating SQlAlchemy engine")
|
87
|
-
self._engine = create_engine(
|
94
|
+
self._engine = create_engine(
|
95
|
+
url=url,
|
96
|
+
echo=False,
|
97
|
+
pool_pre_ping=True,
|
98
|
+
pool_recycle=3600)
|
88
99
|
self._logger.info("SQlAlchemy engine created successfully")
|
89
100
|
|
90
101
|
#* Creating all table from metadata
|
@@ -94,7 +105,11 @@ class DatabaseManager:
|
|
94
105
|
|
95
106
|
#* Initializing session manager
|
96
107
|
self._logger.info("Initializing session manager")
|
97
|
-
|
108
|
+
#* Create session
|
109
|
+
self._session = SessionManager(
|
110
|
+
logger=self._logger,
|
111
|
+
engine=self._engine
|
112
|
+
)
|
98
113
|
self._logger.info("Session manager initialized successfully")
|
99
114
|
|
100
115
|
@property
|
@@ -11,8 +11,10 @@ from typing import Optional
|
|
11
11
|
from maleo_foundation.client.manager import MaleoFoundationClientManager
|
12
12
|
from maleo_foundation.enums import BaseEnums
|
13
13
|
from maleo_foundation.models.schemas.general import BaseGeneralSchemas
|
14
|
-
from maleo_foundation.models.transfers.general.token
|
15
|
-
|
14
|
+
from maleo_foundation.models.transfers.general.token \
|
15
|
+
import MaleoFoundationTokenGeneralTransfers
|
16
|
+
from maleo_foundation.models.transfers.parameters.token \
|
17
|
+
import MaleoFoundationTokenParametersTransfers
|
16
18
|
from maleo_foundation.managers.db import DatabaseConfigurations, DatabaseManager
|
17
19
|
from maleo_foundation.managers.client.google.secret import GoogleSecretManager
|
18
20
|
from maleo_foundation.managers.client.google.storage import GoogleCloudStorage
|
@@ -27,7 +29,11 @@ from maleo_foundation.managers.middleware import (
|
|
27
29
|
from maleo_foundation.types import BaseTypes
|
28
30
|
from maleo_foundation.utils.exceptions import BaseExceptions
|
29
31
|
from maleo_foundation.utils.loaders.yaml import YAMLLoader
|
30
|
-
from maleo_foundation.utils.logging import
|
32
|
+
from maleo_foundation.utils.logging import (
|
33
|
+
SimpleConfig,
|
34
|
+
ServiceLogger,
|
35
|
+
MiddlewareLogger
|
36
|
+
)
|
31
37
|
from maleo_foundation.utils.mergers import BaseMergers
|
32
38
|
|
33
39
|
class Settings(BaseSettings):
|
@@ -161,27 +167,49 @@ class ServiceManager:
|
|
161
167
|
return self._google_credentials
|
162
168
|
|
163
169
|
def _initialize_secret_manager(self) -> None:
|
164
|
-
self._secret_manager = GoogleSecretManager(
|
170
|
+
self._secret_manager = GoogleSecretManager(
|
171
|
+
log_config=self._log_config,
|
172
|
+
service_key=self._settings.SERVICE_KEY,
|
173
|
+
credentials=self._google_credentials
|
174
|
+
)
|
165
175
|
|
166
176
|
@property
|
167
177
|
def secret_manager(self) -> GoogleSecretManager:
|
168
178
|
return self._secret_manager
|
169
179
|
|
170
180
|
def _initialize_cloud_storage(self) -> None:
|
171
|
-
environment =
|
172
|
-
|
181
|
+
environment = (
|
182
|
+
BaseEnums.EnvironmentType.STAGING
|
183
|
+
if self._settings.ENVIRONMENT == BaseEnums.EnvironmentType.LOCAL
|
184
|
+
else self._settings.ENVIRONMENT
|
185
|
+
)
|
186
|
+
self._cloud_storage = GoogleCloudStorage(
|
187
|
+
log_config=self._log_config,
|
188
|
+
service_key=self._settings.SERVICE_KEY,
|
189
|
+
bucket_name=f"maleo-suite-{environment}",
|
190
|
+
credentials=self._google_credentials
|
191
|
+
)
|
173
192
|
|
174
193
|
@property
|
175
194
|
def cloud_storage(self) -> GoogleCloudStorage:
|
176
195
|
return self._cloud_storage
|
177
196
|
|
178
197
|
def _load_maleo_credentials(self) -> None:
|
179
|
-
environment =
|
198
|
+
environment = (
|
199
|
+
BaseEnums.EnvironmentType.STAGING
|
200
|
+
if self._settings.ENVIRONMENT == BaseEnums.EnvironmentType.LOCAL
|
201
|
+
else self._settings.ENVIRONMENT
|
202
|
+
)
|
180
203
|
id = int(self._secret_manager.get(f"maleo-service-account-id-{environment}"))
|
181
204
|
email = self._secret_manager.get("maleo-service-account-email")
|
182
205
|
username = self._secret_manager.get("maleo-service-account-username")
|
183
206
|
password = self._secret_manager.get("maleo-service-account-password")
|
184
|
-
self._maleo_credentials = MaleoCredentials(
|
207
|
+
self._maleo_credentials = MaleoCredentials(
|
208
|
+
id=id,
|
209
|
+
username=username,
|
210
|
+
email=email,
|
211
|
+
password=password
|
212
|
+
)
|
185
213
|
|
186
214
|
@property
|
187
215
|
def maleo_credentials(self) -> MaleoCredentials:
|
@@ -209,10 +237,18 @@ class ServiceManager:
|
|
209
237
|
#* Load database configurations
|
210
238
|
password = self._secret_manager.get(name=f"maleo-db-password-{self._settings.ENVIRONMENT}")
|
211
239
|
host = self._secret_manager.get(name=f"maleo-db-host-{self._settings.ENVIRONMENT}")
|
212
|
-
database = DatabaseConfigurations(
|
240
|
+
database = DatabaseConfigurations(
|
241
|
+
password=password,
|
242
|
+
host=host,
|
243
|
+
database=runtime_configs.database
|
244
|
+
)
|
213
245
|
|
214
246
|
#* Load whole configurations
|
215
|
-
merged_configs = BaseMergers.deep_merge(
|
247
|
+
merged_configs = BaseMergers.deep_merge(
|
248
|
+
static_configs.model_dump(),
|
249
|
+
runtime_configs.model_dump(exclude={"database"}),
|
250
|
+
{"database": database.model_dump()}
|
251
|
+
)
|
216
252
|
self._configs = Configurations.model_validate(merged_configs)
|
217
253
|
|
218
254
|
@property
|
@@ -223,7 +259,11 @@ class ServiceManager:
|
|
223
259
|
password = self._secret_manager.get(name="maleo-key-password")
|
224
260
|
private = self._secret_manager.get(name="maleo-private-key")
|
225
261
|
public = self._secret_manager.get(name="maleo-public-key")
|
226
|
-
self._keys = BaseGeneralSchemas.RSAKeys(
|
262
|
+
self._keys = BaseGeneralSchemas.RSAKeys(
|
263
|
+
password=password,
|
264
|
+
private=private,
|
265
|
+
public=public
|
266
|
+
)
|
227
267
|
|
228
268
|
@property
|
229
269
|
def keys(self) -> BaseGeneralSchemas.RSAKeys:
|
@@ -231,27 +271,54 @@ class ServiceManager:
|
|
231
271
|
|
232
272
|
def _initialize_loggers(self) -> None:
|
233
273
|
#* Service's loggers
|
234
|
-
application = ServiceLogger(
|
235
|
-
|
274
|
+
application = ServiceLogger(
|
275
|
+
type=BaseEnums.LoggerType.APPLICATION,
|
276
|
+
service_key=self._configs.service.key,
|
277
|
+
**self._log_config.model_dump()
|
278
|
+
)
|
279
|
+
database = ServiceLogger(
|
280
|
+
type=BaseEnums.LoggerType.DATABASE,
|
281
|
+
service_key=self._configs.service.key,
|
282
|
+
**self._log_config.model_dump()
|
283
|
+
)
|
236
284
|
#* Middleware's loggers
|
237
|
-
base = MiddlewareLogger(
|
238
|
-
|
285
|
+
base = MiddlewareLogger(
|
286
|
+
middleware_type=BaseEnums.MiddlewareLoggerType.BASE,
|
287
|
+
service_key=self._configs.service.key,
|
288
|
+
**self._log_config.model_dump()
|
289
|
+
)
|
290
|
+
authentication = MiddlewareLogger(
|
291
|
+
middleware_type=BaseEnums.MiddlewareLoggerType.AUTHENTICATION,
|
292
|
+
service_key=self._configs.service.key,
|
293
|
+
**self._log_config.model_dump()
|
294
|
+
)
|
239
295
|
middleware = MiddlewareLoggers(base=base, authentication=authentication)
|
240
|
-
self._loggers = Loggers(
|
296
|
+
self._loggers = Loggers(
|
297
|
+
application=application,
|
298
|
+
database=database,
|
299
|
+
middleware=middleware
|
300
|
+
)
|
241
301
|
|
242
302
|
@property
|
243
303
|
def loggers(self) -> Loggers:
|
244
304
|
return self._loggers
|
245
305
|
|
246
306
|
def _initialize_db(self) -> None:
|
247
|
-
self._database = DatabaseManager(
|
307
|
+
self._database = DatabaseManager(
|
308
|
+
metadata=self._db_metadata,
|
309
|
+
logger=self._loggers.database,
|
310
|
+
url=self._configs.database.url
|
311
|
+
)
|
248
312
|
|
249
313
|
@property
|
250
314
|
def database(self) -> DatabaseManager:
|
251
315
|
return self._database
|
252
316
|
|
253
317
|
def _initialize_foundation(self) -> None:
|
254
|
-
self._foundation = MaleoFoundationClientManager(
|
318
|
+
self._foundation = MaleoFoundationClientManager(
|
319
|
+
log_config=self._log_config,
|
320
|
+
service_key=self._settings.SERVICE_KEY
|
321
|
+
)
|
255
322
|
|
256
323
|
@property
|
257
324
|
def foundation(self) -> MaleoFoundationClientManager:
|
@@ -268,14 +335,26 @@ class ServiceManager:
|
|
268
335
|
u_ut="service",
|
269
336
|
exp_in=1
|
270
337
|
)
|
271
|
-
parameters = MaleoFoundationTokenParametersTransfers.Encode(
|
338
|
+
parameters = MaleoFoundationTokenParametersTransfers.Encode(
|
339
|
+
key=self._keys.private,
|
340
|
+
password=self._keys.password,
|
341
|
+
payload=payload
|
342
|
+
)
|
272
343
|
result = self._foundation.services.token.encode(parameters=parameters)
|
273
344
|
return result.data.token if result.success else None
|
274
345
|
|
275
|
-
def create_app(
|
346
|
+
def create_app(
|
347
|
+
self,
|
348
|
+
router:APIRouter,
|
349
|
+
lifespan:Optional[Lifespan[AppType]]=None
|
350
|
+
) -> FastAPI:
|
276
351
|
self._loggers.application.info("Creating FastAPI application")
|
277
352
|
root_path = "" if self._settings.ENVIRONMENT == "local" else f"/{self._configs.service.key.removeprefix("maleo-")}"
|
278
|
-
self._app = FastAPI(
|
353
|
+
self._app = FastAPI(
|
354
|
+
title=self._configs.service.name,
|
355
|
+
lifespan=lifespan,
|
356
|
+
root_path=root_path
|
357
|
+
)
|
279
358
|
self._loggers.application.info("FastAPI application created successfully")
|
280
359
|
|
281
360
|
#* Add middleware(s)
|
@@ -292,8 +371,14 @@ class ServiceManager:
|
|
292
371
|
|
293
372
|
#* Add exception handler(s)
|
294
373
|
self._loggers.application.info("Adding exception handlers")
|
295
|
-
self._app.add_exception_handler(
|
296
|
-
|
374
|
+
self._app.add_exception_handler(
|
375
|
+
exc_class_or_status_code=RequestValidationError,
|
376
|
+
handler=BaseExceptions.validation_exception_handler
|
377
|
+
)
|
378
|
+
self._app.add_exception_handler(
|
379
|
+
exc_class_or_status_code=HTTPException,
|
380
|
+
handler=BaseExceptions.http_exception_handler
|
381
|
+
)
|
297
382
|
self._loggers.application.info("Exception handlers addedd successfully")
|
298
383
|
|
299
384
|
#* Include router
|