This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
credentials.R
365 lines (321 loc) · 12.2 KB
/
credentials.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#' Creates a credentials file for rAzureBatch package authentication
#'
#' @param fileName Credentials file name
#' @param authenticationType The type of authentication for Azure: SharedKey, ServicePrincipal
#' @param ... Further named parameters
#' \itemize{
#' \item{"batchAccount"}: {Batch account name for Batch Service authentication.}
#' \item{"batchKey"}: {Batch account key for signing REST signatures.}
#' \item{"batchUrl"}: {Batch service url for account.}
#' \item{"storageAccount"}: {Storage account for storing output results.}
#' \item{"storageKey"}: {Storage account key for storage service authentication.}
#' \item{"storageEndpointSuffix"}: {Values: core.windows.net,
#' core.chinacloudapi.cn, core.cloudapi.de, core.usgovcloudapi.net }
#' \item{"githubAuthenticationToken"}: {GitHub authentication token for pulling R
#' packages from private GitHub repositories}
#' \item{"dockerAuthentication"}: {Docker authentication for pulling Docker images
#' from private Docker registries}
#' \item{"dockerUsername"}: {Username to docker registry}
#' \item{"dockerPassword"}: {Password to docker registry}
#' \item{"dockerRegistry"}: {URL to docker registry}
#'
#'}
#' @return The request to the Batch service was successful.
#' @examples {
#' generateCredentialsConfig("test_config.json")
#' generateCredentialsConfig("test_config.json", batchAccount = "testbatchaccount",
#' batchKey = "test_batch_account_key", batchUrl = "http://testbatchaccount.azure.com",
#' storageAccount = "teststorageaccount", storageKey = "test_storage_account_key",
#' storageEndpointSuffix = "core.windows.net")
#' }
#' @export
generateCredentialsConfig <- function(fileName, authenticationType = "SharedKey", ...) {
args <- list(...)
batchAccount <-
ifelse(is.null(args$batchAccount),
"batch_account_name",
args$batchAccount)
batchKey <-
ifelse(is.null(args$batchKey), "batch_account_key", args$batchKey)
batchUrl <-
ifelse(is.null(args$batchUrl), "batch_account_url", args$batchUrl)
storageName <-
ifelse(is.null(args$storageAccount),
"storage_account_name",
args$storageAccount)
storageKey <-
ifelse(is.null(args$storageKey),
"storage_account_key",
args$storageKey)
storageEndpointSuffix <-
ifelse(is.null(args$storageEndpointSuffix),
"core.windows.net",
args$storageEndpointSuffix)
githubAuthenticationToken <-
ifelse(is.null(args$githubAuthenticationToken),
"",
args$githubAuthenticationToken)
dockerAuthentication <-
ifelse(is.null(args$dockerAuthentication),
"",
args$dockerAuthentication)
dockerUsername <-
ifelse(is.null(args$dockerUsername),
"",
args$dockerUsername)
dockerPassword <-
ifelse(is.null(args$dockerPassword),
"",
args$dockerPassword)
dockerRegistry <-
ifelse(is.null(args$dockerRegistry),
"",
args$dockerRegistry)
appInsightsApplicationId <-
ifelse(is.null(args$appInsightsApplicationId),
"",
args$appInsightsApplicationId)
appInsightInstrumentationKey <-
ifelse(is.null(args$appInsightInstrumentationKey),
"",
args$appInsightInstrumentationKey)
if (!file.exists(paste0(getwd(), "/", fileName))) {
authenticationType <- tolower(authenticationType)
if (authenticationType == "sharedkey") {
config <- list(
sharedKey = list(
batchAccount = list(name = batchAccount,
key = batchKey,
url = batchUrl),
storageAccount = list(name = storageName,
key = storageKey,
endpointSuffix = storageEndpointSuffix)
),
githubAuthenticationToken = githubAuthenticationToken,
dockerAuthentication = list(username = dockerUsername,
password = dockerPassword,
registry = dockerRegistry),
applicationInsights = list(
applicationId = appInsightsApplicationId,
instrumentationKey = appInsightInstrumentationKey
)
)
}
else if (authenticationType == "serviceprincipal") {
config <- list(
servicePrincipal = list(
tenantId = "tenant",
clientId = "client",
credential = "credential",
batchAccountResourceId = "batchAccountResourceId",
storageAccountResourceId = "storageAccountResourceId",
storageEndpointSuffix = storageEndpointSuffix),
githubAuthenticationToken = githubAuthenticationToken,
dockerAuthentication = list(username = dockerUsername,
password = dockerPassword,
registry = dockerRegistry)
)
}
else {
stop(sprintf("Incorrect authentication type: %s. Use 'SharedKey' or 'ServicePrincipal'",
authenticationType))
}
configJson <-
jsonlite::toJSON(config, auto_unbox = TRUE, pretty = TRUE)
write(configJson, file = paste0(getwd(), "/", fileName))
print(
sprintf(
"A config file has been generated %s. Please enter your Batch credentials.",
paste0(getwd(), "/", fileName)
)
)
}
}
#' Set azure credentials to R session from credentials object or json file.
#'
#' @param credentials The credentials object or json file
#' @param verbose Enable verbose messaging on setting credentials
#' @param environment Azure environment type values are Azure, AzureGermany, AzureChina, AzureUSGov-
#'
#' @export
setCredentials <- function(credentials = "az_config.json",
verbose = TRUE,
environment = "Azure") {
if (class(credentials) == "character") {
fileName <- credentials
if (file.exists(fileName)) {
config <- rjson::fromJSON(file = paste0(fileName))
}
else{
config <- rjson::fromJSON(file = paste0(getwd(), "/", fileName))
}
} else if (class(credentials) == "list") {
config <- credentials
} else {
stop(sprintf(
"credentials type is not supported: %s\n",
class(clusterSetting)
))
}
environment <- tolower(environment)
if (environment == "azureusgov") {
aadUrl <- "https://login.microsoftonline.us/"
armUrl <- "https://management.usgovcloudapi.net/"
batchUrl <- "https://batch.core.usgovcloudapi.net/"
}
else if (environment == "azurechina") {
aadUrl <- "https://login.chinacloudapi.cn/"
armUrl <- "https://management.chinacloudapi.cn/"
batchUrl <- "https://batch.chinacloudapi.cn/"
}
else if (environment == "azuregermany"){
aadUrl <- "https://login.microsoftonline.de/"
armUrl <- "https://management.microsoftazure.de/"
batchUrl <- "https://batch.microsoftazure.de/"
}
else {
aadUrl <- "https://login.microsoftonline.com/"
armUrl <- "https://management.azure.com/"
batchUrl <- "https://batch.core.windows.net/"
}
config$azureEnvironment <- list(type = environment,
aadUrl = aadUrl,
armUrl = armUrl,
batchUrl = batchUrl)
batchServiceClient <- makeBatchClient(config)
storageServiceClient <- makeStorageClient(config)
config$batchClient <- batchServiceClient
config$storageClient <- storageServiceClient
cat(strrep('=', options("width")), fill = TRUE)
if (!is.null(config$sharedKey)) {
printSharedKeyInformation(config$sharedKey)
config$endpointSuffix <- config$sharedKey$storageAccount$endpointSuffix
}
else if (!is.null(config$servicePrincipal)) {
cat(sprintf("Batch Account Resource Id: %s",
config$servicePrincipal$batchAccountResourceId), fill = TRUE)
cat(sprintf("Storage Account Resource Id: %s",
config$servicePrincipal$storageAccountResourceId), fill = TRUE)
config$endpointSuffix <- config$servicePrincipal$storageEndpointSuffix
}
else {
printSharedKeyInformation(config)
}
if (is.null(config$endpointSuffix)) {
config$endpointSuffix <- "core.windows.net"
}
options("az_config" = config)
cat(strrep('=', options("width")), fill = TRUE)
if (!is.null(config$batchAccountName) &&
!is.null(config$storageAccount) &&
packageVersion("doAzureParallel") != '0.6.2') {
warning("Old version of credentials file: Generate new credentials file.")
}
cat("Your credentials have been successfully set.", fill = TRUE)
}
makeBatchClient <- function(config) {
batchCredentials <- NULL
# Set up SharedKeyCredentials
if (!is.null(config$sharedKey) ||
!is.null(config$batchAccount) && !is.null(config$storageAccount)) {
credentials <- config
if (!is.null(config$sharedKey)) {
credentials <- config$sharedKey
}
batchCredentials <- rAzureBatch::SharedKeyCredentials$new(
name = credentials$batchAccount$name,
key = credentials$batchAccount$key
)
baseUrl <- credentials$batchAccount$url
}
# Set up ServicePrincipalCredentials
else {
info <-
getAccountInformation(config$servicePrincipal$batchAccountResourceId)
batchCredentials <- rAzureBatch::ServicePrincipalCredentials$new(
tenantId = config$servicePrincipal$tenantId,
clientId = config$servicePrincipal$clientId,
clientSecrets = config$servicePrincipal$credential,
resource = config$azureEnvironment$batchUrl,
aadUrl = config$azureEnvironment$aadUrl
)
servicePrincipal <- rAzureBatch::ServicePrincipalCredentials$new(
tenantId = config$servicePrincipal$tenantId,
clientId = config$servicePrincipal$clientId,
clientSecrets = config$servicePrincipal$credential,
resource = config$azureEnvironment$armUrl,
aadUrl = config$azureEnvironment$aadUrl
)
batchAccountInfo <- rAzureBatch::getBatchAccount(
batchAccount = info$account,
resourceGroup = info$resourceGroup,
subscriptionId = info$subscriptionId,
servicePrincipal = servicePrincipal,
verbose = TRUE
)
baseUrl <- sprintf("https://%s/",
batchAccountInfo$properties$accountEndpoint)
}
rAzureBatch::BatchServiceClient$new(
url = baseUrl,
authentication = batchCredentials
)
}
makeStorageClient <- function(config) {
if (!is.null(config$sharedKey) ||
!is.null(config$storageAccount)) {
credentials <- config
if (!is.null(config$sharedKey)) {
credentials <- config$sharedKey
}
storageCredentials <- rAzureBatch::SharedKeyCredentials$new(
name = credentials$storageAccount$name,
key = credentials$storageAccount$key
)
endpointSuffix <- credentials$storageAccount$endpointSuffix
if (is.null(endpointSuffix)) {
endpointSuffix <- "core.windows.net"
}
baseUrl <- sprintf("https://%s.blob.%s",
credentials$storageAccount$name,
endpointSuffix)
}
# Set up ServicePrincipalCredentials
else {
info <-
getAccountInformation(config$servicePrincipal$storageAccountResourceId)
endpointSuffix <- config$servicePrincipal$storageEndpointSuffix
if (is.null(endpointSuffix)) {
endpointSuffix <- "core.windows.net"
}
servicePrincipal <- rAzureBatch::ServicePrincipalCredentials$new(
tenantId = config$servicePrincipal$tenantId,
clientId = config$servicePrincipal$clientId,
clientSecrets = config$servicePrincipal$credential,
resource = config$azureEnvironment$armUrl,
aadUrl = config$azureEnvironment$aadUrl
)
storageKeys <- rAzureBatch::getStorageKeys(
storageAccount = info$account,
resourceGroup = info$resourceGroup,
subscriptionId = info$subscriptionId,
servicePrincipal = servicePrincipal,
verbose = TRUE
)
storageCredentials <- rAzureBatch::SharedKeyCredentials$new(
name = info$account,
key = storageKeys$keys[[1]]$value
)
baseUrl <- sprintf("https://%s.blob.%s",
info$account,
endpointSuffix)
}
rAzureBatch::StorageServiceClient$new(
authentication = storageCredentials,
url = baseUrl
)
}
getConfiguration <- function(){
config <- options("az_config")
return(config$az_config)
}