-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ADMIN roles through config file (#3475)
* Add ADMIN roles through config file PBENCH-1197 With the change to SSO, we've lost our own private realm and the ability to manage roles within it. We may be able to restore roles through Keycloak and LDAP groups, but this provides a temporary "quick and dirty" mechanism to define ADMIN roles for a server based on the username provided and cached from OIDC tokens. We define a simple `admin-role` config variable which can be defined to a comma-separated list of usernames to be granted ADMIN role. This value is processed by the authorization code when it caches local `User` objects for validation. I've added a functional test for the `audit` API, which requires ADMIN role, both to prove that it works and to provide a long-delayed minimal validation of auditing. (I decided not to merge this into the overloaded "datasets" test file, which means it can't easily be run last: this makes it a less rigorous "audit test", but that can be addressed later and it provides an "ADMIN role test" that's necessary now.)
- Loading branch information
Showing
9 changed files
with
175 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,24 @@ | |
|
||
from pbench.client import PbenchServerClient | ||
from pbench.client.oidc_admin import OIDCAdmin | ||
from pbench.client.types import JSONOBJECT | ||
from pbench.server.auth import OpenIDClientError | ||
|
||
USERNAME: str = "tester" | ||
EMAIL: str = "[email protected]" | ||
PASSWORD: str = "123456" | ||
FIRST_NAME: str = "Test" | ||
LAST_NAME: str = "User" | ||
USER = { | ||
"username": "tester", | ||
"email": "[email protected]", | ||
"password": "123456", | ||
"first_name": "Test", | ||
"last_name": "User", | ||
} | ||
|
||
ADMIN = { | ||
"username": "testadmin", | ||
"email": "[email protected]", | ||
"password": "123456", | ||
"first_name": "Admin", | ||
"last_name": "Tester", | ||
} | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
|
@@ -41,17 +52,9 @@ def oidc_admin(server_client: PbenchServerClient): | |
return OIDCAdmin(server_url=server_client.endpoints["openid"]["server"]) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def register_test_user(oidc_admin: OIDCAdmin): | ||
"""Create a test user for functional tests.""" | ||
def register_user(oidc_admin: OIDCAdmin, user: JSONOBJECT): | ||
try: | ||
response = oidc_admin.create_new_user( | ||
username=USERNAME, | ||
email=EMAIL, | ||
password=PASSWORD, | ||
first_name=FIRST_NAME, | ||
last_name=LAST_NAME, | ||
) | ||
response = oidc_admin.create_new_user(**user) | ||
except OpenIDClientError as e: | ||
# To allow testing outside our transient CI containers, allow the tester | ||
# user to already exist. | ||
|
@@ -62,10 +65,31 @@ def register_test_user(oidc_admin: OIDCAdmin): | |
assert response.ok, f"Register failed with {response.json()}" | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def register_test_user(oidc_admin: OIDCAdmin): | ||
"""Create a test user for functional tests.""" | ||
register_user(oidc_admin, USER) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def register_admintest_user(oidc_admin: OIDCAdmin): | ||
"""Create a test user matching the configured Pbench admin.""" | ||
register_user(oidc_admin, ADMIN) | ||
|
||
|
||
@pytest.fixture | ||
def login_user(server_client: PbenchServerClient, register_test_user): | ||
"""Log in the test user and return the authentication token""" | ||
server_client.login(USERNAME, PASSWORD) | ||
server_client.login(USER["username"], USER["password"]) | ||
assert server_client.auth_token | ||
yield | ||
server_client.auth_token = None | ||
|
||
|
||
@pytest.fixture | ||
def login_admin(server_client: PbenchServerClient, register_admintest_user): | ||
"""Log in the test user and return the authentication token""" | ||
server_client.login(ADMIN["username"], ADMIN["password"]) | ||
assert server_client.auth_token | ||
yield | ||
server_client.auth_token = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from pbench.client import API, PbenchServerClient | ||
|
||
|
||
class TestAudit: | ||
def test_get_all(self, server_client: PbenchServerClient, login_admin): | ||
""" | ||
Verify that we can retrieve the Pbench Server audit log. | ||
This relies on a "testadmin" user which has been granted ADMIN role | ||
via the pbench-server.cfg file for functional testing. The audit API | ||
should succeed without permissions failure, and we'll validate the | ||
audit fields of the records we see. | ||
""" | ||
response = server_client.get(API.SERVER_AUDIT, {}) | ||
json = response.json() | ||
assert ( | ||
response.ok | ||
), f"Reading audit log failed {response.status_code},{json['message']}" | ||
assert isinstance(json, list) | ||
print(f" ... read {len(json)} audit records") | ||
for audit in json: | ||
assert isinstance(audit["id"], int) | ||
assert audit["name"] | ||
assert audit["operation"] in ("CREATE", "READ", "UPDATE", "DELETE") | ||
assert audit["reason"] in (None, "PERMISSION", "INTERNAL", "CONSISTENCY") | ||
assert "root_id" in audit | ||
if audit["root_id"]: | ||
assert isinstance(audit["root_id"], int) | ||
assert audit["status"] in ("BEGIN", "SUCCESS", "FAILURE", "WARNING") | ||
assert audit["timestamp"] | ||
assert audit["attributes"] | ||
assert audit["object_type"] in ( | ||
"API_KEY", | ||
"CONFIG", | ||
"DATASET", | ||
"NONE", | ||
"TEMPLATE", | ||
) | ||
if audit["object_type"] != "NONE": | ||
assert audit["object_name"] | ||
if audit["object_type"] == "DATASET": | ||
assert audit["object_id"] | ||
if audit["user_name"] not in (None, "BACKGROUND"): | ||
assert audit["user_id"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.