![](/img/trans.png)
[英]How to save workspace when running multiple lines of R scripts with batch file
[英]How to securely provide secrets to azure batch service running R scripts
我正在尝试找到一种方法来为我的批处理服务作业提供敏感数据。 数据是在 R 脚本中用于连接到 SQL 并获取表的 sql 连接凭据。 所以如果我们在批处理工作目录中有一个配置文件,代码看起来像这样。
dbConfig <- config::get(file = "config.yml")
db_connect <- function(database_config_name){
dbConfig <- config::get(database_config_name)
connection <- DBI::dbConnect(odbc::odbc(),
Driver = dbConfig$driver,
Server = dbConfig$server,
UID = dbConfig$uid,
PWD = dbConfig$pwd,
Database = dbConfig$database,
encoding = "UTF-8"
)
为了创建这个配置文件,我找到了 AzureKeyVault包来验证我在 Azure Key Vault 中的机密并将文件写入 Azure 批处理环境。 所以代码看起来像:
devtools::install_github("Azure/AzureKeyVault")
vault <- AzureKeyVault::key_vault("https://mykeyvault.vault.azure.net")
secret <- vault$secrets$get("secretname")
yaml::write_yaml(secret$value,"config.yml")
dbConfig <- config::get(file = "config.yml")
然后我想对秘密进行的操作并最终将其删除:
unlink("config.yml", recursive = FALSE, force = TRUE)
这个操作的问题是,它不是自动认证,控制台打印了以下命令:
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and
enter the code xxxxxxxx to authenticate.
Waiting for device code in browser...
这不是我想要的。 我希望 Azure 批处理自动进行身份验证并获取机密。 或者,如果有任何其他安全方式向 azure 批次提供机密信息。
您需要使用应用程序身份验证初始化您的保管库:
cert <- PKI.load.cert(file=system.file("certs", "app_cert.pfx", package="PKI"))
token <- AzureAuth::get_azure_token("https://vault.azure.net", "myaadtenant",
app="app_id", certificate=cert)
vault <- AzureKeyVault::key_vault("mykeyvault", token=token)
或者(推荐),使用Managed Identity :
vault <- AzureKeyVault::key_vault("mykeyvault", as_managed_identity=TRUE)
干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.