add token as create_azure_login arg

This commit is contained in:
Hong Ooi 2019-05-12 06:11:41 +10:00
Родитель c29cf2283f
Коммит 2141f66281
3 изменённых файлов: 47 добавлений и 32 удалений

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

@ -9,6 +9,7 @@
#' @param host Your ARM host. Defaults to `https://management.azure.com/`. Change this if you are using a government or private cloud.
#' @param aad_host Azure Active Directory host for authentication. Defaults to `https://login.microsoftonline.com/`. Change this if you are using a government or private cloud.
#' @param config_file Optionally, a JSON file containing any of the arguments listed above. Arguments supplied in this file take priority over those supplied on the command line. You can also use the output from the Azure CLI `az ad sp create-for-rbac` command.
#' @param token Optionally, an OAuth 2.0 token, of class [AzureToken]. This allows you to reuse the authentication details for an existing session. If supplied, all other arguments to `create_azure_login` will be ignored.
#' @param refresh For `get_azure_login`, whether to refresh the authentication token on loading the client.
#' @param selection For `get_azure_login`, if you have multiple logins for a given tenant, which one to use. This can be a number, or the input MD5 hash of the token used for the login. If not supplied, `get_azure_login` will print a menu and ask you to choose a login.
#' @param confirm For `delete_azure_login`, whether to ask for confirmation before deleting.
@ -63,40 +64,44 @@
create_azure_login <- function(tenant="common", app=.az_cli_app_id,
password=NULL, username=NULL, certificate=NULL, auth_type=NULL,
host="https://management.azure.com/", aad_host="https://login.microsoftonline.com/",
config_file=NULL, ...)
config_file=NULL, token=NULL, ...)
{
if(!is.null(config_file))
if(!is_azure_token(token))
{
conf <- jsonlite::fromJSON(config_file)
call <- as.list(match.call())[-1]
call$config_file <- NULL
call <- lapply(modifyList(call, conf), function(x) eval.parent(x))
return(do.call(create_azure_login, call))
if(!is.null(config_file))
{
conf <- jsonlite::fromJSON(config_file)
call <- as.list(match.call())[-1]
call$config_file <- NULL
call <- lapply(modifyList(call, conf), function(x) eval.parent(x))
return(do.call(create_azure_login, call))
}
tenant <- normalize_tenant(tenant)
app <- normalize_guid(app)
token_args <- list(resource=host,
tenant=tenant,
app=app,
password=password,
username=username,
certificate=certificate,
auth_type=auth_type,
aad_host=aad_host,
...)
hash <- do.call(token_hash, token_args)
tokenfile <- file.path(AzureR_dir(), hash)
if(file.exists(tokenfile))
{
message("Deleting existing Azure Active Directory token for this set of credentials")
file.remove(tokenfile)
}
message("Creating Azure Resource Manager login for ", format_tenant(tenant))
token <- do.call(get_azure_token, token_args)
}
tenant <- normalize_tenant(tenant)
app <- normalize_guid(app)
token_args <- list(resource=host,
tenant=tenant,
app=app,
password=password,
username=username,
certificate=certificate,
auth_type=auth_type,
aad_host=aad_host,
...)
hash <- do.call(token_hash, token_args)
tokenfile <- file.path(AzureR_dir(), hash)
if(file.exists(tokenfile))
{
message("Deleting existing Azure Active Directory token for this set of credentials")
file.remove(tokenfile)
}
message("Creating Azure Resource Manager login for ", format_tenant(tenant))
token <- do.call(get_azure_token, token_args)
client <- az_rm$new(token=token)
# save login info for future sessions

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

@ -11,7 +11,7 @@ create_azure_login(tenant = "common", app = .az_cli_app_id,
password = NULL, username = NULL, certificate = NULL,
auth_type = NULL, host = "https://management.azure.com/",
aad_host = "https://login.microsoftonline.com/", config_file = NULL,
...)
token = NULL, ...)
get_azure_login(tenant = "common", selection = NULL, refresh = TRUE)
@ -38,6 +38,8 @@ list_azure_logins()
\item{config_file}{Optionally, a JSON file containing any of the arguments listed above. Arguments supplied in this file take priority over those supplied on the command line. You can also use the output from the Azure CLI \code{az ad sp create-for-rbac} command.}
\item{token}{Optionally, an OAuth 2.0 token, of class \link{AzureToken}. This allows you to reuse the authentication details for an existing session. If supplied, all other arguments to \code{create_azure_login} will be ignored.}
\item{...}{For \code{create_azure_login, other arguments passed to }get_azure_token`.}
\item{selection}{For \code{get_azure_login}, if you have multiple logins for a given tenant, which one to use. This can be a number, or the input MD5 hash of the token used for the login. If not supplied, \code{get_azure_login} will print a menu and ask you to choose a login.}

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

@ -16,6 +16,10 @@ test_that("ARM authentication works",
az <- az_rm$new(tenant=tenant, app=app, password=password)
expect_is(az, "az_rm")
expect_true(is_azure_token(az$token))
tok <- get_azure_token("https://management.azure.com/", tenant, app, password)
az2 <- az_rm$new(token=tok)
expect_is(az2, "az_rm")
})
test_that("Login interface works",
@ -30,10 +34,14 @@ test_that("Login interface works",
writeLines(jsonlite::toJSON(list(tenant=tenant, app=app, password=password)), creds)
az4 <- create_azure_login(config_file=creds)
expect_identical(AzureAuth::normalize_tenant(tenant), az4$tenant)
expect_identical(normalize_tenant(tenant), az4$tenant)
expect_is(az4, "az_rm")
az5 <- get_azure_login(tenant)
expect_is(az5, "az_rm")
tok <- get_azure_token("https://management.azure.com/", tenant, app, password)
az6 <- create_azure_login(token=tok)
expect_is(az6, "az_rm")
})