From e95a1ee8c9e03e624d648e0817385a58a1985193 Mon Sep 17 00:00:00 2001 From: idimov-keeper <78815270+idimov-keeper@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:55:32 -0500 Subject: [PATCH] Added --profile-name option to `ksm profile import` command (#478) --- .../keeper_secrets_manager_cli/__main__.py | 6 ++++-- .../keeper_secrets_manager_cli/profile.py | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/__main__.py b/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/__main__.py index a28c1bed..80fa6c92 100644 --- a/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/__main__.py +++ b/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/__main__.py @@ -489,14 +489,16 @@ def profile_export_command(ctx, plain, file_format, profile_name): cls=HelpColorsCommand, help_options_color='blue' ) +@click.option('--profile-name', '-p', type=str, required=False, help='Config profile to import into.') @click.option('--output-file', '-f', type=str, required=False, help='Create the config in a specific file location.') @click.argument('config-base64', type=str, required=True, nargs=1) @click.pass_context -def profile_import_command(ctx, output_file, config_base64): +def profile_import_command(ctx, profile_name, output_file, config_base64): """Import an encrypted config file""" Profile(cli=ctx.obj["cli"], config=global_config).import_config( - file=output_file, config_base64=config_base64, + file=output_file, + profile_name=profile_name, launched_from_app=global_config.launched_from_app ) diff --git a/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/profile.py b/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/profile.py index b557e269..d4d3bbf2 100644 --- a/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/profile.py +++ b/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/profile.py @@ -372,9 +372,7 @@ def import_config(config_base64, file=None, profile_name=None, launched_from_app """ Take base64 config file and write it back to disk. - This file could be a JSON or a Keeper ini file. - """ config_data = base64.urlsafe_b64decode(config_base64.encode()) @@ -392,15 +390,26 @@ def import_config(config_base64, file=None, profile_name=None, launched_from_app # If a JSON file was import, convert the JSON to a INI. if is_json is True: + if profile_name is None: + profile_name = os.environ.get("KSM_CLI_PROFILE", Profile.default_profile) config = Config(ini_file=file) + + try: + # If the file exists attempt to load as INI to merge new config + config.load() + except: + pass # doesn't exists, inaccessible, or not INI: create new + config.set_profile_using_base64( - profile_name=Profile.default_profile, + profile_name=profile_name, base64_config=config_base64 ) config.save() # Else just save the INI. It's in the right format, just save it. No processing needed. else: + if profile_name: + print("Ignored option --profile-name as incompaible with INI file format that can handle multiple profiles.") with open(file, "w") as fh: fh.write(config_data.decode()) fh.close()