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 e825543 commit 77e2178
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions fedcloudclient/secret.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Implementation of "fedcloud secret" commands for accessing secret management service
"""
from urllib.error import HTTPError

import click
import hvac
import requests
Expand All @@ -17,7 +19,6 @@
from fedcloudclient.secret_helper import decrypt_data, encrypt_data, print_secrets, print_value, secret_params_to_dict
from fedcloudclient.vault_auth import VaultToken


VAULT_ADDR = CONF.get("vault_endpoint")
VAULT_ROLE = CONF.get("vault_role")
VAULT_MOUNT_POINT = CONF.get("vault_mount_point")
Expand Down Expand Up @@ -132,13 +133,13 @@ def secret():
help="Name of output file",
)
def get(
token: VaultToken,
short_path: str,
key: str,
output_format: str,
decrypt_key: str,
binary_file: bool,
output_file: str,
token: VaultToken,
short_path: str,
key: str,
output_format: str,
decrypt_key: str,
binary_file: bool,
output_file: str,
):
"""
Get the secret object in the path. If a key is given, print only the value of the key
Expand All @@ -160,14 +161,22 @@ def get(
@secret_token_params
@click.argument("short_path", metavar="[secret path]", required=False, default="")
def list_(
token: VaultToken,
short_path: str,
token: VaultToken,
short_path: str,
):
"""
List secret objects in the path
"""
response = token.vault_command(command="list", path=short_path, data={})
print("\n".join(map(str, response["data"]["keys"])))
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}")


@secret.command()
Expand All @@ -187,11 +196,11 @@ def list_(
help="True for reading secrets from binary files",
)
def put(
token: VaultToken,
short_path: str,
secrets: list,
encrypt_key: str,
binary_file: bool,
token: VaultToken,
short_path: str,
secrets: list,
encrypt_key: str,
binary_file: bool,
):
"""
Put a secret object to the path. Secrets are provided in form key=value
Expand All @@ -207,8 +216,8 @@ def put(
@secret_token_params
@click.argument("short_path", metavar="[secret path]")
def delete(
token: VaultToken,
short_path,
token: VaultToken,
short_path,
):
"""
Delete the secret object in the path
Expand Down

0 comments on commit 77e2178

Please sign in to comment.