-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from jsmolar/opa_external
Tests for OPA external registry
- Loading branch information
Showing
11 changed files
with
166 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Module for Mockserver integration""" | ||
from urllib.parse import urljoin | ||
|
||
import httpx | ||
|
||
|
||
class Mockserver: | ||
"""Mockserver deployed in Openshift (located in Tools or self-managed)""" | ||
|
||
def __init__(self, url): | ||
self.url = url | ||
|
||
def create_expectation(self, expectation_id, path, opa_policy): | ||
"""Creates an Expectation - response with body that contains OPA policy (Rego query)""" | ||
response = httpx.put( | ||
urljoin(self.url, "/mockserver/expectation"), verify=False, timeout=5, json={ | ||
"id": expectation_id, | ||
"httpRequest": { | ||
"path": path | ||
}, | ||
"httpResponse": { | ||
"headers": { | ||
"Content-Type": ["plain/text"] | ||
}, | ||
"body": opa_policy | ||
} | ||
} | ||
) | ||
response.raise_for_status() | ||
return response | ||
|
||
def clear_expectation(self, expectation_id): | ||
"""Clears Expectation with specific ID""" | ||
httpx.put( | ||
urljoin(self.url, "/mockserver/clear"), verify=False, timeout=5, json={ | ||
"id": expectation_id | ||
} | ||
).raise_for_status() |
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
Empty file.
23 changes: 23 additions & 0 deletions
23
testsuite/tests/kuadrant/authorino/authorization/opa/conftest.py
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,23 @@ | ||
"""Conftest for Open Policy Agent (OPA)""" | ||
import pytest | ||
|
||
from testsuite.mockserver import Mockserver | ||
from testsuite.utils import rego_allow_header | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def header(): | ||
"""Header used by OPA policy""" | ||
return "opa", "opa-test" | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def mockserver(request, testconfig, module_label, header): | ||
"""Returns mockserver and creates Expectation that returns Rego query""" | ||
try: | ||
mockserver = Mockserver(testconfig["mockserver"]["url"]) | ||
request.addfinalizer(lambda: mockserver.clear_expectation(module_label)) | ||
mockserver.create_expectation(module_label, "/opa", rego_allow_header(*header)) | ||
return mockserver | ||
except KeyError as exc: | ||
return pytest.skip(f"Mockserver configuration item is missing: {exc}") |
47 changes: 47 additions & 0 deletions
47
testsuite/tests/kuadrant/authorino/authorization/opa/test_auto_refresh_policy.py
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,47 @@ | ||
""" | ||
Tests for Open Policy Agent (OPA) policy pulled from external registry. | ||
Registry is represented by Mockserver Expectation that returns Rego query. | ||
""" | ||
import time | ||
|
||
import pytest | ||
|
||
from testsuite.utils import rego_allow_header | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def updated_header(): | ||
"""Header for updated OPA policy""" | ||
return "updated", "updated-value" | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def update_external_opa(mockserver, module_label, updated_header): | ||
"""Updates Expectation with updated header""" | ||
mockserver.create_expectation(module_label, "/opa", rego_allow_header(*updated_header)) | ||
# Sleeps for 1 second to compensate auto-refresh cycle `authorization.opa.externalRegistry.ttl = 1` | ||
time.sleep(1) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def authorization(authorization, mockserver): | ||
""" | ||
Adds OPA policy. Rego query is located on external registry (Mockserver). | ||
Policy accepts requests that contain `header`. | ||
""" | ||
authorization.add_external_opa_policy("opa", mockserver.url + "/opa", 1) | ||
return authorization | ||
|
||
|
||
def test_auto_refresh(client, auth, updated_header): | ||
"""Tests auto-refresh of OPA policy from external registry.""" | ||
key, value = updated_header | ||
response = client.get("/get", auth=auth, headers={key: value}) | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_previous(client, auth, header): | ||
"""Tests invalidation of previous OPA policy""" | ||
key, value = header | ||
response = client.get("/get", auth=auth, headers={key: value}) | ||
assert response.status_code == 403 |
26 changes: 26 additions & 0 deletions
26
testsuite/tests/kuadrant/authorino/authorization/opa/test_external_registry.py
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,26 @@ | ||
"""Tests for Open Policy Agent (OPA) using Mockserver Expectations as http endpoint with Rego query""" | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def authorization(authorization, mockserver): | ||
""" | ||
Adds OPA policy. Rego query is located on external registry (Mockserver). | ||
Policy accepts requests that contain `header`. | ||
""" | ||
authorization.add_external_opa_policy("opa", mockserver.url + "/opa") | ||
return authorization | ||
|
||
|
||
def test_allowed_by_opa(client, auth, header): | ||
"""Tests a request that should be authorized by OPA external registry declaration""" | ||
key, value = header | ||
response = client.get("/get", auth=auth, headers={key: value}) | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_denied_by_opa(client, auth): | ||
"""Tests a request should be denied by OPA external registry declaration""" | ||
response = client.get("/get", auth=auth) | ||
assert response.status_code == 403 |
15 changes: 3 additions & 12 deletions
15
...drant/authorino/authorization/test_opa.py → ...ino/authorization/opa/test_inline_rego.py
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