Skip to content

Commit

Permalink
Added region_name check to see if it has been set in the backend or h…
Browse files Browse the repository at this point in the history
…ardcoded #110
  • Loading branch information
DyfanJones committed Apr 30, 2020
1 parent e1d2ae7 commit de5bc2b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 4 additions & 0 deletions R/Connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 20 additions & 2 deletions R/athena_low_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand Down Expand Up @@ -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))},
Expand Down
4 changes: 4 additions & 0 deletions man/session_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit de5bc2b

Please sign in to comment.