Repository navigation
Use Posit Workbench managed credentials for Databricks and Snowflake ODBC connections - #625
Conversation
|
I got this all tested today in a Workbench build with dev Positron from posit-dev/positron#17577 plus this version of the extension. All working!
@thomasp85, this is now ready for your review. If you want to really see it working in Workbench with managed credentials, the dev setup is a bit involved but we do have docs on it. Let me know if you want to do that, or review this more from how it impacts other parts of how ggsql works. |
thomasp85
left a comment
There was a problem hiding this comment.
Overall this looks good, but a few points:
-
We only have a retry in
execute_sqleven though Positron/Workbench will call a bunch of other methods that assumes a valid connections, e.g.list_catalogsand siblings. So, a lot of the ui is stale until a manual reconnect -
Snowflake doesn't have a reconnect - is this on purpose?
-
Temp tables are gone at a reconnect (fine), but the RegisteredTables is not cleared
|
Thanks for the review, @thomasp85! Two of these are fixed, and the Snowflake one is on purpose:
|
|
@juliasilge we might also want the retry in |
|
Thank you for that catch, @thomasp85! In 8a9648e, |
… credentials (#17577) - Addresses #17129 - Related to posit-dev/ggsql#625 On Posit Workbench, Data Connections can now generate ggsql connection code for Snowflake and Databricks connections that use Workbench managed credentials. **Connect With** > **ggsql** writes a `-- @connect: odbc://...` line. The line names the ODBC driver and the account or workspace. It does not contain a token. ggsql adds the Workbench token when it connects, so the token never shows in the console, and the line stays the same when Workbench rotates the token. - **Snowflake** works with ggsql 0.5.2. The Workbench Managed Credentials mechanism gets a line, and the detected `workbench` row from `connections.toml` gets one too, with the warehouse, database, schema, and role from that entry. - **Databricks** needs posit-dev/ggsql#625, which adds the Databricks token to ggsql. The driver offers ggsql only when the preferred ggsql runtime is version 0.5.3 or later. This minimum is the constant `GGSQL_DATABRICKS_TOKEN_VERSION`, to update when the ggsql release is known. - **ODBC driver:** the line uses the Posit Pro Drivers library path when it exists, then it tries the vendor install paths, and then the registered name. ### API change `DataConnectionDriver` in `positron.d.ts` gets an optional `getSupportedLanguageIds(mechanismId, parameters)` method. It gives the languages that one connection can generate code for. Before, **Connect With** showed every driver language on every row. A row with no code for a language reported "Could not generate connection code". A saved connection with a stored secret first asked whether to include its secrets, and then failed. Now the menu, the agent `getConnectionCode` command, and the connections catalog offer only the languages that each connection supports. Drivers that do not implement the method work as before. The menu waits for the driver for 500 ms at most. After that, it uses the last answer from the driver for that connection, or every declared language. Snowflake and Databricks implement the method. As a result, connections.toml rows and Databricks OAuth machine-to-machine connections no longer offer R, because they have no R code. `ggsqlOdbc.ts` is copied into both drivers, and `extensions/ggsqlOdbc-copies.vitest.ts` keeps the two copies identical. ### Known limits - A Databricks ggsql session fails when the Workbench token expires, after about an hour, because ggsql does not reconnect yet. A follow-up in ggsql can add the reconnect. Snowflake sessions continue to work. - The line needs the Posit Pro Drivers, the vendor ODBC driver, or a driver registered as `Snowflake` or `Databricks`. Workbench images include the Pro Drivers. ### Release Notes #### New Features - On Posit Workbench, Data Connections can connect ggsql to Snowflake and Databricks with Workbench managed credentials (#17129) - **Connect With** offers only the languages that a connection can generate code for #### Bug Fixes - N/A ### Validation Steps @:connections @:workbench-snowflake @:workbench-databricks On Posit Workbench, start a session with Snowflake and Databricks managed credentials and the ggsql extension. 1. In the Data Connections pane, add a Snowflake connection with **Workbench Managed Credentials**. Select **Connect With** > **ggsql** and run the code. Open a `.sql` file on the connection, and run `SELECT CURRENT_ROLE();` with <kbd>Cmd+Enter</kbd>. Expected: a ggsql console shows one row. A second statement uses the same console. 2. Do step 1 again from the detected `workbench` row. Expected: the same line and the same console. 3. Look at the console history. Expected: no `Token=`. 4. Open **Connect With** on a Snowflake PAT connection, and on a connections.toml row other than `workbench`. Expected: no ggsql item. The connections.toml row has no R item. 5. With ggsql 0.5.2, open **Connect With** on a Databricks connection with **Workbench Managed Credentials**. Expected: no ggsql item. With a ggsql that includes posit-dev/ggsql#625, do step 1 for Databricks with `SELECT current_catalog();`. 6. On desktop, open **Connect With** on a Snowflake or Databricks connection. Expected: no ggsql item.

On Posit Workbench, ODBC connections to Databricks now use the managed OAuth token that Workbench writes to
$DATABRICKS_CONFIG_FILE. (Snowflake connections already get the Workbench token.) A new credential hook addsAuthMech=11;Auth_Flow=0;Auth_AccessToken=<token>when two conditions are true:UID=,PWD=,AuthMech=, orAuth_*keyHost=is the workspace in the Workbench file.This matches what
odbc::databricks()does in R.The Snowflake hook now adds the Workbench token only when
Server=names the Workbench account. The public hostname, the PrivateLink hostname, the hyphenated alias of an account with underscores, and thehostthat Workbench writes all match. Before this change, the hook added the token to every Snowflake string without one, also a string for a different account. The hook also doesn't add the token when the string hasToken=,UID=andPWD=, or anAuthenticator=other thanoauth. If the string hasAuthenticator=oauthand no token, the hook adds onlyToken=.Neither hook adds a token when
Host=orServer=occurs more than one time. Both hooks compare hosts without the scheme, port, or trailing slash.extract_odbc_valuenow matches whole keys and reads braced values whole, with}}as a literal}. Before this change,ProxyUID=was read asUID=, and a;inside{...}started a new key. A newodbc_valuesfunction returns every value of a key, so the hooks can find a repeated destination.This is needed for Positron, where on Workbench, the Snowflake and Databricks data connections will write
-- @connect: odbc://...lines that name the Posit Pro Drivers by library path and carry no token. ggsql can then add the token at connect time, the token doesn't show in the console or its history, and the line stays the same when Workbench rotates the token every 5 minutes.Known limits:
@connectagain. I'll test this on Workbench before this PR leaves draft.xy12345.us-east-1.snowflakecomputing.com) get no token, because the hook can't derive them from the account name that Workbench writes.take_odbc_ggsql_paramssplits on;inside braces.resolve_connection_namefindsConnectionName=with a substring search and writes values fromconnections.tomlwithout braces.databricks://URI falls back to ODBC, ggsql writesServer=andDatabase=, but the Databricks driver expectsHost=andHTTPPath=. That path gets no token from this hook.Testing:
cargo test --package ggsql --lib reader::odbcpasses, with new tests for each hook rule and for the parser.cargo fmtandcargo clippyare clean. I have not yet tested on a Workbench session but will get on that!