Skip to content

Commit 8de93a8

Browse files
austenLacyhashhar
authored andcommitted
Add CertificateAuthentication class to support cert based authentication
1 parent 83476ee commit 8de93a8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ cur.execute('SELECT * FROM system.runtime.nodes')
9898
rows = cur.fetchall()
9999
```
100100

101+
# Certificate Authentication
102+
- `CertificateAuthentication` class can be used to connect to Trino cluster configured with [certificate based authentication](https://trino.io/docs/current/security/certificate.html). `CertificateAuthentication` requires paths to a valid client certificate and private key.
103+
```python
104+
import trino
105+
conn = trino.dbapi.connect(
106+
host='coordinator-url',
107+
port=8443,
108+
user='the-user',
109+
catalog='the-catalog',
110+
schema='the-schema',
111+
http_scheme='https',
112+
auth=trino.auth.CertificateAuthentication("/path/to/cert", "/path/to/key"),
113+
)
114+
cur = conn.cursor()
115+
cur.execute('SELECT * FROM system.runtime.nodes')
116+
rows = cur.fetchall()
117+
```
118+
101119
# Transactions
102120
The client runs by default in *autocommit* mode. To enable transactions, set
103121
*isolation_level* to a value different than `IsolationLevel.AUTOCOMMIT`:

trino/auth.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,21 @@ def __eq__(self, other):
267267
if not isinstance(other, OAuth2Authentication):
268268
return False
269269
return self._redirect_auth_url == other._redirect_auth_url
270+
271+
272+
class CertificateAuthentication(Authentication):
273+
def __init__(self, cert, key):
274+
self._cert = cert
275+
self._key = key
276+
277+
def set_http_session(self, http_session):
278+
http_session.cert = (self._cert, self._key)
279+
return http_session
280+
281+
def get_exceptions(self):
282+
return ()
283+
284+
def __eq__(self, other):
285+
if not isinstance(other, CertificateAuthentication):
286+
return False
287+
return self._cert == other._cert and self._key == other._key

0 commit comments

Comments
 (0)