Skip to content

Commit 5270bc6

Browse files
authored
Move JWE for user secrets to Kaggle Authorization header instead of r… (#127)
1 parent 095061d commit 5270bc6

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

kaggle/kaggle_bigquery.R

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
KAGGLE_USER_SECRETS_TOKEN <- Sys.getenv("KAGGLE_USER_SECRETS_TOKEN")
1212
KAGGLE_BASE_URL <- Sys.getenv("KAGGLE_URL_BASE")
13-
GET_USER_SECRET_ENDPONT = "/requests/GetUserSecretRequest"
13+
GET_USER_SECRET_ENDPOINT = "/requests/GetUserSecretRequest"
1414

1515
# We create a Token2.0 Credential object (from httr library) and use bigrquery's set_access_cred
1616
# to override the interactive authentication (https://github.com/r-dbi/bigrquery/blob/master/R/auth.R).
@@ -27,8 +27,15 @@ TokenBigQueryKernel <- R6::R6Class("TokenBigQueryKernel", inherit = Token2.0, li
2727
if (KAGGLE_USER_SECRETS_TOKEN == '') {
2828
stop("Expected KAGGLE_USER_SECRETS_TOKEN environment variable to be present.", call. = FALSE)
2929
}
30-
request_body <- list(JWE = KAGGLE_USER_SECRETS_TOKEN, Target = 1)
31-
response <- POST(paste0(KAGGLE_BASE_URL, GET_USER_SECRET_ENDPONT), body = request_body, encode = "json")
30+
request_body <- list(Target = 1)
31+
auth_header <- paste0("Bearer ", KAGGLE_USER_SECRETS_TOKEN)
32+
headers <- add_headers(c("X-Kaggle-Authorization" = auth_header))
33+
response <- POST(paste0(KAGGLE_BASE_URL, GET_USER_SECRET_ENDPOINT),
34+
headers,
35+
# Reset the cookies on each request, since the server expects none.
36+
handle = handle(''),
37+
body = request_body,
38+
encode = "json")
3239
if (http_error(response) || !identical(content(response)$wasSuccessful, TRUE)) {
3340
err <- paste("Unable to refresh token. Please ensure you have a connected BigQuery account. Error: ",
3441
paste(content(response, "text", encoding = 'utf-8')))

kaggle/kaggle_secrets.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@
88
get_user_secret <- function(label) {
99
KAGGLE_USER_SECRETS_TOKEN <- Sys.getenv("KAGGLE_USER_SECRETS_TOKEN")
1010
KAGGLE_BASE_URL <- Sys.getenv("KAGGLE_URL_BASE")
11-
GET_USER_SECRET_BY_LABEL_ENDPONT = "/requests/GetUserSecretByLabelRequest"
11+
GET_USER_SECRET_BY_LABEL_ENDPOINT = "/requests/GetUserSecretByLabelRequest"
1212

1313
if (KAGGLE_USER_SECRETS_TOKEN == '') {
1414
stop("Expected KAGGLE_USER_SECRETS_TOKEN environment variable to be present.", call. = FALSE)
1515
}
16-
request_body <- list(JWE = KAGGLE_USER_SECRETS_TOKEN, Label = label)
17-
response <- POST(paste0(KAGGLE_BASE_URL, GET_USER_SECRET_BY_LABEL_ENDPONT), body = request_body, encode = "json")
16+
request_body <- list(Label = label)
17+
auth_header <- paste0("Bearer ", KAGGLE_USER_SECRETS_TOKEN)
18+
headers <- add_headers(c("X-Kaggle-Authorization" = auth_header))
19+
response <- POST(
20+
paste0(KAGGLE_BASE_URL, GET_USER_SECRET_BY_LABEL_ENDPOINT),
21+
headers,
22+
# Reset the cookies on each request, since the server expects none.
23+
handle = handle(''),
24+
body = request_body,
25+
encode = "json"
26+
)
1827
if (http_error(response) || !identical(content(response)$wasSuccessful, TRUE)) {
1928
err <- paste("Unable to get user secret. Please ensure you have internet enabled. Error: ",
2029
paste(content(response, "text", encoding = 'utf-8')))

tests/test_secrets.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
context("user_secrets")
2+
3+
test_that("get_user_secret exists", {
4+
exists('get_user_secret')
5+
})

0 commit comments

Comments
 (0)