-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Offline Mode E2E tests * Added dependency
- Loading branch information
Showing
6 changed files
with
193 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
PDP_API_KEY=<YOUR_API_KEY> | ||
PDP_CONTROL_PLANE=https://permitio.api.stg.permit.io | ||
PDP_ENABLE_OFFLINE_MODE=True |
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,7 @@ | ||
FROM python:alpine | ||
|
||
RUN pip install --upgrade pip && pip install permit colorlog | ||
|
||
COPY checker.py /checker.py | ||
|
||
CMD ["python", "/checker.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,33 @@ | ||
# E2E tests for PDP Offline Mode | ||
|
||
### Create Permit Environment | ||
|
||
Login to Permit and create a new environment with the following objects: | ||
|
||
* Resource 'file' with action 'create' | ||
* Role 'admin' with permission to create 'file' | ||
* User 'user-1' with role 'admin' | ||
|
||
Copy the `.env.example` file to `.env` and update the values with the environment details. | ||
|
||
### Prepare repo for building PDP image | ||
|
||
This would download the Custom OPA and FactDB source code. | ||
|
||
```bash | ||
VERSION=<my-local-version> make run-prepare | ||
``` | ||
Replace `<my-local-version>` with the version you want to use for the PDP image | ||
|
||
### Run the tests | ||
|
||
```bash | ||
docker compose up | ||
``` | ||
|
||
|
||
### What does it do | ||
|
||
1. Start an online PDP with `PDP_ENABLE_OFFLINE_MODE=True` and connect the `/app/backup` to a volume. | ||
2. Start another offline PDP that is also connected to the same volume. | ||
3. Run a tester that run `permit.check("user-1", "create", "file")` on the online PDP and the offline PDP. |
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,80 @@ | ||
import asyncio | ||
import logging.config | ||
import os | ||
|
||
import aiohttp | ||
from permit import Permit, PermitConfig | ||
|
||
logging.config.dictConfig( | ||
{ | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"formatters": { | ||
"color": { | ||
"()": "colorlog.ColoredFormatter", | ||
"format": "%(log_color)s[%(asctime)s.%(msecs)03d] %(levelname)s - " | ||
"%(name)s:%(lineno)d | %(message)s", | ||
"datefmt": "%H:%M:%S", | ||
"log_colors": { | ||
"DEBUG": "white", | ||
"INFO": "green", | ||
"WARNING": "yellow", | ||
"ERROR": "red", | ||
"CRITICAL": "red,bg_white", | ||
}, | ||
}, | ||
}, | ||
"handlers": { | ||
"console": { | ||
"class": "logging.StreamHandler", | ||
"formatter": "color", | ||
}, | ||
}, | ||
"root": { | ||
"handlers": ["console"], | ||
"level": "INFO", | ||
}, | ||
} | ||
) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def main(): | ||
pdp_url = os.environ["PDP_URL"] | ||
logger.info("Starting PDP checker against: %s", pdp_url) | ||
permit = Permit( | ||
PermitConfig( | ||
token=os.environ["PDP_API_KEY"], | ||
api_url=os.environ["PDP_CONTROL_PLANE"], | ||
pdp=pdp_url, | ||
) | ||
) | ||
async with aiohttp.ClientSession(base_url=pdp_url) as client: | ||
while True: | ||
try: | ||
resp = await client.get("/healthy") | ||
if resp.status == 200: | ||
logger.info("PDP is healthy") | ||
break | ||
else: | ||
logger.warning("PDP is not healthy") | ||
except Exception as e: | ||
logger.exception(f"Error: {e}") | ||
|
||
await asyncio.sleep(1) | ||
|
||
while True: | ||
try: | ||
result = await permit.check("user-1", "create", "file") | ||
if result: | ||
logger.info("Passed") | ||
else: | ||
logger.warning("Failed") | ||
except Exception as e: | ||
logger.exception(f"Error: {e}") | ||
|
||
await asyncio.sleep(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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,67 @@ | ||
volumes: | ||
backup: | ||
|
||
networks: | ||
internet: | ||
driver: bridge | ||
no-internet: | ||
driver: bridge | ||
internal: true | ||
|
||
services: | ||
online-pdp: | ||
container_name: online-pdp | ||
build: | ||
context: .. | ||
dockerfile: Dockerfile | ||
volumes: | ||
- backup:/app/backup:rw | ||
networks: | ||
- internet | ||
env_file: .env | ||
healthcheck: | ||
test: "wget --no-verbose --tries=1 --spider http://127.0.0.1:7000/healthy || exit 1" | ||
|
||
offline-pdp: | ||
container_name: offline-pdp | ||
build: | ||
context: .. | ||
dockerfile: Dockerfile | ||
volumes: | ||
- backup:/app/backup:rw | ||
networks: | ||
- no-internet | ||
env_file: .env | ||
healthcheck: | ||
test: "wget --no-verbose --tries=1 --spider http://127.0.0.1:7000/healthy || exit 1" | ||
depends_on: | ||
online-pdp: | ||
condition: service_healthy | ||
|
||
online-tester: | ||
container_name: online-tester | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
networks: | ||
- internet | ||
env_file: .env | ||
environment: | ||
- PDP_URL=http://online-pdp:7000 | ||
depends_on: | ||
online-pdp: | ||
condition: service_healthy | ||
|
||
offline-tester: | ||
container_name: offline-tester | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
networks: | ||
- no-internet | ||
env_file: .env | ||
environment: | ||
- PDP_URL=http://offline-pdp:7000 | ||
depends_on: | ||
offline-pdp: | ||
condition: service_healthy |