2018-09-25 16:28:26 +00:00
|
|
|
import { run } from '@ember/runloop';
|
|
|
|
import { resolve } from 'rsvp';
|
|
|
|
import { assign } from '@ember/polyfills';
|
|
|
|
import Service from '@ember/service';
|
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import { render, click, find, findAll, fillIn, blur, triggerEvent } from '@ember/test-helpers';
|
2018-04-03 14:16:57 +00:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
|
|
|
import { encodeString } from 'vault/utils/b64';
|
2018-09-25 16:28:26 +00:00
|
|
|
import waitForError from 'vault/tests/helpers/wait-for-error';
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
const storeStub = Service.extend({
|
2018-04-03 14:16:57 +00:00
|
|
|
callArgs: null,
|
|
|
|
keyActionReturnVal: null,
|
|
|
|
rootKeyActionReturnVal: null,
|
|
|
|
adapterFor() {
|
|
|
|
const self = this;
|
|
|
|
return {
|
|
|
|
keyAction(action, { backend, id, payload }, options) {
|
|
|
|
self.set('callArgs', { action, backend, id, payload });
|
|
|
|
self.set('callArgsOptions', options);
|
2018-09-25 16:28:26 +00:00
|
|
|
const rootResp = assign({}, self.get('rootKeyActionReturnVal'));
|
2018-04-03 14:16:57 +00:00
|
|
|
const resp =
|
|
|
|
Object.keys(rootResp).length > 0
|
|
|
|
? rootResp
|
|
|
|
: {
|
2018-09-25 16:28:26 +00:00
|
|
|
data: assign({}, self.get('keyActionReturnVal')),
|
2018-04-03 14:16:57 +00:00
|
|
|
};
|
2018-09-25 16:28:26 +00:00
|
|
|
return resolve(resp);
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
module('Integration | Component | transit key actions', function(hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
hooks.beforeEach(function() {
|
|
|
|
run(() => {
|
|
|
|
this.owner.unregister('service:store');
|
|
|
|
this.owner.register('service:store', storeStub);
|
|
|
|
this.storeService = this.owner.lookup('service:store');
|
|
|
|
});
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
test('it requires `key`', async function(assert) {
|
|
|
|
let promise = waitForError();
|
2020-03-16 22:48:11 +00:00
|
|
|
render(hbs`
|
|
|
|
{{transit-key-actions}}
|
|
|
|
<div id="modal-wormhole"></div>
|
|
|
|
`);
|
2018-09-25 16:28:26 +00:00
|
|
|
let err = await promise;
|
|
|
|
assert.ok(err.message.includes('`key` is required for'), 'asserts without key');
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
test('it renders', async function(assert) {
|
|
|
|
this.set('key', { backend: 'transit', supportedActions: ['encrypt'] });
|
2020-03-16 22:48:11 +00:00
|
|
|
await render(hbs`
|
|
|
|
{{transit-key-actions selectedAction="encrypt" key=key}}
|
|
|
|
<div id="modal-wormhole"></div>
|
|
|
|
`);
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.equal(findAll('[data-test-transit-action="encrypt"]').length, 1, 'renders encrypt');
|
|
|
|
|
|
|
|
this.set('key', { backend: 'transit', supportedActions: ['sign'] });
|
2020-03-16 22:48:11 +00:00
|
|
|
await render(hbs`
|
|
|
|
{{transit-key-actions selectedAction="sign" key=key}}
|
|
|
|
<div id="modal-wormhole"></div>`);
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.equal(findAll('[data-test-transit-action="sign"]').length, 1, 'renders sign');
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
test('it renders: signature_algorithm field', async function(assert) {
|
|
|
|
this.set('key', { backend: 'transit', supportsSigning: true, supportedActions: ['sign', 'verify'] });
|
|
|
|
this.set('selectedAction', 'sign');
|
2020-03-16 22:48:11 +00:00
|
|
|
await render(hbs`
|
|
|
|
{{transit-key-actions selectedAction=selectedAction key=key}}
|
|
|
|
<div id="modal-wormhole"></div>
|
|
|
|
`);
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.equal(
|
|
|
|
findAll('[data-test-signature-algorithm]').length,
|
|
|
|
0,
|
|
|
|
'does not render signature_algorithm field on sign'
|
|
|
|
);
|
|
|
|
this.set('selectedAction', 'verify');
|
|
|
|
assert.equal(
|
|
|
|
findAll('[data-test-signature-algorithm]').length,
|
|
|
|
0,
|
|
|
|
'does not render signature_algorithm field on verify'
|
|
|
|
);
|
2018-04-09 21:50:36 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
this.set('selectedAction', 'sign');
|
|
|
|
this.set('key', {
|
|
|
|
type: 'rsa-2048',
|
|
|
|
supportsSigning: true,
|
|
|
|
backend: 'transit',
|
|
|
|
supportedActions: ['sign', 'verify'],
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
findAll('[data-test-signature-algorithm]').length,
|
|
|
|
1,
|
|
|
|
'renders signature_algorithm field on sign with rsa key'
|
|
|
|
);
|
|
|
|
this.set('selectedAction', 'verify');
|
|
|
|
assert.equal(
|
|
|
|
findAll('[data-test-signature-algorithm]').length,
|
|
|
|
1,
|
|
|
|
'renders signature_algorithm field on verify with rsa key'
|
|
|
|
);
|
2018-04-09 21:50:36 +00:00
|
|
|
});
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
test('it renders: rotate', async function(assert) {
|
|
|
|
this.set('key', { backend: 'transit', id: 'akey', supportedActions: ['rotate'] });
|
2020-03-16 22:48:11 +00:00
|
|
|
await render(hbs`
|
|
|
|
{{transit-key-actions selectedAction="rotate" key=key}}
|
|
|
|
<div id="modal-wormhole"></div>
|
|
|
|
`);
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.equal(find('*').textContent.trim(), '', 'renders an empty div');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
this.set('key.canRotate', true);
|
|
|
|
assert.equal(
|
|
|
|
find('button').textContent.trim(),
|
|
|
|
'Rotate encryption key',
|
|
|
|
'renders confirm-button when key.canRotate is true'
|
|
|
|
);
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
async function doEncrypt(assert, actions = [], keyattrs = {}) {
|
|
|
|
let keyDefaults = { backend: 'transit', id: 'akey', supportedActions: ['encrypt'].concat(actions) };
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
const key = assign({}, keyDefaults, keyattrs);
|
|
|
|
this.set('key', key);
|
|
|
|
this.set('selectedAction', 'encrypt');
|
|
|
|
this.set('storeService.keyActionReturnVal', { ciphertext: 'secret' });
|
2020-03-16 22:48:11 +00:00
|
|
|
await render(hbs`
|
|
|
|
{{transit-key-actions selectedAction=selectedAction key=key}}
|
|
|
|
<div id="modal-wormhole"></div>
|
|
|
|
`);
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2020-03-04 17:36:37 +00:00
|
|
|
find('#plaintext-control .CodeMirror').CodeMirror.setValue('plaintext');
|
2018-09-25 16:28:26 +00:00
|
|
|
await click('button[type="submit"]');
|
|
|
|
assert.deepEqual(
|
|
|
|
this.get('storeService.callArgs'),
|
|
|
|
{
|
|
|
|
action: 'encrypt',
|
|
|
|
backend: 'transit',
|
|
|
|
id: 'akey',
|
|
|
|
payload: {
|
|
|
|
plaintext: encodeString('plaintext'),
|
|
|
|
},
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
2018-09-25 16:28:26 +00:00
|
|
|
'passes expected args to the adapter'
|
|
|
|
);
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2020-03-16 22:48:11 +00:00
|
|
|
assert.equal(find('[data-test-encrypted-value="ciphertext"]').innerText, 'secret');
|
2020-03-04 17:36:37 +00:00
|
|
|
|
2020-03-16 22:48:11 +00:00
|
|
|
// exit modal
|
|
|
|
await click('[data-test-modal-background]');
|
2020-03-04 17:36:37 +00:00
|
|
|
// Encrypt again, with pre-encoded value and checkbox selected
|
2020-03-16 22:48:11 +00:00
|
|
|
const preEncodedValue = encodeString('plaintext');
|
2020-03-04 17:36:37 +00:00
|
|
|
find('#plaintext-control .CodeMirror').CodeMirror.setValue(preEncodedValue);
|
|
|
|
await click('input[data-test-transit-input="encodedBase64"]');
|
|
|
|
await click('button[type="submit"]');
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
this.get('storeService.callArgs'),
|
|
|
|
{
|
|
|
|
action: 'encrypt',
|
|
|
|
backend: 'transit',
|
|
|
|
id: 'akey',
|
|
|
|
payload: {
|
|
|
|
plaintext: preEncodedValue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'passes expected args to the adapter'
|
|
|
|
);
|
2018-09-25 16:28:26 +00:00
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
test('it encrypts', doEncrypt);
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
test('it shows key version selection', async function(assert) {
|
|
|
|
let keyDefaults = { backend: 'transit', id: 'akey', supportedActions: ['encrypt'].concat([]) };
|
|
|
|
let keyattrs = { keysForEncryption: [3, 2, 1], latestVersion: 3 };
|
|
|
|
const key = assign({}, keyDefaults, keyattrs);
|
|
|
|
this.set('key', key);
|
|
|
|
this.set('storeService.keyActionReturnVal', { ciphertext: 'secret' });
|
2020-03-16 22:48:11 +00:00
|
|
|
await render(hbs`
|
|
|
|
{{transit-key-actions selectedAction="encrypt" key=key}}
|
|
|
|
<div id="modal-wormhole"></div>
|
|
|
|
`);
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2020-03-04 17:36:37 +00:00
|
|
|
findAll('.CodeMirror')[0].CodeMirror.setValue('plaintext');
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.equal(findAll('#key_version').length, 1, 'it renders the key version selector');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
await triggerEvent('#key_version', 'change');
|
|
|
|
await click('button[type="submit"]');
|
|
|
|
assert.deepEqual(
|
|
|
|
this.get('storeService.callArgs'),
|
|
|
|
{
|
|
|
|
action: 'encrypt',
|
|
|
|
backend: 'transit',
|
|
|
|
id: 'akey',
|
|
|
|
payload: {
|
|
|
|
plaintext: encodeString('plaintext'),
|
|
|
|
key_version: '0',
|
|
|
|
},
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
2018-09-25 16:28:26 +00:00
|
|
|
'includes key_version in the payload'
|
|
|
|
);
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
test('it hides key version selection', async function(assert) {
|
|
|
|
let keyDefaults = { backend: 'transit', id: 'akey', supportedActions: ['encrypt'].concat([]) };
|
|
|
|
let keyattrs = { keysForEncryption: [1] };
|
|
|
|
const key = assign({}, keyDefaults, keyattrs);
|
|
|
|
this.set('key', key);
|
|
|
|
this.set('storeService.keyActionReturnVal', { ciphertext: 'secret' });
|
2020-03-16 22:48:11 +00:00
|
|
|
await render(hbs`
|
|
|
|
{{transit-key-actions selectedAction="encrypt" key=key}}
|
|
|
|
<div id="modal-wormhole"></div>
|
|
|
|
`);
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2020-03-04 17:36:37 +00:00
|
|
|
// await fillIn('#plaintext', 'plaintext');
|
|
|
|
find('#plaintext-control .CodeMirror').CodeMirror.setValue('plaintext');
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.equal(
|
|
|
|
findAll('#key_version').length,
|
|
|
|
0,
|
|
|
|
'it does not render the selector when there is only one key'
|
|
|
|
);
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2020-03-16 22:48:11 +00:00
|
|
|
test('it does not carry ciphertext value over to decrypt', async function(assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
const plaintext = 'not so secret';
|
|
|
|
await doEncrypt.call(this, assert, ['decrypt']);
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
this.set('storeService.keyActionReturnVal', { plaintext });
|
|
|
|
this.set('selectedAction', 'decrypt');
|
2020-03-04 17:36:37 +00:00
|
|
|
assert.equal(
|
|
|
|
find('#ciphertext-control .CodeMirror').CodeMirror.getValue(),
|
2020-03-16 22:48:11 +00:00
|
|
|
'',
|
|
|
|
'does not prefill ciphertext value'
|
2020-03-04 17:36:37 +00:00
|
|
|
);
|
2018-04-03 14:16:57 +00:00
|
|
|
});
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
const setupExport = async function() {
|
|
|
|
this.set('key', {
|
2018-04-03 14:16:57 +00:00
|
|
|
backend: 'transit',
|
|
|
|
id: 'akey',
|
2018-09-25 16:28:26 +00:00
|
|
|
supportedActions: ['export'],
|
|
|
|
exportKeyTypes: ['encryption'],
|
|
|
|
validKeyVersions: [1],
|
|
|
|
});
|
2020-03-16 22:48:11 +00:00
|
|
|
await render(hbs`
|
|
|
|
{{transit-key-actions key=key}}
|
|
|
|
<div id="modal-wormhole"></div>
|
|
|
|
`);
|
2018-09-25 16:28:26 +00:00
|
|
|
};
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
test('it can export a key:default behavior', async function(assert) {
|
|
|
|
this.set('storeService.rootKeyActionReturnVal', { wrap_info: { token: 'wrapped-token' } });
|
|
|
|
await setupExport.call(this);
|
|
|
|
await click('button[type="submit"]');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
this.get('storeService.callArgs'),
|
|
|
|
{
|
|
|
|
action: 'export',
|
|
|
|
backend: 'transit',
|
|
|
|
id: 'akey',
|
|
|
|
payload: {
|
|
|
|
param: ['encryption'],
|
|
|
|
},
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
2018-09-25 16:28:26 +00:00
|
|
|
'passes expected args to the adapter'
|
|
|
|
);
|
|
|
|
assert.equal(this.get('storeService.callArgsOptions.wrapTTL'), '30m', 'passes value for wrapTTL');
|
2020-03-16 22:48:11 +00:00
|
|
|
assert.equal(find('[data-test-encrypted-value="export"]').innerText, 'wrapped-token', 'wraps by default');
|
2018-09-25 16:28:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('it can export a key:unwrapped behavior', async function(assert) {
|
|
|
|
const response = { keys: { a: 'key' } };
|
|
|
|
this.set('storeService.keyActionReturnVal', response);
|
|
|
|
await setupExport.call(this);
|
|
|
|
await click('#wrap-response');
|
|
|
|
await triggerEvent('#wrap-response', 'change');
|
|
|
|
await click('button[type="submit"]');
|
2020-03-16 22:48:11 +00:00
|
|
|
assert.dom('.modal.is-active').exists('Modal opens after export');
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.deepEqual(
|
2020-03-16 22:48:11 +00:00
|
|
|
find('.modal [data-test-encrypted-value="export"]').innerText,
|
|
|
|
JSON.stringify(response, null, 2),
|
2018-09-25 16:28:26 +00:00
|
|
|
'prints json response'
|
|
|
|
);
|
|
|
|
});
|
2018-05-21 18:50:54 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
test('it can export a key: unwrapped, single version', async function(assert) {
|
|
|
|
const response = { keys: { a: 'key' } };
|
|
|
|
this.set('storeService.keyActionReturnVal', response);
|
|
|
|
await setupExport.call(this);
|
|
|
|
await click('#wrap-response');
|
|
|
|
await triggerEvent('#wrap-response', 'change');
|
|
|
|
await click('#exportVersion');
|
|
|
|
await triggerEvent('#exportVersion', 'change');
|
|
|
|
await click('button[type="submit"]');
|
2020-03-16 22:48:11 +00:00
|
|
|
assert.dom('.modal.is-active').exists('Modal opens after export');
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.deepEqual(
|
2020-03-16 22:48:11 +00:00
|
|
|
find('.modal [data-test-encrypted-value="export"]').innerText,
|
|
|
|
JSON.stringify(response, null, 2),
|
2018-09-25 16:28:26 +00:00
|
|
|
'prints json response'
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
this.get('storeService.callArgs'),
|
|
|
|
{
|
|
|
|
action: 'export',
|
|
|
|
backend: 'transit',
|
|
|
|
id: 'akey',
|
|
|
|
payload: {
|
|
|
|
param: ['encryption', 1],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'passes expected args to the adapter'
|
|
|
|
);
|
2018-05-21 18:50:54 +00:00
|
|
|
});
|
2018-09-25 16:28:26 +00:00
|
|
|
|
|
|
|
test('it includes algorithm param for HMAC', async function(assert) {
|
|
|
|
this.set('key', {
|
2018-05-21 18:50:54 +00:00
|
|
|
backend: 'transit',
|
|
|
|
id: 'akey',
|
2018-09-25 16:28:26 +00:00
|
|
|
supportedActions: ['hmac'],
|
|
|
|
validKeyVersions: [1],
|
|
|
|
});
|
2020-03-16 22:48:11 +00:00
|
|
|
await render(hbs`
|
|
|
|
{{transit-key-actions key=key}}
|
|
|
|
<div id="modal-wormhole"></div>
|
|
|
|
`);
|
2018-09-25 16:28:26 +00:00
|
|
|
await fillIn('#algorithm', 'sha2-384');
|
|
|
|
await blur('#algorithm');
|
|
|
|
await click('button[type="submit"]');
|
|
|
|
assert.deepEqual(
|
|
|
|
this.get('storeService.callArgs'),
|
|
|
|
{
|
|
|
|
action: 'hmac',
|
|
|
|
backend: 'transit',
|
|
|
|
id: 'akey',
|
|
|
|
payload: {
|
|
|
|
algorithm: 'sha2-384',
|
|
|
|
},
|
2018-05-21 18:50:54 +00:00
|
|
|
},
|
2018-09-25 16:28:26 +00:00
|
|
|
'passes expected args to the adapter'
|
|
|
|
);
|
|
|
|
});
|
2018-05-21 18:50:54 +00:00
|
|
|
});
|