r-lib / keyring

:closed_lock_with_key: Access the system credential store from R
https://keyring.r-lib.org/
Other
196 stars 28 forks source link

keyring 1.2.0 not working when kniting R markdown file #116

Open SueYang opened 3 years ago

SueYang commented 3 years ago

Hi, when using latest version (1.2.0) to knit R markdown file, there's no prompt for password.

if(!exists(".pwd")) { tryCatch({ .pwd<- key_get("db", keyring = "ds")

make sure to lock the keyring at the end

keyring_lock("ds")

}, error = function(e) { cat("Error when unlocking ds") stop() } ) }

And the error message are image

After switching back to older version keyring (1.1.0) the problem is gone.

gaborcsardi commented 3 years ago

Can you please include the error message from keyring?

SueYang commented 3 years ago

Hi I already pasted the error message in the op

gaborcsardi commented 3 years ago

No, it is not. Your code catches the error in e and then ignores it and throws another error without an error message.

E.g.

tryCatch(
  stop("original error message"), 
  error = function(e) { 
    cat("something failed"); stop() 
  }
)
#> something failedError in value[[3L]](cond) :

Btw. it also does not run keyring_lock("ds") on error, if that was your intent.

SueYang commented 3 years ago

Got it. I remove the trycatch part and here's the error message from keyring package: image

spgarbet commented 2 years ago

I have this problem and it's really hurting. This works if I use getPass instead.

I could work around this issue if keyring_create allowed an optional password to be passed in. It would be more logic in my code I'm working on.

However at present this blocks me from being able to use keyring to manage api_keys for producing reports in Rmarkdown.

---
title: "test pass"
author: "Biostatistics IT Staff"
date: "11/4/2021"
output: html_document
---

\```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(getPass)
library(askpass)

\```

# getPass

\```{r getpass}
getPass()
\```

# askpass

\```{r askpass}
askpass()
\```
spgarbet commented 2 years ago

Also I'm using R 4.1.2. RStudio, 2021.09.0 Build 351

gaborcsardi commented 2 years ago

This will be eventually fixed in askpass: https://github.com/r-lib/askpass/issues/3

Until then you can use a workaround and query the password with getPass() first:

keyring::keyring_unlock("ds", password = getPass::getPass())
.pwd<- key_get("db", keyring = "ds")