mcli-framework 7.8.4__py3-none-any.whl → 7.9.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.
Potentially problematic release.
This version of mcli-framework might be problematic. Click here for more details.
- mcli/__init__.py +160 -0
- mcli/__main__.py +14 -0
- mcli/app/__init__.py +23 -0
- mcli/app/main.py +10 -17
- mcli/app/model/__init__.py +0 -0
- mcli/app/model_cmd.py +57 -472
- mcli/app/video/__init__.py +5 -0
- mcli/chat/__init__.py +34 -0
- mcli/lib/__init__.py +0 -0
- mcli/lib/api/__init__.py +0 -0
- mcli/lib/auth/__init__.py +1 -0
- mcli/lib/config/__init__.py +1 -0
- mcli/lib/erd/__init__.py +25 -0
- mcli/lib/files/__init__.py +0 -0
- mcli/lib/fs/__init__.py +1 -0
- mcli/lib/lib.py +8 -1
- mcli/lib/logger/__init__.py +3 -0
- mcli/lib/performance/__init__.py +17 -0
- mcli/lib/pickles/__init__.py +1 -0
- mcli/lib/secrets/__init__.py +10 -0
- mcli/lib/secrets/commands.py +185 -0
- mcli/lib/secrets/manager.py +213 -0
- mcli/lib/secrets/repl.py +297 -0
- mcli/lib/secrets/store.py +246 -0
- mcli/lib/shell/__init__.py +0 -0
- mcli/lib/toml/__init__.py +1 -0
- mcli/lib/watcher/__init__.py +0 -0
- mcli/ml/__init__.py +16 -0
- mcli/ml/api/__init__.py +30 -0
- mcli/ml/api/routers/__init__.py +27 -0
- mcli/ml/auth/__init__.py +41 -0
- mcli/ml/backtesting/__init__.py +33 -0
- mcli/ml/cli/__init__.py +5 -0
- mcli/ml/config/__init__.py +33 -0
- mcli/ml/configs/__init__.py +16 -0
- mcli/ml/dashboard/__init__.py +12 -0
- mcli/ml/dashboard/components/__init__.py +7 -0
- mcli/ml/dashboard/pages/__init__.py +6 -0
- mcli/ml/data_ingestion/__init__.py +29 -0
- mcli/ml/database/__init__.py +40 -0
- mcli/ml/experimentation/__init__.py +29 -0
- mcli/ml/features/__init__.py +39 -0
- mcli/ml/mlops/__init__.py +19 -0
- mcli/ml/models/__init__.py +90 -0
- mcli/ml/monitoring/__init__.py +25 -0
- mcli/ml/optimization/__init__.py +27 -0
- mcli/ml/predictions/__init__.py +5 -0
- mcli/ml/preprocessing/__init__.py +24 -0
- mcli/ml/scripts/__init__.py +1 -0
- mcli/ml/trading/__init__.py +63 -0
- mcli/ml/training/__init__.py +7 -0
- mcli/mygroup/__init__.py +3 -0
- mcli/public/__init__.py +1 -0
- mcli/public/commands/__init__.py +2 -0
- mcli/self/__init__.py +3 -0
- mcli/self/self_cmd.py +8 -0
- mcli/self/zsh_cmd.py +259 -0
- mcli/workflow/__init__.py +0 -0
- mcli/workflow/daemon/__init__.py +15 -0
- mcli/workflow/dashboard/__init__.py +5 -0
- mcli/workflow/docker/__init__.py +0 -0
- mcli/workflow/file/__init__.py +0 -0
- mcli/workflow/gcloud/__init__.py +1 -0
- mcli/workflow/git_commit/__init__.py +0 -0
- mcli/workflow/interview/__init__.py +0 -0
- mcli/workflow/politician_trading/__init__.py +4 -0
- mcli/workflow/registry/__init__.py +0 -0
- mcli/workflow/repo/__init__.py +0 -0
- mcli/workflow/scheduler/__init__.py +25 -0
- mcli/workflow/search/__init__.py +0 -0
- mcli/workflow/sync/__init__.py +5 -0
- mcli/workflow/videos/__init__.py +1 -0
- mcli/workflow/wakatime/__init__.py +80 -0
- {mcli_framework-7.8.4.dist-info → mcli_framework-7.9.0.dist-info}/METADATA +2 -1
- {mcli_framework-7.8.4.dist-info → mcli_framework-7.9.0.dist-info}/RECORD +79 -12
- mcli/app/chat_cmd.py +0 -42
- mcli/test/test_cmd.py +0 -20
- {mcli_framework-7.8.4.dist-info → mcli_framework-7.9.0.dist-info}/WHEEL +0 -0
- {mcli_framework-7.8.4.dist-info → mcli_framework-7.9.0.dist-info}/entry_points.txt +0 -0
- {mcli_framework-7.8.4.dist-info → mcli_framework-7.9.0.dist-info}/licenses/LICENSE +0 -0
- {mcli_framework-7.8.4.dist-info → mcli_framework-7.9.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CLI commands for secrets management.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Optional
|
|
7
|
+
|
|
8
|
+
import click
|
|
9
|
+
|
|
10
|
+
from mcli.lib.ui.styling import error, info, success, warning
|
|
11
|
+
|
|
12
|
+
from .manager import SecretsManager
|
|
13
|
+
from .repl import run_repl
|
|
14
|
+
from .store import SecretsStore
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@click.group(name="secrets", help="Secure secrets management with encryption and git sync")
|
|
18
|
+
def secrets_group():
|
|
19
|
+
"""Secrets management commands."""
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@secrets_group.command(name="repl", help="Launch interactive secrets shell")
|
|
24
|
+
def secrets_repl():
|
|
25
|
+
"""Launch the interactive secrets REPL."""
|
|
26
|
+
run_repl()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@secrets_group.command(name="set", help="Set a secret value")
|
|
30
|
+
@click.argument("key")
|
|
31
|
+
@click.argument("value")
|
|
32
|
+
@click.option("-n", "--namespace", default="default", help="Namespace for the secret")
|
|
33
|
+
def secrets_set(key: str, value: str, namespace: str):
|
|
34
|
+
"""Set a secret value."""
|
|
35
|
+
manager = SecretsManager()
|
|
36
|
+
try:
|
|
37
|
+
manager.set(key, value, namespace)
|
|
38
|
+
success(f"Secret '{key}' set in namespace '{namespace}'")
|
|
39
|
+
except Exception as e:
|
|
40
|
+
error(f"Failed to set secret: {e}")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@secrets_group.command(name="get", help="Get a secret value")
|
|
44
|
+
@click.argument("key")
|
|
45
|
+
@click.option("-n", "--namespace", default="default", help="Namespace for the secret")
|
|
46
|
+
@click.option("-s", "--show", is_flag=True, help="Show the full value (not masked)")
|
|
47
|
+
def secrets_get(key: str, namespace: str, show: bool):
|
|
48
|
+
"""Get a secret value."""
|
|
49
|
+
manager = SecretsManager()
|
|
50
|
+
value = manager.get(key, namespace)
|
|
51
|
+
|
|
52
|
+
if value is not None:
|
|
53
|
+
if show:
|
|
54
|
+
click.echo(value)
|
|
55
|
+
else:
|
|
56
|
+
# Mask the value
|
|
57
|
+
masked = (
|
|
58
|
+
value[:3] + "*" * (len(value) - 6) + value[-3:]
|
|
59
|
+
if len(value) > 6
|
|
60
|
+
else "*" * len(value)
|
|
61
|
+
)
|
|
62
|
+
info(f"{key} = {masked}")
|
|
63
|
+
info("Use --show to display the full value")
|
|
64
|
+
else:
|
|
65
|
+
warning(f"Secret '{key}' not found in namespace '{namespace}'")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@secrets_group.command(name="list", help="List all secrets")
|
|
69
|
+
@click.option("-n", "--namespace", help="Filter by namespace")
|
|
70
|
+
def secrets_list(namespace: Optional[str]):
|
|
71
|
+
"""List all secrets."""
|
|
72
|
+
manager = SecretsManager()
|
|
73
|
+
secrets = manager.list(namespace)
|
|
74
|
+
|
|
75
|
+
if secrets:
|
|
76
|
+
info("Secrets:")
|
|
77
|
+
for secret in secrets:
|
|
78
|
+
click.echo(f" • {secret}")
|
|
79
|
+
else:
|
|
80
|
+
info("No secrets found")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@secrets_group.command(name="delete", help="Delete a secret")
|
|
84
|
+
@click.argument("key")
|
|
85
|
+
@click.option("-n", "--namespace", default="default", help="Namespace for the secret")
|
|
86
|
+
@click.confirmation_option(prompt="Are you sure you want to delete this secret?")
|
|
87
|
+
def secrets_delete(key: str, namespace: str):
|
|
88
|
+
"""Delete a secret."""
|
|
89
|
+
manager = SecretsManager()
|
|
90
|
+
if manager.delete(key, namespace):
|
|
91
|
+
success(f"Secret '{key}' deleted from namespace '{namespace}'")
|
|
92
|
+
else:
|
|
93
|
+
warning(f"Secret '{key}' not found in namespace '{namespace}'")
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@secrets_group.command(name="export", help="Export secrets as environment variables")
|
|
97
|
+
@click.option("-n", "--namespace", help="Namespace to export")
|
|
98
|
+
@click.option("-o", "--output", type=click.Path(), help="Output file (defaults to stdout)")
|
|
99
|
+
def secrets_export(namespace: Optional[str], output: Optional[str]):
|
|
100
|
+
"""Export secrets as environment variables."""
|
|
101
|
+
manager = SecretsManager()
|
|
102
|
+
env_vars = manager.export_env(namespace)
|
|
103
|
+
|
|
104
|
+
if env_vars:
|
|
105
|
+
if output:
|
|
106
|
+
with open(output, "w") as f:
|
|
107
|
+
for key, value in env_vars.items():
|
|
108
|
+
f.write(f"export {key}={value}\n")
|
|
109
|
+
success(f"Exported {len(env_vars)} secrets to {output}")
|
|
110
|
+
else:
|
|
111
|
+
for key, value in env_vars.items():
|
|
112
|
+
click.echo(f"export {key}={value}")
|
|
113
|
+
else:
|
|
114
|
+
info("No secrets to export")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@secrets_group.command(name="import", help="Import secrets from environment file")
|
|
118
|
+
@click.argument("env_file", type=click.Path(exists=True))
|
|
119
|
+
@click.option("-n", "--namespace", default="default", help="Namespace to import into")
|
|
120
|
+
def secrets_import(env_file: str, namespace: str):
|
|
121
|
+
"""Import secrets from environment file."""
|
|
122
|
+
manager = SecretsManager()
|
|
123
|
+
count = manager.import_env(Path(env_file), namespace)
|
|
124
|
+
success(f"Imported {count} secrets into namespace '{namespace}'")
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@secrets_group.group(name="store", help="Git-based secrets synchronization")
|
|
128
|
+
def store_group():
|
|
129
|
+
"""Store management commands."""
|
|
130
|
+
pass
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@store_group.command(name="init", help="Initialize secrets store")
|
|
134
|
+
@click.option("-r", "--remote", help="Git remote URL")
|
|
135
|
+
def store_init(remote: Optional[str]):
|
|
136
|
+
"""Initialize the secrets store."""
|
|
137
|
+
store = SecretsStore()
|
|
138
|
+
store.init(remote)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@store_group.command(name="push", help="Push secrets to store")
|
|
142
|
+
@click.option("-m", "--message", help="Commit message")
|
|
143
|
+
def store_push(message: Optional[str]):
|
|
144
|
+
"""Push secrets to store."""
|
|
145
|
+
manager = SecretsManager()
|
|
146
|
+
store = SecretsStore()
|
|
147
|
+
store.push(manager.secrets_dir, message)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
@store_group.command(name="pull", help="Pull secrets from store")
|
|
151
|
+
def store_pull():
|
|
152
|
+
"""Pull secrets from store."""
|
|
153
|
+
manager = SecretsManager()
|
|
154
|
+
store = SecretsStore()
|
|
155
|
+
store.pull(manager.secrets_dir)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
@store_group.command(name="sync", help="Sync secrets with store")
|
|
159
|
+
@click.option("-m", "--message", help="Commit message")
|
|
160
|
+
def store_sync(message: Optional[str]):
|
|
161
|
+
"""Sync secrets with store."""
|
|
162
|
+
manager = SecretsManager()
|
|
163
|
+
store = SecretsStore()
|
|
164
|
+
store.sync(manager.secrets_dir, message)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@store_group.command(name="status", help="Show store status")
|
|
168
|
+
def store_status():
|
|
169
|
+
"""Show store status."""
|
|
170
|
+
store = SecretsStore()
|
|
171
|
+
status = store.status()
|
|
172
|
+
|
|
173
|
+
info("Secrets Store Status:")
|
|
174
|
+
click.echo(f" Initialized: {status['initialized']}")
|
|
175
|
+
click.echo(f" Path: {status['store_path']}")
|
|
176
|
+
|
|
177
|
+
if status["initialized"]:
|
|
178
|
+
click.echo(f" Branch: {status['branch']}")
|
|
179
|
+
click.echo(f" Commit: {status['commit']}")
|
|
180
|
+
click.echo(f" Clean: {status['clean']}")
|
|
181
|
+
|
|
182
|
+
if status["has_remote"]:
|
|
183
|
+
click.echo(f" Remote: {status['remote_url']}")
|
|
184
|
+
else:
|
|
185
|
+
click.echo(" Remote: Not configured")
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Secrets manager for handling secure storage and retrieval of secrets.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Dict, List, Optional
|
|
10
|
+
|
|
11
|
+
import click
|
|
12
|
+
from cryptography.fernet import Fernet
|
|
13
|
+
from cryptography.hazmat.primitives import hashes
|
|
14
|
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
|
15
|
+
|
|
16
|
+
from mcli.lib.logger.logger import get_logger
|
|
17
|
+
|
|
18
|
+
logger = get_logger(__name__)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class SecretsManager:
|
|
22
|
+
"""Manages secrets storage with encryption."""
|
|
23
|
+
|
|
24
|
+
def __init__(self, secrets_dir: Optional[Path] = None):
|
|
25
|
+
"""Initialize the secrets manager.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
secrets_dir: Directory to store secrets. Defaults to ~/.mcli/secrets/
|
|
29
|
+
"""
|
|
30
|
+
self.secrets_dir = secrets_dir or Path.home() / ".mcli" / "secrets"
|
|
31
|
+
self.secrets_dir.mkdir(parents=True, exist_ok=True)
|
|
32
|
+
self._cipher_suite = self._get_cipher_suite()
|
|
33
|
+
|
|
34
|
+
def _get_cipher_suite(self) -> Fernet:
|
|
35
|
+
"""Get or create encryption key."""
|
|
36
|
+
key_file = self.secrets_dir / ".key"
|
|
37
|
+
|
|
38
|
+
if key_file.exists():
|
|
39
|
+
with open(key_file, "rb") as f:
|
|
40
|
+
key = f.read()
|
|
41
|
+
else:
|
|
42
|
+
# Generate a new key from a password
|
|
43
|
+
password = click.prompt("Enter a password for secrets encryption", hide_input=True)
|
|
44
|
+
password_bytes = password.encode()
|
|
45
|
+
|
|
46
|
+
# Use PBKDF2 to derive a key from the password
|
|
47
|
+
kdf = PBKDF2HMAC(
|
|
48
|
+
algorithm=hashes.SHA256(),
|
|
49
|
+
length=32,
|
|
50
|
+
salt=b"mcli-secrets-salt", # In production, use a random salt
|
|
51
|
+
iterations=100000,
|
|
52
|
+
)
|
|
53
|
+
key = base64.urlsafe_b64encode(kdf.derive(password_bytes))
|
|
54
|
+
|
|
55
|
+
# Save the key (in production, this should be stored more securely)
|
|
56
|
+
with open(key_file, "wb") as f:
|
|
57
|
+
f.write(key)
|
|
58
|
+
|
|
59
|
+
# Set restrictive permissions
|
|
60
|
+
os.chmod(key_file, 0o600)
|
|
61
|
+
|
|
62
|
+
return Fernet(key)
|
|
63
|
+
|
|
64
|
+
def set(self, key: str, value: str, namespace: Optional[str] = None) -> None:
|
|
65
|
+
"""Set a secret value.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
key: Secret key
|
|
69
|
+
value: Secret value
|
|
70
|
+
namespace: Optional namespace for grouping secrets
|
|
71
|
+
"""
|
|
72
|
+
namespace = namespace or "default"
|
|
73
|
+
namespace_dir = self.secrets_dir / namespace
|
|
74
|
+
namespace_dir.mkdir(exist_ok=True)
|
|
75
|
+
|
|
76
|
+
# Encrypt the value
|
|
77
|
+
encrypted_value = self._cipher_suite.encrypt(value.encode())
|
|
78
|
+
|
|
79
|
+
# Store the encrypted value
|
|
80
|
+
secret_file = namespace_dir / f"{key}.secret"
|
|
81
|
+
with open(secret_file, "wb") as f:
|
|
82
|
+
f.write(encrypted_value)
|
|
83
|
+
|
|
84
|
+
# Set restrictive permissions
|
|
85
|
+
os.chmod(secret_file, 0o600)
|
|
86
|
+
|
|
87
|
+
logger.debug(f"Secret '{key}' stored in namespace '{namespace}'")
|
|
88
|
+
|
|
89
|
+
def get(self, key: str, namespace: Optional[str] = None) -> Optional[str]:
|
|
90
|
+
"""Get a secret value.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
key: Secret key
|
|
94
|
+
namespace: Optional namespace
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
Decrypted secret value or None if not found
|
|
98
|
+
"""
|
|
99
|
+
namespace = namespace or "default"
|
|
100
|
+
secret_file = self.secrets_dir / namespace / f"{key}.secret"
|
|
101
|
+
|
|
102
|
+
if not secret_file.exists():
|
|
103
|
+
return None
|
|
104
|
+
|
|
105
|
+
with open(secret_file, "rb") as f:
|
|
106
|
+
encrypted_value = f.read()
|
|
107
|
+
|
|
108
|
+
try:
|
|
109
|
+
decrypted_value = self._cipher_suite.decrypt(encrypted_value)
|
|
110
|
+
return decrypted_value.decode()
|
|
111
|
+
except Exception as e:
|
|
112
|
+
logger.error(f"Failed to decrypt secret '{key}': {e}")
|
|
113
|
+
return None
|
|
114
|
+
|
|
115
|
+
def list(self, namespace: Optional[str] = None) -> List[str]:
|
|
116
|
+
"""List all secret keys.
|
|
117
|
+
|
|
118
|
+
Args:
|
|
119
|
+
namespace: Optional namespace filter
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
List of secret keys
|
|
123
|
+
"""
|
|
124
|
+
if namespace:
|
|
125
|
+
namespace_dirs = [self.secrets_dir / namespace]
|
|
126
|
+
else:
|
|
127
|
+
namespace_dirs = [
|
|
128
|
+
d for d in self.secrets_dir.iterdir() if d.is_dir() and not d.name.startswith(".")
|
|
129
|
+
]
|
|
130
|
+
|
|
131
|
+
secrets = []
|
|
132
|
+
for namespace_dir in namespace_dirs:
|
|
133
|
+
if namespace_dir.exists():
|
|
134
|
+
for secret_file in namespace_dir.glob("*.secret"):
|
|
135
|
+
key = secret_file.stem
|
|
136
|
+
ns = namespace_dir.name
|
|
137
|
+
secrets.append(f"{ns}/{key}" if not namespace else key)
|
|
138
|
+
|
|
139
|
+
return sorted(secrets)
|
|
140
|
+
|
|
141
|
+
def delete(self, key: str, namespace: Optional[str] = None) -> bool:
|
|
142
|
+
"""Delete a secret.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
key: Secret key
|
|
146
|
+
namespace: Optional namespace
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
True if deleted, False if not found
|
|
150
|
+
"""
|
|
151
|
+
namespace = namespace or "default"
|
|
152
|
+
secret_file = self.secrets_dir / namespace / f"{key}.secret"
|
|
153
|
+
|
|
154
|
+
if secret_file.exists():
|
|
155
|
+
secret_file.unlink()
|
|
156
|
+
logger.debug(f"Secret '{key}' deleted from namespace '{namespace}'")
|
|
157
|
+
return True
|
|
158
|
+
|
|
159
|
+
return False
|
|
160
|
+
|
|
161
|
+
def export_env(self, namespace: Optional[str] = None) -> Dict[str, str]:
|
|
162
|
+
"""Export secrets as environment variables.
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
namespace: Optional namespace filter
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
Dictionary of key-value pairs
|
|
169
|
+
"""
|
|
170
|
+
env_vars = {}
|
|
171
|
+
|
|
172
|
+
for secret_key in self.list(namespace):
|
|
173
|
+
if "/" in secret_key:
|
|
174
|
+
ns, key = secret_key.split("/", 1)
|
|
175
|
+
value = self.get(key, ns)
|
|
176
|
+
else:
|
|
177
|
+
value = self.get(secret_key, namespace)
|
|
178
|
+
|
|
179
|
+
if value:
|
|
180
|
+
# Convert to uppercase for environment variable convention
|
|
181
|
+
if "/" in secret_key:
|
|
182
|
+
env_key = key.upper().replace("-", "_")
|
|
183
|
+
else:
|
|
184
|
+
env_key = secret_key.upper().replace("-", "_")
|
|
185
|
+
env_vars[env_key] = value
|
|
186
|
+
|
|
187
|
+
return env_vars
|
|
188
|
+
|
|
189
|
+
def import_env(self, env_file: Path, namespace: Optional[str] = None) -> int:
|
|
190
|
+
"""Import secrets from an environment file.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
env_file: Path to .env file
|
|
194
|
+
namespace: Optional namespace
|
|
195
|
+
|
|
196
|
+
Returns:
|
|
197
|
+
Number of secrets imported
|
|
198
|
+
"""
|
|
199
|
+
namespace = namespace or "default"
|
|
200
|
+
count = 0
|
|
201
|
+
|
|
202
|
+
with open(env_file) as f:
|
|
203
|
+
for line in f:
|
|
204
|
+
line = line.strip()
|
|
205
|
+
if line and not line.startswith("#") and "=" in line:
|
|
206
|
+
key, value = line.split("=", 1)
|
|
207
|
+
key = key.strip()
|
|
208
|
+
value = value.strip().strip('"').strip("'")
|
|
209
|
+
|
|
210
|
+
self.set(key.lower().replace("_", "-"), value, namespace)
|
|
211
|
+
count += 1
|
|
212
|
+
|
|
213
|
+
return count
|
mcli/lib/secrets/repl.py
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"""
|
|
2
|
+
REPL (Read-Eval-Print Loop) for LSH secrets management.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import List
|
|
8
|
+
|
|
9
|
+
import click
|
|
10
|
+
from prompt_toolkit import prompt
|
|
11
|
+
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
|
|
12
|
+
from prompt_toolkit.completion import WordCompleter
|
|
13
|
+
from prompt_toolkit.history import FileHistory
|
|
14
|
+
|
|
15
|
+
from mcli.lib.logger.logger import get_logger
|
|
16
|
+
|
|
17
|
+
logger = get_logger(__name__)
|
|
18
|
+
from mcli.lib.ui.styling import console, error, info, success, warning
|
|
19
|
+
|
|
20
|
+
from .manager import SecretsManager
|
|
21
|
+
from .store import SecretsStore
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class SecretsREPL:
|
|
25
|
+
"""Interactive REPL for secrets management."""
|
|
26
|
+
|
|
27
|
+
def __init__(self):
|
|
28
|
+
"""Initialize the REPL."""
|
|
29
|
+
self.manager = SecretsManager()
|
|
30
|
+
self.store = SecretsStore()
|
|
31
|
+
self.running = False
|
|
32
|
+
self.namespace = "default"
|
|
33
|
+
self.history_file = Path.home() / ".mcli" / "secrets_repl_history"
|
|
34
|
+
|
|
35
|
+
# Commands
|
|
36
|
+
self.commands = {
|
|
37
|
+
"set": self.cmd_set,
|
|
38
|
+
"get": self.cmd_get,
|
|
39
|
+
"list": self.cmd_list,
|
|
40
|
+
"delete": self.cmd_delete,
|
|
41
|
+
"namespace": self.cmd_namespace,
|
|
42
|
+
"export": self.cmd_export,
|
|
43
|
+
"import": self.cmd_import,
|
|
44
|
+
"push": self.cmd_push,
|
|
45
|
+
"pull": self.cmd_pull,
|
|
46
|
+
"sync": self.cmd_sync,
|
|
47
|
+
"status": self.cmd_status,
|
|
48
|
+
"help": self.cmd_help,
|
|
49
|
+
"exit": self.cmd_exit,
|
|
50
|
+
"quit": self.cmd_exit,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
# Command completer
|
|
54
|
+
self.completer = WordCompleter(list(self.commands.keys()) + ["ns"], ignore_case=True)
|
|
55
|
+
|
|
56
|
+
def run(self):
|
|
57
|
+
"""Run the REPL."""
|
|
58
|
+
self.running = True
|
|
59
|
+
|
|
60
|
+
# Print welcome message
|
|
61
|
+
console.print("[bold cyan]MCLI Secrets Management Shell[/bold cyan]")
|
|
62
|
+
console.print("Type 'help' for available commands or 'exit' to quit.\n")
|
|
63
|
+
|
|
64
|
+
# Create history file directory if needed
|
|
65
|
+
self.history_file.parent.mkdir(parents=True, exist_ok=True)
|
|
66
|
+
|
|
67
|
+
while self.running:
|
|
68
|
+
try:
|
|
69
|
+
# Build prompt
|
|
70
|
+
prompt_text = f"[{self.namespace}]> "
|
|
71
|
+
|
|
72
|
+
# Get user input
|
|
73
|
+
user_input = prompt(
|
|
74
|
+
prompt_text,
|
|
75
|
+
completer=self.completer,
|
|
76
|
+
history=FileHistory(str(self.history_file)),
|
|
77
|
+
auto_suggest=AutoSuggestFromHistory(),
|
|
78
|
+
).strip()
|
|
79
|
+
|
|
80
|
+
if not user_input:
|
|
81
|
+
continue
|
|
82
|
+
|
|
83
|
+
# Parse command and arguments
|
|
84
|
+
parts = user_input.split()
|
|
85
|
+
command = parts[0].lower()
|
|
86
|
+
args = parts[1:] if len(parts) > 1 else []
|
|
87
|
+
|
|
88
|
+
# Handle command aliases
|
|
89
|
+
if command == "ns":
|
|
90
|
+
command = "namespace"
|
|
91
|
+
|
|
92
|
+
# Execute command
|
|
93
|
+
if command in self.commands:
|
|
94
|
+
self.commands[command](args)
|
|
95
|
+
else:
|
|
96
|
+
error(f"Unknown command: {command}")
|
|
97
|
+
console.print("Type 'help' for available commands.")
|
|
98
|
+
|
|
99
|
+
except KeyboardInterrupt:
|
|
100
|
+
console.print("\nUse 'exit' or 'quit' to leave the shell.")
|
|
101
|
+
except EOFError:
|
|
102
|
+
self.cmd_exit([])
|
|
103
|
+
except Exception as e:
|
|
104
|
+
error(f"Error: {e}")
|
|
105
|
+
logger.exception("REPL error")
|
|
106
|
+
|
|
107
|
+
def cmd_set(self, args: List[str]):
|
|
108
|
+
"""Set a secret value."""
|
|
109
|
+
if len(args) < 2:
|
|
110
|
+
error("Usage: set <key> <value>")
|
|
111
|
+
return
|
|
112
|
+
|
|
113
|
+
key = args[0]
|
|
114
|
+
value = " ".join(args[1:])
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
self.manager.set(key, value, self.namespace)
|
|
118
|
+
success(f"Secret '{key}' set in namespace '{self.namespace}'")
|
|
119
|
+
except Exception as e:
|
|
120
|
+
error(f"Failed to set secret: {e}")
|
|
121
|
+
|
|
122
|
+
def cmd_get(self, args: List[str]):
|
|
123
|
+
"""Get a secret value."""
|
|
124
|
+
if len(args) != 1:
|
|
125
|
+
error("Usage: get <key>")
|
|
126
|
+
return
|
|
127
|
+
|
|
128
|
+
key = args[0]
|
|
129
|
+
value = self.manager.get(key, self.namespace)
|
|
130
|
+
|
|
131
|
+
if value is not None:
|
|
132
|
+
# Mask the value for security
|
|
133
|
+
masked_value = (
|
|
134
|
+
value[:3] + "*" * (len(value) - 6) + value[-3:]
|
|
135
|
+
if len(value) > 6
|
|
136
|
+
else "*" * len(value)
|
|
137
|
+
)
|
|
138
|
+
info(f"{key} = {masked_value}")
|
|
139
|
+
|
|
140
|
+
if click.confirm("Show full value?", default=False):
|
|
141
|
+
console.print(f"[yellow]{value}[/yellow]")
|
|
142
|
+
else:
|
|
143
|
+
warning(f"Secret '{key}' not found in namespace '{self.namespace}'")
|
|
144
|
+
|
|
145
|
+
def cmd_list(self, args: List[str]):
|
|
146
|
+
"""List all secrets."""
|
|
147
|
+
secrets = self.manager.list(self.namespace if args != ["all"] else None)
|
|
148
|
+
|
|
149
|
+
if secrets:
|
|
150
|
+
console.print("[bold]Secrets:[/bold]")
|
|
151
|
+
for secret in secrets:
|
|
152
|
+
console.print(f" • {secret}")
|
|
153
|
+
else:
|
|
154
|
+
info("No secrets found")
|
|
155
|
+
|
|
156
|
+
def cmd_delete(self, args: List[str]):
|
|
157
|
+
"""Delete a secret."""
|
|
158
|
+
if len(args) != 1:
|
|
159
|
+
error("Usage: delete <key>")
|
|
160
|
+
return
|
|
161
|
+
|
|
162
|
+
key = args[0]
|
|
163
|
+
|
|
164
|
+
if click.confirm(f"Delete secret '{key}' from namespace '{self.namespace}'?"):
|
|
165
|
+
if self.manager.delete(key, self.namespace):
|
|
166
|
+
success(f"Secret '{key}' deleted")
|
|
167
|
+
else:
|
|
168
|
+
warning(f"Secret '{key}' not found")
|
|
169
|
+
|
|
170
|
+
def cmd_namespace(self, args: List[str]):
|
|
171
|
+
"""Switch namespace."""
|
|
172
|
+
if len(args) == 0:
|
|
173
|
+
# List namespaces
|
|
174
|
+
namespaces = set()
|
|
175
|
+
for d in self.manager.secrets_dir.iterdir():
|
|
176
|
+
if d.is_dir() and not d.name.startswith("."):
|
|
177
|
+
namespaces.add(d.name)
|
|
178
|
+
|
|
179
|
+
console.print(f"[bold]Current namespace:[/bold] {self.namespace}")
|
|
180
|
+
if namespaces:
|
|
181
|
+
console.print("[bold]Available namespaces:[/bold]")
|
|
182
|
+
for ns in sorted(namespaces):
|
|
183
|
+
marker = "→" if ns == self.namespace else " "
|
|
184
|
+
console.print(f" {marker} {ns}")
|
|
185
|
+
elif len(args) == 1:
|
|
186
|
+
self.namespace = args[0]
|
|
187
|
+
success(f"Switched to namespace '{self.namespace}'")
|
|
188
|
+
else:
|
|
189
|
+
error("Usage: namespace [<name>]")
|
|
190
|
+
|
|
191
|
+
def cmd_export(self, args: List[str]):
|
|
192
|
+
"""Export secrets as environment variables."""
|
|
193
|
+
env_vars = self.manager.export_env(self.namespace)
|
|
194
|
+
|
|
195
|
+
if env_vars:
|
|
196
|
+
if args and args[0] == "file":
|
|
197
|
+
# Export to file
|
|
198
|
+
filename = args[1] if len(args) > 1 else f"{self.namespace}.env"
|
|
199
|
+
with open(filename, "w") as f:
|
|
200
|
+
for key, value in env_vars.items():
|
|
201
|
+
f.write(f"{key}={value}\n")
|
|
202
|
+
success(f"Exported {len(env_vars)} secrets to {filename}")
|
|
203
|
+
else:
|
|
204
|
+
# Display export commands
|
|
205
|
+
console.print("[bold]Export commands:[/bold]")
|
|
206
|
+
for key, value in env_vars.items():
|
|
207
|
+
masked_value = value[:3] + "***" + value[-3:] if len(value) > 6 else "***"
|
|
208
|
+
console.print(f"export {key}={masked_value}")
|
|
209
|
+
else:
|
|
210
|
+
info("No secrets to export")
|
|
211
|
+
|
|
212
|
+
def cmd_import(self, args: List[str]):
|
|
213
|
+
"""Import secrets from environment file."""
|
|
214
|
+
if len(args) != 1:
|
|
215
|
+
error("Usage: import <env-file>")
|
|
216
|
+
return
|
|
217
|
+
|
|
218
|
+
env_file = Path(args[0])
|
|
219
|
+
if not env_file.exists():
|
|
220
|
+
error(f"File not found: {env_file}")
|
|
221
|
+
return
|
|
222
|
+
|
|
223
|
+
count = self.manager.import_env(env_file, self.namespace)
|
|
224
|
+
success(f"Imported {count} secrets from {env_file}")
|
|
225
|
+
|
|
226
|
+
def cmd_push(self, args: List[str]):
|
|
227
|
+
"""Push secrets to git store."""
|
|
228
|
+
message = " ".join(args) if args else None
|
|
229
|
+
self.store.push(self.manager.secrets_dir, message)
|
|
230
|
+
|
|
231
|
+
def cmd_pull(self, args: List[str]):
|
|
232
|
+
"""Pull secrets from git store."""
|
|
233
|
+
self.store.pull(self.manager.secrets_dir)
|
|
234
|
+
|
|
235
|
+
def cmd_sync(self, args: List[str]):
|
|
236
|
+
"""Sync secrets with git store."""
|
|
237
|
+
message = " ".join(args) if args else None
|
|
238
|
+
self.store.sync(self.manager.secrets_dir, message)
|
|
239
|
+
|
|
240
|
+
def cmd_status(self, args: List[str]):
|
|
241
|
+
"""Show store status."""
|
|
242
|
+
status = self.store.status()
|
|
243
|
+
|
|
244
|
+
console.print("[bold]Secrets Store Status:[/bold]")
|
|
245
|
+
console.print(f" Initialized: {status['initialized']}")
|
|
246
|
+
console.print(f" Path: {status['store_path']}")
|
|
247
|
+
|
|
248
|
+
if status["initialized"]:
|
|
249
|
+
console.print(f" Branch: {status['branch']}")
|
|
250
|
+
console.print(f" Commit: {status['commit']}")
|
|
251
|
+
console.print(f" Clean: {status['clean']}")
|
|
252
|
+
|
|
253
|
+
if status["has_remote"]:
|
|
254
|
+
console.print(f" Remote: {status['remote_url']}")
|
|
255
|
+
else:
|
|
256
|
+
console.print(" Remote: [dim]Not configured[/dim]")
|
|
257
|
+
|
|
258
|
+
def cmd_help(self, args: List[str]):
|
|
259
|
+
"""Show help information."""
|
|
260
|
+
console.print("[bold]Available Commands:[/bold]\n")
|
|
261
|
+
|
|
262
|
+
help_text = {
|
|
263
|
+
"set": "Set a secret value",
|
|
264
|
+
"get": "Get a secret value",
|
|
265
|
+
"list": "List all secrets (use 'list all' for all namespaces)",
|
|
266
|
+
"delete": "Delete a secret",
|
|
267
|
+
"namespace": "Switch namespace or list namespaces (alias: ns)",
|
|
268
|
+
"export": "Export secrets as environment variables",
|
|
269
|
+
"import": "Import secrets from .env file",
|
|
270
|
+
"push": "Push secrets to git store",
|
|
271
|
+
"pull": "Pull secrets from git store",
|
|
272
|
+
"sync": "Sync secrets with git store",
|
|
273
|
+
"status": "Show store status",
|
|
274
|
+
"help": "Show this help",
|
|
275
|
+
"exit": "Exit the shell (alias: quit)",
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
for cmd, desc in help_text.items():
|
|
279
|
+
console.print(f" [cyan]{cmd:12}[/cyan] {desc}")
|
|
280
|
+
|
|
281
|
+
console.print("\n[bold]Examples:[/bold]")
|
|
282
|
+
console.print(" set api-key sk-1234567890")
|
|
283
|
+
console.print(" get api-key")
|
|
284
|
+
console.print(" namespace production")
|
|
285
|
+
console.print(" export file production.env")
|
|
286
|
+
console.print(" import .env.local")
|
|
287
|
+
|
|
288
|
+
def cmd_exit(self, args: List[str]):
|
|
289
|
+
"""Exit the REPL."""
|
|
290
|
+
self.running = False
|
|
291
|
+
console.print("\nGoodbye!")
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def run_repl():
|
|
295
|
+
"""Entry point for the REPL."""
|
|
296
|
+
repl = SecretsREPL()
|
|
297
|
+
repl.run()
|