Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements secure value loading method for multiple Windows credentials #191

Merged
merged 46 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
724fad7
Implementws secure value loading method for multiple Windows credentials
samadpls Jun 23, 2023
4752106
Modified implementation of the `__get_secure_value` method
samadpls Jun 26, 2023
6032bcc
fix condition statement
samadpls Jun 26, 2023
c5e997b
modified the logic
samadpls Jun 27, 2023
078e8bb
added comments
samadpls Jun 27, 2023
94fff12
still working not yet compelete
samadpls Jun 27, 2023
eb24899
removed the logic from zosmf_profile
samadpls Jun 28, 2023
079e298
implemneted the get and set logic in config_file
samadpls Jun 29, 2023
e5d817c
typo
samadpls Jun 29, 2023
1eeb12d
fixed typo
samadpls Jun 29, 2023
1997c43
updated test file
samadpls Jun 29, 2023
d55cf99
updated the logic
samadpls Jul 3, 2023
b848160
updated the `_retrieve_password` and added a unit for it
samadpls Jul 4, 2023
12caa47
updated `set_secure_props` and added unit test for testing `normal_cr…
samadpls Jul 4, 2023
2bb7a1f
added
samadpls Jul 4, 2023
0241a08
Added unit test for `set_secure_props`, modified `_retrieve_password`…
samadpls Jul 5, 2023
cb50b26
updated the logic
samadpls Jul 6, 2023
4ac5b7d
updated testcases
samadpls Jul 6, 2023
f75fc33
updated
samadpls Jul 6, 2023
1a4e21e
updated a `CHANGELOG.md`
samadpls Jul 6, 2023
3c5e293
added mock patch
samadpls Jul 6, 2023
7385860
addressed comments and update test cases and `set_secure_prop` logic
samadpls Jul 19, 2023
5046976
Updated the encoding to UTF-16 for Windows
samadpls Jul 28, 2023
8de7a6e
Update WIN32_CRED_MAX_STRING_LENGTH value, refactor ConfigFile set_pr…
samadpls Jul 30, 2023
2b0a708
updated credential manager
samadpls Jul 30, 2023
2ce0dc4
Update credential_manager.py
samadpls Jul 31, 2023
1d93782
Update credential_manager.py
samadpls Jul 31, 2023
a50d56c
refactor: defined charset for file
samadpls Jul 31, 2023
d249d10
fixed the typo in the config file
samadpls Jul 31, 2023
ae629f9
refactor: refactor credential manager
samadpls Aug 3, 2023
07ac8ce
typo fixed
samadpls Aug 3, 2023
4ea12a7
Merge branch 'main' into multi-credentials
samadpls Aug 7, 2023
9d618f5
added unit tests for the `CredentialManager` class and Addressed comment
samadpls Aug 8, 2023
792ded3
added
samadpls Aug 8, 2023
a1b9c66
typo fixed
samadpls Aug 8, 2023
6c71b94
typo fixed in load_secure_props()
samadpls Aug 9, 2023
85ea9e0
fixed the typo mistake in `load_secure_prop`
samadpls Aug 10, 2023
bf4829e
Added condition for non-Windows system
samadpls Aug 11, 2023
99d3fb1
updat the test cases
samadpls Aug 11, 2023
6c16930
Refactored the `save_secure_props` function.
samadpls Aug 16, 2023
39eb6dc
updated the test case
samadpls Aug 17, 2023
ceb3093
removed unused imports
samadpls Aug 22, 2023
fd729be
removed else condition
samadpls Aug 23, 2023
2c1ae28
fixed typo mistakes
samadpls Aug 23, 2023
716ef7d
fixed typo
samadpls Aug 23, 2023
58178c0
updated setUpCred method
samadpls Aug 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 78 additions & 4 deletions src/core/zowe/core_for_zowe_sdk/config_file.py
samadpls marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,11 @@ def load_secure_props(self) -> None:

if sys.platform == "win32":
service_name += "/" + constants["ZoweAccountName"]

secret_value = keyring.get_password(
service_name, constants["ZoweAccountName"]
)
secret_value = self._retrieve_password(service_name)
else:
secret_value = keyring.get_password(
service_name, constants["ZoweAccountName"]
)

except Exception as exc:
raise SecureProfileLoadFailed(
Expand All @@ -340,3 +341,76 @@ def load_secure_props(self) -> None:
f" with error '{error_msg}'",
SecurePropsNotFoundWarning,
)
# self.set_secure_props()

def _retrieve_password(self, service_name: str) -> str:
"""
Retrieve the password from the keyring or storage.
If the password exceeds the maximum length, retrieve it in parts.
Parameters
----------
service_name: str
The service name for the password retrieval
Returns
-------
str
The retrieved password
"""
password = keyring.get_password(service_name, constants["ZoweAccountName"])

if password is None:
# Retrieve the secure value with an index
index = 1
while True:
field_name = f"{constants['ZoweAccountName']}-{index}"
temp_value = keyring.get_password(service_name, field_name)
if temp_value is None:
break
password += temp_value
index += 1

return password

def set_secure_props(self) -> None:
"""
Set secure_props for the given config file
Returns
-------
None
"""
if not HAS_KEYRING:
return

try:
service_name = constants["ZoweServiceName"]

if sys.platform == "win32":
service_name += "/" + constants["ZoweAccountName"]
credential = self.secure_props.get(self.filepath, "")

if len(credential) > constants["WIN32_CRED_MAX_STRING_LENGTH"]:
# Split the credential string into chunks of maximum length
keyring.delete_password(service_name, constants["ZoweAccountName"])
chunk_size = constants["WIN32_CRED_MAX_STRING_LENGTH"]
chunks = [credential[i : i + chunk_size] for i in range(0, len(credential), chunk_size)]

# Set the individual chunks as separate keyring entries
for index, chunk in enumerate(chunks, start=1):
field_name = f"{constants['ZoweAccountName']}-{index}"
keyring.set_password(service_name, field_name, chunk)
else:
# Credential length is within the maximum limit, set it as a single keyring entry
keyring.set_password(service_name, constants["ZoweAccountName"], credential)
else:
credential = self.secure_props.get(self.filepath)
keyring.set_password(
service_name, constants["ZoweAccountName"],
credential)
samadpls marked this conversation as resolved.
Show resolved Hide resolved

except KeyError as exc:
error_msg = str(exc)
warnings.warn(
f"No credentials found for loaded config file '{self.filepath}'"
f" with error '{error_msg}'",
SecurePropsNotFoundWarning,
)
1 change: 1 addition & 0 deletions src/core/zowe/core_for_zowe_sdk/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
"ZoweCredentialKey": "Zowe-Plugin",
"ZoweServiceName": "Zowe",
"ZoweAccountName": "secure_config_props",
"WIN32_CRED_MAX_STRING_LENGTH" : 2560
}
2 changes: 1 addition & 1 deletion src/core/zowe/core_for_zowe_sdk/zosmf_profile.py
samadpls marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,4 @@ def get_password(self, service, username):
else:
return self.__get_password(service, username, collection)

keyring.set_keyring(KeyringBackend())
keyring.set_keyring(KeyringBackend())
2 changes: 0 additions & 2 deletions tests/unit/test_zowe_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from jsonschema import validate, ValidationError
from zowe.core_for_zowe_sdk.validators import validate_config_json
import commentjson

from pyfakefs.fake_filesystem_unittest import TestCase
from zowe.core_for_zowe_sdk import (
ApiConnection,
Expand Down Expand Up @@ -158,7 +157,6 @@ def test_object_should_be_instance_of_class(self):
zosmf_profile = ZosmfProfile(self.profile_name)
self.assertIsInstance(zosmf_profile, ZosmfProfile)


class TestZosmfProfileManager(TestCase):
"""ProfileManager class unit tests."""

Expand Down