From 7d5734af83989be0ddd0e7f9eb7297f2829de73a Mon Sep 17 00:00:00 2001 From: checktheroads Date: Fri, 31 Jul 2020 01:58:18 -0700 Subject: [PATCH] improve hyperglass-agent certificate import --- hyperglass/api/routes.py | 3 ++- hyperglass/api/tasks.py | 18 ++---------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/hyperglass/api/routes.py b/hyperglass/api/routes.py index 76fd4fd..41b69c3 100644 --- a/hyperglass/api/routes.py +++ b/hyperglass/api/routes.py @@ -180,7 +180,7 @@ async def import_certificate(encoded_request: EncodedRequest): secret=matched_device.credential.password.get_secret_value(), ) except HyperglassError as decode_error: - raise HTTPException(detail=str(decode_error), status_code=401) + raise HTTPException(detail=str(decode_error), status_code=400) try: # Write certificate to file @@ -192,6 +192,7 @@ async def import_certificate(encoded_request: EncodedRequest): except RuntimeError as err: raise HyperglassError(str(err), level="danger") + log.info("Added public key for {}", encoded_request.device) return { "output": f"Added public key for {encoded_request.device}", "level": "success", diff --git a/hyperglass/api/tasks.py b/hyperglass/api/tasks.py index 0e2c1ee..1761d4f 100644 --- a/hyperglass/api/tasks.py +++ b/hyperglass/api/tasks.py @@ -1,7 +1,6 @@ """Tasks to be executed from web API.""" # Standard Library -import re from typing import Dict, Union from pathlib import Path @@ -12,20 +11,7 @@ from httpx import Headers def import_public_key( app_path: Union[Path, str], device_name: str, keystring: str ) -> bool: - """Import a public key for hyperglass-agent. - - Arguments: - app_path {Path|str} -- hyperglass app path - device_name {str} -- Device name - keystring {str} -- Public key - - Raises: - RuntimeError: Raised if unable to create certs directory - RuntimeError: Raised if written key does not match input - - Returns: - {bool} -- True if file was written - """ + """Import a public key for hyperglass-agent.""" if not isinstance(app_path, Path): app_path = Path(app_path) @@ -37,7 +23,7 @@ def import_public_key( if not cert_dir.exists(): raise RuntimeError(f"Failed to create certs directory at {str(cert_dir)}") - filename = re.sub(r"[^A-Za-z0-9]", "_", device_name) + ".pem" + filename = f"{device_name}.pem" cert_file = cert_dir / filename with cert_file.open("w+") as file: