From 3d59ee7abe5c3455cc6e364f68583e547a29a43e Mon Sep 17 00:00:00 2001 From: Brian Date: Wed, 9 Nov 2016 13:04:02 -0500 Subject: [PATCH] new code from branch, docs, test, etc --- .gitignore | 1 + .npmignore | 1 + .travis.yml | 5 ++ COPYING | 10 +++ LICENSE | 9 +++ README.md | 83 ++++++++++++++++++++++- lib/alks-api.js | 171 ++++++++++++++++++++++++++++++++++++++++++++--- package.json | 43 ++++++++++++ test/alks-api.js | 10 +++ 9 files changed, 322 insertions(+), 11 deletions(-) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 .travis.yml create mode 100644 COPYING create mode 100644 LICENSE create mode 100644 package.json create mode 100644 test/alks-api.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..096746c --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +/node_modules/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..992827c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - "0.12" + - "0.11" + - "0.10" \ No newline at end of file diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..64fadf8 --- /dev/null +++ b/COPYING @@ -0,0 +1,10 @@ +Copyright (C) 2016, Cox Automotive, Inc. +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +==================================================================== \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b603738 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2016 Cox Automotive + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index a22560a..2dbb46a 100644 --- a/README.md +++ b/README.md @@ -1 +1,82 @@ -# alks-node +#ALKS Node Client + +[![NPM](https://nodei.co/npm/alks-node.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/alks-node/) + +[![Build Status](https://travis-ci.org/Cox-Automotive/alks-node.svg?branch=master)](https://travis-ci.org/Cox-Automotive/alks-node) + +## About +Node client for interfacting with the [ALKS](https://github.com/Cox-Automotive/ALKS) services. + +## Usage + +```js +var alks = require('alks-node'); +``` + +## Utilities + +### getDurations() + +Returns array of valid session durations. + +```js +alks.getDurations(); +``` + +### getAccountSelectorDelimiter() + +Returns string dilimeter used for account/role seperation. + +```js +alks.getAccountSelectorDelimiter(); +``` + +## Methods + +### createKey(account, password, duration, callback) + +Creates a new session key with the provided information. Returns a JSON document. + +```js +var data = { + alksAccount: 'alksAccount', + alksRole: 'alksRole', + sessionTime: 2, + server: 'endpoint', + userid: 'my-network-id', + password: 'my-network-password' +}; + +alks.createKey(data, 'password', 2, function(err, key){ + if(err) console.error(err); + else console.log(JSON.stringify(key)); +}); +``` + +### getAccounts(server, userid, password, options, callback) + +Returns a collection of accounts. Options filter allows you to switch between IAM and non-IAM accounts. + +```js +alks.getAccounts('server', 'username', 'password', {filters: { iamOnly: false }}, function(err, accounts){ + if(err) console.error(err); + else console.log(JSON.stringify(accounts)); +}); +``` + +### generateConsoleUrl(key, callback) + +Returns a AWS console URL for a given key. The URL is good for 15 minutes. + +```js +alks.generateConsoleUrl(alksKey, function(err, url){ + if(err) console.error(err); + else console.log('The console URL is: %s', url); +}); +``` + +### getIamRoleTypes(server, userid, password, callback) + +### createIamKey(account, password, callback) + +### createIamRole(account, password, roleName, roleType, includeDefaultPolicies, callback) \ No newline at end of file diff --git a/lib/alks-api.js b/lib/alks-api.js index 95e19a8..dae7247 100644 --- a/lib/alks-api.js +++ b/lib/alks-api.js @@ -1,14 +1,16 @@ /*jslint node: true */ 'use strict'; -var _ = require('underscore') , +var _ = require('underscore'), request = require('request'), - utils = require('../lib/utils'); + moment = require('moment'); var exports = module.exports = {}; -var ALKS_DURATIONS = [ 2, 6, 12, 18, 24, 36 ], - ACCOUNT_SELECTION_DELIMITER = ' :: '; +var ALKS_DURATIONS = [ 2, 6, 12, 18, 24, 36 ], + ACCOUNT_SELECTION_DELIMITER = ' :: ', + AWS_SIGNIN_URL = 'https://signin.aws.amazon.com/federation', + AWS_CONSOLE_URL = 'https://console.aws.amazon.com/'; exports.getDurations = function(){ return ALKS_DURATIONS; @@ -52,14 +54,82 @@ exports.createKey = function(account, password, duration, callback){ accessKey: results.body.accessKey, secretKey: results.body.secretKey, sessionToken: results.body.sessionToken, - consoleURL: results.body.consoleURL, alksAccount: account.alksAccount, - alksRole: account.alksRole + alksRole: account.alksRole, + sessionTime: payload.sessionTime, + expires: moment().add(payload.sessionTime, 'hours') }, account, password); }); }; -exports.getAccounts = function(server, userid, password, callback){ +exports.createIamKey = function(account, password, callback){ + var payload = _.extend({ + password: password, + sessionTime: 1, + account: account.alksAccount, + role: account.alksRole + }, account); + + request({ + url: account.server + '/getIAMKeys/', + method: 'POST', + json: payload + }, function(err, results){ + if(err){ + return callback(err); + } + else if(results.statusCode !== 200){ + return callback(new Error(getMessageFromBadResponse(results))); + } + + callback(null, { + accessKey: results.body.accessKey, + secretKey: results.body.secretKey, + sessionToken: results.body.sessionToken, + alksAccount: account.alksAccount, + alksRole: account.alksRole, + sessionTime: payload.sessionTime, + expires: moment().add(payload.sessionTime, 'hours') + }, account, password); + }); +}; + +exports.createIamRole = function(account, password, roleName, roleType, includeDefaultPolicies, callback){ + var payload = _.extend({ + password: password, + account: account.alksAccount, + role: account.alksRole, + roleName: roleName, + roleType: roleType, + includeDefaultPolicy: includeDefaultPolicies ? '1' : '0' + }, account); + + request({ + url: account.server + '/createRole/', + method: 'POST', + json: payload + }, function(err, results){ + if(err){ + return callback(err); + } + else if(results.statusCode !== 200){ + return callback(new Error(getMessageFromBadResponse(results))); + } + + if(results.body.statusMessage === 'Success'){ + callback(null, results.body ); + } + else{ + callback(new Error(results.body.statusMessage), null); + } + }); +}; + +exports.getAccounts = function(server, userid, password, options, callback){ + var opts = _.extend({ + filters: {} + }, options); + request({ url: server + '/getAccounts/', method: 'POST', @@ -73,10 +143,91 @@ exports.getAccounts = function(server, userid, password, callback){ } var accounts = []; - _.each(results.body.accountRoles, function(role, acct){ - accounts.push([acct, role[0]].join(ACCOUNT_SELECTION_DELIMITER)); - }); + + // new API style to support IAM + if(results.body.accountListRole){ + var accountRolesFitlered = {}; + _.each(results.body.accountListRole, function(role, acct){ + if((opts.filters.iamOnly && !role[0].iamKeyActive) + || (!opts.filters.iamOnly && role[0].iamKeyActive)){ + return; + } + accountRolesFitlered[acct] = role; + }); + + var accounts = []; + _.each(accountRolesFitlered, function(role, acct){ + accounts.push([acct, role[0].role].join(ACCOUNT_SELECTION_DELIMITER)); + }); + } + // v1 API style without IAM + else{ + _.each(results.body.accountRoles, function(role, acct){ + accounts.push([acct, role[0]].join(ACCOUNT_SELECTION_DELIMITER)); + }); + } + + accounts = _.sortBy(accounts, function(account){ return account; }); callback(null, accounts); }); +}; + +exports.getIamRoleTypes = function(server, userid, password, callback){ + request({ + url: server + '/getAWSRoleTypes/', + method: 'POST', + json: { userid: userid, password: password } + }, function(err, results){ + if(err){ + return callback(err); + } + else if(results.statusCode !== 200){ + return callback(new Error(getMessageFromBadResponse(results))); + } + + callback(null, JSON.parse(results.body.roleTypes)); + }); +}; + +exports.generateConsoleUrl = function(key, callback){ + var payload = { + sessionId: key.accessKey, + sessionKey: key.secretKey, + sessionToken: key.sessionToken + }; + + var urlParms = '?Action=getSigninToken&SessionType=json&Session=' + encodeURIComponent(JSON.stringify(payload)); + + request({ + url: AWS_SIGNIN_URL + urlParms, + method: 'GET' + }, function(err, results){ + if(err){ + return callback(err); + } + else if(results.statusCode !== 200){ + return callback(new Error(results.body)); + } + else{ + var returnedData = JSON.parse(results.body); + + if(!_.isEmpty(returnedData.SigninToken)){ + var consoleUrl = [ + AWS_SIGNIN_URL, + '?Action=login', + '&Destination=', + encodeURIComponent(AWS_CONSOLE_URL), + '&SigninToken=', + encodeURIComponent(returnedData.SigninToken) + ].join(''); + + return callback(null, consoleUrl) + } + else{ + console.log(results.body) + return callback(new Error('AWS didnt return signin token!')); + } + } + }); }; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..dfc2af7 --- /dev/null +++ b/package.json @@ -0,0 +1,43 @@ +{ + "name": "alks-node", + "version": "0.2.0", + "description": "Node client for ALKS", + "main": "lib/alks-api.js", + "scripts": { + "test": "./node_modules/.bin/mocha ./test" + }, + "engines": { + "node": ">=0.10" + }, + "repository": { + "type": "git", + "url": "https://github.com/Cox-Automotive/alks-node.git" + }, + "license": "MIT", + "author": { + "name": "Cox Automotive", + "email": "brian.antonelli@coxautoinc.com", + "url": "https://github.com/Cox-Automotive/" + }, + "bugs": { + "url": "https://github.com/Cox-Automotive/alks-node/issues" + }, + "licenses": [ + { + "type": "MIT", + "url": "https://github.com/Cox-Automotive/alks-node/blob/master/COPYING" + } + ], + "keywords": [ + "alks" + ], + "dependencies": { + "moment": "^2.15.2", + "request": "^2.72.0", + "underscore": "^1.8.3" + }, + "devDependencies": { + "chai": "^3.5.0", + "mocha": "^2.5.3" + } +} diff --git a/test/alks-api.js b/test/alks-api.js new file mode 100644 index 0000000..cc18486 --- /dev/null +++ b/test/alks-api.js @@ -0,0 +1,10 @@ +/*jslint node: true */ +'use strict'; + +var assert = require('chai').assert; + +describe('alks-node', function(){ + it('should write some tests', function(done){ + done(); + }); +}); \ No newline at end of file