Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Mask sensitive fields in secrets and improve secret listing output #25

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions javelin_cli/_internal/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
UnauthorizedError,
)
from javelin_sdk.models import (
JavelinConfig,
Gateway,
GatewayConfig,
Model,
Expand All @@ -24,6 +25,7 @@
Route,
RouteConfig,
Secret,
Secrets,
Template,
Templates,
)
Expand Down Expand Up @@ -63,8 +65,8 @@
javelin_api_key = selected_gateway["api_key_value"]

# Print all the relevant variables for debugging (optional)
print(f"Base URL: {base_url}")
print(f"Javelin API Key: {javelin_api_key}")
# print(f"Base URL: {base_url}")
# print(f"Javelin API Key: {javelin_api_key}")

# Ensure the API key is set before initializing
if not javelin_api_key or javelin_api_key == "":
Expand All @@ -78,11 +80,13 @@
)

# Initialize the JavelinClient when required
return JavelinClient(
config = JavelinConfig(
base_url=base_url,
javelin_api_key=javelin_api_key,
)

return JavelinClient(config)


def create_gateway(args):
try:
Expand Down Expand Up @@ -471,14 +475,31 @@
except Exception as e:
print(f"Unexpected error: {e}")


def list_secrets(args):
try:
client = get_javelin_client()

secrets = client.list_secrets()
print("List of secrets:")
print(json.dumps(secrets, indent=2, default=lambda o: o.__dict__))
# Fetch the list of secrets from the client
secrets_response = client.list_secrets()
# print(secrets_response.json(indent=2))

# Check if the response is an instance of Secrets
if isinstance(secrets_response, Secrets):
secrets_list = secrets_response.secrets

# Check if there are no secrets
if not secrets_list:
print("No secrets available.")
return

# Iterate over the secrets and mask sensitive data
masked_secrets = [secret.masked() for secret in secrets_list]

# Print the masked secrets
print(json.dumps({"secrets": masked_secrets}, indent=2))

else:
print(f"Unexpected secret format: {secrets_response}")

except UnauthorizedError as e:
print(f"UnauthorizedError: {e}")
Expand All @@ -487,14 +508,16 @@
except Exception as e:
print(f"Unexpected error: {e}")


def get_secret(args):
try:
client = get_javelin_client()

# Fetch the secret and mask sensitive data
secret = client.get_secret(args.api_key)
masked_secret = secret.masked() # Ensure the sensitive fields are masked

print(f"Secret details for '{args.api_key}':")
print(json.dumps(secret, indent=2, default=lambda o: o.__dict__))
print(json.dumps(masked_secret, indent=2))

except UnauthorizedError as e:
print(f"UnauthorizedError: {e}")
Expand Down
17 changes: 16 additions & 1 deletion javelin_sdk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,22 @@ class Secret(BaseModel):
default=True, description="Whether the secret is enabled"
)


def masked(self):
"""
Return a version of the model where sensitive fields are masked.
"""
return {
"api_key": self.api_key,
"api_key_secret_name": self.api_key_secret_name,
"api_key_secret_key": "***MASKED***" if self.api_key_secret_key else None,
"api_key_secret_key_javelin": "***MASKED***" if self.api_key_secret_key_javelin else None,
"provider_name": self.provider_name,
"query_param_key": self.query_param_key,
"header_key": self.header_key,
"group": self.group,
"enabled": self.enabled,
}

class Secrets(BaseModel):
secrets: List[Secret] = Field(default=[], description="List of secrets")

Expand Down