Skip to content

Commit 288ec4c

Browse files
Trevor Nederlofatheriel
andcommitted
Add Posit Connect-managed service principal support to databricks().
This commit adds support for service principal credentials managed by Posit Connect alongside our existing support of viewer-based credentials. The meat of this is handled by a new function in the upcoming version of the `connectcreds` package (hence the Remote). Documentation updates are included. Co-authored-by: Aaroon Jacobs <[email protected]>
1 parent f63575c commit 288ec4c

5 files changed

Lines changed: 33 additions & 15 deletions

File tree

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Imports:
2929
Rcpp (>= 0.12.11),
3030
rlang (>= 1.1.0)
3131
Suggests:
32-
connectcreds,
32+
connectcreds (>= 0.1.0.9000),
3333
covr,
3434
DBItest,
3535
httr2,
@@ -90,3 +90,5 @@ Collate:
9090
'utils.R'
9191
'zzz.R'
9292
VignetteBuilder: knitr
93+
Remotes:
94+
posit-dev/connectcreds

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
* Databricks: Fix repeated calls to `dbConnect` (#901).
1818

19+
* `databricks()` now detects service principal credentials when running on Posit Connect (@tnederlof, #930)
20+
1921
# odbc 1.6.1
2022

2123
* odbc will now automatically find statically built installations of

R/driver-databricks.R

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ NULL
1515
#' model, with support for personal access tokens, OAuth machine-to-machine
1616
#' credentials, and OAuth user-to-machine credentials supplied via Posit
1717
#' Workbench or the Databricks CLI on desktop. It can also detect viewer-based
18-
#' credentials on Posit Connect if the \pkg{connectcreds} package is
19-
#' installed. All of these credentials are detected automatically if present
20-
#' using [standard environment variables](https://docs.databricks.com/en/dev-tools/auth.html#environment-variables-and-fields-for-client-unified-authentication).
18+
#' and service principal credentials on Posit Connect if the \pkg{connectcreds}
19+
#' package is installed. All of these credentials are detected automatically if
20+
#' present using [standard environment variables](https://docs.databricks.com/en/dev-tools/auth.html#environment-variables-and-fields-for-client-unified-authentication).
2121
#'
2222
#' In addition, on macOS platforms, the `dbConnect()` method will check
2323
#' for irregularities with how the driver is configured,
@@ -49,12 +49,13 @@ NULL
4949
#' httpPath = "sql/protocolv1/o/4425955464597947/1026-023828-vn51jugj"
5050
#' )
5151
#'
52-
#' # Use credentials from the viewer (when possible) in a Shiny app
53-
#' # deployed to Posit Connect.
52+
#' # Use credentials from the viewer or a service principal (when possible) in
53+
#' # a Shiny app deployed to Posit Connect.
5454
#' library(connectcreds)
5555
#' server <- function(input, output, session) {
5656
#' conn <- DBI::dbConnect(
5757
#' odbc::databricks(),
58+
#' workspace = "https://example.cloud.databricks.com",
5859
#' httpPath = "sql/protocolv1/o/4425955464597947/1026-023828-vn51jugj"
5960
#' )
6061
#' }
@@ -134,9 +135,12 @@ databricks_args <- function(httpPath,
134135
if (running_on_connect()) {
135136
msg <- c(
136137
msg,
137-
"i" = "Or consider enabling Posit Connect's Databricks integration \
138-
for viewer-based credentials. See {.url \
139-
https://docs.posit.co/connect/user/oauth-integrations/#adding-oauth-integrations-to-deployed-content}
138+
"i" = "Or consider enabling Posit Connect's Databricks integration. \
139+
For viewer-based credentials. See {.url \
140+
https://docs.posit.co/connect/user/oauth-integrations/#viewer-oauth-integrations}
141+
for details. \
142+
For service principal credentials, see {.url \
143+
https://docs.posit.co/connect/user/oauth-integrations/#service-account-oauth-integrations}
140144
for details."
141145
)
142146
}
@@ -227,6 +231,15 @@ databricks_auth_args <- function(host, uid = NULL, pwd = NULL) {
227231
))
228232
}
229233

234+
if (is_installed("connectcreds") && connectcreds::has_service_account_token(workspace)) {
235+
token <- connectcreds::connect_service_account_token(workspace)
236+
return(list(
237+
authMech = 11,
238+
auth_flow = 0,
239+
auth_accesstoken = token$access_token
240+
))
241+
}
242+
230243
if (!is.null(uid) && !is.null(pwd)) {
231244
return(list(uid = uid, pwd = pwd, authMech = 3))
232245
} else if (xor(is.null(uid), is.null(pwd))) {

man/databricks.Rd

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-driver-databricks.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ test_that("Workbench-managed credentials are ignored for other hosts", {
153153
expect_equal(databricks_auth_args(host = "some-host"), NULL)
154154
})
155155

156-
test_that("we hint viewer-based credentials on Connect", {
156+
test_that("we hint viewer-based and service principal credentials on Connect", {
157157
local_mocked_bindings(
158158
running_on_connect = function() TRUE
159159
)

0 commit comments

Comments
 (0)