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cryptography.fernet import Fernet
|
4 |
+
# from botocore.exceptions import NoCredentialsError, PartialCredentialsError
|
5 |
+
|
6 |
+
# def get_secret(secret_name, region_name):
|
7 |
+
# """
|
8 |
+
# Retrieve secret value from AWS Secrets Manager.
|
9 |
+
# Args:
|
10 |
+
# secret_name (str): The name of the secret to retrieve.
|
11 |
+
# region_name (str): AWS region where the secret is stored.
|
12 |
+
# Returns:
|
13 |
+
# dict: Decrypted secret values.
|
14 |
+
# """
|
15 |
+
# # Create a Secrets Manager client
|
16 |
+
# session = boto3.session.Session()
|
17 |
+
# client = session.client('secretsmanager', region_name = region_name)
|
18 |
+
# try:
|
19 |
+
# response = client.get_secret_value(SecretId = secret_name)
|
20 |
+
# except NoCredentialsError:
|
21 |
+
# print("Credentials not available")
|
22 |
+
# return None
|
23 |
+
# except PartialCredentialsError:
|
24 |
+
# print("Incomplete credentials provided")
|
25 |
+
# return None
|
26 |
+
# except Exception as e:
|
27 |
+
# print(f"Error retrieving secret: {e}")
|
28 |
+
# return None
|
29 |
+
|
30 |
+
# # Depending on whether the secret is a string or binary, one of these fields will be populated
|
31 |
+
# if 'SecretString' in response:
|
32 |
+
# secret_dict = json.loads(response['SecretString'])
|
33 |
+
# # print("secret_dict", secret_dict)
|
34 |
+
# fernet_key = secret_dict.get('Fernet_Key', None)
|
35 |
+
# # print("fernet_key", fernet_key)
|
36 |
+
# else:
|
37 |
+
# print("Failed to retrieve secret.")
|
38 |
+
# return None
|
39 |
+
|
40 |
+
# if not fernet_key:
|
41 |
+
# print("Fernet key not found in secret.")
|
42 |
+
# return None
|
43 |
+
# # Create a Fernet cipher object
|
44 |
+
# cipher_suite = Fernet(fernet_key)
|
45 |
+
# CONNECTIONS_HOST = cipher_suite.decrypt(secret_dict["CONNECTIONS_HOST"].encode()).decode()
|
46 |
+
# CONNECTIONS_DB = cipher_suite.decrypt(secret_dict["CONNECTIONS_DB"].encode()).decode()
|
47 |
+
# CONNECTIONS_USER = cipher_suite.decrypt(secret_dict["CONNECTIONS_USER"].encode()).decode()
|
48 |
+
# CONNECTIONS_PASS = cipher_suite.decrypt(secret_dict["CONNECTIONS_PASS"].encode()).decode()
|
49 |
+
# ARANGO_URL = cipher_suite.decrypt(secret_dict["ARANGO_URL"].encode()).decode()
|
50 |
+
# ARANGO_USERNAME = cipher_suite.decrypt(secret_dict["ARANGO_USERNAME"].encode()).decode()
|
51 |
+
# ARANGO_PASSWORD = cipher_suite.decrypt(secret_dict["ARANGO_PASSWORD"].encode()).decode()
|
52 |
+
# ARANGO_DB = cipher_suite.decrypt(secret_dict["ARANGO_DB"].encode()).decode()
|
53 |
+
# JAVA_URL = cipher_suite.decrypt(secret_dict["JAVA_URL"].encode()).decode()
|
54 |
+
# SERVER_PORT = cipher_suite.decrypt(secret_dict["SERVER_PORT"].encode()).decode()
|
55 |
+
# PYTHON_URL = cipher_suite.decrypt(secret_dict["PYTHON_URL"].encode()).decode()
|
56 |
+
# AWS_S3_CREDS_KEY_ID = cipher_suite.decrypt(secret_dict["AWS_S3_CREDS_KEY_ID"].encode()).decode()
|
57 |
+
# AWS_S3_CREDS_SECRET_KEY = cipher_suite.decrypt(secret_dict["AWS_S3_CREDS_SECRET_KEY"].encode()).decode()
|
58 |
+
# NOTIFICATION_ENDPOINT = cipher_suite.decrypt(secret_dict["NOTIFICATION_ENDPOINT"].encode()).decode()
|
59 |
+
# SUPPORT_EMAIL = cipher_suite.decrypt(secret_dict["SUPPORT_EMAIL"].encode()).decode()
|
60 |
+
# SUPPORT_EMAIL_PASS = cipher_suite.decrypt(secret_dict["SUPPORT_EMAIL_PASS"].encode()).decode()
|
61 |
+
# MAIL_SERVER_SMTP = cipher_suite.decrypt(secret_dict["MAIL_SERVER_SMTP"].encode()).decode()
|
62 |
+
# MAIL_SERVER_PORT = cipher_suite.decrypt(secret_dict["MAIL_SERVER_PORT"].encode()).decode()
|
63 |
+
# FOLDER_ETL_S3_BUCKET_NAME = ""
|
64 |
+
# FOLDER_UNSTRUCTURED_STORAGE = ""
|
65 |
+
# BUCKET_ETL_S3_BUCKET_NAME = cipher_suite.decrypt(secret_dict["BUCKET_ETL_S3_BUCKET_NAME"].encode()).decode()
|
66 |
+
# BUCKET_UNSTRUCTURED_STORAGE = cipher_suite.decrypt(secret_dict["BUCKET_UNSTRUCTURED_STORAGE"].encode()).decode()
|
67 |
+
# BUCKET_PERFORMANCE_DRIVERS = ""
|
68 |
+
# APP_URL = cipher_suite.decrypt(secret_dict["APP_URL"].encode()).decode()
|
69 |
+
# ALLOWED_HOSTS = cipher_suite.decrypt(secret_dict["ALLOWED_HOSTS"].encode()).decode() + ',http://192.168.0.110:4521/'
|
70 |
+
|
71 |
+
# return CONNECTIONS_HOST, \
|
72 |
+
# CONNECTIONS_DB, CONNECTIONS_USER, CONNECTIONS_PASS, ARANGO_URL, ARANGO_USERNAME, ARANGO_PASSWORD, \
|
73 |
+
# ARANGO_DB, JAVA_URL, SERVER_PORT, PYTHON_URL, AWS_S3_CREDS_KEY_ID, AWS_S3_CREDS_SECRET_KEY, NOTIFICATION_ENDPOINT, SUPPORT_EMAIL, \
|
74 |
+
# SUPPORT_EMAIL_PASS, MAIL_SERVER_SMTP, MAIL_SERVER_PORT, BUCKET_ETL_S3_BUCKET_NAME, \
|
75 |
+
# BUCKET_UNSTRUCTURED_STORAGE, APP_URL, ALLOWED_HOSTS, FOLDER_ETL_S3_BUCKET_NAME, FOLDER_UNSTRUCTURED_STORAGE, BUCKET_PERFORMANCE_DRIVERS
|
76 |
+
|
77 |
+
# secret_name = "Demo/Ingen/skeys"
|
78 |
+
# region_name = "us-east-1"
|
79 |
+
|
80 |
+
# CONNECTIONS_HOST, CONNECTIONS_DB, CONNECTIONS_USER, CONNECTIONS_PASS, ARANGO_URL, ARANGO_USERNAME, ARANGO_PASSWORD, \
|
81 |
+
# ARANGO_DB, JAVA_URL, SERVER_PORT, PYTHON_URL, AWS_S3_CREDS_KEY_ID, AWS_S3_CREDS_SECRET_KEY, NOTIFICATION_ENDPOINT, SUPPORT_EMAIL, \
|
82 |
+
# SUPPORT_EMAIL_PASS, MAIL_SERVER_SMTP, MAIL_SERVER_PORT, BUCKET_ETL_S3_BUCKET_NAME, \
|
83 |
+
# BUCKET_UNSTRUCTURED_STORAGE, APP_URL, ALLOWED_HOSTS, FOLDER_ETL_S3_BUCKET_NAME, FOLDER_UNSTRUCTURED_STORAGE, \
|
84 |
+
# BUCKET_PERFORMANCE_DRIVERS = get_secret(secret_name, region_name)
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
# # Got it! You have AWS credentials configured locally (via `aws configure`), and you want to make sure your `get_secret` function uses those credentials to fetch the secret from AWS Secrets Manager.
|
91 |
+
|
92 |
+
# # The great news is: **boto3 automatically picks up credentials from your AWS CLI configuration (`~/.aws/credentials`)** by default. So if you’ve already run `aws configure` and set your credentials, your existing code will use those credentials without any extra change.
|
93 |
+
|
94 |
+
# # ### What you *don’t* need to do:
|
95 |
+
|
96 |
+
# # * You don’t need to manually pass credentials in the function unless you want to override.
|
97 |
+
# # * You don’t need to add `aws_access_key_id` or `aws_secret_access_key` manually unless you want to hardcode or dynamically pass them.
|
98 |
+
|
99 |
+
# # ### How it works currently:
|
100 |
+
|
101 |
+
# # ```python
|
102 |
+
# # session = boto3.session.Session()
|
103 |
+
# # client = session.client('secretsmanager', region_name=region_name)
|
104 |
+
# # ```
|
105 |
+
|
106 |
+
# # * This uses the default credential provider chain, which looks for credentials in environment variables, AWS CLI config files, EC2 instance roles, etc.
|
107 |
+
# # * Since you have credentials configured via `aws configure`, boto3 will find and use those credentials automatically.
|
108 |
+
|
109 |
+
# # ---
|
110 |
+
|
111 |
+
# # ### Optional: If you want to explicitly use the credentials from your `.env` or environment variables
|
112 |
+
|
113 |
+
# # If you want to explicitly use credentials stored in environment variables (e.g., loaded from `.env`), you can modify your function like this:
|
114 |
+
|
115 |
+
# # ```python
|
116 |
+
# # import os
|
117 |
+
|
118 |
+
# # def get_secret(secret_name, region_name):
|
119 |
+
# # session = boto3.session.Session(
|
120 |
+
# # aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
|
121 |
+
# # aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
|
122 |
+
# # aws_session_token=os.getenv("AWS_SESSION_TOKEN") # optional, if using temporary credentials
|
123 |
+
# # )
|
124 |
+
# # client = session.client('secretsmanager', region_name=region_name)
|
125 |
+
# # # rest of your function
|
126 |
+
# # ```
|
127 |
+
|
128 |
+
# # This assumes you have loaded your `.env` variables (using `python-dotenv` or similar) or have them set in your environment.
|
129 |
+
|
130 |
+
# # ---
|
131 |
+
|
132 |
+
# # ### Summary:
|
133 |
+
|
134 |
+
# # * If you have run `aws configure` and have credentials locally, **no change is needed**. Your current code uses those credentials.
|
135 |
+
# # * If you want to use environment variables or `.env` values for AWS keys explicitly, pass them to `Session()` as shown above.
|
136 |
+
|
137 |
+
# # ---
|
138 |
+
|
139 |
+
# # If you want, I can help you with a quick snippet to load `.env` variables too! Just ask.
|
140 |
+
import boto3
|
141 |
+
import json
|
142 |
+
import os
|
143 |
+
from cryptography.fernet import Fernet
|
144 |
+
from botocore.exceptions import NoCredentialsError, PartialCredentialsError
|
145 |
+
from dotenv import load_dotenv
|
146 |
+
|
147 |
+
load_dotenv()
|
148 |
+
|
149 |
+
def get_secret(secret_name, region_name):
|
150 |
+
try:
|
151 |
+
session = boto3.session.Session(
|
152 |
+
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
|
153 |
+
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
|
154 |
+
aws_session_token=os.getenv("AWS_SESSION_TOKEN")
|
155 |
+
)
|
156 |
+
|
157 |
+
client = session.client('secretsmanager', region_name=region_name)
|
158 |
+
response = client.get_secret_value(SecretId=secret_name)
|
159 |
+
|
160 |
+
if 'SecretString' not in response:
|
161 |
+
print("SecretString not found.")
|
162 |
+
return None
|
163 |
+
|
164 |
+
secret_dict = json.loads(response['SecretString'])
|
165 |
+
fernet_key = secret_dict.get('Fernet_Key')
|
166 |
+
if not fernet_key:
|
167 |
+
print("Fernet key missing.")
|
168 |
+
return None
|
169 |
+
|
170 |
+
cipher_suite = Fernet(fernet_key.encode())
|
171 |
+
def decrypt(key): return cipher_suite.decrypt(secret_dict[key].encode()).decode()
|
172 |
+
|
173 |
+
CONNECTIONS_HOST = decrypt("CONNECTIONS_HOST")
|
174 |
+
CONNECTIONS_DB = decrypt("CONNECTIONS_DB")
|
175 |
+
CONNECTIONS_USER = decrypt("CONNECTIONS_USER")
|
176 |
+
CONNECTIONS_PASS = decrypt("CONNECTIONS_PASS")
|
177 |
+
ARANGO_URL = decrypt("ARANGO_URL")
|
178 |
+
ARANGO_USERNAME = decrypt("ARANGO_USERNAME")
|
179 |
+
ARANGO_PASSWORD = decrypt("ARANGO_PASSWORD")
|
180 |
+
ARANGO_DB = decrypt("ARANGO_DB")
|
181 |
+
JAVA_URL = decrypt("JAVA_URL")
|
182 |
+
SERVER_PORT = decrypt("SERVER_PORT")
|
183 |
+
PYTHON_URL = decrypt("PYTHON_URL")
|
184 |
+
AWS_S3_CREDS_KEY_ID = decrypt("AWS_S3_CREDS_KEY_ID")
|
185 |
+
AWS_S3_CREDS_SECRET_KEY = decrypt("AWS_S3_CREDS_SECRET_KEY")
|
186 |
+
NOTIFICATION_ENDPOINT = decrypt("NOTIFICATION_ENDPOINT")
|
187 |
+
SUPPORT_EMAIL = decrypt("SUPPORT_EMAIL")
|
188 |
+
SUPPORT_EMAIL_PASS = decrypt("SUPPORT_EMAIL_PASS")
|
189 |
+
MAIL_SERVER_SMTP = decrypt("MAIL_SERVER_SMTP")
|
190 |
+
MAIL_SERVER_PORT = decrypt("MAIL_SERVER_PORT")
|
191 |
+
BUCKET_ETL_S3_BUCKET_NAME = decrypt("BUCKET_ETL_S3_BUCKET_NAME")
|
192 |
+
BUCKET_UNSTRUCTURED_STORAGE = decrypt("BUCKET_UNSTRUCTURED_STORAGE")
|
193 |
+
APP_URL = decrypt("APP_URL")
|
194 |
+
ALLOWED_HOSTS = decrypt("ALLOWED_HOSTS") + ',http://192.168.0.110:4521/'
|
195 |
+
|
196 |
+
# Static/empty strings
|
197 |
+
FOLDER_ETL_S3_BUCKET_NAME = ""
|
198 |
+
FOLDER_UNSTRUCTURED_STORAGE = ""
|
199 |
+
BUCKET_PERFORMANCE_DRIVERS = ""
|
200 |
+
|
201 |
+
return CONNECTIONS_HOST, CONNECTIONS_DB, CONNECTIONS_USER, CONNECTIONS_PASS, ARANGO_URL, ARANGO_USERNAME, \
|
202 |
+
ARANGO_PASSWORD, ARANGO_DB, JAVA_URL, SERVER_PORT, PYTHON_URL, AWS_S3_CREDS_KEY_ID, AWS_S3_CREDS_SECRET_KEY, \
|
203 |
+
NOTIFICATION_ENDPOINT, SUPPORT_EMAIL, SUPPORT_EMAIL_PASS, MAIL_SERVER_SMTP, MAIL_SERVER_PORT, \
|
204 |
+
BUCKET_ETL_S3_BUCKET_NAME, BUCKET_UNSTRUCTURED_STORAGE, APP_URL, ALLOWED_HOSTS, \
|
205 |
+
FOLDER_ETL_S3_BUCKET_NAME, FOLDER_UNSTRUCTURED_STORAGE, BUCKET_PERFORMANCE_DRIVERS
|
206 |
+
|
207 |
+
except Exception as e:
|
208 |
+
print(f"Error retrieving secrets: {e}")
|
209 |
+
return None
|
210 |
+
|
211 |
+
# 👇 Move this outside __main__ so it runs on import
|
212 |
+
secret_name = "Demo/Ingen/skeys"
|
213 |
+
region_name = "us-east-1"
|
214 |
+
|
215 |
+
secrets = get_secret(secret_name, region_name)
|
216 |
+
|
217 |
+
if secrets:
|
218 |
+
(
|
219 |
+
CONNECTIONS_HOST, CONNECTIONS_DB, CONNECTIONS_USER, CONNECTIONS_PASS, ARANGO_URL, ARANGO_USERNAME,
|
220 |
+
ARANGO_PASSWORD, ARANGO_DB, JAVA_URL, SERVER_PORT, PYTHON_URL, AWS_S3_CREDS_KEY_ID, AWS_S3_CREDS_SECRET_KEY,
|
221 |
+
NOTIFICATION_ENDPOINT, SUPPORT_EMAIL, SUPPORT_EMAIL_PASS, MAIL_SERVER_SMTP, MAIL_SERVER_PORT,
|
222 |
+
BUCKET_ETL_S3_BUCKET_NAME, BUCKET_UNSTRUCTURED_STORAGE, APP_URL, ALLOWED_HOSTS,
|
223 |
+
FOLDER_ETL_S3_BUCKET_NAME, FOLDER_UNSTRUCTURED_STORAGE, BUCKET_PERFORMANCE_DRIVERS
|
224 |
+
) = secrets
|
225 |
+
print("Secrets successfully loaded and decrypted.")
|
226 |
+
else:
|
227 |
+
raise Exception("❌ Failed to load secrets. Check AWS credentials or secret structure.")
|