This commit is contained in:
Hong Ooi 2019-08-03 17:35:15 +10:00
Родитель 74be229de2
Коммит 56dac696f3
5 изменённых файлов: 136 добавлений и 14 удалений

Просмотреть файл

@ -6,6 +6,7 @@
^\.Rproj\.user$
.travis.yml
CONTRIBUTING.md
CODE_OF_CONDUCT.md
drat.sh
^LICENSE\.md$
azure-pipelines.yml

Просмотреть файл

@ -1,14 +1,13 @@
# Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
# Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.

23
DESCRIPTION Normal file
Просмотреть файл

@ -0,0 +1,23 @@
Package: AzureVMmetadata
Title: Interface to Azure virtual machine instance metadata
Version: 0.0.1
Authors@R: c(
person("Hong", "Ooi", , "hongooi@microsoft.com", role = c("aut", "cre")),
person("Microsoft", role="cph")
)
Description: A simple interface to the instance metadata inside a virtual machine running in Microsoft's 'Azure' cloud. Part of the 'AzureR' family of packages.
URL: https://github.com/Azure/AzureVMmetadata https://github.com/Azure/AzureR
BugReports: https://github.com/Azure/AzureVMmetadata/issues
License: MIT + file LICENSE
VignetteBuilder: knitr
Depends:
R (>= 3.3)
Imports:
openssl,
httr (>= 1.3)
Suggests:
knitr,
testthat,
AzureAuth
Roxygen: list(markdown=TRUE)
RoxygenNote: 6.1.1

7
NAMESPACE Normal file
Просмотреть файл

@ -0,0 +1,7 @@
# Generated by roxygen2: do not edit by hand
export(attested)
export(in_azure_vm)
export(instance)
export(update_attested_metadata)
export(update_instance_metadata)

92
R/AzureVMmetadata.R Normal file
Просмотреть файл

@ -0,0 +1,92 @@
host <- httr::parse_url("http://169.254.169.254")
inst_api_version <- "2019-02-01"
att_api_version <- "2018-10-01"
ev_api_version <- "2017-11-01"
#' @export
instance <- new.env()
#' @export
attested <- new.env()
#' @export
events <- new.env()
#' @export
update_instance_metadata <- function()
{
host$path <- "metadata/instance"
host$query <- list(`api-version`=att_api_version)
res <- try(httr::GET(host, httr::add_headers(metadata=TRUE)), silent=TRUE)
if(!inherits(res, "response") || res$status_code > 299)
return(NULL)
inst <- httr::content(res)
for(x in names(inst))
instance[[x]] <- inst[[x]]
invisible(inst)
}
#' @export
update_attested_metadata <- function(nonce=NULL)
{
host$path <- "metadata/attested/document"
host$query <- list(`api-version`=att_api_version)
res <- try(httr::GET(host, httr::add_headers(metadata=TRUE, nonce=nonce)), silent=TRUE)
if(!inherits(res, "response") || res$status_code > 299)
return(NULL)
att <- httr::content(res)
for(x in names(att))
attested[[x]] <- att[[x]]
invisible(att)
}
#' @export
update_scheduled_events <- function()
{
host$path <- "metadata/scheduledevents"
host$query <- list(`api-version`=ev_api_version)
res <- try(httr::GET(host, httr::add_headers(metadata=TRUE)), silent=TRUE)
if(!inherits(res, "response") || res$status_code > 299)
return(NULL)
ev <- httr::content(res)
for(x in names(ev))
events[[x]] <- ev[[x]]
invisible(ev)
}
#' @export
get_vm_cert <- function()
{
if(is.null(attested$signature))
return(NULL)
openssl::read_p7b(openssl::base64_decode(attested$signature))[[1]]
}
#' @export
in_azure_vm <- function()
{
obj <- try(httr::GET(host), silent=TRUE)
inherits(obj, "response") && httr::status_code(obj) == 400
}
.onLoad <- function(libname, pkgname)
{
update_instance_metadata()
update_attested_metadata()
}