cloudyr / aws.signature

@@ -1,3 +1,9 @@
Loading
1 1
is_blank <- function(value) {
2 2
  return(is.null(value) || value == "")
3 +
}
4 +
5 +
vmsg <- function(verbose, msg, ...) {
6 +
  if (isTRUE(verbose)) {
7 +
    message(sprintf(msg, ...))
8 +
  }
3 9
}

@@ -227,18 +227,14 @@
Loading
227 227
) {
228 228
  # if we have a valid argument, just return it
229 229
  if (!is_blank(region)||(identical(region,"")&&getOption("cloudyr.aws.allow_empty_region", FALSE))) {
230 -
    if (isTRUE(verbose)) {
231 -
      message(sprintf("Using user-supplied value for AWS Region ('%s')", region))
232 -
    }
230 +
    vmsg(verbose, "Using user-supplied value for AWS Region ('%s')", region)
233 231
    return(region)
234 232
  }
235 233
  
236 234
  # look in the environment
237 235
  region <- Sys.getenv("AWS_DEFAULT_REGION")
238 236
  if (!is_blank(region)) {
239 -
    if (isTRUE(verbose)) {
240 -
      message(sprintf("Using Environment Variable 'AWS_DEFAULT_REGION' for AWS Region ('%s')", region))
241 -
    }
237 +
    vmsg(verbose, "Using Environment Variable 'AWS_DEFAULT_REGION' for AWS Region ('%s')", region)
242 238
    return(region)
243 239
  }
244 240
  
@@ -248,9 +244,7 @@
Loading
248 244
    reg <- try(aws.ec2metadata::instance_document()$region, silent = TRUE)
249 245
    if (!inherits(reg, "try-error")&&!is_blank(reg)) {
250 246
      region <- reg
251 -
      if (isTRUE(verbose)) {
252 -
        message(sprintf("Using EC2 Instance Metadata for AWS Region ('%s')", region))
253 -
      }
247 +
      vmsg(verbose, "Using EC2 Instance Metadata for AWS Region ('%s')", region)
254 248
      return(region)
255 249
    }
256 250
    
@@ -258,9 +252,8 @@
Loading
258 252
259 253
  # otherwise, use the default region
260 254
  region <- default_region
261 -
  if (isTRUE(verbose)) {
262 -
    message(sprintf("Using default value for AWS Region ('%s')", region))
263 -
  }
255 +
  vmsg(verbose, "Using default value for AWS Region ('%s')", region)
256 +
  
264 257
  return(region)
265 258
}
266 259
@@ -295,7 +288,7 @@
Loading
295 288
    if (isTRUE(verbose)) {
296 289
        message("Checking for credentials from user-defined profile")
297 290
    }
298 -
    profile_creds <- get_user_supplied_profile(profile, file, region, session_token, default_region, verbose)
291 +
    profile_creds <- check_credentials_file(profile, file, region, default_region, verbose, require_profile = TRUE)
299 292
    if (!is.null(profile_creds)) {
300 293
        return(profile_creds)
301 294
    } 
@@ -348,7 +341,7 @@
Loading
348 341
        # early return
349 342
        return(list(key = key, secret = secret, session_token = session_token, region = region))
350 343
    } else if (!is_blank(env$profile)) {
351 -
        return(get_user_supplied_profile(env$profile, file, region, session_token, default_region, verbose))
344 +
        return(check_credentials_file(env$profile, file, region, default_region, verbose))
352 345
    }
353 346
    return(NULL)
354 347
}
@@ -410,79 +403,51 @@
Loading
410 403
    return(NULL)
411 404
}
412 405
413 -
check_credentials_file <- function(profile, file, region, default_region, verbose) {
414 -
    if (isTRUE(verbose)) {
415 -
        message("Searching for credentials file(s)")
416 -
    }
417 -
    if (file.exists(file.path(".aws", "credentials"))) {
418 -
        ## in working directory
419 -
        cred <- read_credentials(file.path(".aws", "credentials"))
420 -
        if (!is.null(profile) && profile %in% names(cred)) {
421 -
            cred <- cred[[profile]]
422 -
        } else {
423 -
            cred <- cred[["default"]]
424 -
            if (isTRUE(verbose)) {
425 -
                warning(sprintf("Requested profile '%s' not found in file. Using 'default' profile.", profile))
426 -
            }
427 -
        }
428 -
        if (isTRUE(verbose)) {
429 -
            message(sprintf("Using profile '%s' from local credentials files from '%s'", profile, file.path(".aws", "credentials")))
430 -
        }
431 -
        
432 -
        # early return
433 -
        return(credentials_to_list(cred, region = region, default_region = default_region, verbose = verbose))
434 -
    } else if (file.exists(file) || file.exists(default_credentials_file())) {
435 -
        ## in specified location
436 -
        if (file.exists(file)) {
437 -
            cred <- read_credentials(file = file)
438 -
        } else {
439 -
            ## otherwise, default to default location
440 -
            cred <- read_credentials(file = default_credentials_file())
441 -
        }
442 -
        if (!is.null(profile) && profile %in% names(cred)) {
443 -
            cred <- cred[[profile]]
444 -
        } else {
445 -
            cred <- cred[["default"]]
446 -
            if (isTRUE(verbose)) {
447 -
                warning(sprintf("Requested profile '%s' not found in file. Using 'default' profile.", profile))
448 -
            }
449 -
        }
450 -
        if (isTRUE(verbose)) {
451 -
            message(sprintf("Using profile '%s' from global credentials files from '%s'", profile, default_credentials_file()))
452 -
        }
453 -
        
454 -
        # early return
455 -
        return(credentials_to_list(cred, region = region, default_region = default_region, verbose = verbose))
406 +
check_credentials_file <- function(profile, file, region, default_region, verbose, require_profile=FALSE) {
407 +
    vmsg(verbose, "Searching for credentials file(s)")
408 +
409 +
    files_to_check <- c(
410 +
      file.path(".aws", "credentials"),
411 +
      file,
412 +
      default_credentials_file()
413 +
    )
414 +
    files_to_check <- unique(files_to_check)
415 +
    
416 +
    for (f in files_to_check) {
417 +
      vmsg(verbose, sprintf("Attempting to read credentials from '%s'", f))
418 +
      
419 +
      creds <- read_profile_from_credentials_file(profile, f, region, default_region, verbose, require_profile)
420 +
      if (!is.null(creds)) {
421 +
        break
422 +
      }
456 423
    }
457 -
    return(NULL)
424 +
    
425 +
    return(creds)
458 426
}
459 427
460 -
get_user_supplied_profile <- function(profile, file, region, session_token, default_region, verbose) {
461 -
    if (file.exists(file.path(".aws", "credentials"))) {
462 -
         cred <- read_credentials(file.path(".aws", "credentials"))
463 -
         if (profile %in% names(cred)) {
464 -
             cred <- cred[[profile]]
465 -
             if (isTRUE(verbose)) {
466 -
                 message(sprintf("Using profile '%s' from local credentials files from '%s'", profile, file.path(".aws", "credentials")))
467 -
             }
468 -
             # early return
469 -
             return(credentials_to_list(cred, region = region, default_region = default_region, verbose = verbose))
470 -
         }
471 -
    } else if (file.exists(file) || file.exists(default_credentials_file())) {
472 -
         if (file.exists(file)) {
473 -
             cred <- read_credentials(file = file)
474 -
         } else {
475 -
             ## otherwise, default to default location
476 -
             cred <- read_credentials(file = default_credentials_file())
477 -
         }
478 -
         if (profile %in% names(cred)) {
479 -
             cred <- cred[[profile]]
480 -
             if (isTRUE(verbose)) {
481 -
                 message(sprintf("Using profile '%s' from global credentials files from '%s'", profile, default_credentials_file()))
482 -
             }
483 -
             # early return
484 -
             return(credentials_to_list(cred, region = region, default_region = default_region, verbose = verbose))
485 -
         }
486 -
    }   
487 -
    return(NULL)
428 +
read_profile_from_credentials_file <- function(profile, file, region, default_region, verbose, require_profile=FALSE) {
429 +
  if (!file.exists(file)) return(NULL)
430 +
  
431 +
  cred <- read_credentials(file)
432 +
  
433 +
  if (missing(profile)||is.null(profile)) {
434 +
    profile <- "default"
435 +
  }
436 +
  vmsg(verbose, sprintf("Attempting to find profile '%s'", profile))
437 +
  
438 +
  if (profile %in% names(cred)) {
439 +
    cred <- cred[[profile]]
440 +
    vmsg(verbose, sprintf("Using profile '%s' from credentials files from '%s'", profile, file))
441 +
  } else {
442 +
    if (isTRUE(require_profile)) {
443 +
      return(NULL)
444 +
    } else if ("default" %in% names(cred)) {
445 +
      cred <- cred[["default"]]
446 +
      vmsg(verbose, sprintf("Using profile '%s' from credentials files from '%s'", "default", file))
447 +
    } else {
448 +
      return(NULL)
449 +
    }
450 +
  }
451 +
  
452 +
  return(credentials_to_list(cred, region = region, default_region = default_region, verbose = verbose))
488 453
}
Files Coverage
R 77.14%
Project Totals (10 files) 77.14%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading