Skip to content

Commit

Permalink
roles: keycloak role user management updates
Browse files Browse the repository at this point in the history
Adding new methods to KeycloakUser class to extend user management
support:  delete, modify, set_password

Also update docstrings for kcadm to reflect that it returns
SSHProcessResult.
  • Loading branch information
spoore1 committed Apr 23, 2024
1 parent 38f852d commit 828c66f
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions sssd_test_framework/roles/keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def kcadm(self, command: str) -> SSHProcessResult:
:param command: kcadm command
:type command: str
:return: SSH exec result
:rtype: SSHProcessResult
"""
kcadm = "/opt/keycloak/bin/kcadm.sh"
command_split = shlex.split(command)
Expand Down Expand Up @@ -98,6 +100,7 @@ def __init__(self, role: Keycloak, name: str) -> None:
:type name: str
"""
super().__init__(role, name)
self.get()

def add(
self,
Expand Down Expand Up @@ -126,12 +129,66 @@ def add(

return self

def delete(self) -> None:
def delete(
self,
) -> SSHProcessResult:
"""
Delete Keycloak user.
:return: SSH exec result
:rtype: SSHProcessResult
"""
del_user = f"delete users/{self.id}"
self.role.kcadm(del_user)
result = self.role.kcadm(del_user)
return result

def modify(
self,
*,
firstName: str | None = None,
lastName: str | None = None,
email: str | None = None,
enabled: bool | None = None,
) -> KeycloakUser:
"""
Modify keycloak user attributes.
Parameters that are not set are ignored.
:param firstName: User's first name
:type firstName: str | None, optional
:param lastName: User's last name
:type lastName: str | None, optional
:param email: User's email address
:type email: str | None, optional
:param enabled: User account enabled/disabled status
:type enabled: bool | None, optional
:return: SSH exec result
:rtype: SSHProcessResult
"""

attrs = {"firstName": firstName, "lastName": lastName, "email": email, "enabled": enabled}

update_attrs = " ".join([f'-s "{k}={v}"' for k, v in attrs.items() if v is not None])
update_user = f"update users/{self.id} {update_attrs}"
self.role.kcadm(update_user)
return self

def set_password(
self,
password: str | None = "Secret123",
) -> KeycloakUser:
"""
Set Keycloak user password.
:param password: Password, defaults to 'Secret123'
:type password: str | None, optional
:return: Self,
:rtype: KeycloakUser
"""
set_password = "set-password -r master " f"--username {self.name} " f"--new-password {password}"
self.role.kcadm(set_password)
return self

def get(self) -> dict[str, list[str]]:
"""
Expand All @@ -152,6 +209,8 @@ def get(self) -> dict[str, list[str]]:
out.setdefault(key, [])
out[key].append(json1[key])

self.id = out["id"][0]

return out

def get_groups(self) -> list[dict[str, list[str]]]:
Expand Down

0 comments on commit 828c66f

Please sign in to comment.