Skip to content

Commit a1a8f4c

Browse files
feat: add SSL options (#22)
* Added SSL options * Fixed order of attribute initialisation in init * debug * Removed commas * Removed debug print statement * Updated python versions Updated python versions * Update README.md * Update release.yml * Create release.yml --------- Co-authored-by: hsluoyz <hsluoyz@qq.com>
1 parent 7210a6f commit a1a8f4c

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
python-version: [ "3.6", "3.7", "3.8", "3.9", "3.10" ]
14+
python-version: [ "3.9", "3.10", "3.11" ]
1515
os: [ ubuntu-latest, macOS-latest, windows-latest]
1616

1717
steps:
@@ -116,7 +116,7 @@ jobs:
116116
- name: Setup Node.js
117117
uses: actions/setup-node@v1
118118
with:
119-
node-version: '16'
119+
node-version: '18'
120120

121121
- name: Setup
122122
run: npm install -g semantic-release @semantic-release/github @semantic-release/changelog @semantic-release/commit-analyzer @semantic-release/git @semantic-release/release-notes-generator semantic-release-pypi

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,20 @@ watcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD,
2626
watcher.set_update_callback(casbin_enforcer.e.load_policy)
2727
casbin_enforcer.set_watcher(watcher)
2828
```
29+
30+
## Basic Usage Example With SSL Enabled
31+
32+
See [PostgresQL documentation](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS) for full details of SSL parameters.
33+
34+
### With Flask-authz
35+
```python
36+
from flask_authz import CasbinEnforcer
37+
from postgresql_watcher import PostgresqlWatcher
38+
from flask import Flask
39+
from casbin.persist.adapters import FileAdapter
40+
41+
casbin_enforcer = CasbinEnforcer(app, adapter)
42+
watcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME, sslmode="verify_full", sslcert=SSLCERT, sslrootcert=SSLROOTCERT, sslkey=SSLKEY)
43+
watcher.set_update_callback(casbin_enforcer.e.load_policy)
44+
casbin_enforcer.set_watcher(watcher)
45+
```

postgresql_watcher/watcher.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def casbin_subscription(
1717
dbname: Optional[str] = "postgres",
1818
delay: Optional[int] = 2,
1919
channel_name: Optional[str] = POSTGRESQL_CHANNEL_NAME,
20+
sslmode: Optional[str] = None,
21+
sslrootcert: Optional[str] = None,
22+
sslcert: Optional[str] = None,
23+
sslkey: Optional[str] = None
2024
):
2125
# delay connecting to postgresql (postgresql connection failure)
2226
time.sleep(delay)
@@ -25,7 +29,11 @@ def casbin_subscription(
2529
port=port,
2630
user=user,
2731
password=password,
28-
dbname=dbname
32+
dbname=dbname,
33+
sslmode=sslmode,
34+
sslrootcert=sslrootcert,
35+
sslcert=sslcert,
36+
sslkey=sslkey
2937
)
3038
# Can only receive notifications when not in transaction, set this for easier usage
3139
conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
@@ -52,6 +60,10 @@ def __init__(
5260
dbname: Optional[str] = "postgres",
5361
channel_name: Optional[str] = POSTGRESQL_CHANNEL_NAME,
5462
start_process: Optional[bool] = True,
63+
sslmode: Optional[str] = None,
64+
sslrootcert: Optional[str] = None,
65+
sslcert: Optional[str] = None,
66+
sslkey: Optional[str] = None
5567
):
5668
self.update_callback = None
5769
self.parent_conn = None
@@ -61,6 +73,10 @@ def __init__(
6173
self.password = password
6274
self.dbname = dbname
6375
self.channel_name = channel_name
76+
self.sslmode = sslmode
77+
self.sslrootcert = sslrootcert
78+
self.sslcert = sslcert
79+
self.sslkey = sslkey
6480
self.subscribed_process = self.create_subscriber_process(start_process)
6581

6682
def create_subscriber_process(
@@ -82,6 +98,10 @@ def create_subscriber_process(
8298
self.dbname,
8399
delay,
84100
self.channel_name,
101+
self.sslmode,
102+
self.sslrootcert,
103+
self.sslcert,
104+
self.sslkey
85105
),
86106
daemon=True,
87107
)
@@ -100,6 +120,10 @@ def update(self):
100120
user=self.user,
101121
password=self.password,
102122
dbname=self.dbname,
123+
sslmode=self.sslmode,
124+
sslrootcert=self.sslrootcert,
125+
sslcert=self.sslcert,
126+
sslkey=self.sslkey
103127
)
104128
# Can only receive notifications when not in transaction, set this for easier usage
105129
conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)

0 commit comments

Comments
 (0)