2018-09-25 16:28:26 +00:00
|
|
|
import { assign } from '@ember/polyfills';
|
|
|
|
import { copy } from 'ember-copy';
|
|
|
|
import { assert } from '@ember/debug';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import Component from '@ember/component';
|
|
|
|
import { set, get, computed } from '@ember/object';
|
2018-04-03 14:16:57 +00:00
|
|
|
|
|
|
|
const TRANSIT_PARAMS = {
|
2018-04-09 21:50:36 +00:00
|
|
|
hash_algorithm: 'sha2-256',
|
2018-05-21 18:50:54 +00:00
|
|
|
algorithm: 'sha2-256',
|
2018-04-09 21:50:36 +00:00
|
|
|
signature_algorithm: 'pss',
|
2018-04-03 14:16:57 +00:00
|
|
|
bits: 256,
|
|
|
|
bytes: 32,
|
|
|
|
ciphertext: null,
|
|
|
|
context: null,
|
|
|
|
format: 'base64',
|
|
|
|
hmac: null,
|
|
|
|
input: null,
|
|
|
|
key_version: 0,
|
|
|
|
keys: null,
|
|
|
|
nonce: null,
|
|
|
|
param: 'wrapped',
|
|
|
|
prehashed: false,
|
|
|
|
plaintext: null,
|
|
|
|
random_bytes: null,
|
|
|
|
signature: null,
|
|
|
|
sum: null,
|
|
|
|
exportKeyType: null,
|
|
|
|
exportKeyVersion: null,
|
|
|
|
wrappedToken: null,
|
|
|
|
valid: null,
|
|
|
|
plaintextOriginal: null,
|
|
|
|
didDecode: false,
|
2018-04-09 21:50:36 +00:00
|
|
|
verification: 'Signature',
|
2018-04-03 14:16:57 +00:00
|
|
|
};
|
|
|
|
const PARAMS_FOR_ACTION = {
|
2018-04-09 21:50:36 +00:00
|
|
|
sign: ['input', 'hash_algorithm', 'key_version', 'prehashed', 'signature_algorithm'],
|
|
|
|
verify: ['input', 'hmac', 'signature', 'hash_algorithm', 'prehashed'],
|
2018-05-21 18:50:54 +00:00
|
|
|
hmac: ['input', 'algorithm', 'key_version'],
|
2018-04-03 14:16:57 +00:00
|
|
|
encrypt: ['plaintext', 'context', 'nonce', 'key_version'],
|
|
|
|
decrypt: ['ciphertext', 'context', 'nonce'],
|
|
|
|
rewrap: ['ciphertext', 'context', 'nonce', 'key_version'],
|
|
|
|
};
|
2018-09-25 16:28:26 +00:00
|
|
|
export default Component.extend(TRANSIT_PARAMS, {
|
|
|
|
store: service(),
|
2018-04-03 14:16:57 +00:00
|
|
|
|
|
|
|
// public attrs
|
|
|
|
selectedAction: null,
|
|
|
|
key: null,
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
onRefresh() {},
|
2018-04-03 14:16:57 +00:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
if (get(this, 'selectedAction')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
set(this, 'selectedAction', get(this, 'key.supportedActions.firstObject'));
|
2018-09-25 16:28:26 +00:00
|
|
|
assert('`key` is required for `' + this.toString() + '`.', this.getModelInfo());
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.checkAction();
|
|
|
|
if (get(this, 'selectedAction') === 'export') {
|
|
|
|
this.setExportKeyDefaults();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setExportKeyDefaults() {
|
|
|
|
const exportKeyType = get(this, 'key.exportKeyTypes.firstObject');
|
|
|
|
const exportKeyVersion = get(this, 'key.validKeyVersions.lastObject');
|
|
|
|
this.setProperties({
|
|
|
|
exportKeyType,
|
|
|
|
exportKeyVersion,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
keyIsRSA: computed('key.type', function() {
|
2018-04-09 21:50:36 +00:00
|
|
|
let type = get(this, 'key.type');
|
2020-02-15 22:40:50 +00:00
|
|
|
return type === 'rsa-2048' || type === 'rsa-3072' || type === 'rsa-4096';
|
2018-04-09 21:50:36 +00:00
|
|
|
}),
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
getModelInfo() {
|
|
|
|
const model = get(this, 'key') || get(this, 'backend');
|
|
|
|
if (!model) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const backend = get(model, 'backend') || get(model, 'id');
|
|
|
|
const id = get(model, 'id');
|
|
|
|
|
|
|
|
return {
|
|
|
|
backend,
|
|
|
|
id,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
checkAction() {
|
|
|
|
const currentAction = get(this, 'selectedAction');
|
|
|
|
const oldAction = get(this, 'oldSelectedAction');
|
|
|
|
|
|
|
|
this.resetParams(oldAction, currentAction);
|
|
|
|
set(this, 'oldSelectedAction', currentAction);
|
|
|
|
},
|
|
|
|
|
|
|
|
resetParams(oldAction, action) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let params = copy(TRANSIT_PARAMS);
|
2018-04-03 14:16:57 +00:00
|
|
|
let paramsToKeep;
|
|
|
|
let clearWithoutCheck =
|
|
|
|
!oldAction ||
|
|
|
|
// don't save values from datakey
|
|
|
|
oldAction === 'datakey' ||
|
|
|
|
// can rewrap signatures — using that as a ciphertext later would be problematic
|
|
|
|
(oldAction === 'rewrap' && !get(this, 'key.supportsEncryption'));
|
|
|
|
|
|
|
|
if (!clearWithoutCheck && action) {
|
|
|
|
paramsToKeep = PARAMS_FOR_ACTION[action];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paramsToKeep) {
|
|
|
|
paramsToKeep.forEach(param => delete params[param]);
|
|
|
|
}
|
|
|
|
//resets params still left in the object to defaults
|
2020-02-18 16:00:50 +00:00
|
|
|
this.clearErrors();
|
2018-04-03 14:16:57 +00:00
|
|
|
this.setProperties(params);
|
|
|
|
if (action === 'export') {
|
|
|
|
this.setExportKeyDefaults();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleError(e) {
|
|
|
|
this.set('errors', e.errors);
|
|
|
|
},
|
|
|
|
|
2020-02-18 16:00:50 +00:00
|
|
|
clearErrors() {
|
|
|
|
this.set('errors', null);
|
|
|
|
},
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
handleSuccess(resp, options, action) {
|
|
|
|
let props = {};
|
|
|
|
if (resp && resp.data) {
|
|
|
|
if (action === 'export' && resp.data.keys) {
|
|
|
|
const { keys, type, name } = resp.data;
|
|
|
|
resp.data.keys = { keys, type, name };
|
|
|
|
}
|
2018-09-25 16:28:26 +00:00
|
|
|
props = assign({}, props, resp.data);
|
2018-04-03 14:16:57 +00:00
|
|
|
}
|
|
|
|
if (options.wrapTTL) {
|
2018-09-25 16:28:26 +00:00
|
|
|
props = assign({}, props, { wrappedToken: resp.wrap_info.token });
|
2018-04-03 14:16:57 +00:00
|
|
|
}
|
|
|
|
this.setProperties(props);
|
|
|
|
if (action === 'rotate') {
|
2018-09-25 16:28:26 +00:00
|
|
|
this.get('onRefresh')();
|
2018-04-03 14:16:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
compactData(data) {
|
2018-04-09 21:50:36 +00:00
|
|
|
let type = get(this, 'key.type');
|
2020-02-15 22:40:50 +00:00
|
|
|
let isRSA = type === 'rsa-2048' || type === 'rsa-3072' || type === 'rsa-4096';
|
2018-04-03 14:16:57 +00:00
|
|
|
return Object.keys(data).reduce((result, key) => {
|
2018-04-09 21:50:36 +00:00
|
|
|
if (key === 'signature_algorithm' && !isRSA) {
|
|
|
|
return result;
|
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
if (data[key]) {
|
|
|
|
result[key] = data[key];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, {});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
onActionChange(action) {
|
|
|
|
set(this, 'selectedAction', action);
|
|
|
|
this.checkAction();
|
|
|
|
},
|
|
|
|
|
|
|
|
onClear() {
|
|
|
|
this.resetParams(null, get(this, 'selectedAction'));
|
|
|
|
},
|
|
|
|
|
|
|
|
clearParams(params) {
|
|
|
|
const arr = Array.isArray(params) ? params : [params];
|
|
|
|
arr.forEach(param => this.set(param, null));
|
|
|
|
},
|
|
|
|
|
|
|
|
doSubmit(data, options = {}) {
|
|
|
|
const { backend, id } = this.getModelInfo();
|
|
|
|
const action = this.get('selectedAction');
|
|
|
|
let payload = data ? this.compactData(data) : null;
|
|
|
|
this.setProperties({
|
|
|
|
errors: null,
|
|
|
|
result: null,
|
|
|
|
});
|
|
|
|
this.get('store')
|
|
|
|
.adapterFor('transit-key')
|
|
|
|
.keyAction(action, { backend, id, payload }, options)
|
|
|
|
.then(
|
|
|
|
resp => this.handleSuccess(resp, options, action),
|
|
|
|
(...errArgs) => this.handleError(...errArgs)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|