KoteiIto / node-athena

a nodejs simple aws athena client
MIT License
105 stars 73 forks source link

Global config issue #56

Open aliccer opened 5 years ago

aliccer commented 5 years ago

I spent a lot of time trying to find a problem in my code but the problem was hidden from the eyes, and it lives on this line of code https://github.com/KoteiIto/node-athena/blob/master/src/index.ts#L42

I am using AWS STS to be able to access another AWS profile, and run a request with temporary credentials that are passed to the createClient function and they are set globally after the function call, and it breaks the rest of the AWS client calls (like S3, where default server settings must be used) returning in the response Access Denied

phasath commented 5 years ago

@aliccer I have the same problem as I access many things on AWS from different regions. The fix would be just receiving an AWS client and then, creating new clients with the correct region as seen here:

https://stackoverflow.com/a/40366746/2569522

aliccer commented 5 years ago

@phasath thank you for the response but it is not the same thing, take a look at the simple example below

const AWS = require("was-sdk")
const athena = require("athena-client")

new AWS.STS.assumeRole(/*params*/).then((credentials) => {
   // when the Athena Client instance created it writes assumed role to the global AWS config
   const athenaClient = athena.createClient(credentials)

   // and when the S3 bucket instance created it must use default server config
   // but instead it takes config which was reconfigured by `athena-client`
   const s3Bucket = new AWS.S3()
})

So to make my S3 client to look at the production AWS profile I must explicitly pass the server config into S3 (like this new AWS.S3(defaultConfig)), like you did in your example, but this is not the right way in my case, because AWS clients must use the server config respectively if they were created without arguments

phasath commented 5 years ago

@aliccer oh, sorry. I didn't mean it as a way to fix your problem.

I forgot to break line. I believe the fix should be done on the athena-client by either accepting a new AWS client or creating a new AWS client inside the athena-client (without changing the global one).

aliccer commented 5 years ago

@phasath I forked the package and in my example removed this line https://github.com/KoteiIto/node-athena/blob/master/src/index.ts#L42 and add config into Athena and S3 instances like this

const athena = new Athena(Object.assign({ apiVersion: '2017-05-18' }, awsConfig))
const s3 = new S3(Object.assign({ apiVersion: '2006-03-01' }), awsConfig)

and it fixes the issue

phasath commented 5 years ago

@aliccer I forked and changed it too to allow a overwrite flag

https://github.com/phasath/node-athena