diff --git a/R/package-auth.R b/R/package-auth.R index be4b17b..2fc1747 100644 --- a/R/package-auth.R +++ b/R/package-auth.R @@ -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. #' @@ -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) @@ -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() { @@ -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() { @@ -297,4 +297,27 @@ use_databricks_cfg <- function() { use_databricks_cfg <- TRUE } return(use_databricks_cfg) -} \ No newline at end of file +} + + +# 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) +} diff --git a/R/request-helpers.R b/R/request-helpers.R index 0ddf669..afd528e 100644 --- a/R/request-helpers.R +++ b/R/request-helpers.R @@ -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( diff --git a/vignettes/cluster-management.Rmd b/vignettes/cluster-management.Rmd index a2c8d54..5696ec3 100644 --- a/vignettes/cluster-management.Rmd +++ b/vignettes/cluster-management.Rmd @@ -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( diff --git a/vignettes/remote-repl.Rmd b/vignettes/remote-repl.Rmd index d2d0628..f516439 100644 --- a/vignettes/remote-repl.Rmd +++ b/vignettes/remote-repl.Rmd @@ -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 `:` 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. diff --git a/vignettes/setup-auth.Rmd b/vignettes/setup-auth.Rmd index 242425a..9984830 100644 --- a/vignettes/setup-auth.Rmd +++ b/vignettes/setup-auth.Rmd @@ -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`: