-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto set stsConfig when sts expires (#856)
* feat: auto set stsConfig when sts expires * test: add auto set sts test case
- Loading branch information
Showing
9 changed files
with
142 additions
and
5 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
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
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 @@ | ||
export declare function setSTSToken(this: any): Promise<void>; |
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,25 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.setSTSToken = void 0; | ||
const formatObjKey_1 = require("./formatObjKey"); | ||
async function setSTSToken() { | ||
if (!this.options) | ||
this.options = {}; | ||
let credentials = await this.options.refreshSTSToken(); | ||
credentials = formatObjKey_1.formatObjKey(credentials, 'firstLowerCase'); | ||
if (credentials.securityToken) { | ||
credentials.stsToken = credentials.securityToken; | ||
} | ||
checkCredentials(credentials); | ||
Object.assign(this.options, credentials); | ||
} | ||
exports.setSTSToken = setSTSToken; | ||
function checkCredentials(obj) { | ||
const stsTokenKey = ['accessKeySecret', 'accessKeyId', 'stsToken']; | ||
const objKeys = Object.keys(obj); | ||
stsTokenKey.forEach(_ => { | ||
if (!objKeys.find(key => key === _)) { | ||
throw Error(`refreshSTSToken must return contains ${_}`); | ||
} | ||
}); | ||
} |
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,22 @@ | ||
import { formatObjKey } from './formatObjKey'; | ||
|
||
export async function setSTSToken(this: any) { | ||
if (!this.options) this.options = {}; | ||
let credentials = await this.options.refreshSTSToken(); | ||
credentials = formatObjKey(credentials, 'firstLowerCase'); | ||
if (credentials.securityToken) { | ||
credentials.stsToken = credentials.securityToken; | ||
} | ||
checkCredentials(credentials); | ||
Object.assign(this.options, credentials); | ||
} | ||
|
||
function checkCredentials(obj) { | ||
const stsTokenKey = ['accessKeySecret', 'accessKeyId', 'stsToken']; | ||
const objKeys = Object.keys(obj); | ||
stsTokenKey.forEach(_ => { | ||
if (!objKeys.find(key => key === _)) { | ||
throw Error(`refreshSTSToken must return contains ${_}`); | ||
} | ||
}); | ||
} |
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,21 @@ | ||
const { setSTSToken } = require('../../../lib/common/utils/setSTSToken'); | ||
const assert = require('assert'); | ||
|
||
describe('setSTSToken()', () => { | ||
it('should set sts token', async () => { | ||
const refreshSTSToken = () => { | ||
return { | ||
securityToken: 'test-securityToken', | ||
accessKeyId: 'test-AccessKeyId', | ||
accessKeySecret: 'test-accessKeySecret', | ||
}; | ||
}; | ||
const test_set_token = { options: { refreshSTSToken } }; | ||
|
||
await setSTSToken.call(test_set_token); | ||
|
||
assert(test_set_token.options.accessKeyId); | ||
assert(test_set_token.options.accessKeySecret); | ||
assert(test_set_token.options.securityToken); | ||
}); | ||
}); |