ropensci / cyphr

:shipit: Humane encryption
https://docs.ropensci.org/cyphr
Other
93 stars 10 forks source link

Error in match.call: ... used in a situation where it does not exist #42

Closed bersbersbers closed 3 years ago

bersbersbers commented 3 years ago

For convenience, I am trying to do

save_enc_rds <- function(...) {
    cyphr::encrypt(saveRDS(...), key)
}

However, this fails with

Error in match.call(defn, expr, expand.dots = TRUE) : ... used in a situation where it does not exist

I feel this may due to the use of substitute here (but this may be wrong): https://github.com/ropensci/cyphr/blob/b718ca5bdf270b274046bd6563b61eb26ce245a1/R/encrypt_wrapper.R#L57-L59

How can I get the kind of function wrapper I am looking for?

richfitz commented 3 years ago

Use the encrypt_ version directly and construct the call yourself (e.g., using rlang's tools or substitute yourself), or by using the less "helpful" cyphr functions such as encrypt_data

bersbersbers commented 3 years ago

Thanks - relying on undocumented/internal functions such as encrypt_ is nothing I like to do, unfortunately.

I ended up using

save_enc_rds <- function(object, file) {
    cyphr::encrypt(saveRDS(object, file), key)
}

which is enough for my use case. I'll have to add additional arguments as needed, but that API is probably stable enough.

richfitz commented 3 years ago

The encrypt_ functions are neither undocumented nor internal and are part of the package API.

One possible way of using them would be to do:

save_enc_rds <- function(...) {
  ## The 'rlang' package provides other ways of doing this
  envir <- parent.frame()
  call <- sys.call()
  call[[1L]] <- quote(saveRDS)
  ## Following your example, with 'key' found in the global
  ## environment; I'd recommend doing something more robust here
  ## personally.
  cyphr::encrypt_(call, key, envir = envir)
}

then use as:

key <- cyphr::key_sodium(sodium::keygen())
obj <- runif(10)
path <- tempfile(fileext = ".rds")
save_enc_rds(obj, path, compress = FALSE, version = 3) # note extra args
cyphr::decrypt(readRDS(path), key)

However, much simpler would be to use cyphr::encrypt_object directly, unless you really need to pass more arguments into the serialiser.

path <- tempfile(fileext = ".rds")
cyphr::encrypt_object(obj, key, path)
cyphr::decrypt_object(path, key)
bersbersbers commented 3 years ago

Ah, I see. I was under that false impression due the similarity with Python's _single_leading_underscore. In that process, I also learned about single_trailing_underscore_, which is quite a different concept :)

So that looks very useful then, thanks!