diff --git a/integration/keeper_secrets_manager_cli/README.md b/integration/keeper_secrets_manager_cli/README.md index 4ba4837a..e5058d4a 100644 --- a/integration/keeper_secrets_manager_cli/README.md +++ b/integration/keeper_secrets_manager_cli/README.md @@ -6,6 +6,10 @@ For more information see our official documentation page https://docs.keeper.io/ # Change History +## 1.0.12 + +* Fix problem with the same temp file being opened when exporting profile. Was causing a `Permission denied` error. + ## 1.0.11 * Fix missing linefeed when selecting `immutable` for k8s token init. diff --git a/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/export.py b/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/export.py index 8ecba643..ae466811 100644 --- a/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/export.py +++ b/integration/keeper_secrets_manager_cli/keeper_secrets_manager_cli/export.py @@ -2,9 +2,11 @@ import tempfile import base64 import json +import os from keeper_secrets_manager_core.utils import base64_to_bytes from keeper_secrets_manager_core.keeper_globals import keeper_servers +from .exception import KsmCliException class Export: @@ -45,8 +47,17 @@ def _format_ini(self): # Apparently the config parser doesn't like temp files. So create a # temp file, then open a file for writing and use that to write # the config. Then read the temp file to get our new config. - with tempfile.NamedTemporaryFile() as tf: - config = Config(ini_file=tf.name) + + temp_file_name = None + try: + # Create temp file and close it. We want a filename. Prevent the temp file from being deleted + # when we close it. Remove it with a "finally". + tf = tempfile.NamedTemporaryFile(delete=False) + temp_file_name = tf.name + tf.close() + + # Make a configuration with one profile and the save it to the temp file. + config = Config(ini_file=temp_file_name) config.set_profile(Profile.default_profile, client_id=self.config.client_id, private_key=self.config.private_key, @@ -57,9 +68,16 @@ def _format_ini(self): config.config.active_profile = Profile.default_profile config.save() - tf.seek(0) - config_str = tf.read() - tf.close() + # Open the temp file and read the configuration that was created above. + with open(temp_file_name, "r") as config_fh: + config_str = config_fh.read() + config_fh.close() + except Exception as err: + raise KsmCliException("Could not export profile: {}".format(err)) + finally: + # Make sure we delete the temp file. + if temp_file_name is not None and os.path.exists(temp_file_name) is True: + os.unlink(temp_file_name) return config_str diff --git a/integration/keeper_secrets_manager_cli/setup.py b/integration/keeper_secrets_manager_cli/setup.py index f4f74f48..75a4e28d 100644 --- a/integration/keeper_secrets_manager_cli/setup.py +++ b/integration/keeper_secrets_manager_cli/setup.py @@ -25,7 +25,7 @@ # Version set in the keeper_secrets_manager_cli.version file. setup( name="keeper-secrets-manager-cli", - version="1.0.11", + version="1.0.12", description="Command line tool for Keeper Secrets Manager", long_description=long_description, long_description_content_type="text/markdown", diff --git a/integration/keeper_secrets_manager_cli/tests/config_test.py b/integration/keeper_secrets_manager_cli/tests/config_test.py index 2ca5d22f..487ce0a4 100644 --- a/integration/keeper_secrets_manager_cli/tests/config_test.py +++ b/integration/keeper_secrets_manager_cli/tests/config_test.py @@ -19,7 +19,7 @@ def setUp(self) -> None: # Make a fake keeper.ini file. export = Export(config=MockConfig().make_config(), file_format="ini", plain=True) with open("keeper.ini", "w") as fh: - fh.write(export.run().decode()) + fh.write(export.run()) fh.close() def tearDown(self) -> None: