2018-09-25 16:28:26 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { computed, set } from '@ember/object';
|
|
|
|
import Component from '@ember/component';
|
2018-04-03 14:16:57 +00:00
|
|
|
|
|
|
|
const MODEL_TYPES = {
|
|
|
|
'ssh-sign': {
|
|
|
|
model: 'ssh-sign',
|
|
|
|
},
|
|
|
|
'ssh-creds': {
|
|
|
|
model: 'ssh-otp-credential',
|
|
|
|
title: 'Generate SSH Credentials',
|
|
|
|
},
|
|
|
|
'aws-creds': {
|
2018-08-28 00:54:30 +00:00
|
|
|
model: 'aws-credential',
|
|
|
|
title: 'Generate AWS Credentials',
|
2018-04-03 14:16:57 +00:00
|
|
|
backIsListLink: true,
|
|
|
|
},
|
|
|
|
'pki-issue': {
|
|
|
|
model: 'pki-certificate',
|
|
|
|
title: 'Issue Certificate',
|
|
|
|
},
|
|
|
|
'pki-sign': {
|
|
|
|
model: 'pki-certificate-sign',
|
|
|
|
title: 'Sign Certificate',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-08-28 00:54:30 +00:00
|
|
|
export default Component.extend({
|
2018-09-25 16:28:26 +00:00
|
|
|
wizard: service(),
|
|
|
|
store: service(),
|
|
|
|
router: service(),
|
2018-04-03 14:16:57 +00:00
|
|
|
// set on the component
|
2018-09-25 16:28:26 +00:00
|
|
|
backendType: null,
|
|
|
|
backendPath: null,
|
|
|
|
roleName: null,
|
2018-04-03 14:16:57 +00:00
|
|
|
action: null,
|
|
|
|
|
|
|
|
model: null,
|
|
|
|
loading: false,
|
|
|
|
emptyData: '{\n}',
|
|
|
|
|
|
|
|
modelForType() {
|
|
|
|
const type = this.get('options');
|
|
|
|
if (type) {
|
|
|
|
return type.model;
|
|
|
|
}
|
|
|
|
// if we don't have a mode for that type then redirect them back to the backend list
|
2018-09-25 16:28:26 +00:00
|
|
|
this.get('router').transitionTo('vault.cluster.secrets.backend.list-root', this.get('backendPath'));
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
options: computed('action', 'backendType', function() {
|
2018-04-03 14:16:57 +00:00
|
|
|
const action = this.get('action') || 'creds';
|
2018-09-25 16:28:26 +00:00
|
|
|
return MODEL_TYPES[`${this.get('backendType')}-${action}`];
|
2018-04-03 14:16:57 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.createOrReplaceModel();
|
|
|
|
},
|
|
|
|
|
2018-08-28 05:03:55 +00:00
|
|
|
didReceiveAttrs() {
|
|
|
|
if (this.get('wizard.featureState') === 'displayRole') {
|
|
|
|
this.get('wizard').transitionFeatureMachine(
|
|
|
|
this.get('wizard.featureState'),
|
|
|
|
'CONTINUE',
|
2018-09-25 16:28:26 +00:00
|
|
|
this.get('backendType')
|
2018-08-28 05:03:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
willDestroy() {
|
|
|
|
this.get('model').unloadRecord();
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
createOrReplaceModel() {
|
|
|
|
const modelType = this.modelForType();
|
|
|
|
const model = this.get('model');
|
2018-09-25 16:28:26 +00:00
|
|
|
const roleName = this.get('roleName');
|
|
|
|
const backendPath = this.get('backendPath');
|
2018-04-03 14:16:57 +00:00
|
|
|
if (!modelType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (model) {
|
|
|
|
model.unloadRecord();
|
|
|
|
}
|
|
|
|
const attrs = {
|
2018-09-25 16:28:26 +00:00
|
|
|
role: {
|
|
|
|
backend: backendPath,
|
|
|
|
name: roleName,
|
|
|
|
},
|
|
|
|
id: `${backendPath}-${roleName}`,
|
2018-04-03 14:16:57 +00:00
|
|
|
};
|
|
|
|
const newModel = this.get('store').createRecord(modelType, attrs);
|
|
|
|
this.set('model', newModel);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
create() {
|
2018-08-28 00:54:30 +00:00
|
|
|
let model = this.get('model');
|
2018-04-03 14:16:57 +00:00
|
|
|
this.set('loading', true);
|
2018-08-28 05:03:55 +00:00
|
|
|
this.model
|
|
|
|
.save()
|
|
|
|
.catch(() => {
|
|
|
|
if (this.get('wizard.featureState') === 'credentials') {
|
|
|
|
this.get('wizard').transitionFeatureMachine(
|
|
|
|
this.get('wizard.featureState'),
|
|
|
|
'ERROR',
|
2018-09-25 16:28:26 +00:00
|
|
|
this.get('backendType')
|
2018-08-28 05:03:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
model.set('hasGenerated', true);
|
|
|
|
this.set('loading', false);
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
codemirrorUpdated(attr, val, codemirror) {
|
|
|
|
codemirror.performLint();
|
|
|
|
const hasErrors = codemirror.state.lint.marked.length > 0;
|
|
|
|
|
|
|
|
if (!hasErrors) {
|
2018-08-28 00:54:30 +00:00
|
|
|
set(this.get('model'), attr, JSON.parse(val));
|
2018-04-03 14:16:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
newModel() {
|
|
|
|
this.createOrReplaceModel();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|