Skip to content

Commit e852217

Browse files
committed
added multiple private key support by way of comma-separated paths in STACKS_PRIVATE_KEY_PATH
1 parent 5b86c0c commit e852217

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "stacks"
3-
version = "2.0.12"
3+
version = "2.0.13"
44
description = "Stacks, the Terraform code pre-processor"
55
readme = "README.md"
66
requires-python = ">=3.10"

src/stacks/helpers/crypto.py

+26-21
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,32 @@ def decrypt(data, private_key_path=os.getenv("STACKS_PRIVATE_KEY_PATH"), must_de
100100
string_encrypted_base64,
101101
) = data.removeprefix("ENC[").removesuffix("]").split(";")
102102

103-
with open(private_key_path, "rb") as f:
104-
private_key = cryptography.hazmat.primitives.serialization.load_pem_private_key(
105-
f.read(),
106-
password=None,
107-
backend=cryptography.hazmat.backends.default_backend(),
108-
)
109-
110-
try:
111-
symmetric_key = private_key.decrypt(
112-
base64.b64decode(symmetric_key_encrypted_base64.encode()),
113-
cryptography.hazmat.primitives.asymmetric.padding.OAEP(
114-
mgf=cryptography.hazmat.primitives.asymmetric.padding.MGF1(algorithm=cryptography.hazmat.primitives.hashes.SHA256()),
115-
algorithm=cryptography.hazmat.primitives.hashes.SHA256(),
116-
label=None,
117-
),
118-
)
119-
except ValueError as e:
120-
if must_decrypt:
121-
raise e
122-
else:
123-
return data
103+
private_key_paths = private_key_path.split(",")
104+
for i in range(len(private_key_paths)):
105+
with open(private_key_paths[i], "rb") as f:
106+
private_key = cryptography.hazmat.primitives.serialization.load_pem_private_key(
107+
f.read(),
108+
password=None,
109+
backend=cryptography.hazmat.backends.default_backend(),
110+
)
111+
112+
try:
113+
symmetric_key = private_key.decrypt(
114+
base64.b64decode(symmetric_key_encrypted_base64.encode()),
115+
cryptography.hazmat.primitives.asymmetric.padding.OAEP(
116+
mgf=cryptography.hazmat.primitives.asymmetric.padding.MGF1(algorithm=cryptography.hazmat.primitives.hashes.SHA256()),
117+
algorithm=cryptography.hazmat.primitives.hashes.SHA256(),
118+
label=None,
119+
),
120+
)
121+
break
122+
except ValueError as e:
123+
if i < len(private_key_paths)-1:
124+
continue
125+
elif must_decrypt:
126+
raise e
127+
else:
128+
return data
124129

125130
init_vector = base64.b64decode(init_vector_base64.encode())
126131

0 commit comments

Comments
 (0)