-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New features: inline secrets, custom Jinja filters! (#10)
* fixed *.auto.tfvars variable autodeclaration * updated copyright year * added custom jinja2 filters support * simplified directory_remove * added inline encryption support
- Loading branch information
1 parent
ecfb245
commit 8e0491f
Showing
9 changed files
with
291 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Cisco Systems, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
def deepformat(value, params): | ||
if isinstance(value, dict): | ||
return { | ||
deepformat(key, params): deepformat(value, params) | ||
for key, value in value.items() | ||
} | ||
if isinstance(value, list): | ||
return [ | ||
deepformat(item, params) | ||
for item in value | ||
] | ||
if isinstance(value, str): | ||
return value.format(**params) | ||
return value | ||
|
||
|
||
__all__ = [ | ||
deepformat, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
deepmerge==1.0.1 | ||
GitPython==3.1.30 | ||
Jinja2==3.1.2 | ||
python-hcl2==3.0.5 | ||
PyYAML==6.0 | ||
cryptography==43.0.1 | ||
deepmerge==2.0 | ||
GitPython==3.1.43 | ||
Jinja2==3.1.4 | ||
python-hcl2==4.3.5 | ||
PyYAML==6.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Cisco Systems, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
def main(func): | ||
import argparse, inspect, json | ||
parser = argparse.ArgumentParser() | ||
for key, value in inspect.signature(func).parameters.items(): | ||
parser.add_argument( | ||
f"--{key.replace('_','-')}", | ||
action = argparse.BooleanOptionalAction if isinstance(value.default, bool) else None, | ||
default = value.default, | ||
required = value.default == value.empty, | ||
) | ||
output = func(**vars(parser.parse_args())) | ||
if output is not None: | ||
print(json.dumps(output, indent=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Cisco Systems, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
def main(string, private_key_path): | ||
import base64 | ||
import cryptography.hazmat.backends | ||
import cryptography.hazmat.primitives.asymmetric.padding | ||
import cryptography.hazmat.primitives.hashes | ||
import cryptography.hazmat.primitives.padding | ||
import cryptography.hazmat.primitives.serialization | ||
|
||
symmetric_key_encrypted_base64, encryptor_tag_base64, init_vector_base64, string_encrypted_base64 = string.removeprefix("ENC[").removesuffix("]").split(";") | ||
|
||
with open(private_key_path, "rb") as key_file: | ||
symmetric_key = cryptography.hazmat.primitives.serialization.load_pem_private_key( | ||
key_file.read(), | ||
password = None, | ||
backend = cryptography.hazmat.backends.default_backend(), | ||
).decrypt( | ||
base64.b64decode(symmetric_key_encrypted_base64.encode()), | ||
cryptography.hazmat.primitives.asymmetric.padding.OAEP( | ||
mgf = cryptography.hazmat.primitives.asymmetric.padding.MGF1(algorithm=cryptography.hazmat.primitives.hashes.SHA256()), | ||
algorithm = cryptography.hazmat.primitives.hashes.SHA256(), | ||
label = None, | ||
) | ||
) | ||
|
||
decryptor = cryptography.hazmat.primitives.ciphers.Cipher( | ||
cryptography.hazmat.primitives.ciphers.algorithms.AES(symmetric_key), | ||
cryptography.hazmat.primitives.ciphers.modes.GCM( | ||
base64.b64decode(init_vector_base64.encode()), | ||
base64.b64decode(encryptor_tag_base64.encode()), | ||
), | ||
backend = cryptography.hazmat.backends.default_backend(), | ||
).decryptor() | ||
padded = decryptor.update(base64.b64decode(string_encrypted_base64.encode())) + decryptor.finalize() | ||
|
||
unpadder = cryptography.hazmat.primitives.padding.PKCS7(128).unpadder() | ||
unpadded = unpadder.update(padded) + unpadder.finalize() | ||
|
||
return unpadded.decode("utf-8") | ||
|
||
|
||
if __name__ == "__main__": | ||
import cli_wrapper | ||
cli_wrapper.main(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Cisco Systems, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
def main(string, public_key_path): | ||
import base64 | ||
import cryptography.hazmat.backends | ||
import cryptography.hazmat.primitives.asymmetric.padding | ||
import cryptography.hazmat.primitives.ciphers | ||
import cryptography.hazmat.primitives.hashes | ||
import cryptography.hazmat.primitives.padding | ||
import cryptography.hazmat.primitives.serialization | ||
import os | ||
|
||
padder = cryptography.hazmat.primitives.padding.PKCS7(128).padder() | ||
padded = padder.update(string.encode()) + padder.finalize() | ||
|
||
symmetric_key = os.urandom(32) | ||
|
||
init_vector = os.urandom(12) | ||
init_vector_base64 = base64.b64encode(init_vector).decode("utf-8") | ||
|
||
encryptor = cryptography.hazmat.primitives.ciphers.Cipher(cryptography.hazmat.primitives.ciphers.algorithms.AES(symmetric_key), cryptography.hazmat.primitives.ciphers.modes.GCM(init_vector), backend=cryptography.hazmat.backends.default_backend()).encryptor() | ||
|
||
string_encrypted = encryptor.update(padded) + encryptor.finalize() | ||
string_encrypted_base64 = base64.b64encode(string_encrypted).decode("utf-8") | ||
|
||
encryptor_tag_base64 = base64.b64encode(encryptor.tag).decode("utf-8") | ||
|
||
with open(public_key_path, "rb") as f: | ||
public_key = cryptography.hazmat.primitives.serialization.load_pem_public_key( | ||
f.read(), | ||
backend = cryptography.hazmat.backends.default_backend(), | ||
) | ||
|
||
symmetric_key_encrypted_base64 = base64.b64encode(public_key.encrypt( | ||
symmetric_key, | ||
cryptography.hazmat.primitives.asymmetric.padding.OAEP( | ||
mgf = cryptography.hazmat.primitives.asymmetric.padding.MGF1(algorithm=cryptography.hazmat.primitives.hashes.SHA256()), | ||
algorithm = cryptography.hazmat.primitives.hashes.SHA256(), | ||
label = None, | ||
) | ||
)).decode("utf-8") | ||
|
||
return f"ENC[{symmetric_key_encrypted_base64};{encryptor_tag_base64};{init_vector_base64};{string_encrypted_base64}]" | ||
|
||
|
||
if __name__ == "__main__": | ||
import cli_wrapper | ||
cli_wrapper.main(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Cisco Systems, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
def main(public_key_path, private_key_path): | ||
import cryptography.hazmat.backends | ||
import cryptography.hazmat.primitives.serialization | ||
import cryptography.hazmat.primitives.asymmetric.rsa | ||
|
||
key = cryptography.hazmat.primitives.asymmetric.rsa.generate_private_key( | ||
backend = cryptography.hazmat.backends.default_backend(), | ||
key_size = 2**11, | ||
public_exponent = 2**16+1, | ||
) | ||
with open(private_key_path, "wb") as f: | ||
f.write(key.private_bytes( | ||
encoding = cryptography.hazmat.primitives.serialization.Encoding.PEM, | ||
format = cryptography.hazmat.primitives.serialization.PrivateFormat.PKCS8, | ||
encryption_algorithm = cryptography.hazmat.primitives.serialization.NoEncryption(), | ||
)) | ||
with open(public_key_path, "wb") as f: | ||
f.write(key.public_key().public_bytes( | ||
encoding = cryptography.hazmat.primitives.serialization.Encoding.PEM, | ||
format = cryptography.hazmat.primitives.serialization.PublicFormat.SubjectPublicKeyInfo, | ||
)) | ||
|
||
|
||
if __name__ == "__main__": | ||
import cli_wrapper | ||
cli_wrapper.main(main) |