import {encrypt} from '../../kms';

export default async (req, res, next) => {
  const plaintext = req.body.secret;
  let keyObj;

  try {
    keyObj = JSON.parse(req.body.key);
  } catch (err) {
    console.log(err);
    keyObj = null;
  }

  if (!keyObj) {
    return next(new Error('CMK not defined'));
  }

  if (!plaintext) {
    return next(new Error('Plaintext not entered'));
  }

  const region = keyObj.region;
  const key = keyObj.key;
  const params = {
    KeyId: key,
    Plaintext: plaintext,
    region
  };

  let data;

  try {
    data = await encrypt(params);
  } catch (err) {
    return next(err);
  }

  const resp = {
    region: data.region
  };

  if (data instanceof Error) {
    resp.status = 'ERROR';
    resp.text = data.message;
  } else {
    resp.status = 'SUCCESS';
    resp.text = `$tokend:
type: kms
resource: /v1/kms/decrypt
region: ${data.region}
ciphertext: "${data.ciphertext}"
datakey: "${data.datakey}"`;
  }

  return res.json(resp);
};

@@ -300,24 +300,154 @@
 ed'));%0A  %7D%0A%0A
+  if (!keyObj.hasOwnProperty('region') %7C%7C !keyObj.hasOwnProperty('key')) %7B%0A    return next(new Error('Invalid key format'));%0A  %7D%0A%0A
   if (!plain

Add validation for the key submitted in the POST body.