From de5bc2b033156aa1d26f718c41ef2820a08faffd Mon Sep 17 00:00:00 2001 From: DyfanJones Date: Thu, 30 Apr 2020 09:40:15 +0100 Subject: [PATCH] Added region_name check to see if it has been set in the backend or hardcoded #110 --- NEWS.md | 2 ++ R/Connection.R | 4 ++++ R/athena_low_api.R | 22 ++++++++++++++++++++-- man/session_token.Rd | 4 ++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8dc50c00..0ff672d7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -23,6 +23,8 @@ stop("Boto3 is not detected please install boto3 using either: `pip install boto call. = FALSE) ``` +* Added `region_name` check before making a connection to AWS Athena (#110) + ## Bug * `dbWriteTable` would throw `throttling error` every now and again, `retry_api_call` as been built to handle the parsing of data between R and AWS S3. * `dbWriteTable` did not clear down all metadata when uploading to `AWS Athena` diff --git a/R/Connection.R b/R/Connection.R index 8deec91b..fb6cae80 100644 --- a/R/Connection.R +++ b/R/Connection.R @@ -40,6 +40,10 @@ AthenaConnection <- ...), error = function(e) py_error(e)) + # stop connection if region_name is not set in backend or hardcoded + if(is.null(ptr$region_name)) stop("AWS `region_name` is required to be set. Please set `region` in .config file, ", + "`AWS_REGION` in environment variables or `region_name` hard coded in `dbConnect()`.", call. = FALSE) + if(is.null(s3_staging_dir) && !is.null(work_group)){ Athena <- ptr$client("athena") tryCatch(s3_staging_dir <- Athena$get_work_group(WorkGroup = work_group)$WorkGroup$Configuration$ResultConfiguration$OutputLocation, diff --git a/R/athena_low_api.R b/R/athena_low_api.R index 07cc31d3..155b263c 100644 --- a/R/athena_low_api.R +++ b/R/athena_low_api.R @@ -216,6 +216,8 @@ update_work_group <- function(conn, #' @param profile_name The name of a profile to use. If not given, then the default profile is used. #' To set profile name, the \href{https://aws.amazon.com/cli/}{AWS Command Line Interface} (AWS CLI) will need to be configured. #' To configure AWS CLI please refer to: \href{https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html}{Configuring the AWS CLI}. +#' @param region_name Default region when creating new connections. Please refer to \href{https://docs.aws.amazon.com/general/latest/gr/rande.html}{link} for +#' AWS region codes (region code example: Region = EU (Ireland) \code{ region_name = "eu-west-1"}) #' @param serial_number The identification number of the MFA device that is associated with the IAM user who is making the GetSessionToken call. #' Specify this value if the IAM user has a policy that requires MFA authentication. The value is either the serial number for a hardware device #' (such as `GAHT12345678`) or an Amazon Resource Name (ARN) for a virtual device (such as arn:aws:iam::123456789012:mfa/user). @@ -249,11 +251,13 @@ update_work_group <- function(conn, #' @name session_token #' @export get_session_token <- function(profile_name = NULL, + region_name = NULL, serial_number = NULL, token_code = NULL, duration_seconds = 3600L, set_env = FALSE){ stopifnot(is.null(profile_name) || is.character(profile_name), + is.null(region_name) || is.character(region_name), is.character(serial_number), is.null(token_code) || is.character(token_code), is.numeric(duration_seconds), @@ -264,7 +268,14 @@ get_session_token <- function(profile_name = NULL, args$TokenCode <- token_code args$DurationSeconds <- as.integer(duration_seconds) - tryCatch({sts <- boto$Session(profile_name = profile_name)$client("sts") + tryCatch(ptr <- boto$Session(profile_name = profile_name, region_name = region_name), + error = function(e) py_error(e)) + + # stop connection if region_name is not set in backend or hardcoded + if(is.null(ptr$region_name)) stop("AWS `region_name` is required to be set. Please set `region` in .config file, ", + "`AWS_REGION` in environment variables or `region_name` hard coded in function.", call. = FALSE) + + tryCatch({sts <- ptr$client("sts") response <- do.call(sts$get_session_token, args)}, error = function(e) py_error(e)) response$Credentials$Expiration <- py_to_r(response$Credentials$Expiration) @@ -318,7 +329,14 @@ assume_role <- function(profile_name = NULL, is.numeric(duration_seconds), is.logical(set_env)) - tryCatch({sts <- boto$Session(profile_name = profile_name, region_name = region_name)$client("sts") + tryCatch(ptr <- boto$Session(profile_name = profile_name, region_name = region_name), + error = function(e) py_error(e)) + + # stop connection if region_name is not set in backend or hardcoded + if(is.null(ptr$region_name)) stop("AWS `region_name` is required to be set. Please set `region` in .config file, ", + "`AWS_REGION` in environment variables or `region_name` hard coded in function.", call. = FALSE) + + tryCatch({sts <- ptr$client("sts") response <- sts$assume_role(RoleArn = role_arn, RoleSessionName = role_session_name, DurationSeconds = as.integer(duration_seconds))}, diff --git a/man/session_token.Rd b/man/session_token.Rd index cd15bcd2..5427e7cc 100644 --- a/man/session_token.Rd +++ b/man/session_token.Rd @@ -7,6 +7,7 @@ \usage{ get_session_token( profile_name = NULL, + region_name = NULL, serial_number = NULL, token_code = NULL, duration_seconds = 3600L, @@ -18,6 +19,9 @@ get_session_token( To set profile name, the \href{https://aws.amazon.com/cli/}{AWS Command Line Interface} (AWS CLI) will need to be configured. To configure AWS CLI please refer to: \href{https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html}{Configuring the AWS CLI}.} +\item{region_name}{Default region when creating new connections. Please refer to \href{https://docs.aws.amazon.com/general/latest/gr/rande.html}{link} for +AWS region codes (region code example: Region = EU (Ireland) \code{ region_name = "eu-west-1"})} + \item{serial_number}{The identification number of the MFA device that is associated with the IAM user who is making the GetSessionToken call. Specify this value if the IAM user has a policy that requires MFA authentication. The value is either the serial number for a hardware device (such as `GAHT12345678`) or an Amazon Resource Name (ARN) for a virtual device (such as arn:aws:iam::123456789012:mfa/user).}