Skip to content

Commit dc8bba8

Browse files
authored
Merge pull request #105 from cloudgraphdev/fix/update-creds-to-support-sso
feat(creds): update credentials flow to support sso
2 parents b6df1f2 + db29e83 commit dc8bba8

File tree

3 files changed

+794
-15
lines changed

3 files changed

+794
-15
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
"terraform:cleanup": "rimraf ./tests/terraform/{.terraform,.terraform.lock.hcl,tfplan} ./tests/terraform/*.{tfstate,tfplan,backup}"
3232
},
3333
"dependencies": {
34+
"@aws-sdk/credential-providers": "^3.256.0",
35+
"@aws-sdk/shared-ini-file-loader": "^3.254.0",
3436
"@cloudgraph/sdk": "^0.22.1",
3537
"@fast-csv/parse": "^4.3.6",
3638
"@graphql-tools/load-files": "^6.5.3",

src/services/index.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import CloudGraph, {
77
import { loadFilesSync } from '@graphql-tools/load-files'
88
import { mergeTypeDefs } from '@graphql-tools/merge'
99
import AWS, { Config } from 'aws-sdk'
10+
import { loadSharedConfigFiles } from '@aws-sdk/shared-ini-file-loader'
11+
import { fromIni } from '@aws-sdk/credential-providers'
1012
import chalk from 'chalk'
1113
import { DocumentNode } from 'graphql'
1214
import STS from 'aws-sdk/clients/sts'
@@ -78,7 +80,7 @@ export default class Provider extends CloudGraph.Client {
7880
const result: { [key: string]: any } = { ...providerSettings }
7981
let profiles
8082
try {
81-
profiles = this.getProfilesFromSharedConfig()
83+
profiles = await this.getProfilesFromSharedConfig()
8284
} catch (error: any) {
8385
this.logger.warn('No AWS profiles found')
8486
}
@@ -304,19 +306,21 @@ export default class Provider extends CloudGraph.Client {
304306
switch (true) {
305307
case role && role !== '': {
306308
let sts = new AWS.STS()
307-
await new Promise<void>(resolve => {
309+
await new Promise<void>(async resolve => {
308310
if (profile && profile !== 'default') {
309-
const creds = this.getSharedIniFileCredentials(profile)
311+
let creds: AWS.Credentials
312+
const credsFunction = fromIni({
313+
profile
314+
})
310315
if (creds) {
311-
sts = new AWS.STS({ credentials: creds })
316+
sts = new AWS.STS({ credentials: await credsFunction() })
312317
}
313318
}
314319
const options = {
315320
RoleSessionName: 'CloudGraph',
316321
RoleArn: role,
317322
...(externalId && { ExternalId: externalId }),
318323
}
319-
320324
sts.assumeRole(options, (err, data) => {
321325
if (err) {
322326
this.logger.error(
@@ -478,18 +482,22 @@ export default class Provider extends CloudGraph.Client {
478482
return credentials
479483
}
480484

481-
private getProfilesFromSharedConfig(): string[] {
482-
let profiles
485+
private async getProfilesFromSharedConfig(): Promise<string[]> {
486+
let profiles = []
483487
try {
484-
profiles = Object.keys(
485-
AWS['util'].getProfilesFromSharedConfig(AWS['util'].iniLoader)
486-
)
488+
const filesObject = await loadSharedConfigFiles()
489+
const files = Object.keys(filesObject)
490+
for (const file of files) {
491+
const fileProfiles = Object.keys(filesObject[file])
492+
if (fileProfiles && fileProfiles.length > 0) {
493+
profiles.push(...fileProfiles)
494+
}
495+
}
487496
} catch (error: any) {
488497
this.logger.warn('Unable to read AWS shared credential file')
489498
this.logger.debug(error)
490499
}
491-
492-
return profiles || []
500+
return profiles
493501
}
494502

495503
private mergeRawData(
@@ -689,7 +697,7 @@ export default class Provider extends CloudGraph.Client {
689697
const { profile, roleArn: role } = account
690698
// verify that profile exists in the shared credential file
691699
if (profile) {
692-
const profiles = this.getProfilesFromSharedConfig()
700+
const profiles = await this.getProfilesFromSharedConfig()
693701
if (!profiles.includes(profile)) {
694702
this.logger.warn(
695703
`Profile: ${profile} not found in shared credentials file. Skipping...`

0 commit comments

Comments
 (0)