Allow use of public key resource (#19)

* use Azure public key res

* working

* correct context

* expand examples
This commit is contained in:
Hong Ooi 2020-07-24 23:13:35 +10:00 коммит произвёл GitHub
Родитель 660fd11891
Коммит 9fad8b15b7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 108 добавлений и 4 удалений

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

@ -1,5 +1,6 @@
# AzureVM 2.1.1.9000
- Add ability to retrieve an SSH public key from an Azure resource object. See `?user_config` for more information.
- Add Ubuntu 20.04 as a predefined configuration.
# AzureVM 2.1.1

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

@ -1,7 +1,7 @@
#' Resource configuration functions for a virtual machine deployment
#'
#' @param username For `user_config`, the name for the admin user account.
#' @param sshkey For `user_config`, string containing an SSH public key. Can be the key itself, or the name of the public key file.
#' @param sshkey For `user_config`, the SSH public key. This can be supplied in a number of ways: as a string with the key itself; the name of the public key file; or an `AzureRMR::az_resource` object pointing to an SSH public key resource (of type "Microsoft.Compute/sshPublicKeys"). See the examples below.
#' @param password For `user_config`, the admin password. Supply either `sshkey` or `password`, but not both; also, note that Windows does not support SSH logins.
#' @param size For `datadisk_config`, the size of the data disk in GB. St this to NULL for a disk that will be created from an image.
#' @param name For `datadisk_config`, the disk name. Duplicate names will automatically be disambiguated prior to VM deployment.
@ -11,18 +11,56 @@
#' @param publisher,offer,sku,version For `image_config`, the details for a marketplace image.
#' @param id For `image_config`, the resource ID for a disk to use as a custom image.
#'
#' @examples
#' \dontrun{
#'
#' ## user_config: SSH public key resource in Azure
#' # create the resource
#' keyres <- rg$create_resource(type="Microsoft.Compute/sshPublicKeys", name="mysshkey")
#'
#' # generate the public and private keys
#' keys <- keyres$do_operation("generateKeyPair", http_verb="POST")
#' keyres$sync_fields()
#'
#' # save the private key (IMPORTANT)
#' writeBin(keys$privateKey, "mysshkey.pem")
#'
#' # create a new VM using the public key resource for authentication
#' # you can then login to the VM with ssh -i mysshkey.pem <username@vmaddress>
#' rg$create_vm("myvm", user_config("username", sshkey=keyres), config="ubuntu_20.04")
#'
#'
#' ## user_config: SSH public key as a file
#' rg$create_vm("myvm", user_config("username", sshkey="mysshkey.pub"), config="ubuntu_20.04")
#'
#'
#' ## user_config: SSH public key as a string (read from a file)
#' pubkey <- readLines("mysshkey.pub")
#' rg$create_vm("myvm", user_config("username", sshkey=pubkey), config="ubuntu_20.04")
#'
#' }
#'
#' @rdname vm_resource_config
#' @export
user_config <- function(username, sshkey=NULL, password=NULL)
{
keyres <- is_resource(sshkey) && tolower(sshkey$type) == "microsoft.compute/sshpublickeys"
key <- is.character(sshkey) || keyres
pwd <- is.character(password)
key <- is.character(sshkey)
if(!pwd && !key)
stop("Must supply either a login password or SSH key", call.=FALSE)
if(pwd && key)
stop("Supply either a login password or SSH key, but not both", call.=FALSE)
if(key && file.exists(sshkey))
if(keyres)
{
sshkey <- gsub("\r\n", "", sshkey$properties$publicKey)
if(is_empty(sshkey))
stop("Supplied public key resource is uninitialized, run generateKeyPair first and save the returned keys",
call.=FALSE)
}
else if(key && file.exists(sshkey))
sshkey <- readLines(sshkey)
structure(list(user=username, key=sshkey, pwd=password), class="user_config")

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

@ -18,7 +18,7 @@ image_config(publisher = NULL, offer = NULL, sku = NULL,
\arguments{
\item{username}{For \code{user_config}, the name for the admin user account.}
\item{sshkey}{For \code{user_config}, string containing an SSH public key. Can be the key itself, or the name of the public key file.}
\item{sshkey}{For \code{user_config}, the SSH public key. This can be supplied in a number of ways: as a string with the key itself; the name of the public key file; or an \code{AzureRMR::az_resource} object pointing to an SSH public key resource (of type "Microsoft.Compute/sshPublicKeys"). See the examples below.}
\item{password}{For \code{user_config}, the admin password. Supply either \code{sshkey} or \code{password}, but not both; also, note that Windows does not support SSH logins.}
@ -39,3 +39,33 @@ image_config(publisher = NULL, offer = NULL, sku = NULL,
\description{
Resource configuration functions for a virtual machine deployment
}
\examples{
\dontrun{
## user_config: SSH public key resource in Azure
# create the resource
keyres <- rg$create_resource(type="Microsoft.Compute/sshPublicKeys", name="mysshkey")
# generate the public and private keys
keys <- keyres$do_operation("generateKeyPair", http_verb="POST")
keyres$sync_fields()
# save the private key (IMPORTANT)
writeBin(keys$privateKey, "mysshkey.pem")
# create a new VM using the public key resource for authentication
# you can then login to the VM with ssh -i mysshkey.pem <username@vmaddress>
rg$create_vm("myvm", user_config("username", sshkey=keyres), config="ubuntu_20.04")
## user_config: SSH public key as a file
rg$create_vm("myvm", user_config("username", sshkey="mysshkey.pub"), config="ubuntu_20.04")
## user_config: SSH public key as a string (read from a file)
pubkey <- readLines("mysshkey.pub")
rg$create_vm("myvm", user_config("username", sshkey=pubkey), config="ubuntu_20.04")
}
}

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

@ -0,0 +1,35 @@
context("Using SSH public key resource")
tenant <- Sys.getenv("AZ_TEST_TENANT_ID")
app <- Sys.getenv("AZ_TEST_APP_ID")
password <- Sys.getenv("AZ_TEST_PASSWORD")
subscription <- Sys.getenv("AZ_TEST_SUBSCRIPTION")
if(tenant == "" || app == "" || password == "" || subscription == "")
skip("Tests skipped: ARM credentials not set")
rgname <- paste0("vm", paste0(sample(letters, 10, TRUE), collapse=""))
location <- "australiaeast"
size <- "Standard_DS1_v2"
rg <- AzureRMR::az_rm$
new(tenant=tenant, app=app, password=password)$
get_subscription(subscription)$
create_resource_group(rgname, location)
test_that("Deploying VM with SSH public key resource works",
{
vmname <- paste0(sample(letters, 10, TRUE), collapse="")
keyname <- vmname
expect_silent(keyres <- rg$create_resource(type="Microsoft.Compute/sshPublicKeys", name=keyname))
expect_silent(keys <- keyres$do_operation("generateKeyPair", http_verb="POST"))
expect_true(is.character(keys$publicKey))
keyres$sync_fields()
user <- user_config("username", keyres)
vm <- rg$create_vm(vmname, user, size, config="ubuntu_20.04")
expect_is(vm, "az_vm_template")
})
rg$delete(confirm=FALSE)