Skip to content

Commit 94eee12

Browse files
tanho63jennybc
andauthored
convert vignette rmd code chunks to plain R chunks to prevent eval (#303)
* convert vignette rmd code chunks to plain R chunks to prevent eval, closes #301 * avoid butchering backticks * Let's focus only on the single, troublesome vignette * Style the way Air would + typo fix * Revert changes to README as well --------- Co-authored-by: Jenny Bryan <jenny.f.bryan@gmail.com>
1 parent a4bc620 commit 94eee12

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# gargle (development version)
22

3+
* In vignettes, convert all `{r eval=FALSE}` chunks to plain `r` chunks to prevent any chance of vignette code evaluation while maintaining R syntax highlighting (#301, @tanho63)
4+
35
# gargle 1.6.0
46

57
* When retrying a request, the messaging reveals more detail about the failed

vignettes/non-interactive-auth.Rmd

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ vignette: >
77
%\VignetteEncoding{UTF-8}
88
---
99

10-
```{r, include = FALSE}
11-
knitr::opts_chunk$set(
12-
collapse = TRUE,
13-
comment = "#>",
14-
eval = FALSE,
15-
purl = FALSE
16-
)
17-
```
18-
1910
Here we describe how to do auth with a package that uses gargle, without requiring any user interaction.
2011
This comes up in a wide array of contexts, ranging from simple rendering of a local R Markdown document to deploying a data product on a remote server.
2112

@@ -56,7 +47,7 @@ When you embed tokens in the project and deploy, remember, that, by default, the
5647
TL;DR is that you need to successfully authenticate *once* in an interactive session and then, in your code, give gargle permission to use a token it finds in the cache.
5748
These sorts of commands achieve that:
5849

59-
```{r}
50+
```r
6051
# Approach #1: use an option.
6152
# Either specify the user:
6253
options(gargle_oauth_email = "jenny@example.com")
@@ -102,13 +93,17 @@ GCE allows applications to get an OAuth access token from its metadata server an
10293
This token request can be made for specific scopes and, in general, most wrapper packages will indeed be asking for specific scopes relevant to the API they access.
10394
Consider the signature of `googledrive::drive_auth()`:
10495

105-
```{r}
106-
drive_auth <- function(email = gargle::gargle_oauth_email(),
107-
path = NULL,
108-
scopes = "https://www.googleapis.com/auth/drive",
109-
cache = gargle::gargle_oauth_cache(),
110-
use_oob = gargle::gargle_oob_default(),
111-
token = NULL) { ... }
96+
```r
97+
drive_auth <- function(
98+
email = gargle::gargle_oauth_email(),
99+
path = NULL,
100+
scopes = "https://www.googleapis.com/auth/drive",
101+
cache = gargle::gargle_oauth_cache(),
102+
use_oob = gargle::gargle_oob_default(),
103+
token = NULL
104+
) {
105+
...
106+
}
112107
```
113108

114109
The googledrive package asks for a token with `"drive"` scope, by default.
@@ -124,7 +119,7 @@ Be aware that you might also need to explicitly grant the service account an app
124119
Finally, if you want to opt-out of using the default service account and, instead, auth as a normal user, even though you are on GCE, that is also possible.
125120
One way to achieve that is to remove `credentials_gce()` from the set of auth functions tried by `gargle::token_fetch()` by executing this command before any explicit or implicit auth happens:
126121

127-
```{r}
122+
```r
128123
# removes `credentials_gce()` from gargle's registry
129124
gargle::cred_funs_add(credentials_gce = NULL)
130125
```
@@ -178,9 +173,11 @@ Documentation around `GKEStartPodOperator()` within Cloud Composer can be found
178173

179174
Here is example code that you might execute in your Docker container:
180175

181-
```{r}
176+
```r
182177
options(gargle.gce.use_ip = TRUE)
183-
t <- gargle::credentials_gce("my-service-key@my-project.iam.gserviceaccount.com")
178+
t <- gargle::credentials_gce(
179+
"my-service-key@my-project.iam.gserviceaccount.com"
180+
)
184181
# ... do authenticated stuff with the token t ...
185182
```
186183

@@ -189,7 +186,7 @@ At the time of writing the `service_account` argument is not exposed in the usua
189186
So if you need to use a non-`default` service account, you need to call `credentials_gce()` directly and pass that token to `PKG_auth()`:
190187
Here's an example of how that might look:
191188

192-
```{r}
189+
```r
193190
library(PKG)
194191

195192
options(gargle.gce.use_ip = TRUE)
@@ -220,7 +217,7 @@ If you're not working in cloud context with automatic access to a service accoun
220217

221218
Example using googledrive:
222219

223-
```{r}
220+
```r
224221
library(googledrive)
225222

226223
drive_auth(path = "/path/to/your/service-account-token.json")
@@ -253,13 +250,13 @@ Some details:
253250
It is also possible to get a token with an explicit call to, e.g.,
254251
`credentials_service_account()` and then pass that token to the auth function:
255252

256-
```{r}
253+
```r
257254
t <- gargle::credentials_service_account(
258255
path = "/path/to/your/service-account-token.json",
259256
scopes = ...,
260257
subject = "user@example.com"
261258
)
262-
googledrive::dive_auth(token = t)
259+
googledrive::drive_auth(token = t)
263260
```
264261

265262
If delegation of domain-wide authority is impossible or unappealing, you must use an OAuth user token, as described below.
@@ -277,7 +274,7 @@ Your token should just get discovered upon first need.
277274

278275
For troubleshooting purposes, you can set a gargle option to see verbose output about the execution of `gargle::token_fetch()`:
279276

280-
```{r}
277+
```r
281278
options(gargle_verbosity = "debug")
282279
```
283280

@@ -287,7 +284,7 @@ withr-style convenience helpers also exist: `with_gargle_verbosity()` and `local
287284

288285
If you somehow have the OAuth token you want to use as an R object, you can provide it directly to the `token` argument of the main auth function. Example using googledrive:
289286

290-
```{r}
287+
```r
291288
library(googledrive)
292289

293290
my_oauth_token <- # some process that results in the token you want to use
@@ -298,7 +295,7 @@ gargle caches each OAuth user token it obtains to an `.rds` file, by default.
298295
If you know the filepath to the token you want to use, you could use `readRDS()` to read it and provide as the `token` argument to the wrapper's auth function.
299296
Example using googledrive:
300297

301-
```{r}
298+
```r
302299
# googledrive
303300
drive_auth(token = readRDS("/path/to/your/oauth-token.rds"))
304301
```
@@ -327,7 +324,7 @@ There are many ways to do this. We'll work several examples using that convey th
327324

328325
**Step 1**: Get that first token. You must run your code at least once, interactively, do the auth dance, and allow gargle to store the token in its cache.
329326

330-
```{r}
327+
```r
331328
library(googledrive)
332329

333330
# do anything that triggers auth
@@ -353,37 +350,37 @@ You have two choices to make:
353350

354351
This sets an option that allows gargle to use cached tokens whenever there's a unique match:
355352

356-
```{r}
353+
```r
357354
options(gargle_oauth_email = TRUE)
358355
```
359356

360357
This sets an option to use tokens associated with a specific email address:
361358

362-
```{r}
359+
```r
363360
options(gargle_oauth_email = "jenny@example.com")
364361
```
365362

366363
This sets an option to use tokens associated with an email address with a specific domain:
367364

368-
```{r}
365+
```r
369366
options(gargle_oauth_email = "*@example.com")
370367
```
371368

372369
This gets a token *right now* and allows the use of a matching token, using googledrive as an example:
373370

374-
```{r}
371+
```r
375372
drive_auth(email = TRUE)
376373
```
377374

378375
This gets a token *right now*, for the user with a specific email address:
379376

380-
```{r}
377+
```r
381378
drive_auth(email = "jenny@example.com")
382379
```
383380

384381
This gets a token *right now*, first checking the cache for a token associated with a specific domain:
385382

386-
```{r}
383+
```r
387384
drive_auth(email = "*@example.com")
388385
```
389386

@@ -394,7 +391,7 @@ This is like the previous example, but with an added twist: we use a project-lev
394391
**Step 1**: Obtain the token intended for non-interactive use and make sure it's cached in a (hidden) directory of the current project.
395392
Using googledrive as an example:
396393

397-
```{r}
394+
```r
398395
library(googledrive)
399396

400397
# designate project-specific cache
@@ -414,7 +411,7 @@ Do this setup once per project.
414411

415412
Another way to accomplish the same setup is to specify the desired cache location directly in the call to the auth function:
416413

417-
```{r}
414+
```r
418415
library(googledrive)
419416

420417
# trigger auth on purpose --> store a token in the specified cache
@@ -423,7 +420,7 @@ drive_auth(cache = ".secrets")
423420

424421
**Step 2**: In all downstream use, announce the location of the cache and pre-authorize the use of a suitable token discovered there. Continuing the googledrive example:
425422

426-
```{r}
423+
```r
427424
library(googledrive)
428425

429426
options(
@@ -442,7 +439,7 @@ Depending on the context, it might be suitable to accomplish this in a startup f
442439
Here's a variation where we say which token to use by explicitly specifying the associated email.
443440
This is handy if there's a reason to have more than one token in the cache.
444441

445-
```{r}
442+
```r
446443
library(googledrive)
447444

448445
options(
@@ -456,7 +453,7 @@ drive_find(n_max = 5)
456453

457454
Here's another variation where we specify the necessary info directly in an auth call, instead of in options:
458455

459-
```{r}
456+
```r
460457
library(googledrive)
461458

462459
drive_auth(cache = ".secrets", email = TRUE)
@@ -467,7 +464,7 @@ drive_find(n_max = 5)
467464

468465
Here's one last variation that's applicable when the local cache could contain multiple tokens:
469466

470-
```{r}
467+
```r
471468
library(googledrive)
472469

473470
drive_auth(cache = ".secrets", email = "jenny@example.com")
@@ -481,7 +478,7 @@ Personally I would use `here::here(".secrets)"` everywhere above, to make things
481478

482479
For troubleshooting purposes, you can set a gargle option to see verbose output about the execution of `gargle::token_fetch()`:
483480

484-
```{r}
481+
```r
485482
options(gargle_verbosity = "debug")
486483
```
487484

0 commit comments

Comments
 (0)