Skip to content

Commit e20e81b

Browse files
authored
Merge pull request #1 from psvmcc/init
init
2 parents 580c4b1 + b866cb7 commit e20e81b

File tree

8 files changed

+169
-1
lines changed

8 files changed

+169
-1
lines changed

.github/workflows/black.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Lint
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
- uses: psf/black@stable
11+
with:
12+
options: "--check --verbose"
13+
src: "./src"

.github/workflows/publish.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Publish to PyPI.org
2+
on:
3+
release:
4+
types: [published]
5+
jobs:
6+
pypi:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout
10+
uses: actions/checkout@v4
11+
with:
12+
fetch-depth: 0
13+
- run: python3 -m pip install --upgrade build && python3 -m build
14+
- name: Publish package
15+
uses: pypa/gh-action-pypi-publish@release/v1
16+
with:
17+
password: ${{ secrets.PYPI_API_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
src/ansible_vault_decrypt.egg-info

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
# ansible-vault-decrypt
1+
# ansible-vault-decrypt
2+
3+
## usage
4+
5+
```
6+
ansible-vault-decrypt
7+
8+
positional arguments:
9+
encrypted_file Path to file to decrypt
10+
11+
options:
12+
-h, --help show this help message and exit
13+
-v, --version
14+
-d debug mode output
15+
--vault-password-file VAULT_PASSWORD_FILE
16+
vault password file
17+
```

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[build-system]
2+
requires = ["setuptools>=45", "setuptools_scm[toml]>=6.2"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[tool.setuptools_scm]

setup.cfg

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[metadata]
2+
name = ansible-vault-decrypt
3+
description = Ansible Vault Decrypt app.
4+
long_description_content_type = text/markdown
5+
url = https://github.com/psvmcc/ansible-vault-decrypt
6+
project_urls =
7+
Bug Tracker = https://github.com/psvmcc/ansible-vault-decrypt/issues
8+
Changelog = https://github.com/psvmcc/ansible-vault-decrypt/releases
9+
classifiers =
10+
Programming Language :: Python :: 3
11+
License :: OSI Approved :: MIT License
12+
Intended Audience :: Developers
13+
14+
[options]
15+
package_dir =
16+
= src
17+
packages = find:
18+
python_requires = >=3.6
19+
install_requires =
20+
ansible-core
21+
22+
[options.packages.find]
23+
where = src
24+
25+
[options.entry_points]
26+
console_scripts =
27+
ansible-vault-decrypt = ansible_vault_decrypt.app:entry_point

src/ansible_vault_decrypt/__init__.py

Whitespace-only changes.

src/ansible_vault_decrypt/app.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python3
2+
3+
from ansible.parsing.utils.yaml import from_yaml
4+
from ansible.parsing.vault import VaultSecret
5+
6+
import argparse
7+
import os
8+
from getpass import getpass
9+
from importlib.metadata import version
10+
11+
12+
parser = argparse.ArgumentParser(description="ansible-vault-decrypt", add_help=False)
13+
parser.add_argument(
14+
"-h", "--help", action="help", help="show this help message and exit"
15+
)
16+
parser.add_argument("-v", "--version", action="store_true")
17+
parser.add_argument(
18+
"-d",
19+
dest="debug",
20+
help="debug mode output",
21+
action=argparse.BooleanOptionalAction,
22+
)
23+
parser.add_argument(
24+
"--vault-password-file",
25+
dest="vault_password_file",
26+
help="vault password file",
27+
default=os.environ.get("ANSIBLE_VAULT_PASSWORD_FILE"),
28+
)
29+
parser.add_argument("encrypted_file", type=str, help="Path to file to decrypt")
30+
31+
args = parser.parse_args()
32+
33+
34+
def dict_to_yaml(input_dict, indent=0):
35+
result = ""
36+
for key, value in input_dict.items():
37+
if isinstance(value, dict):
38+
result += " " * indent + f"{key}:\n"
39+
result += dict_to_yaml(value, indent + 2)
40+
else:
41+
result += " " * indent + f"{key}: {value}\n"
42+
return result
43+
44+
45+
def read_file(file_path):
46+
try:
47+
with open(file_path, "r") as file:
48+
file = file.read()
49+
return file
50+
except FileNotFoundError:
51+
print(f"File not found: {file_path}")
52+
exit(1)
53+
except Exception as e:
54+
print(f"Error reading file: {e}")
55+
exit(1)
56+
57+
58+
def main(vault_secret):
59+
if args.debug:
60+
print(":: [DEBUG] Vault password: %s" % vault_secret)
61+
data = read_file(args.encrypted_file)
62+
output = ""
63+
try:
64+
unencrypted = from_yaml(
65+
data, vault_secrets=[("default", VaultSecret(vault_secret.encode("utf-8")))]
66+
)
67+
output = dict_to_yaml(unencrypted)
68+
except Exception as e:
69+
print(":: [ERROR] Decryption failure...")
70+
if args.debug:
71+
print(e)
72+
exit(1)
73+
print(output)
74+
75+
76+
if __name__ == "__main__":
77+
if args.version:
78+
print(version("ansible-vault-decrypt"))
79+
exit(0)
80+
if not args.vault_password_file:
81+
vault_secret = getpass()
82+
else:
83+
vault_secret = read_file(args.vault_password_file).replace("\n", "")
84+
try:
85+
main(vault_secret)
86+
except KeyboardInterrupt:
87+
print("Interrupted")
88+
exit(130)

0 commit comments

Comments
 (0)