open-vault/ui/app/components/mount-backend-form.js

190 lines
5.4 KiB
JavaScript
Raw Normal View History

import { inject as service } from '@ember/service';
import { computed } from '@ember/object';
import Component from '@ember/component';
2018-04-03 14:16:57 +00:00
import { task } from 'ember-concurrency';
import { methods } from 'vault/helpers/mountable-auth-methods';
2018-08-28 05:03:55 +00:00
import { engines } from 'vault/helpers/mountable-secret-engines';
2018-04-03 14:16:57 +00:00
const METHODS = methods();
2018-08-28 05:03:55 +00:00
const ENGINES = engines();
2018-04-03 14:16:57 +00:00
2018-08-28 05:03:55 +00:00
export default Component.extend({
store: service(),
wizard: service(),
flashMessages: service(),
2018-04-03 14:16:57 +00:00
/*
* @param Function
* @public
*
* Optional param to call a function upon successfully mounting a backend
*
*/
onMountSuccess() {},
onConfigError() {},
2018-04-03 14:16:57 +00:00
/*
* @param String
* @public
* the type of backend we want to mount
* defaults to `auth`
*
*/
mountType: 'auth',
/*
*
* @param DS.Model
* @private
* Ember Data model corresponding to the `mountType`.
* Created and set during `init`
*
*/
mountModel: null,
2018-08-28 05:03:55 +00:00
showConfig: false,
2018-04-03 14:16:57 +00:00
init() {
this._super(...arguments);
const type = this.get('mountType');
const modelType = type === 'secret' ? 'secret-engine' : 'auth-method';
const model = this.get('store').createRecord(modelType);
this.set('mountModel', model);
},
2018-08-28 05:03:55 +00:00
mountTypes: computed('mountType', function() {
return this.get('mountType') === 'secret' ? ENGINES : METHODS;
}),
2018-04-03 14:16:57 +00:00
willDestroy() {
// if unsaved, we want to unload so it doesn't show up in the auth mount list
this.get('mountModel').rollbackAttributes();
},
getConfigModelType(methodType) {
2018-08-28 05:03:55 +00:00
let mountType = this.get('mountType');
// will be something like secret-aws
// or auth-azure
let key = `${mountType}-${methodType}`;
let noConfig = ['auth-approle', 'auth-alicloud'];
if (mountType === 'secret' || noConfig.includes(key)) {
2018-04-03 14:16:57 +00:00
return;
}
if (methodType === 'aws') {
return 'auth-config/aws/client';
}
return `auth-config/${methodType}`;
},
changeConfigModel(methodType) {
2018-08-28 05:03:55 +00:00
let mount = this.get('mountModel');
if (this.get('mountType') === 'secret') {
return;
}
let configRef = mount.hasMany('authConfigs').value();
let currentConfig = configRef && configRef.get('firstObject');
2018-04-03 14:16:57 +00:00
if (currentConfig) {
// rollbackAttributes here will remove the the config model from the store
// because `isNew` will be true
currentConfig.rollbackAttributes();
2018-08-28 05:03:55 +00:00
currentConfig.unloadRecord();
2018-04-03 14:16:57 +00:00
}
2018-08-28 05:03:55 +00:00
let configType = this.getConfigModelType(methodType);
2018-04-03 14:16:57 +00:00
if (!configType) return;
2018-08-28 05:03:55 +00:00
let config = this.get('store').createRecord(configType);
2018-04-03 14:16:57 +00:00
config.set('backend', mount);
},
checkPathChange(type) {
2018-08-28 05:03:55 +00:00
let mount = this.get('mountModel');
let currentPath = mount.get('path');
let list = this.get('mountTypes');
2018-04-03 14:16:57 +00:00
// if the current path matches a type (meaning the user hasn't altered it),
// change it here to match the new type
2018-08-28 05:03:55 +00:00
let isUnchanged = list.findBy('type', currentPath);
if (!currentPath || isUnchanged) {
2018-04-03 14:16:57 +00:00
mount.set('path', type);
}
},
mountBackend: task(function*() {
const mountModel = this.get('mountModel');
const { type, path } = mountModel.getProperties('type', 'path');
try {
yield mountModel.save();
} catch (err) {
// err will display via model state
return;
}
this.get('flashMessages').success(
`Successfully mounted ${type} ${this.get('mountType')} method at ${path}.`
);
2018-08-28 05:03:55 +00:00
if (this.get('mountType') === 'secret') {
yield this.get('onMountSuccess')(type, path);
return;
}
2018-04-03 14:16:57 +00:00
yield this.get('saveConfig').perform(mountModel);
}).drop(),
advanceWizard() {
this.get('wizard').transitionFeatureMachine(
this.get('wizard.featureState'),
'CONTINUE',
this.get('mountModel').get('type')
);
},
2018-04-03 14:16:57 +00:00
saveConfig: task(function*(mountModel) {
const configRef = mountModel.hasMany('authConfigs').value();
const { type, path } = mountModel.getProperties('type', 'path');
if (!configRef) {
this.advanceWizard();
yield this.get('onMountSuccess')(type, path);
return;
}
const config = configRef.get('firstObject');
2018-04-03 14:16:57 +00:00
try {
if (config && Object.keys(config.changedAttributes()).length) {
yield config.save();
this.advanceWizard();
2018-04-03 14:16:57 +00:00
this.get('flashMessages').success(
`The config for ${type} ${this.get('mountType')} method at ${path} was saved successfully.`
);
}
2018-08-28 05:03:55 +00:00
yield this.get('onMountSuccess')(type, path);
2018-04-03 14:16:57 +00:00
} catch (err) {
this.get('flashMessages').danger(
`There was an error saving the configuration for ${type} ${this.get(
'mountType'
)} method at ${path}. ${err.errors.join(' ')}`
);
yield this.get('onConfigError')(mountModel.id);
}
}).drop(),
actions: {
onTypeChange(path, value) {
if (path === 'type') {
2018-08-28 05:03:55 +00:00
this.get('wizard').set('componentState', value);
2018-04-03 14:16:57 +00:00
this.changeConfigModel(value);
this.checkPathChange(value);
}
},
2018-08-28 05:03:55 +00:00
toggleShowConfig(value) {
this.set('showConfig', value);
if (value === true && this.get('wizard.featureState') === 'idle') {
this.get('wizard').transitionFeatureMachine(
this.get('wizard.featureState'),
'CONTINUE',
this.get('mountModel').get('type')
);
} else {
this.get('wizard').transitionFeatureMachine(
this.get('wizard.featureState'),
'RESET',
this.get('mountModel').get('type')
);
}
},
2018-04-03 14:16:57 +00:00
},
});