Skip to content

Commit

Permalink
trying to be more specific for interactive session detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac Davies committed Nov 9, 2024
1 parent 3807c74 commit b324304
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
33 changes: 28 additions & 5 deletions R/package-auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ db_host <- function(id = NULL, prefix = NULL, profile = default_config_profile()
#'
#' @description
#' The function will check for a token in the `DATABRICKS_HOST` environment variable.
#' `.databrickscfg` will be searched if `db_profile` and `use_databrickscfg` are set or
#' `.databrickscfg` will be searched if `db_profile` and `use_databrickscfg` are set or
#' if Posit Workbench managed OAuth credentials are detected.
#' If none of the above are found then will default to using OAuth U2M flow.
#'
Expand Down Expand Up @@ -98,7 +98,7 @@ db_token <- function(profile = default_config_profile()) {
#' @description
#' Workspace ID, optionally specified to make connections pane more powerful.
#' Specified as an environment variable `DATABRICKS_WSID`.
#' `.databrickscfg` will be searched if `db_profile` and `use_databrickscfg` are set or
#' `.databrickscfg` will be searched if `db_profile` and `use_databrickscfg` are set or
#' if Posit Workbench managed OAuth credentials are detected.
#'
#' Refer to [api authentication docs](https://docs.databricks.com/dev-tools/api/latest/authentication.html)
Expand Down Expand Up @@ -273,7 +273,7 @@ db_oauth_client <- function(host = db_host()) {
#' Returns the default config profile
#' @details Returns the config profile first looking at `DATABRICKS_CONFIG_PROFILE`
#' and then the `db_profile` option.
#'
#'
#' @return profile name
#' @keywords internal
default_config_profile <- function() {
Expand All @@ -288,7 +288,7 @@ default_config_profile <- function() {
#' Returns whether or not to use a `.databrickscfg` file
#' @details Indicates `.databrickscfg` should be used instead of environment variables when
#' either the `use_databrickscfg` option is set or Posit Workbench managed OAuth credentials are detected.
#'
#'
#' @return boolean
#' @keywords internal
use_databricks_cfg <- function() {
Expand All @@ -297,4 +297,27 @@ use_databricks_cfg <- function() {
use_databricks_cfg <- TRUE
}
return(use_databricks_cfg)
}
}


# Extended from {odbc}
#
# Try to determine whether we can redirect the user's browser to a server on
# localhost, which isn't possible if we are running on a hosted platform.
#
# This is based on the strategy pioneered by the {gargle} package and {httr2}.
is_hosted_session <- function() {

if (on_databricks()) {
return(TRUE)
}

if (nzchar(Sys.getenv("COLAB_RELEASE_TAG"))) {
return(TRUE)
}

# If RStudio Server or Posit Workbench is running locally (which is possible,
# though unusual), it's not acting as a hosted environment.
Sys.getenv("RSTUDIO_PROGRAM_MODE") == "server" &&
!grepl("localhost", Sys.getenv("RSTUDIO_HTTP_REFERER"), fixed = TRUE)
}
2 changes: 1 addition & 1 deletion R/request-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ db_request <- function(endpoint, method, version = NULL, body = NULL, host, toke
# otherwise initiate OAuth 2.0 U2M Workspace flow
if (!is.null(token)) {
req <- httr2::req_auth_bearer_token(req = req, token = token)
} else {
} else if (!is_hosted_session() && rlang::is_interactive()) {

# fetch client
oauth_client <- getOption(
Expand Down
2 changes: 1 addition & 1 deletion vignettes/cluster-management.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ cluster_info$state

You can edit Databricks clusters to change various parameters using `db_cluster_edit()`. For example, we may decide we want our cluster to autoscale between 2-8 nodes and add some tags.

```{r, results='hide', eval=FALSE}
```{r, results='hide'}
# we are required to input all parameters
db_cluster_edit(
Expand Down
26 changes: 13 additions & 13 deletions vignettes/remote-repl.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ After successfully connecting to the cluster you can run commands against the re

The REPL has a shortcut you can enter `:<language>` to change the active language. You can change between the following languages:

+----------------------------------+-----------------------------------+
| Language | Shortcut |
+==================================+===================================+
| R | `:r` |
+----------------------------------+-----------------------------------+
| Python | `:py` |
+----------------------------------+-----------------------------------+
| SQL | `:sql` |
+----------------------------------+-----------------------------------+
| Scala | `:scala` |
+----------------------------------+-----------------------------------+
| Shell | `:sh` |
+----------------------------------+-----------------------------------+
+---------------------------------+-----------------------------------+
| Language | Shortcut |
+=================================+===================================+
| R | `:r` |
+---------------------------------+-----------------------------------+
| Python | `:py` |
+---------------------------------+-----------------------------------+
| SQL | `:sql` |
+---------------------------------+-----------------------------------+
| Scala | `:scala` |
+---------------------------------+-----------------------------------+
| Shell | `:sh` |
+---------------------------------+-----------------------------------+

When you change between languages all variables should persist unless REPL is exited.

Expand Down
2 changes: 1 addition & 1 deletion vignettes/setup-auth.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ It's recommended to use option (1) when using `{brickster}` interactively, if yo

Personal Access Tokens can be generated in a few steps, for a step-by-step breakdown [refer to the documentation](https://docs.databricks.com/dev-tools/api/latest/authentication.html).

Once you have a token you'll be able to store it alongside the workspace URL in an `.Renviron` file. The `.Renviron` is used for storing the variables, such as those which may be sensitive (e.g. credentials) and de-couple them from the code (additional reading: [1](https://support.posit.co/hc/en-us/articles/360047157094-Managing-R-with-Rprofile-Renviron-Rprofile-site-Renviron-site-rsession-conf-and-repos-conf), [2](https://CRAN.R-project.org/package=startup/vignettes/startup-intro.html)).
Once you have a token you'll be able to store it alongside the workspace URL in an `.Renviron` file. The `.Renviron` is used for storing the variables, such as those which may be sensitive (e.g. credentials) and de-couple them from the code [additional reading](https://CRAN.R-project.org/package=startup/vignettes/startup-intro.html).

To get started add the following to your `.Renviron`:

Expand Down

0 comments on commit b324304

Please sign in to comment.