Skip to content

Commit b5bf570

Browse files
Ensure persistant HMAC keys, proper snapshot handling, and security
1 parent 403c936 commit b5bf570

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

pydatastructs/graphs/graph.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@
1010
def rotate_secret_key():
1111
""" Automatically rotates secret key after 30 days """
1212
while True:
13-
os.environ["HMAC_SECRET_KEY"] = secrets.token_hax(32)
13+
os.environ["HMAC_SECRET_KEY"] = secrets.token_hex(32)
1414
time.sleep(30 * 24 * 60 * 60)
1515
def get_secret_key():
1616
""" Gets the HMAC secret key """
1717
secret_key = os.getenv("HMAC_SECRET_KEY")
1818
if secret_key is None:
19-
raise RuntimeError("Secret key is missing!")
20-
return secret_key.encode()
19+
try:
20+
with open("hmac_key.txt", "r") as f:
21+
secret_key = f.read().strip()
22+
except FileNotFoundError:
23+
raise RuntimeError("Secret key is missing! Set HMAC_SECRET_KEY or create hmac_key.txt.")
24+
return secret_key.encode()
25+
2126
def generate_hmac(data):
2227
"""Generating HMAC signature for integrity verification"""
23-
return hmac.new(get_secret_key(), data.encode(),hashlib.sha256).haxdigit()
28+
return hmac.new(get_secret_key(), data.encode(),hashlib.sha256).hexdigit()
2429
def serialize_graph(graph):
2530
"""Converts a graph into a string for HMAC signing."""
2631
if not graph.vertices or not graph.edge_weights:
@@ -222,3 +227,5 @@ def num_edges(self):
222227
"""
223228
raise NotImplementedError(
224229
"This is an abstract method.")
230+
threading.Thread(target=rotate_secret_key, daemon=True).start()
231+

0 commit comments

Comments
 (0)