vaultconfig 0.1.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.
- vaultconfig/__init__.py +49 -0
- vaultconfig/_version.py +34 -0
- vaultconfig/cli.py +288 -0
- vaultconfig/config.py +483 -0
- vaultconfig/crypt.py +384 -0
- vaultconfig/exceptions.py +51 -0
- vaultconfig/formats/__init__.py +10 -0
- vaultconfig/formats/base.py +72 -0
- vaultconfig/formats/ini_format.py +115 -0
- vaultconfig/formats/toml_format.py +112 -0
- vaultconfig/formats/yaml_format.py +110 -0
- vaultconfig/obscure.py +229 -0
- vaultconfig/schema.py +204 -0
- vaultconfig-0.1.0.dist-info/METADATA +453 -0
- vaultconfig-0.1.0.dist-info/RECORD +19 -0
- vaultconfig-0.1.0.dist-info/WHEEL +5 -0
- vaultconfig-0.1.0.dist-info/entry_points.txt +2 -0
- vaultconfig-0.1.0.dist-info/licenses/LICENSE +21 -0
- vaultconfig-0.1.0.dist-info/top_level.txt +1 -0
vaultconfig/__init__.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""VaultConfig - Secure configuration management library.
|
|
2
|
+
|
|
3
|
+
VaultConfig provides easy-to-use configuration management with support for:
|
|
4
|
+
- Multiple formats (TOML, INI, YAML)
|
|
5
|
+
- Password obscuring for sensitive fields
|
|
6
|
+
- Full config file encryption (NaCl secretbox)
|
|
7
|
+
- Schema validation with Pydantic
|
|
8
|
+
- CLI tool for config management
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from vaultconfig import crypt, obscure
|
|
14
|
+
from vaultconfig.config import ConfigEntry, ConfigManager
|
|
15
|
+
from vaultconfig.exceptions import (
|
|
16
|
+
ConfigExistsError,
|
|
17
|
+
ConfigNotFoundError,
|
|
18
|
+
DecryptionError,
|
|
19
|
+
EncryptionError,
|
|
20
|
+
FormatError,
|
|
21
|
+
InvalidPasswordError,
|
|
22
|
+
SchemaValidationError,
|
|
23
|
+
VaultConfigError,
|
|
24
|
+
)
|
|
25
|
+
from vaultconfig.schema import ConfigSchema, FieldDef, create_simple_schema
|
|
26
|
+
|
|
27
|
+
__version__ = "0.1.0"
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
# Core classes
|
|
31
|
+
"ConfigManager",
|
|
32
|
+
"ConfigEntry",
|
|
33
|
+
"ConfigSchema",
|
|
34
|
+
"FieldDef",
|
|
35
|
+
# Functions
|
|
36
|
+
"create_simple_schema",
|
|
37
|
+
# Modules
|
|
38
|
+
"crypt",
|
|
39
|
+
"obscure",
|
|
40
|
+
# Exceptions
|
|
41
|
+
"VaultConfigError",
|
|
42
|
+
"EncryptionError",
|
|
43
|
+
"DecryptionError",
|
|
44
|
+
"InvalidPasswordError",
|
|
45
|
+
"FormatError",
|
|
46
|
+
"SchemaValidationError",
|
|
47
|
+
"ConfigNotFoundError",
|
|
48
|
+
"ConfigExistsError",
|
|
49
|
+
]
|
vaultconfig/_version.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# file generated by setuptools-scm
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
"__version__",
|
|
6
|
+
"__version_tuple__",
|
|
7
|
+
"version",
|
|
8
|
+
"version_tuple",
|
|
9
|
+
"__commit_id__",
|
|
10
|
+
"commit_id",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
TYPE_CHECKING = False
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from typing import Tuple
|
|
16
|
+
from typing import Union
|
|
17
|
+
|
|
18
|
+
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
19
|
+
COMMIT_ID = Union[str, None]
|
|
20
|
+
else:
|
|
21
|
+
VERSION_TUPLE = object
|
|
22
|
+
COMMIT_ID = object
|
|
23
|
+
|
|
24
|
+
version: str
|
|
25
|
+
__version__: str
|
|
26
|
+
__version_tuple__: VERSION_TUPLE
|
|
27
|
+
version_tuple: VERSION_TUPLE
|
|
28
|
+
commit_id: COMMIT_ID
|
|
29
|
+
__commit_id__: COMMIT_ID
|
|
30
|
+
|
|
31
|
+
__version__ = version = '0.1.0'
|
|
32
|
+
__version_tuple__ = version_tuple = (0, 1, 0)
|
|
33
|
+
|
|
34
|
+
__commit_id__ = commit_id = None
|
vaultconfig/cli.py
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
"""Command-line interface for vaultconfig."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import click
|
|
9
|
+
from rich.console import Console
|
|
10
|
+
from rich.table import Table
|
|
11
|
+
|
|
12
|
+
from vaultconfig.config import ConfigManager
|
|
13
|
+
from vaultconfig.exceptions import VaultConfigError
|
|
14
|
+
|
|
15
|
+
console = Console()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@click.group()
|
|
19
|
+
@click.version_option()
|
|
20
|
+
def main() -> None:
|
|
21
|
+
"""VaultConfig - Secure configuration management with encryption support."""
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@main.command()
|
|
26
|
+
@click.argument("config_dir", type=click.Path())
|
|
27
|
+
@click.option("--format", "-f", default="toml", help="Config format (toml/ini/yaml)")
|
|
28
|
+
@click.option("--encrypt", "-e", is_flag=True, help="Enable encryption")
|
|
29
|
+
def init(config_dir: str, format: str, encrypt: bool) -> None:
|
|
30
|
+
"""Initialize a new config directory."""
|
|
31
|
+
config_path = Path(config_dir)
|
|
32
|
+
|
|
33
|
+
if config_path.exists() and any(config_path.iterdir()):
|
|
34
|
+
console.print(
|
|
35
|
+
f"[yellow]Warning:[/yellow] Directory {config_dir} already exists "
|
|
36
|
+
"and is not empty"
|
|
37
|
+
)
|
|
38
|
+
if not click.confirm("Continue?"):
|
|
39
|
+
return
|
|
40
|
+
|
|
41
|
+
config_path.mkdir(parents=True, exist_ok=True)
|
|
42
|
+
|
|
43
|
+
password = None
|
|
44
|
+
if encrypt:
|
|
45
|
+
password = click.prompt("Enter encryption password", hide_input=True)
|
|
46
|
+
confirm = click.prompt("Confirm password", hide_input=True)
|
|
47
|
+
if password != confirm:
|
|
48
|
+
console.print("[red]Error:[/red] Passwords do not match")
|
|
49
|
+
sys.exit(1)
|
|
50
|
+
|
|
51
|
+
# Initialize the ConfigManager to create the directory structure
|
|
52
|
+
ConfigManager(config_path, format=format, password=password)
|
|
53
|
+
|
|
54
|
+
console.print(f"[green]✓[/green] Initialized config directory: {config_dir}")
|
|
55
|
+
console.print(f" Format: {format}")
|
|
56
|
+
console.print(f" Encrypted: {'Yes' if encrypt else 'No'}")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@main.command(name="list")
|
|
60
|
+
@click.argument("config_dir", type=click.Path(exists=True))
|
|
61
|
+
@click.option("--format", "-f", help="Config format (autodetect if not specified)")
|
|
62
|
+
def list_command(config_dir: str, format: str | None) -> None:
|
|
63
|
+
"""List all configurations."""
|
|
64
|
+
try:
|
|
65
|
+
manager = _get_manager(config_dir, format)
|
|
66
|
+
configs = manager.list_configs()
|
|
67
|
+
|
|
68
|
+
if not configs:
|
|
69
|
+
console.print("No configurations found")
|
|
70
|
+
return
|
|
71
|
+
|
|
72
|
+
table = Table(title="Configurations")
|
|
73
|
+
table.add_column("Name", style="cyan")
|
|
74
|
+
table.add_column("Encrypted", style="yellow")
|
|
75
|
+
|
|
76
|
+
for name in sorted(configs):
|
|
77
|
+
encrypted = "Yes" if manager.is_encrypted() else "No"
|
|
78
|
+
table.add_row(name, encrypted)
|
|
79
|
+
|
|
80
|
+
console.print(table)
|
|
81
|
+
|
|
82
|
+
except VaultConfigError as e:
|
|
83
|
+
console.print(f"[red]Error:[/red] {e}")
|
|
84
|
+
sys.exit(1)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@main.command(name="show")
|
|
88
|
+
@click.argument("config_dir", type=click.Path(exists=True))
|
|
89
|
+
@click.argument("name")
|
|
90
|
+
@click.option("--format", "-f", help="Config format")
|
|
91
|
+
@click.option("--reveal", "-r", is_flag=True, help="Reveal obscured passwords")
|
|
92
|
+
def show_command(config_dir: str, name: str, format: str | None, reveal: bool) -> None:
|
|
93
|
+
"""Show configuration."""
|
|
94
|
+
try:
|
|
95
|
+
manager = _get_manager(config_dir, format)
|
|
96
|
+
config = manager.get_config(name)
|
|
97
|
+
|
|
98
|
+
if not config:
|
|
99
|
+
console.print(f"[red]Error:[/red] Config '{name}' not found")
|
|
100
|
+
sys.exit(1)
|
|
101
|
+
|
|
102
|
+
console.print(f"[bold]Configuration:[/bold] {name}")
|
|
103
|
+
console.print()
|
|
104
|
+
|
|
105
|
+
data = config.get_all(reveal_secrets=reveal)
|
|
106
|
+
_print_dict(data)
|
|
107
|
+
|
|
108
|
+
if not reveal:
|
|
109
|
+
console.print()
|
|
110
|
+
console.print(
|
|
111
|
+
"[yellow]Note:[/yellow] Use --reveal to show obscured passwords"
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
except VaultConfigError as e:
|
|
115
|
+
console.print(f"[red]Error:[/red] {e}")
|
|
116
|
+
sys.exit(1)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@main.command(name="delete")
|
|
120
|
+
@click.argument("config_dir", type=click.Path(exists=True))
|
|
121
|
+
@click.argument("name")
|
|
122
|
+
@click.option("--format", "-f", help="Config format")
|
|
123
|
+
@click.confirmation_option(prompt="Are you sure you want to delete this config?")
|
|
124
|
+
def delete_command(config_dir: str, name: str, format: str | None) -> None:
|
|
125
|
+
"""Delete a configuration."""
|
|
126
|
+
try:
|
|
127
|
+
manager = _get_manager(config_dir, format)
|
|
128
|
+
|
|
129
|
+
if manager.remove_config(name):
|
|
130
|
+
console.print(f"[green]✓[/green] Deleted config: {name}")
|
|
131
|
+
else:
|
|
132
|
+
console.print(f"[red]Error:[/red] Config '{name}' not found")
|
|
133
|
+
sys.exit(1)
|
|
134
|
+
|
|
135
|
+
except VaultConfigError as e:
|
|
136
|
+
console.print(f"[red]Error:[/red] {e}")
|
|
137
|
+
sys.exit(1)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@main.group()
|
|
141
|
+
def encrypt() -> None:
|
|
142
|
+
"""Manage config encryption."""
|
|
143
|
+
pass
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@encrypt.command(name="set")
|
|
147
|
+
@click.argument("config_dir", type=click.Path(exists=True))
|
|
148
|
+
@click.option("--format", "-f", help="Config format")
|
|
149
|
+
def encrypt_set(config_dir: str, format: str | None) -> None:
|
|
150
|
+
"""Set or change encryption password."""
|
|
151
|
+
try:
|
|
152
|
+
manager = _get_manager(config_dir, format, ask_password=False)
|
|
153
|
+
|
|
154
|
+
password = click.prompt("Enter new encryption password", hide_input=True)
|
|
155
|
+
confirm = click.prompt("Confirm password", hide_input=True)
|
|
156
|
+
|
|
157
|
+
if password != confirm:
|
|
158
|
+
console.print("[red]Error:[/red] Passwords do not match")
|
|
159
|
+
sys.exit(1)
|
|
160
|
+
|
|
161
|
+
manager.set_encryption_password(password)
|
|
162
|
+
console.print("[green]✓[/green] Encryption password updated")
|
|
163
|
+
|
|
164
|
+
except VaultConfigError as e:
|
|
165
|
+
console.print(f"[red]Error:[/red] {e}")
|
|
166
|
+
sys.exit(1)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@encrypt.command(name="remove")
|
|
170
|
+
@click.argument("config_dir", type=click.Path(exists=True))
|
|
171
|
+
@click.option("--format", "-f", help="Config format")
|
|
172
|
+
@click.confirmation_option(
|
|
173
|
+
prompt=(
|
|
174
|
+
"Are you sure you want to remove encryption? "
|
|
175
|
+
"Configs will be stored in plaintext."
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
def encrypt_remove(config_dir: str, format: str | None) -> None:
|
|
179
|
+
"""Remove encryption from all configs."""
|
|
180
|
+
try:
|
|
181
|
+
manager = _get_manager(config_dir, format)
|
|
182
|
+
manager.remove_encryption()
|
|
183
|
+
console.print("[green]✓[/green] Encryption removed")
|
|
184
|
+
|
|
185
|
+
except VaultConfigError as e:
|
|
186
|
+
console.print(f"[red]Error:[/red] {e}")
|
|
187
|
+
sys.exit(1)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
@encrypt.command(name="check")
|
|
191
|
+
@click.argument("config_dir", type=click.Path(exists=True))
|
|
192
|
+
@click.option("--format", "-f", help="Config format")
|
|
193
|
+
def encrypt_check(config_dir: str, format: str | None) -> None:
|
|
194
|
+
"""Check if configs are encrypted."""
|
|
195
|
+
try:
|
|
196
|
+
manager = _get_manager(config_dir, format, ask_password=False)
|
|
197
|
+
|
|
198
|
+
if manager.is_encrypted():
|
|
199
|
+
console.print("[green]✓[/green] Configs are encrypted")
|
|
200
|
+
else:
|
|
201
|
+
console.print("[yellow]![/yellow] Configs are NOT encrypted")
|
|
202
|
+
|
|
203
|
+
except VaultConfigError as e:
|
|
204
|
+
console.print(f"[red]Error:[/red] {e}")
|
|
205
|
+
sys.exit(1)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _get_manager(
|
|
209
|
+
config_dir: str, format: str | None = None, ask_password: bool = True
|
|
210
|
+
) -> ConfigManager:
|
|
211
|
+
"""Get ConfigManager instance with password handling.
|
|
212
|
+
|
|
213
|
+
Args:
|
|
214
|
+
config_dir: Config directory path
|
|
215
|
+
format: Config format (autodetect if None)
|
|
216
|
+
ask_password: If True, ask for password if needed
|
|
217
|
+
|
|
218
|
+
Returns:
|
|
219
|
+
ConfigManager instance
|
|
220
|
+
"""
|
|
221
|
+
from vaultconfig import crypt
|
|
222
|
+
|
|
223
|
+
config_path = Path(config_dir)
|
|
224
|
+
|
|
225
|
+
# Detect format if not specified
|
|
226
|
+
if format is None:
|
|
227
|
+
format = _detect_format(config_path)
|
|
228
|
+
|
|
229
|
+
# Check if any config file is encrypted
|
|
230
|
+
password = None
|
|
231
|
+
if ask_password:
|
|
232
|
+
extension = {"toml": ".toml", "ini": ".ini", "yaml": ".yaml"}.get(
|
|
233
|
+
format, ".toml"
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
for config_file in config_path.glob(f"*{extension}"):
|
|
237
|
+
with open(config_file, "rb") as f:
|
|
238
|
+
if crypt.is_encrypted(f.read()):
|
|
239
|
+
password = crypt.get_password()
|
|
240
|
+
break
|
|
241
|
+
|
|
242
|
+
return ConfigManager(config_path, format=format, password=password)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _detect_format(config_dir: Path) -> str:
|
|
246
|
+
"""Detect config format from existing files.
|
|
247
|
+
|
|
248
|
+
Args:
|
|
249
|
+
config_dir: Config directory
|
|
250
|
+
|
|
251
|
+
Returns:
|
|
252
|
+
Detected format name
|
|
253
|
+
"""
|
|
254
|
+
# Count files by extension
|
|
255
|
+
extensions = {".toml": 0, ".ini": 0, ".yaml": 0, ".yml": 0}
|
|
256
|
+
|
|
257
|
+
for ext in extensions:
|
|
258
|
+
extensions[ext] = len(list(config_dir.glob(f"*{ext}")))
|
|
259
|
+
|
|
260
|
+
# Return most common format
|
|
261
|
+
if extensions[".toml"] > 0:
|
|
262
|
+
return "toml"
|
|
263
|
+
elif extensions[".ini"] > 0:
|
|
264
|
+
return "ini"
|
|
265
|
+
elif extensions[".yaml"] > 0 or extensions[".yml"] > 0:
|
|
266
|
+
return "yaml"
|
|
267
|
+
|
|
268
|
+
# Default to toml
|
|
269
|
+
return "toml"
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _print_dict(data: dict, indent: int = 0) -> None:
|
|
273
|
+
"""Pretty print dictionary.
|
|
274
|
+
|
|
275
|
+
Args:
|
|
276
|
+
data: Dictionary to print
|
|
277
|
+
indent: Indentation level
|
|
278
|
+
"""
|
|
279
|
+
for key, value in data.items():
|
|
280
|
+
if isinstance(value, dict):
|
|
281
|
+
console.print(" " * indent + f"[bold]{key}:[/bold]")
|
|
282
|
+
_print_dict(value, indent + 1)
|
|
283
|
+
else:
|
|
284
|
+
console.print(" " * indent + f"{key}: {value}")
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
if __name__ == "__main__":
|
|
288
|
+
main()
|