Skip to content

Commit b31ab41

Browse files
committed
Replace secret scope with private secret feature (+ fix all linter errors)
1 parent e83139a commit b31ab41

File tree

14 files changed

+375
-231
lines changed

14 files changed

+375
-231
lines changed

src/zenml/cli/secret.py

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
from zenml.constants import SECRET_VALUES
3939
from zenml.enums import (
4040
CliCategories,
41-
SecretScope,
4241
)
4342
from zenml.exceptions import EntityExistsError, ZenKeyError
4443
from zenml.logger import get_logger
@@ -59,11 +58,12 @@ def secret() -> None:
5958
)
6059
@click.argument("name", type=click.STRING)
6160
@click.option(
62-
"--scope",
63-
"-s",
64-
"scope",
65-
type=click.Choice([scope.value for scope in list(SecretScope)]),
66-
default=SecretScope.WORKSPACE.value,
61+
"--private",
62+
"-p",
63+
"private",
64+
is_flag=True,
65+
help="Whether the secret is private. A private secret is only accessible "
66+
"to the user who creates it.",
6767
)
6868
@click.option(
6969
"--interactive",
@@ -84,13 +84,13 @@ def secret() -> None:
8484
)
8585
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
8686
def create_secret(
87-
name: str, scope: str, interactive: bool, values: str, args: List[str]
87+
name: str, private: bool, interactive: bool, values: str, args: List[str]
8888
) -> None:
8989
"""Create a secret.
9090
9191
Args:
9292
name: The name of the secret to create.
93-
scope: The scope of the secret to create.
93+
private: Whether the secret is private.
9494
interactive: Whether to use interactive mode to enter the secret values.
9595
values: Secret key-value pairs to be passed as JSON or YAML.
9696
args: The arguments to pass to the secret.
@@ -152,7 +152,7 @@ def create_secret(
152152
with console.status(f"Saving secret `{name}`..."):
153153
try:
154154
client.create_secret(
155-
name=name, values=parsed_args, scope=SecretScope(scope)
155+
name=name, values=parsed_args, private=private
156156
)
157157
declare(f"Secret '{name}' successfully created.")
158158
except EntityExistsError as e:
@@ -186,7 +186,7 @@ def list_secrets(**kwargs: Any) -> None:
186186
dict(
187187
name=secret.name,
188188
id=str(secret.id),
189-
scope=secret.scope.value,
189+
private=secret.private,
190190
)
191191
for secret in secrets.items
192192
]
@@ -200,22 +200,26 @@ def list_secrets(**kwargs: Any) -> None:
200200
type=click.STRING,
201201
)
202202
@click.option(
203-
"--scope",
204-
"-s",
205-
type=click.Choice([scope.value for scope in list(SecretScope)]),
206-
default=None,
203+
"--private",
204+
"-p",
205+
"private",
206+
type=click.BOOL,
207+
required=False,
208+
help="Use this flag to explicitly fetch a private secret or a public secret.",
207209
)
208-
def get_secret(name_id_or_prefix: str, scope: Optional[str] = None) -> None:
210+
def get_secret(name_id_or_prefix: str, private: Optional[bool] = None) -> None:
209211
"""Get a secret and print it to the console.
210212
211213
Args:
212214
name_id_or_prefix: The name of the secret to get.
213-
scope: The scope of the secret to get.
215+
private: Private status of the secret to filter for.
214216
"""
215-
secret = _get_secret(name_id_or_prefix, scope)
217+
secret = _get_secret(name_id_or_prefix, private)
218+
scope = ""
219+
if private is not None:
220+
scope = "private " if private else "public "
216221
declare(
217-
f"Fetched secret with name `{secret.name}` and ID `{secret.id}` in "
218-
f"scope `{secret.scope.value}`:"
222+
f"Fetched {scope}secret with name `{secret.name}` and ID `{secret.id}`:"
219223
)
220224
if not secret.secret_values:
221225
warning(f"Secret with name `{name_id_or_prefix}` is empty.")
@@ -224,25 +228,22 @@ def get_secret(name_id_or_prefix: str, scope: Optional[str] = None) -> None:
224228

225229

226230
def _get_secret(
227-
name_id_or_prefix: str, scope: Optional[str] = None
231+
name_id_or_prefix: str, private: Optional[bool] = None
228232
) -> SecretResponse:
229233
"""Get a secret with a given name, prefix or id.
230234
231235
Args:
232236
name_id_or_prefix: The name of the secret to get.
233-
scope: The scope of the secret to get.
237+
private: Private status of the secret to filter for.
234238
235239
Returns:
236240
The secret response model.
237241
"""
238242
client = Client()
239243
try:
240-
if scope:
241-
return client.get_secret(
242-
name_id_or_prefix=name_id_or_prefix, scope=SecretScope(scope)
243-
)
244-
else:
245-
return client.get_secret(name_id_or_prefix=name_id_or_prefix)
244+
return client.get_secret(
245+
name_id_or_prefix=name_id_or_prefix, private=private
246+
)
246247
except ZenKeyError as e:
247248
error(
248249
f"Error fetching secret with name id or prefix "
@@ -267,9 +268,12 @@ def _get_secret(
267268
type=click.STRING,
268269
)
269270
@click.option(
270-
"--new-scope",
271-
"-s",
272-
type=click.Choice([scope.value for scope in list(SecretScope)]),
271+
"--private",
272+
"-p",
273+
"private",
274+
type=click.BOOL,
275+
required=False,
276+
help="Update the private status of the secret.",
273277
)
274278
@click.option(
275279
"--interactive",
@@ -293,7 +297,7 @@ def _get_secret(
293297
def update_secret(
294298
name_or_id: str,
295299
extra_args: List[str],
296-
new_scope: Optional[str] = None,
300+
private: Optional[bool] = None,
297301
remove_keys: List[str] = [],
298302
interactive: bool = False,
299303
values: str = "",
@@ -302,7 +306,7 @@ def update_secret(
302306
303307
Args:
304308
name_or_id: The name or id of the secret to update.
305-
new_scope: The new scope of the secret.
309+
private: Private status of the secret to update.
306310
extra_args: The arguments to pass to the secret.
307311
interactive: Whether to use interactive mode to update the secret.
308312
remove_keys: The keys to remove from the secret.
@@ -331,10 +335,7 @@ def update_secret(
331335
except NotImplementedError as e:
332336
error(f"Centralized secrets management is disabled: {str(e)}")
333337

334-
declare(
335-
f"Updating secret with name '{secret.name}' and ID '{secret.id}' in "
336-
f"scope '{secret.scope.value}:"
337-
)
338+
declare(f"Updating secret with name '{secret.name}' and ID '{secret.id}'")
338339

339340
if "name" in parsed_args:
340341
error("The word 'name' cannot be used as a key for a secret.")
@@ -388,7 +389,7 @@ def update_secret(
388389

389390
client.update_secret(
390391
name_id_or_prefix=secret.id,
391-
new_scope=SecretScope(new_scope) if new_scope else None,
392+
update_private=private,
392393
add_or_update_values=secret_args_add_update,
393394
remove_values=remove_keys,
394395
)
@@ -492,10 +493,12 @@ def delete_secret(name_or_id: str, yes: bool = False) -> None:
492493
type=click.STRING,
493494
)
494495
@click.option(
495-
"--scope",
496-
"-s",
497-
type=click.Choice([scope.value for scope in list(SecretScope)]),
498-
default=None,
496+
"--private",
497+
"-p",
498+
"private",
499+
type=click.BOOL,
500+
required=False,
501+
help="Use this flag to explicitly fetch a private secret or a public secret.",
499502
)
500503
@click.option(
501504
"--filename",
@@ -509,7 +512,7 @@ def delete_secret(name_or_id: str, yes: bool = False) -> None:
509512
)
510513
def export_secret(
511514
name_id_or_prefix: str,
512-
scope: Optional[str] = None,
515+
private: Optional[bool] = None,
513516
filename: Optional[str] = None,
514517
) -> None:
515518
"""Export a secret as a YAML file.
@@ -519,12 +522,12 @@ def export_secret(
519522
520523
Args:
521524
name_id_or_prefix: The name of the secret to export.
522-
scope: The scope of the secret to export.
525+
private: Private status of the secret to export.
523526
filename: The name of the file to export the secret to.
524527
"""
525528
from zenml.utils.yaml_utils import write_yaml
526529

527-
secret = _get_secret(name_id_or_prefix=name_id_or_prefix, scope=scope)
530+
secret = _get_secret(name_id_or_prefix=name_id_or_prefix, private=private)
528531
if not secret.secret_values:
529532
warning(f"Secret with name `{name_id_or_prefix}` is empty.")
530533
return

0 commit comments

Comments
 (0)