Skip to content

Commit

Permalink
Fixing commented concerns.
Browse files Browse the repository at this point in the history
Signed-off-by: Elijah Swift <elijah.swift@ibm.com>
  • Loading branch information
ElijahSwiftIBM committed Oct 27, 2023
1 parent e2ad702 commit c497017
Show file tree
Hide file tree
Showing 26 changed files with 126 additions and 134 deletions.
4 changes: 2 additions & 2 deletions pyracf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from .access.access_admin import AccessAdmin
from .common.add_operation_error import AddOperationError
from .common.alter_operation_error import AlterOperationError
from .common.invalid_segment_name_error import InvalidSegmentNameError
from .common.invalid_segment_trait_error import InvalidSegmentTraitError
from .common.security_request_error import SecurityRequestError
from .common.segment_error import SegmentError
from .common.segment_trait_error import SegmentTraitError
from .connection.connection_admin import ConnectionAdmin
from .data_set.data_set_admin import DataSetAdmin
from .group.group_admin import GroupAdmin
Expand Down
8 changes: 4 additions & 4 deletions pyracf/common/add_operation_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

class AddOperationError(Exception):
"""
Raised when a profile passed into an Add is successfully extracted.
Raised when a profile cannot be added because it already exists.
"""

def __init__(self, profile_name: str, class_name: str) -> None:
self.message = "Security request made to IRRSMO00 failed."
admin_types = ["USER", "GROUP", "DATASET"]
admin_types = ["user", "group", "dataSet"]
if class_name not in admin_types:
self.message += (
"\n\nTarget profile "
+ f"'{profile_name}' already exists as a profile in the {class_name} class."
+ f"'{profile_name}' already exists as a profile in the '{class_name}' class."
)
else:
self.message += (
"\n\nTarget profile "
+ f"'{profile_name}' already exists as a {class_name} profile."
+ f"'{profile_name}' already exists as a '{class_name}' profile."
)

def __str__(self) -> str:
Expand Down
8 changes: 4 additions & 4 deletions pyracf/common/alter_operation_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

class AlterOperationError(Exception):
"""
Raised when a profile passed into an Alter is not successfully extracted.
Raised when a profile cannot be altered because it does not exist.
"""

def __init__(self, profile_name: str, class_name: str) -> None:
self.message = "Security request made to IRRSMO00 failed."
admin_types = ["USER", "GROUP", "DATASET"]
admin_types = ["user", "group", "dataSet"]
if class_name not in admin_types:
self.message += (
"\n\nTarget profile "
+ f"'{profile_name}' does not exist as a profile in the {class_name} class."
+ f"'{profile_name}' does not exist as a profile in the '{class_name}' class."
)
else:
self.message += (
"\n\nTarget profile "
+ f"'{profile_name}' does not exist as a {class_name} profile."
+ f"'{profile_name}' does not exist as a '{class_name}' profile."
)

def __str__(self) -> str:
Expand Down
18 changes: 0 additions & 18 deletions pyracf/common/invalid_segment_name_error.py

This file was deleted.

18 changes: 0 additions & 18 deletions pyracf/common/invalid_segment_trait_error.py

This file was deleted.

32 changes: 15 additions & 17 deletions pyracf/common/security_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from datetime import datetime
from typing import Any, List, Tuple, Union

from .invalid_segment_name_error import InvalidSegmentNameError
from .invalid_segment_trait_error import InvalidSegmentTraitError
from .irrsmo00 import IRRSMO00
from .logger import Logger
from .security_request import SecurityRequest
from .security_request_error import SecurityRequestError
from .security_result import SecurityResult
from .segment_error import SegmentError
from .segment_trait_error import SegmentTraitError


class SecurityAdmin:
Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(
"base:passphrase": "racf:phrase",
}
self.__irrsmo00 = IRRSMO00()
self.__profile_type = profile_type
self._profile_type = profile_type
self._segment_traits = {}
# used to preserve segment traits for debug logging.
self.__preserved_segment_traits = {}
Expand Down Expand Up @@ -262,21 +262,20 @@ def __validate_and_add_trait(

def _build_bool_segment_dictionaries(self, segments: List[str]) -> None:
"""Build segment dictionaries for profile extract."""
invalid_segments = []
bad_segments = []
for segment in segments:
if segment in self._valid_segment_traits:
self._segment_traits[segment] = True
else:
invalid_segments.append(segment)

if invalid_segments:
raise InvalidSegmentNameError(invalid_segments)
bad_segments.append(segment)
if bad_segments:
raise SegmentError(bad_segments, self._profile_type)
# preserve segment traits for debug logging.
self.__preserved_segment_traits = self._segment_traits

def _build_segment_dictionaries(self, traits: dict) -> None:
"""Build segemnt dictionaries for each segment."""
invalid_traits = []
bad_traits = []
for trait in traits:
trait_valid = False
for segment in self._valid_segment_traits:
Expand All @@ -286,10 +285,9 @@ def _build_segment_dictionaries(self, traits: dict) -> None:
if trait_valid:
break
if not trait_valid:
invalid_traits.append(trait)

if invalid_traits:
raise InvalidSegmentTraitError(invalid_traits)
bad_traits.append(trait)
if bad_traits:
raise SegmentTraitError(bad_traits, self._profile_type)

# preserve segment traits for debug logging.
self.__preserved_segment_traits = self._segment_traits
Expand Down Expand Up @@ -325,7 +323,7 @@ def _get_profile(
if self._generate_requests_only:
# Allows this function to work with "self._generate_requests_only" mode.
return result
return result["securityResult"][self.__profile_type]["commands"][0]["profiles"][
return result["securityResult"][self._profile_type]["commands"][0]["profiles"][
index
]

Expand Down Expand Up @@ -373,15 +371,15 @@ def _format_profile_generic(self, messages: str) -> None:
current_segment = messages[i].split()[0].lower()
profile[current_segment] = {}
i += 2
if self.__profile_type in ("dataSet", "resource"):
if self._profile_type in ("dataSet", "resource"):
i = self.__format_data_set_generic_profile_data(
messages, profile, current_segment, i
)
if self.__profile_type == "user":
if self._profile_type == "user":
i = self.__format_user_profile_data(
messages, profile, current_segment, i
)
if self.__profile_type == "group":
if self._profile_type == "group":
i = self.__format_group_profile_data(
messages, profile, current_segment, i
)
Expand Down
6 changes: 4 additions & 2 deletions pyracf/common/security_request_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ def __init__(self, result: dict) -> None:
def __str__(self) -> str:
return self.message

def scan_for_error(self, security_definition_tag: str, error_id: str):
def contains_error_message(
self, security_definition_tag: str, error_message_id: str
):
commands = self.result["securityResult"][security_definition_tag].get(
"commands"
)
if not isinstance(commands, list):
return False
messages = commands[0].get("messages", [])
if error_id in "".join(messages):
if error_message_id in "".join(messages):
return True
else:
return False
17 changes: 17 additions & 0 deletions pyracf/common/segment_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Exception to use when the user passes bad segment name(s) on an extract request."""


class SegmentError(Exception):
"""
Raised when a user passes a bad segment name on an extract request.
"""

def __init__(self, invalid_segments: list, profile_type: str) -> None:
self.message = "Unable to build Security Request.\n\n"
for segment in invalid_segments:
self.message += (
f"'{segment}' is not a valid segment for '{profile_type}'.\n"
)

def __str__(self) -> str:
return self.message
16 changes: 16 additions & 0 deletions pyracf/common/segment_trait_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Exception to use when the user passes bad segment-trait name(s) on an add/alter request."""


class SegmentTraitError(Exception):
"""
Raised when a user passes an invalid segment-trait combination in the traits dictionary.
"""

def __init__(self, invalid_traits: list, profile_type: str) -> None:
self.message = "Unable to build Security Request.\n\n"
for trait in invalid_traits:
self.message += f"'{trait}' is not a valid segment-trait "
self.message += f"combination for '{profile_type}'.\n"

def __str__(self) -> str:
return self.message
6 changes: 3 additions & 3 deletions pyracf/data_set/data_set_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ def add(
try:
self.extract(data_set, volume=volume, generic=generic)
except SecurityRequestError as exception:
if not exception.scan_for_error("dataSet", "ICH35003I"):
if not exception.contains_error_message(self._profile_type, "ICH35003I"):
raise exception
self._build_segment_dictionaries(traits)
data_set_request = DataSetRequest(data_set, "set", volume, generic)
self._build_xml_segments(data_set_request)
return self._make_request(data_set_request)
raise AddOperationError(data_set, "DATASET")
raise AddOperationError(data_set, self._profile_type)

def alter(
self,
Expand All @@ -131,7 +131,7 @@ def alter(
try:
self.extract(data_set)
except SecurityRequestError:
raise AlterOperationError(data_set, "DATASET")
raise AlterOperationError(data_set, self._profile_type)
"""Alter an existing data set profile."""
self._build_segment_dictionaries(traits)
data_set_request = DataSetRequest(data_set, "set", volume, generic)
Expand Down
6 changes: 3 additions & 3 deletions pyracf/group/group_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,20 @@ def add(self, group: str, traits: dict = {}) -> Union[dict, bytes]:
try:
self.extract(group)
except SecurityRequestError as exception:
if not exception.scan_for_error("group", "ICH51003I"):
if not exception.contains_error_message(self._profile_type, "ICH51003I"):
raise exception
self._build_segment_dictionaries(traits)
group_request = GroupRequest(group, "set")
self._build_xml_segments(group_request)
return self._make_request(group_request)
raise AddOperationError(group, "GROUP")
raise AddOperationError(group, self._profile_type)

def alter(self, group: str, traits: dict) -> Union[dict, bytes]:
"""Alter an existing group."""
try:
self.extract(group)
except SecurityRequestError:
raise AlterOperationError(group, "GROUP")
raise AlterOperationError(group, self._profile_type)
self._build_segment_dictionaries(traits)
group_request = GroupRequest(group, "set")
self._build_xml_segments(group_request, alter=True)
Expand Down
2 changes: 1 addition & 1 deletion pyracf/resource/resource_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def add(
try:
self.extract(resource, class_name)
except SecurityRequestError as exception:
if not exception.scan_for_error("resource", "ICH13003I"):
if not exception.contains_error_message(self._profile_type, "ICH13003I"):
raise exception
self._build_segment_dictionaries(traits)
profile_request = ResourceRequest(resource, class_name, "set")
Expand Down
Loading

0 comments on commit c497017

Please sign in to comment.