-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
service_creation.R
320 lines (273 loc) · 10.1 KB
/
service_creation.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
#' Provision a service account
#'
#' This uses all the \link{gar_service_create} functions to enable creating service account roles more easily
#'
#' @inheritParams gar_service_create
#' @inheritParams gar_set_client
#' @inheritParams gar_auth
#' @details
#'
#' You will need the OAuth2.0 Client ID JSON from your GCP project via
#' \code{menu icon > APIs & Services > Credentials > Create Credentials > OAuth client ID}
#'
#' You need to authenticate with a user with permission \code{iam.serviceAccounts.create} for the project. Most often the user is an Owner/Editor
#'
#' @seealso \url{https://cloud.google.com/iam/docs/creating-managing-service-accounts#iam-service-accounts-create-rest}
#'
#' @export
#' @family IAM functions
#' @examples
#' \dontrun{
#'
#' gar_service_provision("my-service-account",
#' c("roles/viewer", "roles/bigquery.jobUser"))
#' }
gar_service_provision <- function(accountId,
roles,
json = Sys.getenv("GAR_CLIENT_JSON"),
file = paste0(accountId,"-auth-key.json"),
email = Sys.getenv("GARGLE_EMAIL")){
projectId <- gar_set_client(json,
scopes = "https://www.googleapis.com/auth/cloud-platform")
if(email == ""){
email <- NULL
}
gar_auth(email = email, cache = FALSE)
created <- gar_service_create(accountId, projectId = projectId)
gar_service_grant_roles(created$email,
roles = roles,
projectId = projectId)
gar_service_key(accountId, projectId = projectId, file = file)
}
#' Work with service accounts via the API
#'
#' These functions let you create a service JSON key from an OAuth2 login. You can then assign it roles and do a one time download of a service account key to use for authentication in other Google APIs
#'
#' @param projectId The projectId containing the service account
#' @param accountId The service accountId
#' @param serviceName Name of service account
#' @param serviceDescription Description of service account
#'
#' @return If it already exists, returns it via \link{gar_service_get}, else creates the service key
#'
#' @seealso Combine these functions to provision emails in one step with \link{gar_service_provision}
#'
#' @export
#' @family IAM functions
gar_service_create <- function(
accountId,
projectId,
serviceName = "googleAuthR::gar_service_create",
serviceDescription = "A service account created via googleAuthR"
){
candidate <- sprintf("%s@%s.iam.gserviceaccount.com",
accountId, projectId)
tryCatch(
# does the service email exist?
gar_service_get(candidate, projectId = projectId),
error = function(err){
# if not, lets try and make it
tryCatch(
{
myMessage("Creating new service account: ", candidate, level = 3)
the_url <- sprintf(
"https://iam.googleapis.com/v1/projects/%s/serviceAccounts",
projectId
)
body <- list(
accountId = accountId,
serviceAccount = list(
description = serviceDescription,
displayName = serviceName
)
)
myMessage("Creating service accountId - ", accountId, level = 3)
api_call <- gar_api_generator(the_url, "POST",
data_parse_function = function(x) x)
api_call(the_body = body)
},
# maybe it was a real error
error = function(err2){
stop(paste(err2$message, err$message), call. = FALSE)
}
)
}
)
}
#' Grant IAM roles to accountIds
#'
#' @param accountIds A vector of accountIds in the form \code{[email protected]}
#' @param roles A character vector of roles to give the accountIds e.g. \code{roles/editor} - see list of roles here \url{https://cloud.google.com/iam/docs/understanding-roles#predefined_roles} or in your GCP console \code{https://console.cloud.google.com/iam-admin/roles/details/roles}
#' @param type The type of accountId to add role for - e.g. \code{user:[email protected]} or \code{serviceAccount:[email protected]}
#'
#' @details
#'
#' It will download the existing roles, and append the role you add to it here.
#'
#' @export
#' @rdname gar_service_create
#' @seealso \url{https://cloud.google.com/resource-manager/reference/rest/v1/projects/setIamPolicy}
gar_service_grant_roles <- function(accountIds,
roles,
projectId,
type = c("serviceAccount", "user", "group")){
type <- match.arg(type)
existing <- extract_existing(gar_service_get_roles(projectId))
the_url <- sprintf(
"https://cloudresourcemanager.googleapis.com/v1/projects/%s:setIamPolicy",
projectId
)
the_roles <- lapply(roles, function(x){
list(role = x, members = list(paste0(type,":",accountIds)))
})
body <- list(
policy = list(
bindings = unname(c(existing, the_roles))
)
)
myMessage("Granting roles: ", paste(roles, collapse = " "),
"to accountIds: ", paste(accountIds, collapse = " "),
level = 3)
api_call <- gar_api_generator(the_url, "POST",
data_parse_function = function(x) x$bindings)
api_call(the_body = body)
}
extract_existing <- function(bs){
lapply(bs$role, function(x){
list(role = x,
members = list(bs$members[[which(bs$role == x)]]))
})
}
#' Get current IAM roles
#'
#' @details
#'
#' If you supply an accountId to \code{gar_service_get_roles} then it will return only those roles that accountId has.
#'
#' @export
#' @rdname gar_service_create
#' @seealso \url{https://cloud.google.com/resource-manager/reference/rest/v1/projects/setIamPolicy}
#' @examples
#'
#' \dontrun{
#'
#' # all roles
#' projectId <- gar_set_client(
#' json = Sys.getenv("GAR_CLIENT_JSON"),
#' scopes = "https://www.googleapis.com/auth/cloud-platform")
#' gar_service_get_roles(projectId)
#'
#' # roles for one accountId
#' gar_service_get_roles(
#' projectId,
#' accountId = "[email protected]")
#'
#' }
gar_service_get_roles <- function(
projectId,
accountId = NULL,
type = c("serviceAccount", "user", "group")){
type <- match.arg(type)
the_url <- sprintf(
"https://cloudresourcemanager.googleapis.com/v1/projects/%s:getIamPolicy",
projectId
)
myMessage("Checking existing roles", level = 3)
api_call <- gar_api_generator(the_url, "POST",
data_parse_function = function(x) x$bindings)
existing_roles <- api_call()
if(!is.null(accountId)){
# return roles only for this accountId
check_email <- paste0(type, ":", accountId)
present <- unlist(lapply(existing_roles$members,
function(x) check_email %in% x))
present_roles <- existing_roles[present, "role"]
if(length(present_roles) < 1){
stop("Could not find any roles for ", check_email, call. = FALSE)
}
existing_roles <- data.frame(roles = present_roles,
members = check_email,
stringsAsFactors = FALSE)
}
existing_roles
}
#' Create a service account key
#'
#'
#' @seealso https://cloud.google.com/iam/docs/reference/rest/v1/projects.serviceAccounts.keys/create
#'
#' @param file The file to download the private JSON key to
#'
#' @export
#' @examples
#' \dontrun{
#' library(googleAuthR)
#' gar_set_client(scopes = "https://www.googleapis.com/auth/cloud-platform")
#' gar_auth()
#' gar_service_create("test12345678", "my-project")
#'
#' gar_service_get("[email protected]",
#' projectId = "my-project")
#'
#' gar_service_grant_roles("[email protected]",
#' role = "roles/editor",
#' projectId = "my-project")
#'
#' gar_service_key("test12345678", "my-project", "my-auth.json")
#'
#' gar_service_list("my-project")
#'
#' gar_service_key_list("test12345678", "my-project")
#' }
#' @rdname gar_service_create
gar_service_key <- function(accountId,
projectId,
file = paste0(accountId,"-auth-key.json")){
the_url <- sprintf(
"https://iam.googleapis.com/v1/projects/%s/serviceAccounts/%s@%s.iam.gserviceaccount.com/keys",
projectId, accountId, projectId)
myMessage("Creating secret auth key for service account", accountId,
" for project ", projectId, level = 3)
api_call <- gar_api_generator(
the_url, "POST", data_parse_function = decode_key)
o <- api_call()
myMessage("Writing secret auth JSON key to ", file,
" and adding to .gitignore", level = 3)
add_line(file, ".gitignore")
write(o, file = file)
}
decode_key <- function(x){
rawToChar(jsonlite::base64_dec(x$privateKeyData))
}
#' @rdname gar_service_create
#' @export
gar_service_key_list <- function(accountId,
projectId){
the_url <- sprintf(
"https://iam.googleapis.com/v1/projects/%s/serviceAccounts/%s@%s.iam.gserviceaccount.com/keys", projectId, accountId, projectId
)
api_call <- gar_api_generator(the_url, "GET",
data_parse_function = function(x) x$keys)
api_call()
}
#' @rdname gar_service_create
#' @export
gar_service_list <- function(projectId){
the_url <- sprintf(
"https://iam.googleapis.com/v1/projects/%s/serviceAccounts", projectId
)
api_call <- gar_api_generator(the_url, "GET",
data_parse_function = function(x) x$accounts)
api_call()
}
#' @rdname gar_service_create
#' @export
gar_service_get <- function(accountId, projectId){
the_url <- sprintf(
"https://iam.googleapis.com/v1/projects/%s/serviceAccounts/%s",
projectId, accountId
)
api_call <- gar_api_generator(the_url, "GET",
data_parse_function = function(x) x)
api_call()
}