UNPKG

ks3

Version:

本代码库为`金山云存储KS3`服务.主要提供`KS3 nodejs SDK`和`命令行工具`.

82 lines (73 loc) 2.34 kB
const requestPromise = require('request-promise') var aws4 = require('aws4'); const util = require('./util') const debug = require('debug')('ks3:sts') /** * 获取sts服务 * @param {*} options * @description * */ function STS (options) { if (!options || !options.ak || !options.sk) { throw new Error('require the ak, sk') } this.options = Object.assign({ endpoint: options.endpoint || 'https://sts.cn-beijing-6.api.ksyun.com', host: options.host || 'sts.cn-beijing-6.api.ksyun.com', region: options.host || 'cn-beijing-6', service: 'sts', Action: 'AssumeRole', Version: '2015-11-01' }, options) } /** * 获取临时角色 * @param {*} params - { roleKrn, durationSeconds, roleSessionName, policy } * @param {*} cb * @description * - roleKrn 必填 * - roleSessionName 必填 * - durationSeconds 选填,默认3600 * - policy 选填,默认跟随iam角色的权限策略 */ STS.prototype.assumeRole = function (params) { const { roleKrn, durationSeconds, roleSessionName, policy } = params if (!roleKrn || !roleSessionName) { throw new Error('require the roleKrn, roleSessionName') } const { Action, Version, format, apiVersion, ak, sigMethod, sigVersion } = this.options const p = util.clearEmpty(Object.assign({ RoleKrn: roleKrn, RoleSessionName: roleSessionName, DurationSeconds: durationSeconds, Policy: policy }, { Action, Version })) const path = '/?' + util.paramsEncode(p) var opts = { host: this.options.host, path: path, service: this.options.service, region: this.options.region } var awsOpts = aws4.sign(opts, { accessKeyId: this.options.ak, secretAccessKey: this.options.sk }) debug('awsOpts', awsOpts) var options = { 'method': 'GET', 'url': this.options.endpoint + path, 'headers': { 'accept': 'application/json', 'x-amz-date': awsOpts.headers['X-Amz-Date'], 'Authorization': awsOpts.headers['Authorization'] } }; return requestPromise(options, function (error, response) { if (error) throw new Error(error); debug(response.body); return JSON.parse(response.body) }); } module.exports = STS