chiefy/vaulted

View on GitHub
lib/sys/init.js

Summary

Maintainability
A
1 hr
Test Coverage
'use strict';
var
  Vaulted = {},
  Promise = require('bluebird'),
  _ = require('lodash');

/**
 * @module init
 * @extends Vaulted
 * @desc Provides implementation for the Vault Initialization APIs
 *
 */

module.exports = function extend(Proto) {
  _.extend(Proto, Vaulted);
};

/**
 * @method init
 * @desc Initializes a remote vault
 *
 * @param {number} [options.secret_shares=config['secret_shares']] - number of shares to split the master key into
 * @param {number} [options.secret_threshold=config['secret_threshold']] - number of shares required to reconstruct the master key
 * @resolve {Vaulted} Resolves with keys and token or current instance of Vaulted
 * @reject {Error} An error indicating what went wrong
 * @return {Promise}
 */
Vaulted.init = Promise.method(function init(options) {
  if (this.initialized === true) {
    return Promise.resolve(this);
  }

  options = _.defaults(options || {}, {
    secret_shares: this.config.get('secret_shares'),
    secret_threshold: this.config.get('secret_threshold')
  });

  return this.api.getEndpoint('sys/init')
    .put({
      body: options
    })
    .promise()
    .bind(this).then(function keysAndToken(keys_and_token) {
      this.emit('init');
      return keys_and_token;
    });
});

/**
 * @method getInitStatus
 * @desc Gets the initialize status of a vault
 *
 * @resolve {Status} Resolves with the vault initializtion status
 * @reject {Error} An error indicating what went wrong
 * @return {Promise}
 */
Vaulted.getInitStatus = Promise.method(function getInitStatus() {
  return this.api.getEndpoint('sys/init')
    .get();
});