保护 .Rprofile 中私人 R 回购的凭据

问题描述 投票:0回答:1

与 python pip 不同,R 似乎公开了为

.Rprofile
中定义的私有 R 存储库配置的凭据。我想这是由于 R 将字符串视为 URL。

local({r <- getOption("repos")
       r["Nexus"] <- "https://username:[email protected]/repository/r-group"
       options(repos=r)
})

然后当我安装一个包时:

> install.packages("shinydashboard")
trying URL 'https://username:[email protected]/repository/r-group/bin/macosx/el-capitan/contrib/3.6/shinydashboard_0.7.1.tgz'
Content type 'application/x-tgz' length 326031 bytes (318 KB)
==================================================
downloaded 318 KB


The downloaded binary packages are in
    /var/folders/7_/pt_pgg2j531f2jc_n5znht600000gn/T//RtmpZkpXkN/downloaded_packages

R 是否有防止凭证暴露的配置选项?

r credentials
1个回答
0
投票

我使用 renv 和 Authorization header 解决了这个(和其他问题)。
renv 帮助您为 R 项目创建虚拟环境,锁定项目中使用的所有包(及其版本)。
您可以在此处阅读有关身份验证标头的信息:Web API Authentication Basic vs Bearer

您可以通过将

.Rprofile
文件添加到项目中来使 renv 与私有存储库一起工作,如下所示:

source("renv/activate.R")
local({
  project_repos <- c(
    CRAN = "https://cloud.r-project.org",
    PRIVATE_CRAN = "https://your-private-cran.io"
    )

  options(repos = project_repos)
  options(
    renv.download.headers = function(url) {
      if (grepl(paste0("^", project_repos["PRIVATE_CRAN"]), url))
        return(c(Authorization = paste0("Bearer ", Sys.getenv("AUTH_TOKEN"))))
    })

这将在您每次从私有 CRAN 访问文件时自动添加授权标头。
如果满足所有先决条件,安装包将如下所示:

> renv::install("[email protected]")
Retrieving 'https://your-private-cran.io/Cran-local/src/contrib/Archive/private_packge/2.7.2/private_packge_2.7.2.tar.gz' ...
    OK [file is up to date]
Installing private_packge [2.7.2] ...
    OK [built from source]
Moving private_packge [2.7.2] into the cache ...
    OK [moved to cache in 1.3 milliseconds]

先决条件是:

  1. 为您的项目安装和配置renv
  2. 您的私人 CRAN 应该能够接受授权标头。
  3. 身份验证令牌存储在名为
    AUTH_TOKEN
    的环境变量中。
  4. 请注意,我使用 Bearer 身份验证,这意味着我从我的私人 CRAN 获得了专用令牌。如果你想使用
    username:password
    ,那么你需要使用Basic身份验证,并且名为
    AUTH_TOKEN
    的环境变量应该是
    username:password
    的base64编码。
© www.soinside.com 2019 - 2024. All rights reserved.