-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse AWS configuration with support for profile section
- Loading branch information
Showing
6 changed files
with
187 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
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,15 @@ | ||
[uni] | ||
region = somewhere-1 | ||
endpoint_url = https://example.com/bucket/prefix/1 | ||
key = value | ||
extrakey = willbepropagated | ||
|
||
[profile unidata] | ||
region = us-east-1 | ||
endpoint_url = https://play.min.io/ | ||
dummy_key = dummy_value | ||
|
||
[profile play] | ||
region = us-east-1 | ||
endpoint_url = https://play.min.io/ | ||
|
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,8 @@ | ||
[play] | ||
aws_access_key_id = DummyKeys | ||
aws_secret_access_key = DummySecret | ||
|
||
[uni] | ||
region = somewhere-2 | ||
endpoint_url = https://example.com/bucket/prefix/2 | ||
key = value2 |
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,96 @@ | ||
/********************************************************************* | ||
* Copyright 2018, UCAR/Unidata | ||
* See netcdf/COPYRIGHT file for copying and redistribution conditions. | ||
*********************************************************************/ | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include "netcdf.h" | ||
#include "ncrc.h" | ||
#include "ncpathmgr.h" | ||
#include "ncs3sdk.h" | ||
#include "ncuri.h" | ||
#include "nc4internal.h" | ||
|
||
NCS3INFO s3info; | ||
void* s3client = NULL; | ||
|
||
/* Forward */ | ||
static void cleanup(void); | ||
|
||
#define STR(p) p?p:"NULL" | ||
#define CHECK(code) do {stat = check(code,__func__,__LINE__); if(stat) {goto done;}} while(0) | ||
|
||
static int | ||
check(int code, const char* fcn, int line) | ||
{ | ||
if(code == NC_NOERR) return code; | ||
fprintf(stderr,"***FAIL: (%d) %s @ %s:%d\n",code,nc_strerror(code),fcn,line); | ||
abort(); | ||
} | ||
|
||
static void | ||
cleanup(void) | ||
{ | ||
if(s3client) | ||
NC_s3sdkclose(s3client, &s3info, 0/*deleteit*/, NULL); | ||
s3client = NULL; | ||
NC_s3clear(&s3info); | ||
} | ||
|
||
int | ||
main(int argc, char** argv) | ||
{ | ||
int c = 0,stat = NC_NOERR; | ||
|
||
/* Load RC and .aws/config */ | ||
CHECK(nc_initialize()); | ||
CHECK(NC_s3sdkinitialize()); | ||
NCglobalstate* gs = NC_getglobalstate(); | ||
//Not checking, aborts if ~/.aws/config doesn't exist | ||
//int notloaded = NC_aws_load_credentials(gs); | ||
//fprintf(stderr,"RETCODE: %d\n", notloaded); | ||
CHECK(NC_aws_load_credentials(gs)); | ||
|
||
// Lets ensure the active profile is loaded | ||
// from the configurtion files instead of an URL | ||
const char* activeprofile = NULL; | ||
CHECK(NC_getactives3profile(NULL, &activeprofile)); | ||
|
||
//if (notloaded) { // If not loaded, we'll use profile "no" | ||
// CHECK(strncmp(activeprofile, "no", sizeof("no"))); | ||
// // And we passed this test... | ||
//} else { | ||
fprintf(stderr, "Active profile:%s\n", STR(activeprofile)); | ||
// ARGV contains should contain "key[=[value]]" pairs to verify | ||
// if parsed when loading the aws config on the respective profile | ||
|
||
for(int i = 1; i < argc; i++) { | ||
const char *argkey = strtok(argv[i],"="); | ||
const char *argvalue = strtok(NULL,"="); | ||
const char* value = NULL; | ||
|
||
NC_s3profilelookup(activeprofile,argkey,&value); | ||
fprintf(stderr, "%s\t%s -> %s\n",value?"":"*** FAIL:", argv[i],value?value:"NOT DEFINED!"); | ||
if ( value == NULL | ||
|| (argvalue != NULL | ||
&& strncmp(value, argvalue, strlen(value))) | ||
){ | ||
c++; | ||
stat |= NC_ERCFILE; | ||
} | ||
} | ||
//} | ||
|
||
done: | ||
cleanup(); | ||
if(stat) | ||
printf("*** FAIL: a total of %d keys were not found on the profile %s\n", c, STR(activeprofile)); | ||
else | ||
printf("***PASS\n"); | ||
(void)NC_s3sdkfinalize(); | ||
(void)nc_finalize(); | ||
exit(stat?:0); | ||
} |
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,34 @@ | ||
#!/bin/sh | ||
|
||
if test "x$srcdir" = x ; then srcdir=`pwd`; fi | ||
. ../test_common.sh | ||
|
||
set -e | ||
|
||
#CMD="valgrind --leak-check=full" | ||
|
||
isolate "testdir_ut_aws_config" | ||
|
||
THISDIR=`pwd` | ||
cd $ISOPATH | ||
|
||
echo -e "Testing loading AWS configuration in ${THISDIR}/.aws/config" | ||
env NC_TEST_AWS_DIR=${THISDIR} AWS_PROFILE=unidata ${CMD} ${execdir}/aws_config endpoint_url region dummy_key | ||
echo "Status: $?" | ||
|
||
env NC_TEST_AWS_DIR=${THISDIR} AWS_PROFILE=play ${CMD} ${execdir}/aws_config endpoint_url region | ||
echo "Status: $?" | ||
|
||
env NC_TEST_AWS_DIR=${THISDIR} AWS_PROFILE=uni ${CMD} ${execdir}/aws_config endpoint_url region key | ||
echo "Status: $?" | ||
|
||
env NC_TEST_AWS_DIR=${THISDIR} AWS_PROFILE=uni ${CMD} ${execdir}/aws_config region=somewhere-2 endpoint_url=https://example.com/bucket/prefix/2 key=value2 extrakey=willbepropagated | ||
echo "Status: $?" | ||
|
||
echo ${CMD} ${execdir}/aws_config | ||
${CMD} ${execdir}/aws_config | ||
echo "Status: $? [this is expected]" | ||
echo -e "Finished" | ||
|
||
exit | ||
|