libMyCarrier 0.0.1__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.
libMyCarrier/__init__.py
ADDED
|
File without changes
|
libMyCarrier/classes.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import hvac
|
|
2
|
+
import logging
|
|
3
|
+
import pyodbc
|
|
4
|
+
class Vault:
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
The Vault class provides methods to authenticate and interact with Vault.
|
|
8
|
+
|
|
9
|
+
Methods:
|
|
10
|
+
- __init__(role_id, secret_id):
|
|
11
|
+
Initializes the Vault class instance.
|
|
12
|
+
|
|
13
|
+
:param role_id: The role ID to authenticate with.
|
|
14
|
+
:param secret_id: The secret ID to authenticate with.
|
|
15
|
+
|
|
16
|
+
- get_kv_secret(path, mount_point='secret', version=None):
|
|
17
|
+
Retrieves a key-value secret from Vault.
|
|
18
|
+
|
|
19
|
+
:param path: The path of the secret.
|
|
20
|
+
:param mount_point: The mount point for the secret engine. Default is 'secret'.
|
|
21
|
+
:param version: The version of the secret. Default is None.
|
|
22
|
+
:return: The secret value.
|
|
23
|
+
|
|
24
|
+
- get_dynamic_credentials(mount_point, database):
|
|
25
|
+
Generates dynamic credentials for a database from Vault.
|
|
26
|
+
|
|
27
|
+
:param mount_point: The mount point for the database engine.
|
|
28
|
+
:param database: The name of the database.
|
|
29
|
+
:return: The generated credentials (username and password).
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, role_id, secret_id):
|
|
33
|
+
"""
|
|
34
|
+
Initialize the class instance.
|
|
35
|
+
|
|
36
|
+
:param role_id: The role ID to authenticate with.
|
|
37
|
+
:param secret_id: The secret ID to authenticate with.
|
|
38
|
+
"""
|
|
39
|
+
self.Client = hvac.Client(url='https://vault.mycarrier.tech')
|
|
40
|
+
self.SourceCredentials = None
|
|
41
|
+
try:
|
|
42
|
+
self.Client.auth.approle.login(
|
|
43
|
+
role_id=role_id,
|
|
44
|
+
secret_id=secret_id,
|
|
45
|
+
)
|
|
46
|
+
except Exception as error:
|
|
47
|
+
raise error
|
|
48
|
+
self.ServicePrincipalCredentials = None
|
|
49
|
+
|
|
50
|
+
def get_kv_secret(self, path, mount_point='secret', version=None):
|
|
51
|
+
output = None
|
|
52
|
+
if version is None:
|
|
53
|
+
output = self.Client.secrets.kv.v2.read_secret_version(path=path, mount_point=mount_point)
|
|
54
|
+
else:
|
|
55
|
+
output = self.Client.secrets.kv.v2.read_secret_version(path=path, mount_point=mount_point, version=version)
|
|
56
|
+
return output
|
|
57
|
+
|
|
58
|
+
def get_dynamic_credentials(self, mount_point, database):
|
|
59
|
+
try:
|
|
60
|
+
credentials = self.Client.secrets.database.generate_credentials(
|
|
61
|
+
name=database,
|
|
62
|
+
mount_point=mount_point
|
|
63
|
+
)
|
|
64
|
+
except Exception as e:
|
|
65
|
+
logging.error(msg=f"Failed to retrieve database credentials: {e}")
|
|
66
|
+
raise
|
|
67
|
+
output = {
|
|
68
|
+
'username': credentials['username'],
|
|
69
|
+
'password': credentials['password']
|
|
70
|
+
}
|
|
71
|
+
return output
|
|
72
|
+
|
|
73
|
+
class dbConnection:
|
|
74
|
+
"""
|
|
75
|
+
Class for establishing a database connection and executing queries.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
server (str): The name or IP address of the server.
|
|
79
|
+
port (int): The port number of the server.
|
|
80
|
+
db_name (str): The name of the database to connect to.
|
|
81
|
+
driver (str, optional): The ODBC driver name. Defaults to 'ODBC Driver 18 for SQL Server'.
|
|
82
|
+
encrypt (str, optional): Specify whether the connection should be encrypted. Defaults to 'yes'.
|
|
83
|
+
trustservercertificate (str, optional): Specify whether to trust the server certificate. Defaults to 'no'.
|
|
84
|
+
timeout (int, optional): The connection timeout in seconds. Defaults to 30.
|
|
85
|
+
|
|
86
|
+
Attributes:
|
|
87
|
+
connection: The pyodbc connection object representing the database connection.
|
|
88
|
+
|
|
89
|
+
Methods:
|
|
90
|
+
query: Executes a SQL query and returns the results.
|
|
91
|
+
close: Closes the database connection.
|
|
92
|
+
|
|
93
|
+
Examples:
|
|
94
|
+
# Instantiate dbConnection
|
|
95
|
+
conn = dbConnection('localhost', 1433, 'mydb')
|
|
96
|
+
|
|
97
|
+
# Execute a query and get all results
|
|
98
|
+
results = conn.query('SELECT * FROM mytable', outputResults='all')
|
|
99
|
+
|
|
100
|
+
# Close the connection
|
|
101
|
+
conn.close()
|
|
102
|
+
"""
|
|
103
|
+
def __init__(self, server, port, db_name, driver='ODBC Driver 18 for SQL Server', encrypt='yes',
|
|
104
|
+
trustservercertificate='no', timeout=30):
|
|
105
|
+
self.connection = pyodbc.connect(f"Driver={{{driver}}}; "
|
|
106
|
+
f"Server={server},{port}; "
|
|
107
|
+
f"Database={db_name}; Encrypt={encrypt}; TrustServerCertificate={trustservercertificate}; Connection Timeout={timeout}")
|
|
108
|
+
|
|
109
|
+
def query(self, sql, outputResults: str = None, commit: bool = False):
|
|
110
|
+
cursor = self.connection.cursor()
|
|
111
|
+
try:
|
|
112
|
+
if commit:
|
|
113
|
+
self.connection.autocommit = True
|
|
114
|
+
cursor.execute(sql)
|
|
115
|
+
if outputResults == "one":
|
|
116
|
+
return cursor.fetchone()
|
|
117
|
+
if outputResults == "all":
|
|
118
|
+
return cursor.fetchall()
|
|
119
|
+
finally:
|
|
120
|
+
cursor.close()
|
|
121
|
+
self.connection.autocommit = False
|
|
122
|
+
def close(self):
|
|
123
|
+
self.connection.close()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 MyCarrier
|
|
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.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: libMyCarrier
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Author-email: MyCarrier-DevOps <sre@mycarrier.io>
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2024 MyCarrier
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
Project-URL: Repository, https://github.com/MyCarrier-DevOps/libMyCarrier.git
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: hvac
|
|
30
|
+
Requires-Dist: pyodbc
|
|
31
|
+
|
|
32
|
+
# libMyCarrier
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from libMyCarrier.classes import Vault, dbConnection
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from libMyCarrier.classes import *
|
|
40
|
+
```
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
libMyCarrier/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
libMyCarrier/classes.py,sha256=3IMTGd3jldtaU-1vzne7rKUF6ifSvYztuYuHzuwaUTk,4734
|
|
3
|
+
libMyCarrier-0.0.1.dist-info/LICENSE,sha256=kP0r6PWnAE4LAzBDrLIBatNfzp0ZzUkz2YDuszMIDeo,1065
|
|
4
|
+
libMyCarrier-0.0.1.dist-info/METADATA,sha256=hbKecaPNa6SPgGb03YdzA_2appa2qj4lKKux5YNDFZI,1657
|
|
5
|
+
libMyCarrier-0.0.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
6
|
+
libMyCarrier-0.0.1.dist-info/top_level.txt,sha256=DneoVDrYCPs16r02IOoHkbRNwj9xgjHeqxrpRGbPX-k,13
|
|
7
|
+
libMyCarrier-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
libMyCarrier
|