vism-lib 0.1.1__tar.gz
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.
- vism_lib-0.1.1/LICENSE +21 -0
- vism_lib-0.1.1/PKG-INFO +45 -0
- vism_lib-0.1.1/README.md +1 -0
- vism_lib-0.1.1/pyproject.toml +36 -0
- vism_lib-0.1.1/setup.cfg +4 -0
- vism_lib-0.1.1/src/vism_lib/__init__.py +0 -0
- vism_lib-0.1.1/src/vism_lib/config.py +72 -0
- vism_lib-0.1.1/src/vism_lib/controller.py +53 -0
- vism_lib-0.1.1/src/vism_lib/database.py +189 -0
- vism_lib-0.1.1/src/vism_lib/errors.py +63 -0
- vism_lib-0.1.1/src/vism_lib/logs.py +194 -0
- vism_lib-0.1.1/src/vism_lib/s3.py +80 -0
- vism_lib-0.1.1/src/vism_lib/util.py +79 -0
- vism_lib-0.1.1/src/vism_lib.egg-info/PKG-INFO +45 -0
- vism_lib-0.1.1/src/vism_lib.egg-info/SOURCES.txt +16 -0
- vism_lib-0.1.1/src/vism_lib.egg-info/dependency_links.txt +1 -0
- vism_lib-0.1.1/src/vism_lib.egg-info/requires.txt +4 -0
- vism_lib-0.1.1/src/vism_lib.egg-info/top_level.txt +1 -0
vism_lib-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Author Name
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
vism_lib-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vism_lib
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Library for vism CA and ACME
|
|
5
|
+
Author-email: atrivisc <atrivisc.shawl130@passmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Author Name
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/atrivisc/vism-lib
|
|
29
|
+
Project-URL: Repository, https://github.com/atrivisc/vism-lib.git
|
|
30
|
+
Project-URL: Bug Tracker, https://github.com/atrivisc/vism-lib/issues
|
|
31
|
+
Keywords: vism
|
|
32
|
+
Classifier: Programming Language :: Python :: 3
|
|
33
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
34
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
35
|
+
Classifier: Operating System :: OS Independent
|
|
36
|
+
Requires-Python: >=3.14
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
License-File: LICENSE
|
|
39
|
+
Requires-Dist: python-pkcs11>=0.9.3
|
|
40
|
+
Requires-Dist: yaml-pydantic-dataclass>=0.1.1
|
|
41
|
+
Requires-Dist: pydantic>=2.12.5
|
|
42
|
+
Requires-Dist: starlette>=0.52.1
|
|
43
|
+
Dynamic: license-file
|
|
44
|
+
|
|
45
|
+
# vism-lib
|
vism_lib-0.1.1/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# vism-lib
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "vism_lib"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="atrivisc", email="atrivisc.shawl130@passmail.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "Library for vism CA and ACME"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.14"
|
|
14
|
+
license = { file = "LICENSE" }
|
|
15
|
+
keywords = ["vism"]
|
|
16
|
+
dependencies = [
|
|
17
|
+
"python-pkcs11>=0.9.3",
|
|
18
|
+
"yaml-pydantic-dataclass>=0.1.1",
|
|
19
|
+
"pydantic>=2.12.5",
|
|
20
|
+
"starlette>=0.52.1"
|
|
21
|
+
|
|
22
|
+
]
|
|
23
|
+
classifiers = [
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
26
|
+
"License :: OSI Approved :: MIT License",
|
|
27
|
+
"Operating System :: OS Independent",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
"Homepage" = "https://github.com/atrivisc/vism-lib"
|
|
32
|
+
"Repository" = "https://github.com/atrivisc/vism-lib.git"
|
|
33
|
+
"Bug Tracker" = "https://github.com/atrivisc/vism-lib/issues"
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.packages.find]
|
|
36
|
+
where = ["src"]
|
vism_lib-0.1.1/setup.cfg
ADDED
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Shared configuration classes for VISM components."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from dataclasses import field
|
|
5
|
+
from pydantic import field_validator
|
|
6
|
+
from pydantic.dataclasses import dataclass
|
|
7
|
+
from yaml_dataclass import YamlConfigCached
|
|
8
|
+
|
|
9
|
+
from lib.logs import LoggingConfig
|
|
10
|
+
|
|
11
|
+
shared_logger = logging.getLogger("vism_shared")
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class DatabaseConfig:
|
|
15
|
+
"""Database connection configuration."""
|
|
16
|
+
|
|
17
|
+
data_validation_key: str
|
|
18
|
+
host: str
|
|
19
|
+
port: int
|
|
20
|
+
database: str
|
|
21
|
+
username: str
|
|
22
|
+
password: str
|
|
23
|
+
driver: str = "postgresql+psycopg2"
|
|
24
|
+
|
|
25
|
+
@field_validator("host")
|
|
26
|
+
@classmethod
|
|
27
|
+
def host_must_be_valid(cls, v):
|
|
28
|
+
if not v:
|
|
29
|
+
raise ValueError("database host can not be empty.")
|
|
30
|
+
|
|
31
|
+
return v
|
|
32
|
+
|
|
33
|
+
@field_validator("port")
|
|
34
|
+
@classmethod
|
|
35
|
+
def port_must_be_valid(cls, v):
|
|
36
|
+
"""Validate that port is in valid range."""
|
|
37
|
+
if v < 1 or v > 65535:
|
|
38
|
+
raise ValueError("Port must be between 1 and 65535")
|
|
39
|
+
return v
|
|
40
|
+
|
|
41
|
+
@dataclass
|
|
42
|
+
class DataExchange:
|
|
43
|
+
"""Configuration for data exchange module."""
|
|
44
|
+
|
|
45
|
+
module: str
|
|
46
|
+
validation_key: str = None
|
|
47
|
+
|
|
48
|
+
@dataclass
|
|
49
|
+
class Security:
|
|
50
|
+
"""Security configuration including validation and encryption."""
|
|
51
|
+
|
|
52
|
+
data_exchange: DataExchange = None
|
|
53
|
+
chroot_base_dir: str = None
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class S3Config:
|
|
57
|
+
"""Configuration for s3."""
|
|
58
|
+
|
|
59
|
+
bucket: str
|
|
60
|
+
endpoint: str
|
|
61
|
+
access_key: str
|
|
62
|
+
secret_key: str
|
|
63
|
+
region: str = ""
|
|
64
|
+
|
|
65
|
+
@dataclass
|
|
66
|
+
class VismConfig(YamlConfigCached):
|
|
67
|
+
"""Base configuration class for VISM components."""
|
|
68
|
+
|
|
69
|
+
security: Security = field(default_factory=Security)
|
|
70
|
+
logging: LoggingConfig = field(default_factory=LoggingConfig)
|
|
71
|
+
database: DatabaseConfig = None
|
|
72
|
+
s3: S3Config = None
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Base controller class for VISM components."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
|
|
5
|
+
from yaml_dataclass import YamlConfigCached
|
|
6
|
+
|
|
7
|
+
from lib.config import shared_logger, DataExchange
|
|
8
|
+
from lib.database import VismDatabase
|
|
9
|
+
from lib.logs import setup_logger, SensitiveDataFilter
|
|
10
|
+
from lib.s3 import AsyncS3Client
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Controller:
|
|
14
|
+
"""Base controller class for managing modules and configuration."""
|
|
15
|
+
|
|
16
|
+
configClass = YamlConfigCached
|
|
17
|
+
databaseClass = VismDatabase
|
|
18
|
+
|
|
19
|
+
def __init__(self):
|
|
20
|
+
self.config = self.configClass.read_config()
|
|
21
|
+
self.setup_logging()
|
|
22
|
+
self.database = self.databaseClass(self.config.database)
|
|
23
|
+
self.s3 = AsyncS3Client(self.config.s3)
|
|
24
|
+
self.data_exchange_module = None
|
|
25
|
+
|
|
26
|
+
self._shutdown_event = asyncio.Event()
|
|
27
|
+
|
|
28
|
+
def __post_init__(self):
|
|
29
|
+
self.setup_logging()
|
|
30
|
+
|
|
31
|
+
def setup_logging(self):
|
|
32
|
+
"""Set up logging configuration."""
|
|
33
|
+
shared_logger.info("Setting up logging")
|
|
34
|
+
setup_logger(self.config.logging)
|
|
35
|
+
|
|
36
|
+
def shutdown(self):
|
|
37
|
+
"""Initiates shutdown of the CA."""
|
|
38
|
+
shared_logger.info("Received shutdown signal, shutting down")
|
|
39
|
+
self._shutdown_event.set()
|
|
40
|
+
|
|
41
|
+
async def setup_data_exchange_module(self) -> DataExchange:
|
|
42
|
+
"""Set up the data exchange module from configuration."""
|
|
43
|
+
data_exchange_module_imports = __import__(
|
|
44
|
+
f'modules.{self.config.security.data_exchange.module}',
|
|
45
|
+
fromlist=['Module', 'ModuleConfig']
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
SensitiveDataFilter.SENSITIVE_PATTERNS.update(
|
|
49
|
+
data_exchange_module_imports.LOGGING_SENSITIVE_PATTERNS
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
self.data_exchange_module = data_exchange_module_imports.Module(self)
|
|
53
|
+
return self.data_exchange_module
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""Shared database models and utilities for VISM components."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from contextlib import contextmanager
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from typing import Any, Generator, Type
|
|
7
|
+
from uuid import UUID, uuid4
|
|
8
|
+
from sqlalchemy import Uuid, String, DateTime, func
|
|
9
|
+
from sqlalchemy import ColumnExpressionArgument
|
|
10
|
+
from sqlalchemy.orm import (
|
|
11
|
+
MappedAsDataclass,
|
|
12
|
+
DeclarativeBase,
|
|
13
|
+
Mapped,
|
|
14
|
+
mapped_column,
|
|
15
|
+
sessionmaker,
|
|
16
|
+
Session
|
|
17
|
+
)
|
|
18
|
+
from sqlalchemy.engine import URL, create_engine
|
|
19
|
+
from lib.config import shared_logger, DatabaseConfig
|
|
20
|
+
from sqlalchemy.exc import OperationalError, StatementError
|
|
21
|
+
from sqlalchemy.orm.query import Query as _Query
|
|
22
|
+
|
|
23
|
+
from lib.data.validation import DataValidation
|
|
24
|
+
from lib.errors import VismDatabaseException
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Base(MappedAsDataclass, DeclarativeBase):
|
|
28
|
+
"""Base class for all database entities with common fields."""
|
|
29
|
+
|
|
30
|
+
id: Mapped[UUID] = mapped_column(
|
|
31
|
+
Uuid, primary_key=True, default=uuid4, init=False
|
|
32
|
+
)
|
|
33
|
+
signature: Mapped[str] = mapped_column(
|
|
34
|
+
String, nullable=True, default=None, init=False
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
created_at: Mapped[datetime] = mapped_column(
|
|
38
|
+
DateTime,
|
|
39
|
+
server_default=func.now(),
|
|
40
|
+
init=False
|
|
41
|
+
)
|
|
42
|
+
updated_at: Mapped[datetime] = mapped_column(
|
|
43
|
+
DateTime,
|
|
44
|
+
server_default=func.now(),
|
|
45
|
+
onupdate=func.now(),
|
|
46
|
+
init=False
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
def to_dict(self) -> dict[str, Any]:
|
|
50
|
+
"""Convert entity to dictionary representation."""
|
|
51
|
+
raise NotImplementedError()
|
|
52
|
+
|
|
53
|
+
def validate(self, validation_module: DataValidation) -> bool:
|
|
54
|
+
"""Validate entity signature using validation module."""
|
|
55
|
+
shared_logger.info(
|
|
56
|
+
"Validating %s %s",
|
|
57
|
+
self.__class__.__name__,
|
|
58
|
+
self.id
|
|
59
|
+
)
|
|
60
|
+
data_bytes = json.dumps(self.to_dict()).encode("utf-8")
|
|
61
|
+
data_signature = validation_module.sign(data_bytes)
|
|
62
|
+
return data_signature == self.signature
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def sign(self, validation_module: DataValidation) -> str:
|
|
66
|
+
"""Sign entity data using validation module."""
|
|
67
|
+
shared_logger.info(
|
|
68
|
+
"Signing %s %s",
|
|
69
|
+
self.__class__.__name__,
|
|
70
|
+
self.id
|
|
71
|
+
)
|
|
72
|
+
data_bytes = json.dumps(self.to_dict()).encode("utf-8")
|
|
73
|
+
data_signature = validation_module.sign(data_bytes)
|
|
74
|
+
return data_signature
|
|
75
|
+
|
|
76
|
+
class RetryingQuery(_Query):
|
|
77
|
+
__max_retry_count__ = 3
|
|
78
|
+
|
|
79
|
+
def __init__(self, *args, **kwargs):
|
|
80
|
+
super().__init__(*args, **kwargs)
|
|
81
|
+
|
|
82
|
+
def __iter__(self):
|
|
83
|
+
attempts = 0
|
|
84
|
+
while True:
|
|
85
|
+
attempts += 1
|
|
86
|
+
try:
|
|
87
|
+
return super().__iter__()
|
|
88
|
+
except OperationalError as ex:
|
|
89
|
+
if "server closed the connection unexpectedly" not in str(ex):
|
|
90
|
+
raise
|
|
91
|
+
if attempts <= self.__max_retry_count__:
|
|
92
|
+
continue
|
|
93
|
+
else:
|
|
94
|
+
raise
|
|
95
|
+
except StatementError as ex:
|
|
96
|
+
if "reconnect until invalid transaction is rolled back" not in str(ex):
|
|
97
|
+
raise
|
|
98
|
+
self.session.rollback()
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class VismDatabase:
|
|
102
|
+
"""Database interface for VISM operations."""
|
|
103
|
+
|
|
104
|
+
def __init__(self, database_config: DatabaseConfig):
|
|
105
|
+
shared_logger.debug("Initializing database")
|
|
106
|
+
self.db_url = URL.create(
|
|
107
|
+
drivername=database_config.driver,
|
|
108
|
+
username=database_config.username,
|
|
109
|
+
password=database_config.password,
|
|
110
|
+
host=database_config.host,
|
|
111
|
+
port=database_config.port,
|
|
112
|
+
database=database_config.database
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
self.engine = create_engine(self.db_url, echo=False, pool_pre_ping=True)
|
|
116
|
+
self.session_maker = sessionmaker(bind=self.engine, query_cls=RetryingQuery)
|
|
117
|
+
self._create_tables()
|
|
118
|
+
|
|
119
|
+
self.validation_module = DataValidation(validation_key=database_config.data_validation_key)
|
|
120
|
+
|
|
121
|
+
def get(
|
|
122
|
+
self,
|
|
123
|
+
obj_type: Type[Base],
|
|
124
|
+
*criterion: ColumnExpressionArgument[bool],
|
|
125
|
+
multiple: bool = False
|
|
126
|
+
):
|
|
127
|
+
"""
|
|
128
|
+
Get entity or entities from database by criteria.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
obj_type: Type of entity to retrieve
|
|
132
|
+
*criterion: Filter criteria
|
|
133
|
+
multiple: Whether to return multiple results
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
Single entity, list of entities, or None
|
|
137
|
+
"""
|
|
138
|
+
with self._get_session() as session:
|
|
139
|
+
objs: list[Base] = session.query(obj_type).filter(*criterion).all()
|
|
140
|
+
for obj in objs:
|
|
141
|
+
if not obj.validate(self.validation_module):
|
|
142
|
+
raise VismDatabaseException("Object has invalid signature")
|
|
143
|
+
if not multiple:
|
|
144
|
+
if len(objs) == 0:
|
|
145
|
+
return None
|
|
146
|
+
if len(objs) > 1:
|
|
147
|
+
raise VismDatabaseException("Multiple objects found.")
|
|
148
|
+
return objs[0]
|
|
149
|
+
return objs
|
|
150
|
+
|
|
151
|
+
def save_to_db(self, obj: Base):
|
|
152
|
+
"""
|
|
153
|
+
Save entity to database with signature.
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
obj: Entity to save
|
|
157
|
+
|
|
158
|
+
Returns:
|
|
159
|
+
Saved entity with updated signature
|
|
160
|
+
"""
|
|
161
|
+
with self._get_session() as session:
|
|
162
|
+
merged = session.merge(obj)
|
|
163
|
+
session.flush()
|
|
164
|
+
merged.signature = merged.sign(self.validation_module)
|
|
165
|
+
merged = session.merge(merged)
|
|
166
|
+
session.flush()
|
|
167
|
+
shared_logger.info(
|
|
168
|
+
"Saved %s %s to database",
|
|
169
|
+
merged.__class__.__name__,
|
|
170
|
+
merged.id
|
|
171
|
+
)
|
|
172
|
+
return merged
|
|
173
|
+
|
|
174
|
+
def _create_tables(self):
|
|
175
|
+
"""Create all database tables."""
|
|
176
|
+
Base.metadata.create_all(self.engine)
|
|
177
|
+
|
|
178
|
+
@contextmanager
|
|
179
|
+
def _get_session(self) -> Generator[Session, Any, None]:
|
|
180
|
+
"""Get database session with automatic commit/rollback."""
|
|
181
|
+
session = self.session_maker(expire_on_commit=False)
|
|
182
|
+
try:
|
|
183
|
+
yield session
|
|
184
|
+
session.commit()
|
|
185
|
+
except Exception:
|
|
186
|
+
session.rollback()
|
|
187
|
+
raise
|
|
188
|
+
finally:
|
|
189
|
+
session.close()
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Shared exception classes for VISM components."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
shared_logger = logging.getLogger("vism_shared")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class VismBreakingException(SystemExit):
|
|
9
|
+
"""Critical exception that causes system exit."""
|
|
10
|
+
|
|
11
|
+
log_level = logging.CRITICAL
|
|
12
|
+
include_traceback = True
|
|
13
|
+
|
|
14
|
+
def __init__(self, message: str, *args):
|
|
15
|
+
super().__init__(message, *args)
|
|
16
|
+
self._log_error(message, *args)
|
|
17
|
+
|
|
18
|
+
def _log_error(self, message: str, *args):
|
|
19
|
+
shared_logger.log(
|
|
20
|
+
self.log_level,
|
|
21
|
+
"%s: %s",
|
|
22
|
+
self.__class__.__name__,
|
|
23
|
+
message,
|
|
24
|
+
exc_info=self.include_traceback,
|
|
25
|
+
*args
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class VismException(RuntimeError):
|
|
30
|
+
"""Base exception class for VISM errors."""
|
|
31
|
+
|
|
32
|
+
log_level = logging.ERROR
|
|
33
|
+
include_traceback = False
|
|
34
|
+
|
|
35
|
+
def __init__(self, message: str, *args):
|
|
36
|
+
super().__init__(message, *args)
|
|
37
|
+
self._log_error(message, *args)
|
|
38
|
+
|
|
39
|
+
def _log_error(self, message: str, *args):
|
|
40
|
+
shared_logger.log(
|
|
41
|
+
self.log_level,
|
|
42
|
+
"%s: %s",
|
|
43
|
+
self.__class__.__name__,
|
|
44
|
+
message,
|
|
45
|
+
exc_info=self.include_traceback,
|
|
46
|
+
*args
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class VismDatabaseException(VismException):
|
|
51
|
+
"""Exception raised for database-related errors."""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class ChrootWriteFileExists(VismException):
|
|
55
|
+
"""Exception raised when attempting to write to an existing file."""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class ChrootWriteToFileException(VismException):
|
|
59
|
+
"""Exception raised when writing to a file fails."""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class ChrootOpenFileException(VismException):
|
|
63
|
+
"""Exception raised when opening a file fails."""
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"""Logging configuration and utilities for VISM components."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import logging
|
|
5
|
+
import re
|
|
6
|
+
import sys
|
|
7
|
+
import logging.config
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from typing import ClassVar
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class LoggingConfig:
|
|
14
|
+
"""Configuration for the logging system."""
|
|
15
|
+
|
|
16
|
+
log_root: str = "vism"
|
|
17
|
+
log_dir: str = "logs"
|
|
18
|
+
verbose: bool = False
|
|
19
|
+
log_file: str = "app.log"
|
|
20
|
+
error_file: str = "error.log"
|
|
21
|
+
log_level: str = "DEBUG"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class SensitiveDataFilter(logging.Filter): # pylint: disable=too-few-public-methods
|
|
25
|
+
"""Filter to mask sensitive data in log messages."""
|
|
26
|
+
|
|
27
|
+
SENSITIVE_PATTERNS = {}
|
|
28
|
+
|
|
29
|
+
def filter(self, record):
|
|
30
|
+
"""Filter and mask sensitive data in log record."""
|
|
31
|
+
for _pattern_name, pattern in self.SENSITIVE_PATTERNS.items():
|
|
32
|
+
if isinstance(record.msg, str):
|
|
33
|
+
record.msg = re.sub(
|
|
34
|
+
pattern['pattern'],
|
|
35
|
+
rf'{pattern["replace"]}',
|
|
36
|
+
record.msg
|
|
37
|
+
)
|
|
38
|
+
if record.args:
|
|
39
|
+
record.args = tuple(
|
|
40
|
+
re.sub(pattern['pattern'], rf'{pattern["replace"]}', str(arg))
|
|
41
|
+
if isinstance(arg, str) else arg
|
|
42
|
+
for arg in record.args
|
|
43
|
+
)
|
|
44
|
+
return True
|
|
45
|
+
|
|
46
|
+
class ErrorFilter(logging.Filter):
|
|
47
|
+
def filter(self, record):
|
|
48
|
+
return record.levelno <= logging.WARNING
|
|
49
|
+
|
|
50
|
+
class ColoredFormatter(logging.Formatter): # pylint: disable=too-few-public-methods
|
|
51
|
+
"""Formatter that adds color codes to log messages."""
|
|
52
|
+
|
|
53
|
+
RESET = "\033[0m"
|
|
54
|
+
|
|
55
|
+
LEVEL_COLORS = {
|
|
56
|
+
"DEBUG": "\033[36m",
|
|
57
|
+
"INFO": "\033[32m",
|
|
58
|
+
"WARNING": "\033[33m",
|
|
59
|
+
"ERROR": "\033[31m",
|
|
60
|
+
"CRITICAL": "\033[41m",
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
def format(self, record):
|
|
64
|
+
levelname = record.levelname
|
|
65
|
+
color = self.LEVEL_COLORS.get(levelname, "")
|
|
66
|
+
|
|
67
|
+
padded = f"{levelname:<6}"
|
|
68
|
+
|
|
69
|
+
if color:
|
|
70
|
+
record.levelname = f"{color}{padded}{self.RESET}"
|
|
71
|
+
else:
|
|
72
|
+
record.levelname = padded
|
|
73
|
+
|
|
74
|
+
formatted = super().format(record)
|
|
75
|
+
record.levelname = levelname
|
|
76
|
+
return formatted
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def setup_logger(config: LoggingConfig):
|
|
80
|
+
"""
|
|
81
|
+
Set up logging configuration with handlers and formatters.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
config: Logging configuration
|
|
85
|
+
"""
|
|
86
|
+
if not os.path.exists(config.log_dir):
|
|
87
|
+
os.makedirs(config.log_dir)
|
|
88
|
+
|
|
89
|
+
logging_config = {
|
|
90
|
+
'version': 1,
|
|
91
|
+
'disable_existing_loggers': False,
|
|
92
|
+
'filters': {
|
|
93
|
+
'sensitive_data': {
|
|
94
|
+
'()': SensitiveDataFilter,
|
|
95
|
+
},
|
|
96
|
+
'info_debug_only': {
|
|
97
|
+
'()': ErrorFilter,
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
'formatters': {
|
|
101
|
+
'verbose': {
|
|
102
|
+
'()': ColoredFormatter,
|
|
103
|
+
'format': '%(asctime)s [%(name)-12s] [%(levelname)-7s] %(message)s',
|
|
104
|
+
'datefmt': '%Y-%m-%d %H:%M:%S',
|
|
105
|
+
},
|
|
106
|
+
'simple': {
|
|
107
|
+
'()': ColoredFormatter,
|
|
108
|
+
'format': '%(asctime)s [%(levelname)-7s] %(message)s',
|
|
109
|
+
'datefmt': '%Y-%m-%d %H:%M:%S',
|
|
110
|
+
},
|
|
111
|
+
'verbose_error': {
|
|
112
|
+
'()': ColoredFormatter,
|
|
113
|
+
'format': '%(asctime)s [%(name)-30s] [%(levelname)-7s] %(message)s',
|
|
114
|
+
'datefmt': '%Y-%m-%d %H:%M:%S',
|
|
115
|
+
},
|
|
116
|
+
'simple_error': {
|
|
117
|
+
'()': ColoredFormatter,
|
|
118
|
+
'format': '%(asctime)s [%(levelname)-7s] %(message)s',
|
|
119
|
+
'datefmt': '%Y-%m-%d %H:%M:%S',
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
'handlers': {
|
|
123
|
+
'file': {
|
|
124
|
+
'level': f"{config.log_level}",
|
|
125
|
+
'class': 'logging.FileHandler',
|
|
126
|
+
'formatter': 'simple' if not config.verbose else 'verbose',
|
|
127
|
+
'filename': f'{config.log_dir.rstrip("/")}/{config.log_file}',
|
|
128
|
+
'encoding': 'utf8',
|
|
129
|
+
'filters': ['sensitive_data', 'info_debug_only']
|
|
130
|
+
},
|
|
131
|
+
"stdout_info_debug_only": {
|
|
132
|
+
"level": f"{config.log_level}",
|
|
133
|
+
"class": "logging.StreamHandler",
|
|
134
|
+
"formatter": 'simple' if not config.verbose else 'verbose',
|
|
135
|
+
"stream": sys.stdout,
|
|
136
|
+
'filters': ['sensitive_data', 'info_debug_only']
|
|
137
|
+
},
|
|
138
|
+
"stdout": {
|
|
139
|
+
"level": f"{config.log_level}",
|
|
140
|
+
"class": "logging.StreamHandler",
|
|
141
|
+
"formatter": 'simple' if not config.verbose else 'verbose',
|
|
142
|
+
"stream": sys.stdout,
|
|
143
|
+
},
|
|
144
|
+
"stderr": {
|
|
145
|
+
"level": "ERROR",
|
|
146
|
+
"class": "logging.StreamHandler",
|
|
147
|
+
"formatter": 'simple_error' if not config.verbose else 'verbose_error',
|
|
148
|
+
"stream": sys.stderr,
|
|
149
|
+
},
|
|
150
|
+
'error_file': {
|
|
151
|
+
'level': f"ERROR",
|
|
152
|
+
'class': 'logging.FileHandler',
|
|
153
|
+
'formatter': 'simple_error' if not config.verbose else 'verbose_error',
|
|
154
|
+
'filename': f'{config.log_dir.rstrip("/")}/{config.error_file}',
|
|
155
|
+
'encoding': 'utf8',
|
|
156
|
+
'filters': ['sensitive_data']
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
'loggers': {
|
|
160
|
+
f'{config.log_root}': {
|
|
161
|
+
'level': config.log_level,
|
|
162
|
+
'handlers': ['file', 'error_file', 'stdout_info_debug_only', 'stderr'],
|
|
163
|
+
"propagate": False,
|
|
164
|
+
},
|
|
165
|
+
'vism_shared': {
|
|
166
|
+
'level': config.log_level,
|
|
167
|
+
'handlers': ['file', 'error_file', 'stdout_info_debug_only', 'stderr'],
|
|
168
|
+
"propagate": False,
|
|
169
|
+
},
|
|
170
|
+
'vism_module': {
|
|
171
|
+
'level': config.log_level,
|
|
172
|
+
'handlers': ['file', 'error_file', 'stdout_info_debug_only', 'stderr'],
|
|
173
|
+
"propagate": False,
|
|
174
|
+
},
|
|
175
|
+
"uvicorn": {
|
|
176
|
+
"handlers": ["stdout"],
|
|
177
|
+
"level": "INFO",
|
|
178
|
+
"propagate": False,
|
|
179
|
+
},
|
|
180
|
+
"uvicorn.error": {
|
|
181
|
+
"handlers": ["stderr"],
|
|
182
|
+
"level": "INFO",
|
|
183
|
+
"propagate": False,
|
|
184
|
+
},
|
|
185
|
+
"uvicorn.access": {
|
|
186
|
+
"handlers": ["stdout"],
|
|
187
|
+
"level": "INFO",
|
|
188
|
+
"propagate": False,
|
|
189
|
+
},
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
logging.config.dictConfig(logging_config)
|
|
194
|
+
logging.debug("Logging is set up and ready")
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
from typing import Optional, Dict, Any, Union
|
|
2
|
+
import aioboto3
|
|
3
|
+
from lib.config import S3Config
|
|
4
|
+
from lib.config import shared_logger
|
|
5
|
+
|
|
6
|
+
class AsyncS3Client:
|
|
7
|
+
def __init__(self, config: S3Config) -> None:
|
|
8
|
+
self.bucket_name = config.bucket
|
|
9
|
+
self._session = aioboto3.Session(
|
|
10
|
+
aws_access_key_id=config.access_key,
|
|
11
|
+
aws_secret_access_key=config.secret_key,
|
|
12
|
+
region_name=config.region,
|
|
13
|
+
)
|
|
14
|
+
self._endpoint = config.endpoint
|
|
15
|
+
|
|
16
|
+
async def list_files(self, prefix: str) -> list[str]:
|
|
17
|
+
"""
|
|
18
|
+
List files directly under the given prefix (non-recursive).
|
|
19
|
+
Returns only objects, not "directories".
|
|
20
|
+
|
|
21
|
+
:param prefix: e.g. "folder/" or "" for root.
|
|
22
|
+
"""
|
|
23
|
+
async with self._session.client("s3", endpoint_url=self._endpoint) as s3:
|
|
24
|
+
paginator = s3.get_paginator("list_objects_v2")
|
|
25
|
+
|
|
26
|
+
results = []
|
|
27
|
+
|
|
28
|
+
async for page in paginator.paginate(
|
|
29
|
+
Bucket=self.bucket_name,
|
|
30
|
+
Prefix=prefix,
|
|
31
|
+
Delimiter="/",
|
|
32
|
+
):
|
|
33
|
+
contents = page.get("Contents", [])
|
|
34
|
+
for obj in contents:
|
|
35
|
+
key = obj["Key"]
|
|
36
|
+
if key != prefix:
|
|
37
|
+
results.append(key)
|
|
38
|
+
|
|
39
|
+
return results
|
|
40
|
+
|
|
41
|
+
async def upload_bytes(
|
|
42
|
+
self,
|
|
43
|
+
data: Union[bytes, bytearray, memoryview],
|
|
44
|
+
key: str,
|
|
45
|
+
extra_args: Optional[Dict[str, Any]] = None,
|
|
46
|
+
) -> None:
|
|
47
|
+
shared_logger.info(f"Uploading file {key} to s3")
|
|
48
|
+
async with self._session.client("s3", endpoint_url=self._endpoint) as s3:
|
|
49
|
+
extra_args = extra_args or {}
|
|
50
|
+
|
|
51
|
+
await s3.put_object(
|
|
52
|
+
Bucket=self.bucket_name,
|
|
53
|
+
Key=key,
|
|
54
|
+
Body=data,
|
|
55
|
+
**extra_args,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
async def download_bytes(self, key: str) -> Optional[bytes]:
|
|
59
|
+
shared_logger.info(f"Downloading file '{key}' from s3")
|
|
60
|
+
async with self._session.client("s3", endpoint_url=self._endpoint) as s3:
|
|
61
|
+
try:
|
|
62
|
+
resp = await s3.get_object(Bucket=self.bucket_name, Key=key)
|
|
63
|
+
except Exception as e:
|
|
64
|
+
shared_logger.error(f"Failed to download file '{key}' from s3: {e}")
|
|
65
|
+
return None
|
|
66
|
+
|
|
67
|
+
body = resp["Body"]
|
|
68
|
+
return await body.read()
|
|
69
|
+
|
|
70
|
+
async def exists(self, key: str) -> bool:
|
|
71
|
+
async with self._session.client("s3", endpoint_url=self._endpoint) as s3:
|
|
72
|
+
try:
|
|
73
|
+
await s3.head_object(Bucket=self.bucket_name, Key=key)
|
|
74
|
+
return True
|
|
75
|
+
except s3.exceptions.NoSuchKey:
|
|
76
|
+
return False
|
|
77
|
+
except Exception as e:
|
|
78
|
+
if getattr(e, "response", {}).get("ResponseMetadata", {}).get("HTTPStatusCode") == 404:
|
|
79
|
+
return False
|
|
80
|
+
raise
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Utility functions for VISM components."""
|
|
2
|
+
import ipaddress
|
|
3
|
+
import base64
|
|
4
|
+
|
|
5
|
+
import pkcs11
|
|
6
|
+
from starlette.requests import Request
|
|
7
|
+
|
|
8
|
+
def is_valid_ip(ip_str):
|
|
9
|
+
"""Check if a string is a valid IP address."""
|
|
10
|
+
try:
|
|
11
|
+
ipaddress.ip_address(ip_str)
|
|
12
|
+
return True
|
|
13
|
+
except ValueError:
|
|
14
|
+
return False
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def is_valid_subnet(subnet_str):
|
|
18
|
+
"""Check if a string is a valid subnet."""
|
|
19
|
+
try:
|
|
20
|
+
ipaddress.ip_network(subnet_str, strict=False)
|
|
21
|
+
return True
|
|
22
|
+
except ValueError:
|
|
23
|
+
return False
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def b64u_decode(data: str) -> bytes:
|
|
27
|
+
"""Decode base64url encoded data."""
|
|
28
|
+
if data is None:
|
|
29
|
+
return b""
|
|
30
|
+
|
|
31
|
+
if isinstance(data, bytes):
|
|
32
|
+
data = data.decode("ascii")
|
|
33
|
+
|
|
34
|
+
data = data.strip()
|
|
35
|
+
if data == "":
|
|
36
|
+
return b""
|
|
37
|
+
|
|
38
|
+
rem = len(data) % 4
|
|
39
|
+
if rem:
|
|
40
|
+
data += "=" * (4 - rem)
|
|
41
|
+
|
|
42
|
+
return base64.urlsafe_b64decode(data)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def snake_to_camel(name):
|
|
46
|
+
"""Convert snake_case to camelCase."""
|
|
47
|
+
split = name.split('_')
|
|
48
|
+
return split[0] + ''.join(word.capitalize() for word in split[1:])
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def absolute_url(request: Request, path: str) -> str:
|
|
52
|
+
"""Build absolute URL from request and path."""
|
|
53
|
+
scheme = request.url.scheme
|
|
54
|
+
if request.headers.get("X-Forwarded-Proto"):
|
|
55
|
+
scheme = request.headers.get("X-Forwarded-Proto")
|
|
56
|
+
|
|
57
|
+
base = str(request.base_url.replace(scheme=scheme)).rstrip("/")
|
|
58
|
+
if not path.startswith("/"):
|
|
59
|
+
path = "/" + path
|
|
60
|
+
|
|
61
|
+
return f"{base}{path}"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def get_client_ip(request: Request):
|
|
65
|
+
"""Get client IP address from request, respecting X-Forwarded-For."""
|
|
66
|
+
x_forwarded_for = request.headers.get("X-Forwarded-For")
|
|
67
|
+
if x_forwarded_for:
|
|
68
|
+
ip = x_forwarded_for.split(",")[0].strip()
|
|
69
|
+
else:
|
|
70
|
+
ip = request.client.host
|
|
71
|
+
return ip
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def fix_base64_padding(base64_string):
|
|
75
|
+
"""Fix base64 string padding if missing."""
|
|
76
|
+
padding_needed = len(base64_string) % 4
|
|
77
|
+
if padding_needed != 0:
|
|
78
|
+
base64_string += "=" * (4 - padding_needed)
|
|
79
|
+
return base64_string
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vism_lib
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Library for vism CA and ACME
|
|
5
|
+
Author-email: atrivisc <atrivisc.shawl130@passmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Author Name
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/atrivisc/vism-lib
|
|
29
|
+
Project-URL: Repository, https://github.com/atrivisc/vism-lib.git
|
|
30
|
+
Project-URL: Bug Tracker, https://github.com/atrivisc/vism-lib/issues
|
|
31
|
+
Keywords: vism
|
|
32
|
+
Classifier: Programming Language :: Python :: 3
|
|
33
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
34
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
35
|
+
Classifier: Operating System :: OS Independent
|
|
36
|
+
Requires-Python: >=3.14
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
License-File: LICENSE
|
|
39
|
+
Requires-Dist: python-pkcs11>=0.9.3
|
|
40
|
+
Requires-Dist: yaml-pydantic-dataclass>=0.1.1
|
|
41
|
+
Requires-Dist: pydantic>=2.12.5
|
|
42
|
+
Requires-Dist: starlette>=0.52.1
|
|
43
|
+
Dynamic: license-file
|
|
44
|
+
|
|
45
|
+
# vism-lib
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/vism_lib/__init__.py
|
|
5
|
+
src/vism_lib/config.py
|
|
6
|
+
src/vism_lib/controller.py
|
|
7
|
+
src/vism_lib/database.py
|
|
8
|
+
src/vism_lib/errors.py
|
|
9
|
+
src/vism_lib/logs.py
|
|
10
|
+
src/vism_lib/s3.py
|
|
11
|
+
src/vism_lib/util.py
|
|
12
|
+
src/vism_lib.egg-info/PKG-INFO
|
|
13
|
+
src/vism_lib.egg-info/SOURCES.txt
|
|
14
|
+
src/vism_lib.egg-info/dependency_links.txt
|
|
15
|
+
src/vism_lib.egg-info/requires.txt
|
|
16
|
+
src/vism_lib.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
vism_lib
|