Skip to content

Commit

Permalink
Use modern gargle secret management (#572)
Browse files Browse the repository at this point in the history
Also enable for pkgdown and switch to `@examplesIf` for better display.
  • Loading branch information
hadley authored Nov 10, 2023
1 parent 157f6fc commit 19724c3
Show file tree
Hide file tree
Showing 27 changed files with 63 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/live-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ jobs:
with:
upload-snapshots: true
env:
BIGRQUERY_PASSWORD: ${{ secrets.BIGRQUERY_PASSWORD }}
BIGRQUERY_KEY: ${{ secrets.BIGRQUERY_KEY }}
BIGQUERY_TEST_PROJECT: gargle-169921
BIGQUERY_TEST_BUCKET: bigrquery-test-bucket-calliope
4 changes: 4 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ jobs:
- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}
env:
BIGRQUERY_KEY: ${{ secrets.BIGRQUERY_KEY }}
BIGQUERY_TEST_PROJECT: gargle-169921
BIGQUERY_TEST_BUCKET: bigrquery-test-bucket-calliope

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
Expand Down
10 changes: 10 additions & 0 deletions R/bq-auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ bq_auth <- function(email = gargle::gargle_oauth_email(),
invisible()
}

has_internal_auth <- function() {
gargle::secret_has_key("BIGRQUERY_KEY")
}

bq_auth_internal <- function() {
path <- system.file("secret", "bigrquery-testing.json", package = "bigrquery")
json <- gargle::secret_decrypt_json(path, "BIGRQUERY_KEY")
bq_auth(path = json)
}

#' Clear current token
#'
#' Clears any currently stored token. The next time bigrquery needs a token, the
Expand Down
4 changes: 1 addition & 3 deletions R/bq-dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
#' * [insert](https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/insert)
#' * [delete](https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/delete)
#' * [list](https://cloud.google.com/bigquery/docs/reference/rest/v2/tables/list)
#' @examples
#' if (bq_testable()) {
#' @examplesIf bq_testable()
#' ds <- bq_dataset(bq_test_project(), "dataset_api")
#' bq_dataset_exists(ds)
#'
Expand All @@ -31,7 +30,6 @@
#' bq_table_create(bq_table(ds, "x2"))
#' bq_table_create(bq_table(ds, "x3"))
#' bq_dataset_tables(ds)
#' }
#' @name api-dataset
NULL

Expand Down
6 changes: 2 additions & 4 deletions R/bq-download.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@
#' @section Google BigQuery API documentation:
#' * [list](https://cloud.google.com/bigquery/docs/reference/rest/v2/tabledata/list)
#' @export
#' @examples
#' if (bq_testable()) {
#' df <- bq_table_download("publicdata.samples.natality", n_max = 35000)
#' }
#' @examplesIf bq_testable()
#' df <- bq_table_download("publicdata.samples.natality", n_max = 35000)
bq_table_download <-
function(x,
n_max = Inf,
Expand Down
4 changes: 1 addition & 3 deletions R/bq-job.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#' @section Google BigQuery API documentation:
#' * [get](https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/get)
#'
#' @examples
#' if (bq_testable()) {
#' @examplesIf bq_testable()
#' jobs <- bq_project_jobs(bq_test_project())
#' jobs[[1]]
#'
Expand All @@ -16,7 +15,6 @@
#'
#' # Wait for job to complete
#' bq_job_wait(jobs[[1]])
#' }
#' @name api-job
NULL

Expand Down
4 changes: 1 addition & 3 deletions R/bq-perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
#'
#' @return A [bq_job].
#' @keywords internal
#' @examples
#' if (bq_testable()) {
#' @examplesIf bq_testable()
#' ds <- bq_test_dataset()
#' bq_mtcars <- bq_table(ds, "mtcars")
#' job <- bq_perform_upload(bq_mtcars, mtcars)
Expand All @@ -32,7 +31,6 @@
#' bq_job_wait(job)
#' bq_table_exists(bq_mtcars)
#' head(bq_table_download(bq_mtcars))
#' }
#' @name api-perform
NULL

Expand Down
6 changes: 1 addition & 5 deletions R/bq-project.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
#' * `bq_project_jobs()`: a list of [bq_job]s.
#'
#' @name api-project
#' @examples
#' if (bq_authable()) {
#' @examplesIf bq_testable()
#' bq_project_datasets("bigquery-public-data")
#' bq_project_datasets("githubarchive")
#' }
#'
#' if (bq_testable()) {
#' bq_project_jobs(bq_test_project(), page_size = 10)
#' }
NULL

#' @export
Expand Down
4 changes: 1 addition & 3 deletions R/bq-projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
#' @param warn If `TRUE`, warn when there are unretrieved pages.
#' @return A character vector.
#' @export
#' @examples
#' if (bq_authable()) {
#' @examplesIf bq_testable()
#' bq_projects()
#' }
bq_projects <- function(page_size = 100, max_pages = 1, warn = TRUE) {
pages <- bq_get_paginated(
"projects",
Expand Down
4 changes: 1 addition & 3 deletions R/bq-query.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
#' @param ... Passed on to [bq_perform_query()]
#' @name bq_query
#' @return A [bq_table]
#' @examples
#' if (bq_testable()) {
#' @examplesIf bq_testable()
#' # Querying a project requires full name in query
#' tb <- bq_project_query(
#' bq_test_project(),
Expand All @@ -38,7 +37,6 @@
#' billing = bq_test_project()
#' )
#' bq_table_download(tb)
#' }
NULL

#' @export
Expand Down
4 changes: 1 addition & 3 deletions R/bq-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
#' * `bq_table_size()`: the size of the table in bytes
#' * `bq_table_fields()`: a [bq_fields].
#'
#' @examples
#' if (bq_testable()) {
#' @examplesIf bq_testable()
#' ds <- bq_test_dataset()
#'
#' bq_mtcars <- bq_table(ds, "mtcars")
Expand All @@ -47,7 +46,6 @@
#'
#' my_natality <- bq_table(ds, "mynatality")
#' bq_table_copy("publicdata.samples.natality", my_natality)
#' }
#' @name api-table
NULL

Expand Down
10 changes: 4 additions & 6 deletions R/bq-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
#' tied to the lifetime of the object that it returns.
#' @export
#' @keywords internal
#' @examples
#' if (bq_testable()) {
#' ds <- bq_test_dataset()
#' bq_mtcars <- bq_table_upload(bq_table(ds, "mtcars"), mtcars)
#' @examplesIf bq_testable()
#' ds <- bq_test_dataset()
#' bq_mtcars <- bq_table_upload(bq_table(ds, "mtcars"), mtcars)
#'
#' # dataset and table will be automatically deleted when ds is GC'd
#' }
#' # dataset and table will be automatically deleted when ds is GC'd
bq_test_project <- function() {
if (is_testing() && !bq_authable()) {
testthat::skip("No BigQuery access credentials available")
Expand Down
5 changes: 1 addition & 4 deletions R/dbi-driver.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ NULL
#' @export
#' @usage NULL
#' @import methods DBI
#' @examples
#' if (bq_testable()) {
#' @examplesIf bq_testable()
#' con <- DBI::dbConnect(
#' bigquery(),
#' project = "publicdata",
Expand Down Expand Up @@ -41,8 +40,6 @@ NULL
#' dbFetch(res, 10)
#' dbFetch(res, -1)
#' DBI::dbHasCompleted(res)
#'
#' }
bigquery <- function() {
new("BigQueryDriver")
}
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ defer <- function (expr, env = caller_env(), after = FALSE) {
do.call(on.exit, list(thunk, TRUE, after), envir = env)
}

in_pkgdown <- function(){
identical(Sys.getenv("IN_PKGDOWN"), "true")
}

as_query <- function(x, error_arg = caller_arg(x), error_call = caller_env()) {
if (is(x, "SQL")) {
x <- x@.Data
Expand Down
4 changes: 4 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
auth_active = TRUE
)

if (has_internal_auth() && in_pkgdown()) {
bq_auth_internal()
}

# S3 methods --------------------------------------------------------------
s3_register("dplyr::collect", "tbl_BigQueryConnection")

Expand Down
Binary file modified inst/secret/bigrquery-testing.json
Binary file not shown.
4 changes: 2 additions & 2 deletions man/api-dataset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/api-job.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/api-perform.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions man/api-project.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/api-table.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/bigquery.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/bq_projects.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/bq_query.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/bq_table_download.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/bq_test_project.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions tests/testthat/helper-auth.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
if (gargle:::secret_can_decrypt("bigrquery")) {
json <- gargle:::secret_read("bigrquery", "bigrquery-testing.json")
bq_auth(path = rawToChar(json))
if (has_internal_auth()) {
bq_auth_internal()
}

0 comments on commit 19724c3

Please sign in to comment.