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

Save Profile Properties to Config File & Secure Vault Storage (#73, #72) #201

Merged
merged 33 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3f5ee4c
added methods for saving profile properties to profile_manager
samadpls Jul 21, 2023
ff47822
updated the logic of `set_property`
samadpls Jul 24, 2023
d60cdca
updated the logic to save the profile properties to the config file
samadpls Jul 27, 2023
4cd58a3
added a is_secure logic to the config_file.py
samadpls Jul 27, 2023
82e3d46
working on it
samadpls Jul 27, 2023
97c9e2c
updated the set_profile_property method to update the profile property
samadpls Jul 27, 2023
028b0bc
refactor: set profile properties in config file
samadpls Jul 28, 2023
8b0c155
working on save profile prop
samadpls Jul 29, 2023
3395018
refactored `set_property()` and added `save()` on both profile and co…
samadpls Jul 30, 2023
4ed9126
updated
samadpls Jul 31, 2023
584da0e
added `set_profile` method to ProfileManager & ConfigFile class
samadpls Jul 31, 2023
83d8d58
updated the config file
samadpls Aug 3, 2023
0e9ca9c
Merge branch 'multi-credentials' into save-profile-prop
t1m0thyj Aug 11, 2023
f79f447
Added unit tests for profile_manager `set_property`, `save`, and `ge…
samadpls Aug 14, 2023
4b885ee
working on nested profile logic
samadpls Aug 15, 2023
3ce1c93
added nested logic in get_highest_priority for handling profiles not …
samadpls Aug 16, 2023
0cf156e
updated the test cases
samadpls Aug 17, 2023
4f7e483
Merge branch 'multi-credentials' into save-profile-prop
samadpls Aug 17, 2023
13dff17
updated save() method
samadpls Aug 17, 2023
b02d437
Implemented and Validated Nested and Single Profile Functionality
samadpls Aug 18, 2023
5721d09
typo fixed
samadpls Aug 19, 2023
4058b07
added unit test of config_file methods
samadpls Aug 21, 2023
edf6eef
fixed typo
samadpls Aug 23, 2023
2627db3
refactored code and addressed issue
samadpls Aug 23, 2023
b13bad3
fixed typo
samadpls Aug 23, 2023
46ac09d
Updated tests and added load_secure_props to set_property & set_profi…
samadpls Aug 24, 2023
00c9349
Merge branch 'main' into save-profile-prop
zFernand0 Aug 29, 2023
e20f624
Merge branch 'main' into save-profile-prop
samadpls Aug 30, 2023
ffc50b1
updated set_proprty
samadpls Aug 30, 2023
08f2897
updated CHANGELOG.md file
samadpls Sep 1, 2023
0f27841
refactored the code, and added error handling support
samadpls Sep 14, 2023
8c64d72
Merge branch 'main' into save-profile-prop
samadpls Sep 14, 2023
858c66e
Merge branch 'main' into save-profile-prop
t1m0thyj Sep 27, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

All notable changes to the Zowe Client Python SDK will be documented in this file.

## Recent Changes

- Feature: Added method to Save profile properties to zowe.config.json file
57 changes: 57 additions & 0 deletions src/core/zowe/core_for_zowe_sdk/config_file.py
traeok marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,60 @@ def load_secure_props(self) -> None:
f" with error '{error_msg}'",
SecurePropsNotFoundWarning,
)

def __is_secure(self, json_path: str) -> bool:
"""
Check whether the given JSON path corresponds to a secure property.

Parameters:
json_path (str): The JSON path of the property to check.

Returns:
bool: True if the property should be stored securely, False otherwise.
"""
profile_name, property_name = json_path.split(".")[1:]
profile = self.find_profile(profile_name, self.profiles)
if profile and profile.get("secure"):
return property_name in profile["secure"]
return False

def set_property(self, json_path, value, secure=False):
samadpls marked this conversation as resolved.
Show resolved Hide resolved
"""
Set a property in the profile, storing it securely if necessary.

Parameters:
json_path (str): The JSON path of the property to set.
value (str): The value to be set for the property.
secure (bool): If True, the property will be stored securely. Default is False.
"""
if self.profiles is None:
self.init_from_file()

segments = json_path.split(".")
updated_profiles = self.profiles

while len(segments) > 1:
profile_name = segments[0]
updated_profiles[profile_name] = updated_profiles.get(profile_name, {})
updated_profiles = updated_profiles[profile_name]
segments.pop(0)

# If the property should be stored securely, add it to the secure layer
if secure or self.__is_secure(json_path):
updated_profiles["secure"] = updated_profiles.get("secure", [])
property_name = segments[1]
if property_name not in updated_profiles["secure"]:
updated_profiles["secure"].append(property_name)


else:
# Store the property in plain text
updated_profiles["properties"] = updated_profiles.get("properties", {})
updated_profiles["properties"][segments[1]] = value

# Save the updated profile to the file
self.save()
def save(self) :
"""
working on it
"""
43 changes: 43 additions & 0 deletions src/core/zowe/core_for_zowe_sdk/profile_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,46 @@ def load(
warnings.resetwarnings()

return profile_props

def get_highest_priority_layer(self, profile_name: str, layers: list[ConfigFile]) -> Optional[ConfigFile]:
"""
Get the highest priority layer (configuration file) based on the given profile name and the list of layers.

Parameters:
profile_name (str): The name of the profile to look for in the layers.
layers (list[ConfigFile]): The list of ConfigFile objects representing different configuration files.

Returns:
Optional[ConfigFile]: The highest priority layer (configuration file) that contains the specified profile,
or None if the profile is not found in any layer.
"""
highest_priority_layer = None

for layer in layers:
try:
profile = layer.get_profile(profile_name=profile_name, profile_type=None)
if profile.name:
highest_priority_layer = layer
break
except ProfileNotFound:
pass

return highest_priority_layer

def set_property(self, json_path, value, secure=False):
# Get all layers to search for the highest priority layer
layers = [self.project_user_config, self.project_config, self.global_user_config, self.global_config]
profile_name = ".".join(json_path.split(".")[:2])

# Find the highest priority layer for the given profile name
highest_priority_layer = self.get_highest_priority_layer(profile_name, layers)

# If the profile doesn't exist in any layer, use the project user config as the default location
if not highest_priority_layer:
highest_priority_layer = self.project_user_config

# Set the property in the highest priority layer
highest_priority_layer.set_property(json_path, value, highest_priority_layer, secure=secure)

# Save the modified configuration file
highest_priority_layer.save()