pycraftbox-secrets 4.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,131 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ .vscode/*
6
+ !.vscode/extensions.json
7
+ !.vscode/tasks.json
8
+ # C extensions
9
+ *.so
10
+
11
+ # Distribution / packaging
12
+ .Python
13
+ build/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ lib/
20
+ lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ wheels/
25
+ pip-wheel-metadata/
26
+ share/python-wheels/
27
+ *.egg-info/
28
+ .installed.cfg
29
+ *.egg
30
+ MANIFEST
31
+
32
+ # PyInstaller
33
+ # Usually these files are written by a python script from a template
34
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
35
+ *.manifest
36
+ *.spec
37
+
38
+ # Installer logs
39
+ pip-log.txt
40
+ pip-delete-this-directory.txt
41
+
42
+ # Unit test / coverage reports
43
+ htmlcov/
44
+ .tox/
45
+ .nox/
46
+ .coverage
47
+ .coverage.*
48
+ .cache
49
+ nosetests.xml
50
+ coverage.xml
51
+ *.cover
52
+ *.py,cover
53
+ .hypothesis/
54
+ .pytest_cache/
55
+
56
+ # Translations
57
+ *.mo
58
+ *.pot
59
+
60
+ # Django stuff:
61
+ *.log
62
+ local_settings.py
63
+ db.sqlite3
64
+ db.sqlite3-journal
65
+
66
+ # Flask stuff:
67
+ instance/
68
+ .webassets-cache
69
+
70
+ # Scrapy stuff:
71
+ .scrapy
72
+
73
+ # Sphinx documentation
74
+ docs/_build/
75
+
76
+ # PyBuilder
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ .python-version
88
+
89
+ # pipenv
90
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
91
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
92
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
93
+ # install all needed dependencies.
94
+ #Pipfile.lock
95
+
96
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
97
+ __pypackages__/
98
+
99
+ # Celery stuff
100
+ celerybeat-schedule
101
+ celerybeat.pid
102
+
103
+ # SageMath parsed files
104
+ *.sage.py
105
+
106
+ # Environments
107
+ .env
108
+ .venv
109
+ env/
110
+ venv/
111
+ ENV/
112
+ env.bak/
113
+ venv.bak/
114
+
115
+ # Spyder project settings
116
+ .spyderproject
117
+ .spyproject
118
+
119
+ # Rope project settings
120
+ .ropeproject
121
+
122
+ # mkdocs documentation
123
+ /site
124
+
125
+ # mypy
126
+ .mypy_cache/
127
+ .dmypy.json
128
+ dmypy.json
129
+
130
+ # Pyre type checker
131
+ .pyre/
@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.5
2
+ Name: pycraftbox-secrets
3
+ Version: 4.0.0
4
+ Summary: GPG-backed secret file utilities for pycraftbox
5
+ Project-URL: Homepage, https://gitlab.com/erch/pycraftbox
6
+ Project-URL: Repository, https://gitlab.com/erch/pycraftbox
7
+ Project-URL: Issues, https://gitlab.com/erch/pycraftbox/-/issues
8
+ Requires-Python: >=3.12
@@ -0,0 +1,21 @@
1
+ [project]
2
+ name = "pycraftbox-secrets"
3
+ version = "4.0.0"
4
+ description = "GPG-backed secret file utilities for pycraftbox"
5
+ requires-python = ">=3.12"
6
+ dependencies = []
7
+
8
+ [project.urls]
9
+ Homepage = "https://gitlab.com/erch/pycraftbox"
10
+ Repository = "https://gitlab.com/erch/pycraftbox"
11
+ Issues = "https://gitlab.com/erch/pycraftbox/-/issues"
12
+
13
+ [build-system]
14
+ requires = ["hatchling"]
15
+ build-backend = "hatchling.build"
16
+
17
+ [tool.hatch.build]
18
+ dev-mode-dirs = ["src"]
19
+
20
+ [tool.hatch.build.targets.wheel]
21
+ packages = ["src/pycraftbox"]
@@ -0,0 +1,5 @@
1
+ """GPG-backed secret file utilities."""
2
+
3
+ from .manager import SecretCredentials, SecretManager
4
+
5
+ __all__ = ["SecretCredentials", "SecretManager"]
@@ -0,0 +1,85 @@
1
+ import json
2
+ import subprocess
3
+ import shlex
4
+ from getpass import getpass
5
+ from pathlib import Path
6
+ from dataclasses import dataclass
7
+
8
+
9
+ @dataclass
10
+ class SecretCredentials:
11
+ username: str
12
+ password: str
13
+
14
+
15
+ class SecretManager:
16
+ """Manager for handling secrets, such as decrypting .gpg files to retrieve credentials.
17
+
18
+ How to create a .gpg file with credentials:
19
+ 1. Create a JSON file (e.g. creds.json) with the following structure:
20
+ {
21
+ "ib" : {
22
+ "username": "your_username",
23
+ "pass": "your_password"
24
+ }
25
+ }
26
+ 2. Encrypt the file using GPG:
27
+ gpg --symmetric --cipher-algo AES256 creds.json
28
+ 3. This will create a creds.json.gpg file. You can then delete the original
29
+ creds.json file for security.
30
+ 4. Place the creds.json.gpg file in the appropriate location and set
31
+ secret_file_path in the configuration to point to it.
32
+
33
+ How to update an existing secrets file safely:
34
+ 1. Restrict permissions for newly created files in the current shell:
35
+ ``umask 077``
36
+ 2. Decrypt the secrets to a temporary plaintext file:
37
+ ``gpg --output creds.json --decrypt creds.json.gpg``
38
+ 3. Edit ``creds.json`` and keep the same JSON structure shown above.
39
+ 4. Encrypt the updated content to a new file without overwriting the current
40
+ encrypted file:
41
+ ``gpg --symmetric --cipher-algo AES256 --output creds.json.gpg.new creds.json``
42
+ 5. Verify that the new file can be decrypted and contains valid JSON:
43
+ ``gpg --decrypt creds.json.gpg.new | python -m json.tool > /dev/null``
44
+ 6. Replace the old encrypted file only after verification:
45
+ ``mv creds.json.gpg.new creds.json.gpg``
46
+ 7. Immediately remove the plaintext file:
47
+ ``rm -f creds.json``
48
+
49
+ Do not commit, copy, or leave the decrypted JSON file in a temporary directory.
50
+ """
51
+
52
+ secret_file_path: Path
53
+
54
+ def __init__(self, secret_file_path: Path) -> None:
55
+ self.secret_file_path = secret_file_path
56
+
57
+ def read(self, provider: str) -> SecretCredentials | None:
58
+ """
59
+ Déchiffre un fichier .gpg en mémoire et renvoie le texte.
60
+ - gpg-agent/pinentry peut s'ouvrir (recommandé).
61
+ """
62
+
63
+ if not self.secret_file_path.is_file():
64
+ return None
65
+
66
+ base_cmd = [
67
+ "gpg",
68
+ "--batch",
69
+ "--yes",
70
+ "--quiet",
71
+ "--decrypt",
72
+ str(self.secret_file_path),
73
+ ]
74
+
75
+ # Laisse gpg-agent/pinentry gérer l'entrée du mot de passe
76
+ res = subprocess.run(base_cmd, check=True, capture_output=True, text=True)
77
+ json_text = json.loads(res.stdout)
78
+ provider_creds = json_text.get(provider, {})
79
+ if not provider_creds:
80
+ return None
81
+ creds = SecretCredentials(
82
+ username=provider_creds.get("username", ""),
83
+ password=provider_creds.get("pass", ""),
84
+ )
85
+ return creds