Skip to content

Commit

Permalink
SIMPLE-7063 Added group_display_attribute and refactored test_auth (#122
Browse files Browse the repository at this point in the history
)

Do not fail if importing topologies for previous CML versions
  • Loading branch information
tmikuska authored Oct 29, 2024
1 parent 3a995a8 commit 708438e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 52 deletions.
92 changes: 43 additions & 49 deletions virl2_client/models/auth_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,78 +196,62 @@ def refresh_ldap_groups(self):
url = self._url_for("refresh")
self._session.put(url)

def test_auth(self, config: dict, username: str, password: str) -> dict:
@staticmethod
def _get_auth(
username: str | None = None,
password: str | None = None,
group_name: str | None = None,
) -> dict:
result = {}
if username is not None and password is not None:
result["auth-data"] = {"username": username, "password": password}
if group_name is not None:
result["group-data"] = {"group_name": group_name}
return result

def test_auth(
self,
config: dict,
username: str | None = None,
password: str | None = None,
group_name: str | None = None,
) -> dict:
"""
Test a set of credentials against the specified authentication configuration.
Test a set of credentials and/or group against the specified authentication
configuration.
:param config: A dictionary of authentication settings to test against
(including manager password).
:param username: The username to test.
:param password: The password to test.
:param group_name: The group name to test.
:returns: Results of the test.
"""
body = {
"auth-config": config,
"auth-data": {"username": username, "password": password},
}
url = self._url_for("test")
response = self._session.post(url, json=body)
return response.json()

def test_group(self, config: dict, group_name: str) -> dict:
"""
Test a group against the specified authentication configuration.
:param config: A dictionary of authentication settings to test against
(including manager password).
:param username: The group name to test.
:returns: Results of the test.
"""
body = {
"auth-config": config,
"auth-data": {"group_name": group_name},
}
body = {"auth-config": config} | self._get_auth(username, password, group_name)
url = self._url_for("test")
response = self._session.post(url, json=body)
return response.json()

def test_current_auth(
self, manager_password: str, username: str, password: str
self,
manager_password: str,
username: str | None = None,
password: str | None = None,
group_name: str | None = None,
) -> dict:
"""
Test a set of credentials against the currently applied authentication
configuration.
Test a set of credentials and/or group against the currently applied
authentication configuration.
:param manager_password: The manager password to allow testing.
:param username: The username to test.
:param password: The password to test.
:param group_name: The group name to test.
:returns: Results of the test.
"""
current = self.get_settings()
current["manager_password"] = manager_password
body = {
"auth-config": current,
"auth-data": {"username": username, "password": password},
}
url = self._url_for("test")
response = self._session.post(url, json=body)
return response.json()

def test_current_group(self, manager_password: str, group_name: str) -> dict:
"""
Test a group against the currently applied authentication
configuration.
:param manager_password: The manager password to allow testing.
:param username: The group name to test.
:returns: Results of the test.
"""
current = self.get_settings()
current["manager_password"] = manager_password
body = {
"auth-config": current,
"auth-data": {"group_name": group_name},
}
body = {"auth-config": current} | self._get_auth(username, password, group_name)
url = self._url_for("test")
response = self._session.post(url, json=body)
return response.json()
Expand Down Expand Up @@ -491,6 +475,16 @@ def display_attribute(self, value: str) -> None:
"""Set the display name LDAP attribute."""
self._update_setting("display_attribute", value)

@property
def group_display_attribute(self) -> str:
"""Return the group display name LDAP attribute."""
return self._get_setting("group_display_attribute")

@group_display_attribute.setter
def group_display_attribute(self, value: str) -> None:
"""Set the group display name LDAP attribute."""
self._update_setting("group_display_attribute", value)

@property
def email_address_attribute(self) -> str:
"""Return the email address LDAP attribute."""
Expand Down
7 changes: 4 additions & 3 deletions virl2_client/models/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,10 @@ def _handle_import_annotations(self, topology: dict) -> None:
raise ElementAlreadyExists("Annotation already exists")

self._import_annotation(annotation_id, annotation)

if "smart_annotations" not in topology:
return

for smart_annotation in topology["smart_annotations"]:
smart_annotation_id = smart_annotation["id"]

Expand Down Expand Up @@ -2039,7 +2043,6 @@ def _find_node_in_topology(node_id: str, topology: dict) -> dict:
:returns: The node with the specified ID.
:raises NodeNotFound: If the node cannot be found in the topology.
"""

for node in topology["nodes"]:
if node["id"] == node_id:
return node
Expand All @@ -2059,7 +2062,6 @@ def _find_annotation_in_topology(
:returns: The annotation with the specified ID.
:raises AnnotationNotFound: If the annotation cannot be found in the topology.
"""

for annotation in topology["annotations"]:
if annotation["id"] == annotation_id:
return annotation
Expand All @@ -2079,7 +2081,6 @@ def _find_smart_annotation_in_topology(
:returns: The annotation with the specified ID.
:raises AnnotationNotFound: If the annotation cannot be found in the topology.
"""

for annotation in topology["smart_annotations"]:
if annotation["id"] == annotation_id:
return annotation
Expand Down

0 comments on commit 708438e

Please sign in to comment.