简体   繁体   English

R CMD check fails with ubuntu when trying to download file, but function works within R

[英]R CMD check fails with ubuntu when trying to download file, but function works within R

I am writing an R package and one of its functions download and unzips a file from a link (it is not exported to the user, though):我正在写一个 R package ,它的一个功能从链接下载并解压缩文件(虽然它不会导出给用户):

download_f <- function(download_dir) {
  utils::download.file(
    url = "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php",
    destfile = file.path(download_dir, "fines.rar"),
    mode = 'wb',
    method = 'libcurl'
  )

  utils::unzip(
    zipfile = file.path(download_dir, "fines.rar"),
    exdir = file.path(download_dir)
  )
}

This function works fine with me when I run it within some other function to compile an example in a vignette.当我在其他一些 function 中运行它以在小插图中编译示例时,这个 function 对我来说很好。

However, with R CMD check in github action, it fails consistently on ubuntu 16.04, release and devel.但是,使用 R CMD 检查 github 操作,它在 Z1D41C853AF248D47A7AEZ5416CE 上始终失败。 It [says][1]:它[说][1]:

Error: Error: processing vignette 'IBAMA.Rmd' failed with diagnostics:
cannot open URL 'https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php'
--- failed re-building ‘IBAMA.Rmd’

SUMMARY: processing the following file failed:
  ‘IBAMA.Rmd’

Error: Error: Vignette re-building failed.
Execution halted
Error: Error in proc$get_built_file() : Build process failed
Calls: <Anonymous> ... build_package -> with_envvar -> force -> <Anonymous>
Execution halted
Error: Process completed with exit code 1.

When I run devtools::check() it never finishes running it, staying in "creating vignettes" forever.当我运行devtools::check()时,它永远不会完成运行,永远停留在“创建小插曲”中。 I don't know if these problems are related though because there are other vignettes on the package.我不知道这些问题是否相关,因为 package 上还有其他小插曲。

I pass the R CMD checks with mac os and windows.我使用 mac os 和 windows 通过了 R CMD 检查。 I've tried switching the "mode" and "method" arguments on utils::download.file , but to no avail.我尝试在utils::download.file上切换“模式”和“方法” arguments ,但无济于事。

Any suggestions?有什么建议么? [1]: https://github.com/datazoompuc/datazoom.amazonia/pull/16/checks?check_run_id=2026865974 [1]: https://github.com/datazoompuc/datazoom.amazonia/pull/16/checks?check_run_id=2026865974

The download fails because libcurl tries to verify the webservers certificate, but can't.下载失败,因为 libcurl 尝试验证网络服务器证书,但不能。

I can reproduce this on my system:我可以在我的系统上重现这个:

trying URL 'https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php'
Error in utils::download.file(url = "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php",  : 
  cannot open URL 'https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php'
In addition: Warning message:
In utils::download.file(url = "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php",  :
  URL 'https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php': status was 'SSL peer certificate or SSH remote key was not OK'

The server does not allow you to download from http but redirects to https, so the only thing to do now is to tell libcurl to not check the certificate and accept what it is getting.服务器不允许你从 http 下载,而是重定向到 https,所以现在唯一要做的就是告诉 libcurl 不要检查证书并接受它所获得的。

You can do this by specifying the argument -k to curl您可以通过将参数-k指定为curl

download_f <- function(download_dir) {
  utils::download.file(
    url = "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php",
    destfile = file.path(download_dir, "fines.rar"),
    mode = 'wb',
    method = 'curl',
    extra = '-k'
  ) 
  
  utils::unzip(
    zipfile = file.path(download_dir, "fines.rar"),
    exdir = file.path(download_dir)
  ) 
} 

This also produces some download progress bar, you can silence this by setting extra to -k -s这也会产生一些下载进度条,您可以通过将额外设置为-k -s来使其静音

This now opens you up to a Machine In The Middle Attack.现在,这使您可以使用中间攻击的机器。 (You possibly already are attacked this way, there is no way to check without verifying the current certificate with someone you know at the other side) So you could implement an extra check, eg check the sha256sum of the downloaded file and see if it matches what you expect to receive before proceeding. (您可能已经受到这种方式的攻击,如果不与对方认识的人验证当前证书就无法检查)因此您可以实施额外检查,例如检查下载文件的 sha256sum并查看它是否匹配在继续之前您希望收到的内容。

myfile <- system.file("fines.rar")
hash <- sha256(file(myfile))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM