From 3cf1013e639bae40984c51e5ce04f734f0e436d7 Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Fri, 13 Sep 2024 19:28:41 +0200 Subject: [PATCH] Support base64 encoded credentials for VIM creation Change-Id: I719d76ba089047358b9447a2df64929600fd08d5 Signed-off-by: garciadeblas --- osmclient/cli_commands/vim.py | 38 +++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/osmclient/cli_commands/vim.py b/osmclient/cli_commands/vim.py index bb7622d..c3377a9 100755 --- a/osmclient/cli_commands/vim.py +++ b/osmclient/cli_commands/vim.py @@ -13,8 +13,10 @@ # License for the specific language governing permissions and limitations # under the License. +import base64 import click from osmclient.common import print_output +from osmclient.common.exceptions import ClientException from osmclient.cli_commands import utils from prettytable import PrettyTable import yaml @@ -52,7 +54,11 @@ def _check_ca_cert(vim_config: dict) -> None: default=None, help="VIM specific config parameters in YAML or JSON file", ) -@click.option("--account_type", default="openstack", help="VIM type") +@click.option( + "--account_type", + default="openstack", + help="VIM type (openstack, aws, azure, gcp, vmware)", +) @click.option("--description", default=None, help="human readable description") @click.option( "--sdn_controller", @@ -139,8 +145,18 @@ def vim_create( vim_config = utils.create_config(config_file, config) _check_ca_cert(vim_config) if creds: - with open(creds, "r") as cf: - vim_config["credentials"] = yaml.safe_load(cf.read()) + with open(creds, "rb") as cf: + creds_content = cf.read() + vim_config["credentials_base64"] = base64.b64encode(creds_content).decode( + "utf-8" + ) + if account_type != "aws": + try: + # Convert binary content to text before loading as YAML + creds_text = creds_content.decode("utf-8") + vim_config["credentials"] = yaml.safe_load(creds_text) + except (UnicodeDecodeError, yaml.YAMLError) as e: + raise ClientException(f"Error decoding credentials file: {e}") ctx.obj.vim.create( name, vim, vim_config, sdn_controller, sdn_port_mapping, wait=wait ) @@ -159,7 +175,7 @@ def vim_create( default=None, help="VIM specific config parameters in YAML or JSON file", ) -@click.option("--account_type", help="VIM type") +@click.option("--account_type", help="VIM type (openstack, aws, azure, gcp, vmware)") @click.option("--description", help="human readable description") @click.option( "--sdn_controller", @@ -245,8 +261,18 @@ def vim_update( vim_config = utils.create_config(config_file, config) _check_ca_cert(vim_config) if creds: - with open(creds, "r") as cf: - vim_config["credentials"] = yaml.safe_load(cf.read()) + with open(creds, "rb") as cf: + creds_content = cf.read() + vim_config["credentials_base64"] = base64.b64encode(creds_content).decode( + "utf-8" + ) + if account_type != "aws": + try: + # Convert binary content to text before loading as YAML + creds_text = creds_content.decode("utf-8") + vim_config["credentials"] = yaml.safe_load(creds_text) + except (UnicodeDecodeError, yaml.YAMLError) as e: + raise ClientException(f"Error decoding credentials file: {e}") prometheus_config = {} if prometheus_url: prometheus_config["prometheus-url"] = prometheus_url -- 2.25.1