-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from PeakBI/master
Add support for assuming roles from web identities.
- Loading branch information
Showing
6 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
Package: aws.signature | ||
Type: Package | ||
Title: Amazon Web Services Request Signatures | ||
Version: 0.6.0 | ||
Version: 0.6.1 | ||
Date: 2020-06-01 | ||
Authors@R: c(person("Thomas J.", "Leeper", | ||
role = c("aut"), | ||
email = "[email protected]", | ||
comment = c(ORCID = "0000-0003-4097-6326")), | ||
person("Jonathan", "Stott", email = "[email protected]", role = c("cre", "aut")), | ||
person("Mike", "Kaminsky", email = "[email protected]", role = "ctb") | ||
person("Mike", "Kaminsky", email = "[email protected]", role = "ctb"), | ||
person("Mark", "Douthwaite", email = "[email protected]", role = "ctb"), | ||
person("Jason", "Gofford", email = "[email protected]", role = "ctb"), | ||
person("Luke", "Dyer", email = "[email protected]", role = "ctb") | ||
) | ||
Description: Generates version 2 and version 4 request signatures for Amazon Web Services ('AWS') <https://aws.amazon.com/> Application Programming Interfaces ('APIs') and provides a mechanism for retrieving credentials from environment variables, 'AWS' credentials files, and 'EC2' instance metadata. For use on 'EC2' instances, users will need to install the suggested package 'aws.ec2metadata' <https://cran.r-project.org/package=aws.ec2metadata>. | ||
License: GPL (>= 2) | ||
Imports: | ||
digest, | ||
base64enc | ||
base64enc, | ||
jsonlite, | ||
curl | ||
Suggests: | ||
devtools, | ||
testthat (>= 2.1.0), | ||
aws.ec2metadata (>= 0.1.6) | ||
URL: https://github.com/cloudyr/aws.signature | ||
BugReports: https://github.com/cloudyr/aws.signature/issues | ||
RoxygenNote: 7.1.0 | ||
RoxygenNote: 7.1.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#' @rdname assume_role_with_web_identity | ||
#' @title Assume Role with AWS Web Identity | ||
#' @description Assume a role from a provided Web Identity Token and ARN using AWS Secure Token Service (STS). | ||
#' @param role_arn A character string containing the AWS Role Amazon Resource Name (ARN). This specifies the permissions you have to access other AWS services. | ||
#' @param token_file A character string containing a path to a Web Identity Token file. | ||
#' @param base_url The AWS STS endpoint to use to retrieve your credentials from. | ||
#' @param session_name A character string optionally specifying the name. | ||
#' @param duration The expiry time on the retrieved credentials. | ||
#' @param version The AWS STS specification version to use. | ||
#' @param verbose A logical indicating whether to be verbose. | ||
#' @export | ||
assume_role_with_web_identity <- function( | ||
role_arn, | ||
token_file, | ||
base_url=Sys.getenv("AWS_STS_ENDPOINT", "https://sts.amazonaws.com"), | ||
session_name=NULL, | ||
duration=3600, | ||
version="2011-06-15", | ||
verbose = getOption("verbose", FALSE) | ||
){ | ||
if (is.null(session_name)) { | ||
# strip resource ID from arn and use as default session name | ||
# https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html | ||
session_name <- gsub("/", "-", utils::tail(strsplit(role_arn, ":")[[1]], 1)) | ||
} | ||
|
||
token <- readChar(token_file, file.info(token_file)$size) | ||
|
||
query_params <- list( | ||
Action="AssumeRoleWithWebIdentity", | ||
DurationSeconds=duration, | ||
RoleArn=role_arn, | ||
RoleSessionName=session_name, | ||
WebIdentityToken=token, | ||
Version=version | ||
) | ||
query_params_names <- curl::curl_escape(names(query_params)) | ||
query_params_values <- lapply(query_params, curl::curl_escape) | ||
query_str <- paste0(query_params_names, "=", query_params_values, collapse = "&") | ||
query_url <- paste0(base_url, "/?", query_str) | ||
|
||
handle <- curl::new_handle() # need to accept json headers | ||
curl::handle_setheaders(handle, "accept" = "application/json") | ||
|
||
response <- curl::curl_fetch_memory(query_url, handle = handle) | ||
content <- jsonlite::fromJSON(rawToChar(response$content)) | ||
|
||
if (response$status_code == 200) { | ||
if (isTRUE(verbose)) { | ||
message("Successfully fetched token from web identiy provider.") | ||
} | ||
return(content) | ||
} else { | ||
stop("Failed to assume role.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.