Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ Title: Fetch credentials on the fly as you Connect to Databases
Version: 0.0.6
Authors@R: c(person("David", "Robinson", email = "drob@datacamp.com", role = c("aut", "cre")),
person("Filip", "Schouwenaars", email = "filip@datacamp", role = c("aut")),
person("Dieter", "De Mesmaeker", role = c("aut")))
person("Dieter", "De Mesmaeker", role = c("aut")),
person("Pieter", "Moors", email = "pieter.moors@datacamp.com", role = c("ctb")))
Maintainer: David Robinson <drob@datacamp.com>
URL: http://www.datacamp.com
BugReports: https://github.com/datacamp/dbconnectr/issues
Description: Fetch credentials from AWS automatically when you connect to one of DataCamp's databases.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: TRUE
Depends:
R (>= 3.0.0),
Expand All @@ -21,9 +23,11 @@ Imports:
stringr,
RMariaDB,
RPostgres,
odbc
odbc,
bigrquery,
httr
Suggests:
testthat,
knitr
RoxygenNote: 6.1.1
RoxygenNote: 7.2.0
VignetteBuilder: knitr
2 changes: 1 addition & 1 deletion R/creds.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fetch_creds <- function(dbname = "main-app", ...) {
field_values <- as.list(creds$Value[match(names, creds$Name)])
names(field_values) <- names(fields)

if (Sys.getenv("DOCKER_DASHBOARDS") == 1){
if (Sys.getenv("DOCKER_DASHBOARDS") == "1"){
print("Running Dashboards, so using global creds")
field_values

Expand Down
60 changes: 46 additions & 14 deletions R/dbconnectr.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,54 @@ create_connection <- function(dbname = "main-app", cache = FALSE, cache_folder =
profile = NULL,
region = NULL,
...) {
creds <- get_creds(dbname, cache, cache_folder, profile = profile, region = region)
creds$host <- 'datalake-redshift-public-prod.us-east-1.internal.datacamp.com'
if(dbname == "bigquery-prod"){
if (Sys.getenv("SHINY_SERVER") == "1" && Sys.getenv("AIRFLOW") == "1"){
auth <- bigrquery::bq_auth(path = "../sa_gcp_key.json")
} else if (Sys.getenv("DOCKER_DASHBOARDS") == "1") {
resp <- get_parameter(Sys.getenv("GCP_AUTH"))
auth <- bigrquery::bq_auth(path = resp)
} else {
google_app <- httr::oauth_app(
"Datacampr",
key = Sys.getenv("BQ_KEY"),
secret = Sys.getenv("BQ_SECRET")
)
bigrquery::bq_auth_configure(app = google_app)
}
tryCatch(
do.call(DBI::dbConnect, list(drv = bigrquery::bigquery(),
project = "datacamp-data-platform",
page_size = 2500)),
error = function(e){
error_token_expired <- "IAM Authentication token has expired"
error_onload_failed <- ".onLoad failed in loadNamespace()"
if (grepl(error_token_expired, e) || grepl(error_onload_failed, e)){
message("Deleting expired credentials from cache")
file.remove(get_cache_file(cache_folder, dbname))
message("Retrying connection to database ", dbname)
create_connection(dbname, cache, cache_folder, profile, region, ...)
}
}
)
}
else{
creds <- get_creds(dbname, cache, cache_folder, profile = profile, region = region)
creds$host <- 'datalake-redshift-public-prod.us-east-1.internal.datacamp.com'

tryCatch(
do.call(DBI::dbConnect, c(transform_creds(creds), list(...))),
error = function(e){
error_token_expired <- "IAM Authentication token has expired"
error_onload_failed <- ".onLoad failed in loadNamespace()"
if (grepl(error_token_expired, e) || grepl(error_onload_failed, e)){
message("Deleting expired credentials from cache")
file.remove(get_cache_file(cache_folder, dbname))
message("Retrying connection to database ", dbname)
create_connection(dbname, cache, cache_folder, profile, region, ...)
tryCatch(
do.call(DBI::dbConnect, c(transform_creds(creds), list(...))),
error = function(e){
error_token_expired <- "IAM Authentication token has expired"
error_onload_failed <- ".onLoad failed in loadNamespace()"
if (grepl(error_token_expired, e) || grepl(error_onload_failed, e)){
message("Deleting expired credentials from cache")
file.remove(get_cache_file(cache_folder, dbname))
message("Retrying connection to database ", dbname)
create_connection(dbname, cache, cache_folder, profile, region, ...)
}
}
}
)
)
}
}

#' @rdname create_connection
Expand Down
20 changes: 16 additions & 4 deletions man/create_connection.Rd

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

8 changes: 6 additions & 2 deletions man/get_creds.Rd

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