|
1 | 1 | import logging |
2 | 2 |
|
| 3 | +from redash.models.users import ApiUser, User |
3 | 4 | from redash.query_runner import ( |
4 | 5 | TYPE_BOOLEAN, |
5 | 6 | TYPE_DATE, |
@@ -57,20 +58,34 @@ def configuration_schema(cls): |
57 | 58 | "port": {"type": "number"}, |
58 | 59 | "username": {"type": "string"}, |
59 | 60 | "password": {"type": "string"}, |
| 61 | + "source": {"type": "string", "default": "redash"}, |
60 | 62 | "catalog": {"type": "string"}, |
61 | 63 | "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 | + }, |
62 | 71 | }, |
63 | 72 | "order": [ |
64 | 73 | "protocol", |
65 | 74 | "host", |
66 | 75 | "port", |
67 | 76 | "username", |
68 | 77 | "password", |
| 78 | + "source", |
69 | 79 | "catalog", |
70 | 80 | "schema", |
| 81 | + "impersonation", |
71 | 82 | ], |
72 | 83 | "required": ["host", "username"], |
73 | 84 | "secret": ["password"], |
| 85 | + "extra_options": [ |
| 86 | + "impersonation", |
| 87 | + "impersonationField", |
| 88 | + ], |
74 | 89 | } |
75 | 90 |
|
76 | 91 | @classmethod |
@@ -127,20 +142,41 @@ def _get_catalogs(self): |
127 | 142 | catalogs.append(catalog) |
128 | 143 | return catalogs |
129 | 144 |
|
| 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 | + |
130 | 164 | def run_query(self, query, user): |
131 | 165 | if self.configuration.get("password"): |
132 | 166 | auth = trino.auth.BasicAuthentication( |
133 | 167 | username=self.configuration.get("username"), password=self.configuration.get("password") |
134 | 168 | ) |
135 | 169 | else: |
136 | 170 | auth = trino.constants.DEFAULT_AUTH |
| 171 | + |
137 | 172 | connection = trino.dbapi.connect( |
138 | 173 | http_scheme=self.configuration.get("protocol", "http"), |
139 | 174 | host=self.configuration.get("host", ""), |
| 175 | + source=self.configuration.get("source", "redash"), |
140 | 176 | port=self.configuration.get("port", 8080), |
141 | 177 | catalog=self.configuration.get("catalog", ""), |
142 | 178 | schema=self.configuration.get("schema", ""), |
143 | | - user=self.configuration.get("username"), |
| 179 | + user=self._get_trino_user(user), |
144 | 180 | auth=auth, |
145 | 181 | ) |
146 | 182 |
|
|
0 commit comments