Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix checking and logging of envVars in deployApp() #994

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

gadenbuie
Copy link
Member

Two small fixes for the checking of envVars in deployApp():

  1. First, the values of the envvars were being printed in to the logs, where we'd only want the names.

  2. The check ensuring that envVars is only used for Posit Connect servers would not run if the user was only setting a single variable.

R/deployApp.R Outdated
Comment on lines 363 to 365
if (!isConnectServer(target$server) && length(envVars) > 0) {
cli::cli_abort("{.arg envVars} only supported for Posit Connect servers")
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Rather than checking here by enforcing that target$server points to a Connect server, you could instead check just a little bit further down after creating client. In that case, you could use a block like the following, and check that client implements the setEnvVars() psuedo-method.

  client <- clientForAccount(accountDetails)
  if (length(envVars) > 0 && !"setEnvVars" %in% names(client)) {
    cli::cli_abort("{target$server} does not support setting {.arg envVars}")
  }

Copy link
Member

Choose a reason for hiding this comment

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

I like that idea. Want to do it?

Copy link
Member Author

Choose a reason for hiding this comment

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

No problem, done in f3dac5b

R/deployApp.R Outdated
@@ -432,7 +432,7 @@ deployApp <- function(appDir = getwd(),
taskComplete(quiet, "Visibility updated")
}
if (length(target$envVars) > 0) {
taskStart(quiet, "Updating environment variables {envVars}...")
taskStart(quiet, "Updating {length(envVars)} environment variable{?s}: {.field {names(envVars)}}...")
Copy link
Member

Choose a reason for hiding this comment

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

These are already just the names of the env vars?

Copy link
Member Author

@gadenbuie gadenbuie Sep 8, 2023

Choose a reason for hiding this comment

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

Sorry, I had a hard time following the flow for that property. What happened for me -- before I applied the other fix -- was that I tried to deploy an app with one env var to shinyapps.io. Something like envVars = c("FLAG" = "true"). This made it past the first check and failed here (because the shinyapps client doesn't implement $setEnvVars()) at which point this call was printing environment variables true.

I made this change first and then realized that the earlier check was failing, and in my probably semi-broken state envVars was a named vector. This may have been the result of a local rsconnect deploy record with a value that shouldn't have been written?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, maybe we need some earlier validation that envVars is an unnamed character vector?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ohhh, I read the documentation more carefully and realized that the environment variables should be set in the current session and the user should provide just the names, as in envVars = c("FLAG", "OTHER_FLAG").

I think my confusion stemmed from being familiar with connectapi, where there's an interface for setting environment variables during deployment that uses the FLAG = "value" pattern.

I updated the docs to give a little guidance about how those values should be set, and I added a check that the vector provided to envVars is unnamed. This feels a little aggressive but I think it's worth it, given the potential risk of leaking a credential if a user misunderstands how envVars should be used.

Copy link
Member

Choose a reason for hiding this comment

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

In case it's not clear, the interface looks like this is to prevent you from typing the values of env vars in executed code since that will be captured in .Rhistory.

R/deployApp.R Outdated Show resolved Hide resolved
R/deployApp.R Outdated Show resolved Hide resolved
R/deployApp.R Outdated
#' encrypted connection and are not stored in the bundle, making this a safe
#' way to send private data to Connect.
#' Connect only). The values of the environment variables should be set in the
#' current session with [Sys.setenv()] or via an `.Renviron` file. Values are
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's good practice to recommend Sys.setenv() since that'll result in the value of the env var appearing in your .Rhistory. It might be good to say that explicitly here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I took another pass and rewrote this sentence:

A character vector giving the names of environment variables whose values should be synchronised with the server (currently supported by Connect only). The values of sensitive environment variables should be set in the current session via an .Renviron file or with the help of a credential store like keyring. Values are sent over an encrypted connection and are not stored in the bundle, making this a safe way to send private data to Connect.

gadenbuie and others added 4 commits September 9, 2023 14:03
Co-authored-by: Hadley Wickham <hadley@posit.co>
Co-authored-by: Hadley Wickham <hadley@posit.co>
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