Skip to content

Use Posit Workbench managed credentials for Databricks and Snowflake ODBC connections - #625

Merged
thomasp85 merged 6 commits into
mainfrom
workbench-managed-credentials
Dec 3, 2026
Merged

thomasp85 merged 6 commits into
mainfrom
workbench-managed-credentials

Conversation

@juliasilge

@juliasilge juliasilge commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

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 adds AuthMech=11;Auth_Flow=0;Auth_AccessToken=<token> when two conditions are true:

  • The connection string has no UID=, PWD=, AuthMech=, or Auth_* key
  • Its Host= 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 the host that 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 has Token=, UID= and PWD=, or an Authenticator= other than oauth. If the string has Authenticator=oauth and no token, the hook adds only Token=.

Neither hook adds a token when Host= or Server= occurs more than one time. Both hooks compare hosts without the scheme, port, or trailing slash.

extract_odbc_value now matches whole keys and reads braced values whole, with }} as a literal }. Before this change, ProxyUID= was read as UID=, and a ; inside {...} started a new key. A new odbc_values function 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:

  • The ODBC reader keeps one connection. If an ODBC driver drops the session after the token expires, the console fails until the user runs @connect again. I'll test this on Workbench before this PR leaves draft.
  • Legacy Snowflake account URLs (for example xy12345.us-east-1.snowflakecomputing.com) get no token, because the hook can't derive them from the account name that Workbench writes.
  • Some ODBC parsing problems are older than this PR and are not changed here. take_odbc_ggsql_params splits on ; inside braces. resolve_connection_name finds ConnectionName= with a substring search and writes values from connections.toml without braces.
  • When a databricks:// URI falls back to ODBC, ggsql writes Server= and Database=, but the Databricks driver expects Host= and HTTPPath=. That path gets no token from this hook.

Testing: cargo test --package ggsql --lib reader::odbc passes, with new tests for each hook rule and for the parser. cargo fmt and cargo clippy are clean. I have not yet tested on a Workbench session but will get on that!

@juliasilge

juliasilge commented Dec 2, 2026 •

Copy link
Copy Markdown
Member Author

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!

Screenshot 2026-12-01 at 5 51 35 PM

@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.

@juliasilge
juliasilge requested a review from thomasp85 December 2, 2026 00:54
@juliasilge
juliasilge marked this pull request as ready for review December 2, 2026 00:57

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks good, but a few points:

  • We only have a retry in execute_sql even though Positron/Workbench will call a bunch of other methods that assumes a valid connections, e.g. list_catalogs and 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

@juliasilge

Copy link
Copy Markdown
Member Author

Thanks for the review, @thomasp85! Two of these are fixed, and the Snowflake one is on purpose:

  • I fixed how the retry was only in execute_sql in 56fd4c7. list_catalogs, list_schemas, list_tables, and list_columns now use the same retry as execute_sql. If the token has expired, the reader reconnects and tries the call one more time. This also fixes a worse symptom where the kernel's depth_offset changes a failed list_catalogs call into an empty list, so the Connections pane showed the wrong level of the tree, not only stale data.

  • There is no reconnect for Snowflake on purpose, because the two drivers use the Workbench token differently. I tested both on Workbench with a console that was idle for about 75 minutes, which is longer than one token lifetime. The Databricks driver sends the OAuth token as a bearer token with each request and when the token expires after about an hour, the next statement fails with [08006] ... Access token expired: Bearer error="invalid_token". A new connection with the current token from $DATABRICKS_CONFIG_FILE is the only way to continue. The Snowflake driver uses the OAuth token only at login and then gives the driver a session of its own, which doesn't depend on the OAuth token. (It's possible for a Snowflake session to still time out after a much longer idle time but I think we can see how often that happens in real use.)

  • I fixed how RegisteredTables were not cleared in ce1206e. A successful reconnect now clears the list. Before, a later unregister tried to drop a table that was gone and returned an error. If the reconnect fails, the old connection stays, and so does the list.

@juliasilge
juliasilge requested a review from thomasp85 December 2, 2026 17:30
@thomasp85

Copy link
Copy Markdown
Collaborator

@juliasilge we might also want the retry in register()/unregister()?

@juliasilge

Copy link
Copy Markdown
Member Author

Thank you for that catch, @thomasp85! In 8a9648e, register() and unregister() now use the same retry as execute_sql and the catalog lookups. If the token has expired, the reader reconnects and tries the call one more time. (A reconnect clears the registered tables, so a retry of unregister() returns success and does not try to drop a table that is already gone.)

@thomasp85
thomasp85 merged commit f74e721 into main Dec 3, 2026
23 checks passed
juliasilge added a commit to posit-dev/positron that referenced this pull request Dec 4, 2026
… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants