Skip to content

Commit

Permalink
feat: [DEVOPS-1467] Upgrade aws-sdk to version 3.
Browse files Browse the repository at this point in the history
And fix tests.
  • Loading branch information
IanMExtend committed Sep 21, 2023
1 parent 7bc013e commit e609d7a
Show file tree
Hide file tree
Showing 6 changed files with 796 additions and 148 deletions.
32 changes: 15 additions & 17 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
module.exports = {
roots: ['<rootDir>/src'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
roots: ['<rootDir>/src'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
collectCoverageFrom: ['src/**/*'],
coverageDirectory: './coverage',
coverageThreshold: {
global: {
branches: 10,
functions: 10,
lines: 10,
statements: 10,
},
collectCoverageFrom: [
'src/**/*',
],
coverageDirectory: './coverage',
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80
}
},
coverageReporters: ['json', 'lcov', 'text', 'clover'],
testEnvironment: 'node',
},
coverageReporters: ['json', 'lcov', 'text', 'clover'],
testEnvironment: 'node',
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "extaws",
"version": "0.0.14",
"version": "0.0.16",
"description": "Extend Okta Aws Authentication",
"main": "dist/index.js",
"files": [
Expand Down Expand Up @@ -28,14 +28,14 @@
"start": "node --no-deprecation -r ts-node/register src/lib/extaws-cli.ts"
},
"dependencies": {
"@aws-sdk/client-sts": "^3.335.0",
"@types/elementtree": "^0.1.0",
"@types/ini": "^1.3.30",
"@types/inquirer": "^7.3.1",
"@types/jest": "^26.0.13",
"@types/node": "^14.11.1",
"@types/shelljs": "^0.8.8",
"@types/tough-cookie": "^4.0.0",
"aws-sdk": "^2.751.0",
"axios": "^0.20.0",
"axios-cookiejar-support": "^1.0.0",
"color": "^3.1.2",
Expand Down
12 changes: 6 additions & 6 deletions src/lib/extaws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, {AxiosError, AxiosInstance, AxiosResponse} from 'axios'
import {CookieJar} from 'tough-cookie'
import axiosCookieJarSupport from 'axios-cookiejar-support'
import * as htmlparser from 'htmlparser2'
import {STS} from 'aws-sdk'
import {STS, AssumeRoleWithSAMLResponse} from '@aws-sdk/client-sts'
import * as et from 'elementtree'
import * as soup from './soup'
import {sleep, writeAwsCredentials, forwardSlashRegEx, writeAwsConfig} from './util'
Expand Down Expand Up @@ -343,10 +343,10 @@ export class ExtAws {
/**
* Creates and configures the Axios client. Also attaches the necessary coookie jar for session management
*/
createAxiosClient(): void {
createAxiosClient(oktaOrgName?: string): void {
this.client = axios.create({
withCredentials: true,
baseURL: `https://${this.config.extaws.oktaOrgName}.okta.com`,
baseURL: `https://${oktaOrgName || this.config.extaws.oktaOrgName}.okta.com`,
headers: {
'Content-Type': 'application/json'
}
Expand All @@ -360,7 +360,7 @@ export class ExtAws {
* Main method for logging a user into AWS via Okta. Will log a user in and write credentials to aws profile
* @returns AWS Credentials
*/
async login(props?: { profile?: string, duration?: number, region?: string, role?: string }, inputSpinner?: Ora): Promise<STS.Types.AssumeRoleWithSAMLResponse> {
async login(props?: { profile?: string, duration?: number, region?: string, role?: string }, inputSpinner?: Ora): Promise<AssumeRoleWithSAMLResponse> {
const configResult = await ExtAws.getConfig()
if (configResult === null ) {
throw new Error('Missing configuration. Please `init`')
Expand Down Expand Up @@ -630,13 +630,13 @@ export class ExtAws {
*
* @returns AWS Credentials
*/
private async assumeRole(role: STSAssumeRole, duration?: number): Promise<STS.Types.AssumeRoleWithSAMLResponse> {
private async assumeRole(role: STSAssumeRole, duration?: number): Promise<AssumeRoleWithSAMLResponse> {
return await this.sts.assumeRoleWithSAML({
PrincipalArn: role.principal,
RoleArn: role.role,
SAMLAssertion: this.b64Assertion,
DurationSeconds: duration || 43200,
}).promise()
})
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'fs'
import { parse, encode } from 'ini'
import {homedir} from 'os'
import { STS } from 'aws-sdk'
import type { Credentials } from '@aws-sdk/client-sts'

export const forwardSlashRegEx = /^([^/])/i

Expand Down Expand Up @@ -81,7 +81,7 @@ export const writeAwsConfig = (

export const writeAwsCredentials = (
profile: string,
creds: STS.Types.Credentials,
creds: Credentials,
): void => {
const { credsIni } = parseAWSIni()
credsIni[profile] = {
Expand Down
6 changes: 3 additions & 3 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,18 @@ describe('ExtAws', () => {

describe( 'axios client', () => {
test('properly sets withCredentials', () => {
auth.createAxiosClient()
auth.createAxiosClient('fake-org-name')
expect(auth.client.defaults.withCredentials).toBe(true)
})
test('has a cookie jar', () => {
auth.createAxiosClient()
auth.createAxiosClient('fake-org-name')
expect(auth.client.defaults.jar).toBeInstanceOf(CookieJar)
})
})

test( 'function selectToken returns the factor when only one provided', async () => {
const factor = await (ExtAws as any).selectToken([factor1])
expect(factor).toBe('aaaaabbbbbccccc')
expect(factor).toMatchObject({factor: 'aaaaabbbbbccccc', type: 'push'})
})

test( 'properly handles MFA', async() => {
Expand Down
Loading

0 comments on commit e609d7a

Please sign in to comment.