2022-04-20 18:40:27 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
2022-04-26 14:23:31 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import { waitFor } from '@ember/test-waiters';
|
2022-04-20 18:40:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @module KeymgmtKeyEdit
|
|
|
|
* KeymgmtKeyEdit components are used to display KeyMgmt Secrets engine UI for Key items
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* <KeymgmtKeyEdit @model={model} @mode="show" @tab="versions" />
|
|
|
|
* ```
|
|
|
|
* @param {object} model - model is the data from the store
|
|
|
|
* @param {string} [mode=show] - mode controls which view is shown on the component
|
|
|
|
* @param {string} [tab=details] - Options are "details" or "versions" for the show mode only
|
|
|
|
*/
|
|
|
|
|
|
|
|
const LIST_ROOT_ROUTE = 'vault.cluster.secrets.backend.list-root';
|
|
|
|
const SHOW_ROUTE = 'vault.cluster.secrets.backend.show';
|
|
|
|
export default class KeymgmtKeyEdit extends Component {
|
|
|
|
@service store;
|
|
|
|
@service router;
|
|
|
|
@service flashMessages;
|
|
|
|
@tracked isDeleteModalOpen = false;
|
|
|
|
|
|
|
|
get mode() {
|
|
|
|
return this.args.mode || 'show';
|
|
|
|
}
|
|
|
|
|
|
|
|
get keyAdapter() {
|
|
|
|
return this.store.adapterFor('keymgmt/key');
|
|
|
|
}
|
|
|
|
|
2022-04-26 14:23:31 +00:00
|
|
|
get isMutable() {
|
|
|
|
return ['create', 'edit'].includes(this.args.mode);
|
2022-04-20 18:40:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 14:23:31 +00:00
|
|
|
get isCreating() {
|
|
|
|
return this.args.mode === 'create';
|
2022-04-20 18:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-04-26 14:23:31 +00:00
|
|
|
toggleModal(bool) {
|
|
|
|
this.isDeleteModalOpen = bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
@waitFor
|
|
|
|
*saveKey(evt) {
|
2022-04-20 18:40:27 +00:00
|
|
|
evt.preventDefault();
|
2022-04-26 14:23:31 +00:00
|
|
|
const { model } = this.args;
|
|
|
|
try {
|
|
|
|
yield model.save();
|
|
|
|
this.router.transitionTo(SHOW_ROUTE, model.name);
|
|
|
|
} catch (error) {
|
2022-05-20 15:41:24 +00:00
|
|
|
let errorMessage = error;
|
|
|
|
if (error.errors) {
|
|
|
|
// if errors come directly from API they will be in this shape
|
|
|
|
errorMessage = error.errors.join('. ');
|
|
|
|
}
|
|
|
|
this.flashMessages.danger(errorMessage);
|
|
|
|
if (!error.errors) {
|
|
|
|
// If error was custom from save, only partial fail
|
|
|
|
// so it's safe to show the key
|
|
|
|
this.router.transitionTo(SHOW_ROUTE, model.name);
|
|
|
|
}
|
2022-04-26 14:23:31 +00:00
|
|
|
}
|
2022-04-20 18:40:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 15:41:24 +00:00
|
|
|
@task
|
|
|
|
@waitFor
|
|
|
|
*removeKey() {
|
2022-04-26 14:23:31 +00:00
|
|
|
try {
|
2022-05-20 15:41:24 +00:00
|
|
|
yield this.keyAdapter.removeFromProvider(this.args.model);
|
|
|
|
yield this.args.model.reload();
|
2022-04-26 14:23:31 +00:00
|
|
|
this.flashMessages.success('Key has been successfully removed from provider');
|
|
|
|
} catch (error) {
|
|
|
|
this.flashMessages.danger(error.errors?.join('. '));
|
|
|
|
}
|
2022-04-20 18:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
deleteKey() {
|
|
|
|
const secret = this.args.model;
|
|
|
|
const backend = secret.backend;
|
|
|
|
secret
|
|
|
|
.destroyRecord()
|
|
|
|
.then(() => {
|
2022-04-26 14:23:31 +00:00
|
|
|
this.router.transitionTo(LIST_ROOT_ROUTE, backend);
|
2022-04-20 18:40:27 +00:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.flashMessages.danger(e.errors?.join('. '));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-20 15:41:24 +00:00
|
|
|
@task
|
|
|
|
@waitFor
|
|
|
|
*rotateKey() {
|
|
|
|
const id = this.args.model.name;
|
|
|
|
const backend = this.args.model.backend;
|
2022-04-20 18:40:27 +00:00
|
|
|
const adapter = this.keyAdapter;
|
2022-05-20 15:41:24 +00:00
|
|
|
yield adapter
|
2022-04-20 18:40:27 +00:00
|
|
|
.rotateKey(backend, id)
|
|
|
|
.then(() => {
|
|
|
|
this.flashMessages.success(`Success: ${id} connection was rotated`);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.flashMessages.danger(e.errors);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|