diff --git a/.Rbuildignore b/.Rbuildignore index 3cf22fa..9bb255d 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -6,6 +6,7 @@ ^\.Rproj\.user$ .travis.yml CONTRIBUTING.md +CODE_OF_CONDUCT.md drat.sh ^LICENSE\.md$ azure-pipelines.yml diff --git a/README.md b/CONTRIBUTING.md similarity index 98% rename from README.md rename to CONTRIBUTING.md index b81a84e..180de51 100644 --- a/README.md +++ b/CONTRIBUTING.md @@ -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. diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..80fc945 --- /dev/null +++ b/DESCRIPTION @@ -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 diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..4cdf482 --- /dev/null +++ b/NAMESPACE @@ -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) diff --git a/R/AzureVMmetadata.R b/R/AzureVMmetadata.R new file mode 100644 index 0000000..2ea9218 --- /dev/null +++ b/R/AzureVMmetadata.R @@ -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() +} +