Skip to content

Commit c9dfe57

Browse files
kimsehwan96devops-sewhan-kim
authored andcommitted
Add impersonation option in trino datasource
1 parent 45bc24a commit c9dfe57

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

redash/query_runner/trino.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
from redash.models.users import ApiUser, User
34
from redash.query_runner import (
45
TYPE_BOOLEAN,
56
TYPE_DATE,
@@ -57,20 +58,34 @@ def configuration_schema(cls):
5758
"port": {"type": "number"},
5859
"username": {"type": "string"},
5960
"password": {"type": "string"},
61+
"source": {"type": "string", "default": "redash"},
6062
"catalog": {"type": "string"},
6163
"schema": {"type": "string"},
64+
"impersonation": {"type": "boolean", "default": False},
65+
"impersonationField": {
66+
"type": "string",
67+
"title": "Impersonation User Attribute",
68+
"default": "email",
69+
"extendedEnum": [{"value": "email", "name": "Email"}, {"value": "name", "name": "Name"}],
70+
},
6271
},
6372
"order": [
6473
"protocol",
6574
"host",
6675
"port",
6776
"username",
6877
"password",
78+
"source",
6979
"catalog",
7080
"schema",
81+
"impersonation",
7182
],
7283
"required": ["host", "username"],
7384
"secret": ["password"],
85+
"extra_options": [
86+
"impersonation",
87+
"impersonationField",
88+
],
7489
}
7590

7691
@classmethod
@@ -127,20 +142,41 @@ def _get_catalogs(self):
127142
catalogs.append(catalog)
128143
return catalogs
129144

145+
def _get_trino_user(self, user):
146+
"""Determine the Trino user based on impersonation settings."""
147+
default_user = self.configuration.get("username")
148+
149+
if not self.configuration.get("impersonation") or user is None:
150+
return default_user
151+
152+
impersonation_field = self.configuration.get("impersonationField", "email")
153+
154+
if isinstance(user, User):
155+
if impersonation_field == "email":
156+
return user.email or default_user
157+
elif impersonation_field == "name":
158+
return user.name or default_user
159+
elif isinstance(user, ApiUser):
160+
return user.name or default_user
161+
162+
return default_user
163+
130164
def run_query(self, query, user):
131165
if self.configuration.get("password"):
132166
auth = trino.auth.BasicAuthentication(
133167
username=self.configuration.get("username"), password=self.configuration.get("password")
134168
)
135169
else:
136170
auth = trino.constants.DEFAULT_AUTH
171+
137172
connection = trino.dbapi.connect(
138173
http_scheme=self.configuration.get("protocol", "http"),
139174
host=self.configuration.get("host", ""),
175+
source=self.configuration.get("source", "redash"),
140176
port=self.configuration.get("port", 8080),
141177
catalog=self.configuration.get("catalog", ""),
142178
schema=self.configuration.get("schema", ""),
143-
user=self.configuration.get("username"),
179+
user=self._get_trino_user(user),
144180
auth=auth,
145181
)
146182

0 commit comments

Comments
 (0)