Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
new code from branch, docs, test, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
brianantonelli committed Nov 9, 2016
1 parent 70414a0 commit 3d59ee7
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "0.12"
- "0.11"
- "0.10"
10 changes: 10 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -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.

====================================================================
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
83 changes: 82 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
171 changes: 161 additions & 10 deletions lib/alks-api.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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',
Expand All @@ -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!'));
}
}
});
};
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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"
}
}
10 changes: 10 additions & 0 deletions test/alks-api.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 3d59ee7

Please sign in to comment.