Skip to content

Commit

Permalink
user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
DyfanJones committed Sep 13, 2024
1 parent f903a93 commit 58e3052
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
13 changes: 8 additions & 5 deletions paws.common/R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,8 @@ get_instance_metadata <- function(query_path = "") {
"PUT",
metadata_token_url,
timeout = 1,
header = c("X-aws-ec2-metadata-token-ttl-seconds" = token_ttl)
header = c("X-aws-ec2-metadata-token-ttl-seconds" = token_ttl, "User-Agent", user_agent_handler())
)
metadata_token_request <- sdk_version_user_agent_handler(metadata_token_request)
metadata_token_response <- tryCatch(
{
issue(metadata_token_request)
Expand All @@ -226,12 +225,16 @@ get_instance_metadata <- function(query_path = "") {
"GET",
metadata_url,
timeout = 1,
header = c("X-aws-ec2-metadata-token" = token)
header = c("X-aws-ec2-metadata-token" = token, "User-Agent" = user_agent_handler())
)
} else {
metadata_request <- new_http_request("GET", metadata_url, timeout = 1)
metadata_request <- new_http_request(
"GET",
metadata_url,
timeout = 1,
header = "User-Agent" = user_agent_handler()
)
}
metadata_request <- sdk_version_user_agent_handler(metadata_request)
metadata_response <- tryCatch(
{
issue(metadata_request)
Expand Down
13 changes: 6 additions & 7 deletions paws.common/R/credential_providers.R
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ get_container_credentials <- function(credentials_uri, credentials_full_uri) {
kwargs <- list(method = "GET", url = metadata_url, timeout = 1)
kwargs[["header"]] <- headers
metadata_request <- do.call(new_http_request, kwargs)
metadata_request <- sdk_version_user_agent_handler(metadata_request)
metadata_response <- tryCatch(
{
issue(metadata_request)
Expand Down Expand Up @@ -452,20 +451,20 @@ get_container_credentials <- function(credentials_uri, credentials_full_uri) {
# Developed from:
# https://github.com/boto/botocore/blob/ba7da3497853ac83e9b8552f41de83cb27932fe9/botocore/credentials.py#L1945-L1955C22
set_container_credentails_headers <- function() {
auth_token <- NULL
auth_token <- list()
auth_token[["User-Agent"]] <- user_agent()
ENV_VAR_AUTH_TOKEN <- get_env("AWS_CONTAINER_AUTHORIZATION_TOKEN")
ENV_VAR_AUTH_TOKEN_FILE <- get_env("AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE")
if (ENV_VAR_AUTH_TOKEN_FILE != "") {
auth_token <- readLines("token_file", encoding = "utf-8", warn = FALSE)
auth_token[["Authorization"]] <- readLines(ENV_VAR_AUTH_TOKEN_FILE, encoding = "utf-8", warn = FALSE)
} else if (ENV_VAR_AUTH_TOKEN != "") {
auth_token <- ENV_VAR_AUTH_TOKEN
auth_token[["Authorization"]] <- ENV_VAR_AUTH_TOKEN
}
# validate auth token
if (!is.null(auth_token)) {
if (grepl("\r|\n", auth_token)) {
if (!is.null(auth_token[["Authorization"]])) {
if (grepl("\r|\n", auth_token[["Authorization"]])) {
stop("Auth token value is not a legal header value")
}
names(auth_token) <- "Authorization"
}
return(auth_token)
}
Expand Down
9 changes: 7 additions & 2 deletions paws.common/R/handlers_core.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
#' @include util.R

# Add the name/version to the User-Agent request header.
sdk_version_user_agent_handler <- function(request) {

user_agent_handler <- function() {
paws_version <- .__NAMESPACE__.[["spec"]]["version"] %||% packageVersion("paws.common")
user_agent <- sprintf(
"paws/%s (R%s; %s; %s)",
paws_version, getRversion(), R.version$os, R.version$arch
)
request$http_request$header["User-Agent"] <- user_agent
return(user_agent)
}

sdk_version_user_agent_handler <- function(request) {
request$http_request$header["User-Agent"] <- user_agent_handler()
return(request)
}

Expand Down

0 comments on commit 58e3052

Please sign in to comment.