-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add initial code #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
08aba22
Ignore .idea/
siuhui 5f1f6ff
feat: add async PostgreSQL watcher (#1)
siuhui ad5057c
test: add tests for AsyncPostgresWatcher
siuhui 32478c0
docs: add examples
siuhui 03b6613
docs: update README.md
siuhui 30cccbe
feat: add Apache header
siuhui fc765db
feat: add requirements.txt
siuhui 5fcb0c1
docs: update README.md
siuhui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1 +1,53 @@ | ||
# async-postgres-watcher | ||
# async-postgres-watcher | ||
|
||
hsluoyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[](https://discord.gg/S5UjpzGZjN) | ||
|
||
Async casbin role watcher to be used for monitoring updates to casbin policies | ||
|
||
## Basic Usage Example | ||
|
||
### With Flask-authz | ||
|
||
```python | ||
from flask_authz import CasbinEnforcer | ||
from async_postgres_watcher import AsyncPostgresWatcher | ||
from flask import Flask | ||
from casbin.persist.adapters import FileAdapter | ||
|
||
casbin_enforcer = CasbinEnforcer(app, adapter) | ||
|
||
watcher = AsyncPostgresWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME) | ||
watcher.set_update_callback(casbin_enforcer.e.load_policy) | ||
|
||
casbin_enforcer.set_watcher(watcher) | ||
``` | ||
|
||
## Basic Usage Example With SSL Enabled | ||
|
||
See [asyncpg documentation](https://magicstack.github.io/asyncpg/current/api/index.html#connection) for full details of SSL parameters. | ||
|
||
### With Flask-authz | ||
|
||
```python | ||
from flask_authz import CasbinEnforcer | ||
from async_postgres_watcher import AsyncPostgresWatcher | ||
from flask import Flask | ||
from casbin.persist.adapters import FileAdapter | ||
|
||
casbin_enforcer = CasbinEnforcer(app, adapter) | ||
|
||
# If check_hostname is True, the SSL context is created with sslmode=verify-full. | ||
# If check_hostname is False, the SSL context is created with sslmode=verify-ca. | ||
watcher = AsyncPostgresWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME, sslrootcert=SSLROOTCERT, check_hostname = True, sslcert=SSLCERT, sslkey=SSLKEY) | ||
|
||
watcher.set_update_callback(casbin_enforcer.e.load_policy) | ||
casbin_enforcer.set_watcher(watcher) | ||
``` | ||
|
||
## Getting Help | ||
|
||
- [PyCasbin](https://github.com/casbin/pycasbin) | ||
|
||
## License | ||
|
||
This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text. |
This file contains hidden or 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,15 @@ | ||
# Copyright 2024 The casbin Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from .watcher import AsyncPostgresWatcher |
This file contains hidden or 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,100 @@ | ||
# Copyright 2024 The casbin Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Optional, Callable | ||
hsluoyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import asyncio | ||
import asyncpg | ||
import ssl | ||
import time | ||
|
||
POSTGRESQL_CHANNEL_NAME = "casbin_role_watcher" | ||
|
||
|
||
class AsyncPostgresWatcher: | ||
def __init__( | ||
self, | ||
host: str, | ||
user: str, | ||
password: str, | ||
port: Optional[int] = 5432, | ||
dbname: Optional[str] = "postgres", | ||
channel_name: Optional[str] = POSTGRESQL_CHANNEL_NAME, | ||
sslrootcert: Optional[str] = None, | ||
# If True, equivalent to sslmode=verify-full, if False: sslmode=verify-ca. | ||
check_hostname: Optional[bool] = True, | ||
sslcert: Optional[str] = None, | ||
sslkey: Optional[str] = None | ||
): | ||
self.loop = asyncio.get_event_loop() | ||
self.running = True | ||
self.callback = None | ||
self.host = host | ||
self.port = port | ||
self.user = user | ||
self.password = password | ||
self.dbname = dbname | ||
self.channel_name = channel_name | ||
|
||
if sslrootcert is not None and sslcert is not None and sslkey is not None: | ||
self.sslctx = ssl.create_default_context( | ||
ssl.Purpose.SERVER_AUTH, | ||
cafile=sslrootcert | ||
) | ||
self.sslctx.check_hostname = check_hostname | ||
self.sslctx.load_cert_chain(sslcert, keyfile=sslkey) | ||
else: | ||
self.sslctx = False | ||
|
||
self.loop.create_task(self.subscriber()) | ||
|
||
async def notify(self, pid, channel, payload): | ||
print(f"Notify: {payload}") | ||
if self.callback is not None: | ||
if asyncio.iscoroutinefunction(self.callback): | ||
await self.callback(payload) | ||
else: | ||
self.callback(payload) | ||
|
||
async def subscriber(self): | ||
conn = await asyncpg.connect( | ||
host=self.host, | ||
port=self.port, | ||
user=self.user, | ||
password=self.password, | ||
database=self.dbname, | ||
ssl=self.sslctx | ||
) | ||
await conn.add_listener(self.channel_name, self.notify) | ||
while self.running: | ||
await asyncio.sleep(1) # keep the coroutine alive | ||
|
||
async def set_update_callback(self, fn_name: Callable): | ||
print("runtime is set update callback", fn_name) | ||
self.callback = fn_name | ||
|
||
async def update(self): | ||
conn = await asyncpg.connect( | ||
host=self.host, | ||
port=self.port, | ||
user=self.user, | ||
password=self.password, | ||
database=self.dbname, | ||
ssl=self.sslctx | ||
) | ||
async with conn.transaction(): | ||
await conn.execute( | ||
f"NOTIFY {self.channel_name},'casbin policy update at {time.time()}'" | ||
) | ||
await conn.close() | ||
return True |
This file contains hidden or 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,14 @@ | ||
[request_definition] | ||
r = sub, obj, act | ||
|
||
[policy_definition] | ||
p = sub, obj, act | ||
|
||
[role_definition] | ||
g = _, _ | ||
|
||
[policy_effect] | ||
e = some(where (p.eft == allow)) | ||
|
||
[matchers] | ||
m = (p.sub == "*" || g(r.sub, p.sub)) && r.obj == p.obj && (p.act == "*" || r.act == p.act) |
This file contains hidden or 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,5 @@ | ||
p,alice,data1,read | ||
p,bob,data2,write | ||
p,data2_admin,data2,read | ||
p,data2_admin,data2,write | ||
g,alice,data2_admin |
Binary file not shown.
This file contains hidden or 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,56 @@ | ||
# Copyright 2024 The casbin Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
hsluoyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
from async_postgres_watcher import AsyncPostgresWatcher | ||
|
||
# Please set up yourself config | ||
HOST = "127.0.0.1" | ||
PORT = 5432 | ||
USER = "postgres" | ||
PASSWORD = "123456" | ||
DBNAME = "postgres" | ||
|
||
|
||
class TestConfig(unittest.IsolatedAsyncioTestCase): | ||
|
||
async def asyncSetUp(self): | ||
self.pg_watcher = AsyncPostgresWatcher( | ||
host=HOST, | ||
port=PORT, | ||
user=USER, | ||
password=PASSWORD, | ||
dbname=DBNAME | ||
) | ||
|
||
async def test_update_pg_watcher(self): | ||
assert await self.pg_watcher.update() is True | ||
|
||
def test_default_update_callback(self): | ||
assert self.pg_watcher.callback is None | ||
|
||
async def test_add_update_callback(self): | ||
def _test_callback(): | ||
pass | ||
|
||
await self.pg_watcher.set_update_callback(_test_callback) | ||
assert self.pg_watcher.callback == _test_callback | ||
|
||
async def asyncTearDown(self): | ||
self.pg_watcher.running = False | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.