pycraftbox-secrets 4.0.0__py3-none-any.whl
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,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
|
|
@@ -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,5 @@
|
|
|
1
|
+
pycraftbox/secrets/__init__.py,sha256=ofn1tOA9KjdWc-ANfnaClF-UZbs7EiKfqz5tBMvyPxo,145
|
|
2
|
+
pycraftbox/secrets/manager.py,sha256=UVi0u-nmck6e_B93GpD0GtUKDG-cKJorFJcrrNTDHa4,2989
|
|
3
|
+
pycraftbox_secrets-4.0.0.dist-info/METADATA,sha256=Dfw_DbpVaPon1TD7p_4lKlVmj2RYXzbVAmLalgk0-N4,326
|
|
4
|
+
pycraftbox_secrets-4.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
5
|
+
pycraftbox_secrets-4.0.0.dist-info/RECORD,,
|