All files / lib opensesame.js

54.87% Statements 45/82
40% Branches 2/5
33.33% Functions 1/3
54.87% Lines 45/82

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 831x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                 1x 1x                           1x 1x 1x 1x 1x 1x  
"use strict"
/* eslint no-unused-vars: 0 */
import async from 'async';
import Aws from './service/aws.js';
import OpenapiIpify from 'openapi_ipify';
import util from 'util';
 
/**
 * class OpenSesame
 */
class OpenSesame {
 
  /**
   * Construct OpenSesame object.
   *
   * @param {Object} opts: optional settings
   *   - port: port numbeer of the ingress rule
   *   - protocol: protocol of the ingress rule
   *   - name: name of the ingress rule
   *   - dryRun: when true, changelog file won't be modified
   */
  constructor(opts) {
    this.opts = opts || {};
    this.opts.port = this.opts.port || 22;
    this.opts.protocol = this.opts.protocol || 'tcp';
    this.opts.dryRun = this.opts.dryRun || false;
    this.opts.name = this.opts.name || 'open-sesame';
  }
 
  /**
   * Add public IP address to AWS security group inbound rules.
   *
   * @param {String} region: AWS region
   * @param {Array} secGroupIds: an array of AWS security group IDs
   * @param {Function} cb: standard cb(err, result) callback
   */
  aws(region, secGroupIds, cb) {

    const self = this;

    function createAddIpToSecGroupTask(aws, ip, secGroupId) {
      function task(cb) {
        aws.addIpToSecGroup(self.opts.protocol, ip, self.opts.port, secGroupId, self.opts.name, cb);
      }
      return task;
    }
    function addPublicIp(err, ip) {
      if (err) {
        cb(err);
      } else {
        const tasks = [];
        const aws = new Aws(region, self.opts);
        secGroupIds.forEach(function (secGroupId) {
          tasks.push(createAddIpToSecGroupTask(aws, ip, secGroupId));
        });
        async.series(tasks, cb);
      }
    }

    this._getPublicIp(addPublicIp);
  }
 
  _getPublicIp(cb) {
    console.log('--\nRetrieving public IP address...');
    const api = new OpenapiIpify.DefaultApi();
    const _cb = function(err, data, res) {
      if (err) {
        console.error('Unable to retrieve public IP');
        cb(err);
      } else {
        console.log(util.format('IP is %s', data));
        cb(null, data);
      }
    };
    api.getIp({}, _cb);
  }
 
}
 
export {
  OpenSesame as default
};