Skip to content

Commit cba12fb

Browse files
richardantalRichard AntalHarshg999
authored
[phoenixdb] Switch to old user impersonation code for phoenix (#2852)
- Change impersonation to use principal_username broke phoenixdb - Fix Python code styling issues Co-authored-by: Richard Antal <[email protected]> Co-authored-by: Harsh Gupta <[email protected]>
1 parent fea6894 commit cba12fb

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

desktop/libs/notebook/src/notebook/connectors/sql_alchemy.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
ENGINES = {}
9090
CONNECTIONS = {}
9191
ENGINE_KEY = '%(username)s-%(connector_name)s'
92+
URL_PATTERN = '(?P<driver_name>.+?://)(?P<host>[^:/ ]+):(?P<port>[0-9]*).*'
9293

9394
LOG = logging.getLogger(__name__)
9495

@@ -171,6 +172,18 @@ def _create_engine(self):
171172
s3_staging_dir = url.rsplit('s3_staging_dir=', 1)[1]
172173
url = url.replace(s3_staging_dir, urllib_quote_plus(s3_staging_dir))
173174

175+
m = re.search(URL_PATTERN, url)
176+
driver_name = m.group('driver_name')
177+
if self.options.get('has_impersonation'):
178+
if not driver_name:
179+
raise QueryError('Driver name of %(url)s could not be found and impersonation is turned on' % {'url': url})
180+
181+
if driver_name.startswith("phoenix"):
182+
url = url.replace(driver_name, '%(driver_name)s%(username)s@' % {
183+
'driver_name': driver_name,
184+
'username': self.user.username
185+
})
186+
174187
if self.options.get('credentials_json'):
175188
self.options['credentials_info'] = json.loads(
176189
self.options.pop('credentials_json')
@@ -183,7 +196,8 @@ def _create_engine(self):
183196
self.options.pop('connect_args')
184197
)
185198

186-
if self.options.get('has_impersonation'):
199+
# phoenixdb does not support impersonation using principal_username parameter
200+
if self.options.get('has_impersonation') and not driver_name.startswith("phoenix"):
187201
self.options.setdefault('connect_args', {}).setdefault('principal_username', self.user.username)
188202

189203
options = self.options.copy()
@@ -258,7 +272,7 @@ def execute(self, notebook, snippet):
258272
}
259273
CONNECTIONS[guid] = cache
260274

261-
response = {
275+
response = {
262276
'sync': False,
263277
'has_result_set': result.cursor != None,
264278
'modified_row_count': 0,
@@ -330,7 +344,7 @@ def check_status(self, notebook, snippet):
330344
@query_error_handler
331345
def progress(self, notebook, snippet, logs=''):
332346
progress = 50
333-
if self.options['url'].startswith('presto://') | self.options['url'].startswith('trino://') :
347+
if self.options['url'].startswith('presto://') | self.options['url'].startswith('trino://'):
334348
guid = snippet['result']['handle']['guid']
335349
handle = CONNECTIONS.get(guid)
336350
stats = None

desktop/libs/notebook/src/notebook/connectors/sql_alchemy_tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,29 @@ def test_create_engine_with_impersonation(self):
244244
connect_args={'principal_username': 'test'},
245245
pool_pre_ping=True)
246246

247+
def test_create_engine_with_impersonation_phoenix(self):
248+
interpreter = {
249+
'name': 'phoenix',
250+
'options': {
251+
'url': 'phoenix://hue:8080/hue',
252+
'session': {},
253+
'has_impersonation': False # Off
254+
}
255+
}
256+
257+
with patch('notebook.connectors.sql_alchemy.create_engine') as create_engine:
258+
engine = SqlAlchemyApi(self.user, interpreter)._create_engine()
259+
260+
create_engine.assert_called_with('phoenix://hue:8080/hue', pool_pre_ping=False)
261+
262+
263+
interpreter['options']['has_impersonation'] = True # On
264+
265+
with patch('notebook.connectors.sql_alchemy.create_engine') as create_engine:
266+
engine = SqlAlchemyApi(self.user, interpreter)._create_engine()
267+
268+
create_engine.assert_called_with('phoenix://test@hue:8080/hue', pool_pre_ping=False)
269+
247270

248271
def test_explain(self):
249272

0 commit comments

Comments
 (0)