Skip to content

Commit

Permalink
Cleanup + Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricky White committed Jul 26, 2024
1 parent 9905437 commit a035b6e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 40 deletions.
3 changes: 1 addition & 2 deletions sdk/python/core/keeper_secrets_manager_core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from keeper_secrets_manager_core.exceptions import KeeperError
from keeper_secrets_manager_core.keeper_globals import keeper_secrets_manager_sdk_client_id, keeper_public_keys, \
logger_name, keeper_servers
from keeper_secrets_manager_core.storage import FileKeyValueStorage, KeyValueStorage, InMemoryKeyValueStorage, SecureOSStorage
from keeper_secrets_manager_core.storage import FileKeyValueStorage, KeyValueStorage, InMemoryKeyValueStorage
from keeper_secrets_manager_core.utils import base64_to_bytes, dict_to_json, \
url_safe_str_to_bytes, bytes_to_base64, generate_random_bytes, now_milliseconds, string_to_bytes, json_to_dict, \
bytes_to_string, strtobool
Expand Down Expand Up @@ -86,7 +86,6 @@ def __init__(self,
# If the config is not defined and the KSM_CONFIG env var exists, get the config from the env var.
if config is None and os.environ.get("KSM_CONFIG") is not None:
config = InMemoryKeyValueStorage(os.environ.get("KSM_CONFIG"))

elif token:

token = token.strip()
Expand Down
13 changes: 2 additions & 11 deletions sdk/python/core/tests/config_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import sys
import unittest
import os
import tempfile
import json
import codecs

from keeper_secrets_manager_core.storage import FileKeyValueStorage, InMemoryKeyValueStorage, SecureOSStorage
from keeper_secrets_manager_core.storage import FileKeyValueStorage, InMemoryKeyValueStorage
from keeper_secrets_manager_core import SecretsManager
from keeper_secrets_manager_core.configkeys import ConfigKeys
from keeper_secrets_manager_core.mock import MockConfig
Expand All @@ -24,13 +23,6 @@ def setUp(self):
# Make the the config is not set in the env var. Will screw up certain tests.
os.environ.pop("KSM_CONFIG", None)

# Create mock secure storage executable
self.mock_exec_path = os.path.join(self.orig_working_dir, "keeper_secrets_manager_core", "mock_secure_exec.py")

# sys.executable returns the path of the current python interpreter
# which is used to run the mock_secure_exec.py file
self.python_interpreter = sys.executable

def tearDown(self):

os.chdir(self.orig_working_dir)
Expand Down Expand Up @@ -407,5 +399,4 @@ def test_config_file_mode(self):
if platform.lower().startswith("win") is True:
subprocess.run('icacls.exe "{}" /grant Everyone:F'.format(file))
os.unlink(file)
os.chdir(self.orig_working_dir)

os.chdir(self.orig_working_dir)
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import platform
import subprocess

from core.keeper_secrets_manager_core import exceptions
from core.keeper_secrets_manager_core.configkeys import ConfigKeys
from core.keeper_secrets_manager_core.storage import KeyValueStorage
from core.keeper_secrets_manager_core.keeper_globals import logger_name
from core.keeper_secrets_manager_core.utils import base64_to_string, json_to_dict
from keeper_secrets_manager_core import exceptions
from keeper_secrets_manager_core.configkeys import ConfigKeys
from keeper_secrets_manager_core.storage import KeyValueStorage
from keeper_secrets_manager_core.keeper_globals import logger_name
from keeper_secrets_manager_core.utils import base64_to_string, json_to_dict


class SecureOSStorage(KeyValueStorage):
Expand Down Expand Up @@ -78,7 +78,6 @@ def read_storage(self) -> dict:
config = json_to_dict(base64_to_string(result))
for key in config:
self.config[ConfigKeys.get_enum(key)] = config[key]

return self.config

def save_storage(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# |_|
#
# Keeper Secrets Manager
# Copyright 2023 Keeper Security Inc.
# Copyright 2024 Keeper Security Inc.
# Contact: [email protected]

import sys
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import sys
import unittest
import os
import tempfile
import json
import codecs

from keeper_secrets_manager_core.storage import FileKeyValueStorage, InMemoryKeyValueStorage, SecureOSStorage
from keeper_secrets_manager_core import SecretsManager
from keeper_secrets_manager_core.configkeys import ConfigKeys
from keeper_secrets_manager_core.mock import MockConfig
import io
from contextlib import redirect_stderr
from sys import platform
import subprocess
from keeper_secrets_manager_storage.storage_secure_os import SecureOSStorage


class SecureOSTest(unittest.TestCase):
def setUp(self):
self.orig_working_dir = os.getcwd()

# Create mock secure storage executable
self.mock_exec_path = os.path.join(self.orig_working_dir, "keeper_secrets_manager_core", "mock_secure_exec.py")

# sys.executable returns the path of the current python interpreter
self.mock_exec_path = os.path.join(
self.orig_working_dir, "tests", "mock_secure_exec.py"
)

# sys.executable returns the path of the current python interpreter
# which is used to run the mock_secure_exec.py file
self.python_interpreter = sys.executable

Expand All @@ -37,9 +31,13 @@ def test_secure_os_storage(self):
storage.set(ConfigKeys.KEY_CLIENT_ID, mock_config.get("clientId"))
storage.set(ConfigKeys.KEY_APP_KEY, mock_config.get("appKey"))
storage.set(ConfigKeys.KEY_PRIVATE_KEY, mock_config.get("privateKey"))
self.assertEqual(mock_config.get("clientId"), storage.get(ConfigKeys.KEY_CLIENT_ID))
self.assertEqual(
mock_config.get("clientId"), storage.get(ConfigKeys.KEY_CLIENT_ID)
)
self.assertEqual(mock_config.get("appKey"), storage.get(ConfigKeys.KEY_APP_KEY))
self.assertEqual(mock_config.get("privateKey"), storage.get(ConfigKeys.KEY_PRIVATE_KEY))
self.assertEqual(
mock_config.get("privateKey"), storage.get(ConfigKeys.KEY_PRIVATE_KEY)
)

# test contains()
self.assertTrue(storage.contains(ConfigKeys.KEY_CLIENT_ID))
Expand All @@ -54,20 +52,17 @@ def test_secure_os_storage(self):

def test_secure_os_storage_read_storage(self):
storage = SecureOSStorage(
app_name="TEST",
exec_path=[self.python_interpreter, self.mock_exec_path]
app_name="TEST", exec_path=[self.python_interpreter, self.mock_exec_path]
)

storage.read_storage()
self.assertIsNotNone(storage.get(ConfigKeys.KEY_CLIENT_ID))

def test_secure_os_storage_save_storage(self):
storage = SecureOSStorage(
app_name="TEST",
exec_path=[self.python_interpreter, self.mock_exec_path]
app_name="TEST", exec_path=[self.python_interpreter, self.mock_exec_path]
)
storage.config = MockConfig.make_config()

# Test save_storage() doesn't raise an exception
storage.save_storage()

0 comments on commit a035b6e

Please sign in to comment.