Security
R
Shiny
Foundations series · part 7

Secrets management

Names in git, values in a vault, and no long-lived keys if you can avoid them

Separate configuration names in version control from secrets stored in vaults and environment variables.
Published

2026-09-26

Modified

2026-09-27

The principle

A secret is anything that grants access: a database password, an API token, a PAT, a service-account key. The rule that makes everything else simple:

The name of a secret lives in git. The value never does.

Code asks for Sys.getenv("DB_PASSWORD"). Where the value comes from depends on where the code runs, and none of those places is the repo. Secret scanning is the safety net for when this rule slips; this post is about not needing the net.

Where values live

Where the code runs Store How code reads it
Your laptop Project .Renviron (gitignored), or the OS keychain Sys.getenv(), keyring::key_get()
GitHub Actions Repository/environment secrets (gh secret set) ${{ secrets.NAME }}; masked in logs
Posit Connect Content Vars (environment variables) Sys.getenv()
shinyapps.io / Connect via rsconnect rsconnect::deployApp(envVars = c("DB_PASSWORD")) sends your local values Sys.getenv()
Google Cloud Secret Manager, referenced by name Injected as env vars at runtime

The web-repo way (what we did)

  • The production vault is GCP Secret Manager. The app config (apphosting.yaml) names the secret, and the platform injects the value at runtime:

    env:
      - variable: CONTACT_NOTIFY_TO
        secret: contactNotifyTo      # a name, not a value
  • .env.example is committed, with every variable listed and no values. Real .env* files are gitignored.

  • NEXT_PUBLIC_* means public. Anything with that prefix is shipped to browsers, so it’s never a secret. In Shiny terms: anything sent to the UI (including session$sendCustomMessage() payloads) is public.

  • No JSON service-account keys, ever. A key file is a password that never expires.

Keyless deploys: the modern default

Our playbook deploys from GitHub Actions to Firebase Hosting without any secret at all. GitHub issues each workflow run a short-lived OIDC token. Google Cloud’s Workload Identity Federation trusts tokens from one specific repo (pinned by numeric repo ID) and lets it act as a deploy-only service account for the length of the job:

permissions:
  id-token: write
steps:
  - uses: google-github-actions/auth@v3
    with:
      workload_identity_provider: ${{ vars.GCP_WORKLOAD_IDENTITY_PROVIDER }}
      service_account: ${{ vars.GCP_SERVICE_ACCOUNT }}

The provider and service-account names are identifiers, so they’re stored as plain repository variables, not secrets. Firebase’s own deploy action asks for a JSON key, which is why we used this route with the Firebase CLI instead. AWS and Azure support the same OIDC pattern, so ask your platform team whether your cloud or Connect setup does too.

The R way

Local: .Renviron with a committed example

usethis::edit_r_environ(scope = "project")   # creates/opens ./.Renviron
usethis::use_git_ignore(".Renviron")         # git_vaccinate() does NOT cover it (see secret scanning)

Commit a .Renviron.example with the names and no values. It’s the R twin of .env.example:

# .Renviron.example: copy to .Renviron and fill in
DB_HOST=
DB_USER=
DB_PASSWORD=
R_CONFIG_ACTIVE=default

Remember from identity and environment isolation: a project .Renviron replaces ~/.Renviron. It doesn’t add to it.

Keychain: keyring

For secrets you’d rather not keep in a plain file:

keyring::key_set("warehouse-db", username = "analyst")   # prompts; stored in macOS Keychain / Windows Credential Store / Secret Service
keyring::key_get("warehouse-db", username = "analyst")

Environments: config

config picks a block from config.yml using R_CONFIG_ACTIVE, and !expr pulls values from the environment, so the YAML file itself holds no secrets:

default:
  db_host: localhost
  db_password: !expr Sys.getenv("DB_PASSWORD")
rsconnect:
  db_host: warehouse.internal
cfg <- config::get()
DBI::dbConnect(odbc::odbc(), server = cfg$db_host, pwd = cfg$db_password)

Posit Connect sets R_CONFIG_ACTIVE=rsconnect, according to config’s own documentation, so the same code picks the right block on deploy without any if.

Deploying

  • Posit Connect: set values in the content’s Vars panel, or pass envVars = c("DB_PASSWORD") to rsconnect::deployApp(), which sends your local values. Leaving a name out of envVars leaves the value on the server unchanged.
  • GitHub Actions: gh secret set DB_PASSWORD, then env: DB_PASSWORD: ${{ secrets.DB_PASSWORD }}. Secrets aren’t passed to workflows triggered by PRs from forks.

Pitfalls

  • Printing a secret while debugging. print(Sys.getenv()) ends up in logs and rendered reports.
  • Secrets as function defaults. connect <- function(pwd = "hunter2") is a secret in git.
  • One shared “team” token. Use per-person or per-service credentials, so you can revoke one without breaking all.
  • Rotation nobody owns. Write down who rotates what, and how often. Keyless auth removes most of this.

Sources

Vaults and platforms

R package documentation

NoteUpdates
  • 2026-09-26: Removed the end-of-post checklist. Marked interactive and environment-dependent blocks as .norun.
  • 2026-09-27: Added a Sources section.