-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimpersonation.R
190 lines (162 loc) · 7.1 KB
/
impersonation.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
wif_params_list <- list(
azure_resource = "https://googlewif.example.com/.default",
azure_tenant = "911aacex-ampl-exam-pleex-ampleexample",
azure_app = "f8d0dexa-mple-exam-plee-xampleexampl",
gcp_project_number = '123456789012',
gcp_project_wifPoolIdName = 'wip-azure',
gcp_project_wifPoviderName = 'azure',
wif_email = 'wif-azure@example.iam.gserviceaccount.com',
sts_grantType = "urn:ietf:params:oauth:grant-type:token-exchange",
sts_scope = "https://www.googleapis.com/auth/cloud-platform",
sts_subjectTokenType = "urn:ietf:params:oauth:token-type:jwt",
sts_requestedTokenType = "urn:ietf:params:oauth:token-type:access_token",
url = 'https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/'
)
get_azure_wif_token <- function(wif_params = wif_params_list, wif_lifetime = '3600s') {
#############################
##### BEGIN constructor ####
#############################
WifToken <- R6::R6Class("WifToken", inherit = httr::Token2.0, list(
#' @description Get a token via workload identity federation
#' @param params A list of parameters for `init_oauth_external_account()`.
#' @return A WifToken.
initialize = function(params = list()) {
message("WifToken initialize")
# TODO: any desired validity checks on contents of params
# NOTE: the final token exchange with
# https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/generateAccessToken
# takes scopes as an **array**, not a space delimited string
# so we do NOT collapse scopes in this flow
params$scope <- params$scopes
self$params <- params
self$init_credentials()
},
#' @description Enact the actual token exchange for workload identity
#' federation.
init_credentials = function() {
message("WifToken init_credentials")
creds <- init_oauth_external_account()
# for some reason, the serviceAccounts.generateAccessToken method of
# Google's Service Account Credentials API returns in camelCase, not
# snake_case
# as in, we get this:
# "accessToken":"ya29.c.KsY..."
# "expireTime":"2021-06-01T18:01:06Z"
# instead of this:
# "access_token": "ya29.a0A..."
# "expires_in": 3599
snake_case <- function(x) {
gsub("([a-z0-9])([A-Z])", "\\1_\\L\\2", x, perl = TRUE)
}
names(creds) <- snake_case(names(creds))
self$credentials <- creds
self
},
#' @description Refreshes the token, which means re-doing the entire token
#' flow in this case.
refresh = function() {
message("WifToken refresh")
# There's something kind of wrong about this, because it's not a true
# refresh. But this method is basically required by the way httr currently
# works.
# This means that some uses of $refresh() aren't really appropriate for a
# WifToken.
# For example, if I attempt token_userinfo(x) on a WifToken that lacks
# appropriate scope, it fails with 401.
# httr tries to "fix" things by refreshing the token. But this is
# not a problem that refreshing can fix.
# I've now prevented that particular phenomenon in token_userinfo().
self$init_credentials()
},
#' @description Format a [WifToken()].
#' @param ... Not used.
format = function(...) {
x <- list(
scopes = wif_params$sts_scope,
credentials = wif_params$wif_email
)
c(
cli::cli_format_method(
cli::cli_h1("<WifToken (via {.pkg gargle})>")
),
glue::glue("{fr(names(x))}: {fl(x)}")
)
},
#' @description Print a [WifToken()].
#' @param ... Not used.
print = function(...) {
# a format method is not sufficient for WifToken because the parent class
# has a print method
cli::cat_line(self$format())
},
#' @description Placeholder implementation of required method. Returns `TRUE`.
can_refresh = function() {
# TODO: see above re: my ambivalence about the whole notion of refresh with
# respect to this flow
TRUE
},
# TODO: are cache and load_from_cache really required?
# alternatively, what if calling them threw an error?
#' @description Placeholder implementation of required method. Returns self.
cache = function() self,
#' @description Placeholder implementation of required method. Returns self.
load_from_cache = function() self,
# TODO: are these really required?
#' @description Placeholder implementation of required method.
validate = function() {},
#' @description Placeholder implementation of required method.
revoke = function() {}
))
#############################
##### END of constructor ####
#############################
library(AzureAuth)
library(tidyverse)
library(httr2)
library(gargle)
fr <- function(x) format(x, justify = "right")
fl <- function(x) format(x, justify = "left")
init_oauth_external_account <- function() {
# first get the Azure token
subject_token <- get_azure_token(resource = wif_params$azure_resource,
tenant = wif_params$azure_tenant,
app = wif_params$azure_app,
auth_type = "authorization_code",
version = 2 # for work accounts / version = 1 for private
)
# next, get the STS token
audience = paste0("//iam.googleapis.com/projects/",
wif_params$gcp_project_number,
"/locations/global/workloadIdentityPools/",
wif_params$gcp_project_wifPoolIdName,
"/providers/",
wif_params$gcp_project_wifPoviderName)
google_sts_request <- request("https://sts.googleapis.com/v1/token") %>%
req_body_form(audience = audience) %>%
req_body_form(grantType = wif_params$sts_grantType) %>%
req_body_form(subjectToken = subject_token$credentials$access_token) %>%
req_body_form(scope = wif_params$sts_scope) %>%
req_body_form(subjectTokenType = wif_params$sts_subjectTokenType) %>%
req_body_form(requestedTokenType = wif_params$sts_requestedTokenType)
google_sts_response <- req_perform(google_sts_request)
# Now you extract the Google Security Token Service token
federated_access_token <- resp_body_json(google_sts_response)
# finally get the WIF token
req <- list(
method = "POST",
url = str_c('https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/',
wif_params$wif_email,
':generateAccessToken'),
body = list(scope = wif_params$sts_scope,
lifetime = wif_lifetime),
token = httr::add_headers(
Authorization = paste("Bearer", federated_access_token$access_token)
)
)
resp <- request_make(req)
response_process(resp)
}
params <- list(scopes = wif_params$sts_scope,
as_header = TRUE)
WifToken$new(params = params)
}