Skip to content

Commit

Permalink
Catching exceptions for listing empty secret folders
Browse files Browse the repository at this point in the history
  • Loading branch information
tdviet committed Nov 10, 2023
1 parent 171f291 commit c14ea4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions fedcloudclient/locker_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def vault_command(self, command: str, path: str, data: dict, vo: str = None):
log_and_raise(f"Invalid command {command}", ConfigError)

response.raise_for_status()

if command in ["list", "get"]:
response_json = response.json()
return dict(response_json)
Expand Down
34 changes: 18 additions & 16 deletions fedcloudclient/secret.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Implementation of "fedcloud secret" commands for accessing secret management service
"""
from urllib.error import HTTPError
import sys

import click
import hvac
Expand Down Expand Up @@ -145,16 +145,19 @@ def get(
Get the secret object in the path. If a key is given, print only the value of the key
"""

response = token.vault_command(command="get", path=short_path, data={})
if decrypt_key:
decrypt_data(decrypt_key, response["data"])
if not key:
print_secrets(output_file, output_format, response["data"])
else:
if key in response["data"]:
print_value(output_file, binary_file, response["data"][key])
try:
response = token.vault_command(command="get", path=short_path, data={})
if decrypt_key:
decrypt_data(decrypt_key, response["data"])
if not key:
print_secrets(output_file, output_format, response["data"])
else:
raise SystemExit(f"Error: {key} not found in {short_path}")
if key in response["data"]:
print_value(output_file, binary_file, response["data"][key])
else:
raise SystemExit(f"Error: {key} not found in {short_path}")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}", file=sys.stderr)


@secret.command("list")
Expand All @@ -170,13 +173,12 @@ def list_(
try:
response = token.vault_command(command="list", path=short_path, data={})
print("\n".join(map(str, response["data"]["keys"])))
except HTTPError as e:
if e.code == 404:
pass
else:
print(f"HTTPError occurred. Error code: {e.code}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
message = str(e)
if "HTTPError: 404" in message:
print(f"The target path is empty or does not exist.", file=sys.stderr)
else:
print(f"An unexpected error occurred: {str(e)}", file=sys.stderr)


@secret.command()
Expand Down

0 comments on commit c14ea4d

Please sign in to comment.