Bug Fix and Glimmerize secret-edit component (#14941)

* inital glimmerize

* wip

* wip

* wip

* fix maybeQueryRecord

* fix

* fix

* fix test

* cleanup

* add changelog

* clean up
This commit is contained in:
Angel Garbarino 2022-04-07 11:07:33 -06:00 committed by GitHub
parent ca45941f41
commit 55afadfc35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 124 deletions

3
changelog/14941.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
ui: Fix issue with KV not recomputing model when you changed versions.
```

View File

@ -12,7 +12,6 @@
* @secretDataIsAdvanced={{secretDataIsAdvanced}} * @secretDataIsAdvanced={{secretDataIsAdvanced}}
* @showAdvancedMode={{showAdvancedMode}} * @showAdvancedMode={{showAdvancedMode}}
* @modelForData={{this.modelForData}} * @modelForData={{this.modelForData}}
* @navToNearestAncestor={{this.navToNearestAncestor}}
* @canUpdateSecretData={{canUpdateSecretData}} * @canUpdateSecretData={{canUpdateSecretData}}
* @codemirrorString={{codemirrorString}} * @codemirrorString={{codemirrorString}}
* @wrappedData={{wrappedData}} * @wrappedData={{wrappedData}}
@ -30,7 +29,6 @@
* @param {boolean} secretDataIsAdvanced - used to determine if show JSON toggle * @param {boolean} secretDataIsAdvanced - used to determine if show JSON toggle
* @param {boolean} showAdvacnedMode - used for JSON toggle * @param {boolean} showAdvacnedMode - used for JSON toggle
* @param {object} modelForData - a modified version of the model with secret data * @param {object} modelForData - a modified version of the model with secret data
* @param {string} navToNearestAncestor - route to nav to if press cancel
* @param {boolean} canUpdateSecretData - permissions that show the create new version button or not. * @param {boolean} canUpdateSecretData - permissions that show the create new version button or not.
* @param {string} codemirrorString - used to copy the JSON * @param {string} codemirrorString - used to copy the JSON
* @param {object} wrappedData - when copy the data it's the token of the secret returned. * @param {object} wrappedData - when copy the data it's the token of the secret returned.

View File

@ -1,84 +1,63 @@
/* eslint ember/no-computed-properties-in-native-classes: 'warn' */
/** /**
* @module SecretEdit * @module SecretEdit
* SecretEdit component manages the secret and model data, and displays either the create, update, empty state or show view of a KV secret. * SecretEdit component manages the secret and model data, and displays either the create, update, empty state or show view of a KV secret.
* *
* @example * @example
* ```js * ```js
* <SecretEdit @model={{model}}/> * <SecretEdit @model={{model}} @mode="create" @baseKey={{this.baseKey}} @key={{this.model}} @initialKey={{this.initialKey}} @onRefresh={{action "refresh"}} @onToggleAdvancedEdit={{action "toggleAdvancedEdit"}} @preferAdvancedEdit={{this.preferAdvancedEdit}}/>
* ``` * ```
/ /This component is initialized from the secret-edit-layout.hbs file
* @param {object} model - Model returned from route secret-v2 * @param {object} model - Model returned from secret-v2 which is generated in the secret-edit route
* @param {string} mode - Edit, create, etc.
* @param {string} baseKey - Provided for navigation.
* @param {object} key - Passed through, copy of the model.
* @param {string} initialKey - model's name.
* @param {function} onRefresh - action that refreshes the model
* @param {function} onToggleAdvancedEdit - changes the preferAdvancedEdit to true or false
* @param {boolean} preferAdvancedEdit - property set from the controller of show/edit/create route passed in through secret-edit-layout
*/ */
import { inject as service } from '@ember/service'; import { inject as service } from '@ember/service';
import Component from '@ember/component'; import Component from '@glimmer/component';
import { computed } from '@ember/object'; import { action } from '@ember/object';
import { alias, or } from '@ember/object/computed'; import { tracked } from '@glimmer/tracking';
import FocusOnInsertMixin from 'vault/mixins/focus-on-insert';
import WithNavToNearestAncestor from 'vault/mixins/with-nav-to-nearest-ancestor';
import KVObject from 'vault/lib/kv-object'; import KVObject from 'vault/lib/kv-object';
import { maybeQueryRecord } from 'vault/macros/maybe-query-record'; import { maybeQueryRecord } from 'vault/macros/maybe-query-record';
import { alias, or } from '@ember/object/computed';
export default Component.extend(FocusOnInsertMixin, WithNavToNearestAncestor, { export default class SecretEdit extends Component {
wizard: service(), @service wizard;
store: service(), @service store;
// a key model @tracked secretData = null;
key: null, @tracked isV2 = false;
model: null, @tracked codemirrorString = null;
// a value to pre-fill the key input - this is populated by the corresponding constructor() {
// 'initialKey' queryParam super(...arguments);
initialKey: null, let secrets = this.args.model.secretData;
if (!secrets && this.args.model.selectedVersion) {
// set in the route's setupController hook this.isV2 = true;
mode: null, secrets = this.args.model.belongsTo('selectedVersion').value().secretData;
secretData: null,
// called with a bool indicating if there's been a change in the secretData and customMetadata
onDataChange() {},
onRefresh() {},
onToggleAdvancedEdit() {},
// did user request advanced mode
preferAdvancedEdit: false,
// use a named action here so we don't have to pass one in
// this will bubble to the route
toggleAdvancedEdit: 'toggleAdvancedEdit',
codemirrorString: null,
isV2: false,
init() {
this._super(...arguments);
let secrets = this.model.secretData;
if (!secrets && this.model.selectedVersion) {
this.set('isV2', true);
secrets = this.model.belongsTo('selectedVersion').value().secretData;
} }
const data = KVObject.create({ content: [] }).fromJSON(secrets); const data = KVObject.create({ content: [] }).fromJSON(secrets);
this.set('secretData', data); this.secretData = data;
this.set('codemirrorString', data.toJSONString()); this.codemirrorString = data.toJSONString();
if (data.isAdvanced()) { if (this.wizard.featureState === 'details' && this.args.mode === 'create') {
this.set('preferAdvancedEdit', true); let engine = this.args.model.backend.includes('kv') ? 'kv' : this.args.model.backend;
}
if (this.wizard.featureState === 'details' && this.mode === 'create') {
let engine = this.model.backend.includes('kv') ? 'kv' : this.model.backend;
this.wizard.transitionFeatureMachine('details', 'CONTINUE', engine); this.wizard.transitionFeatureMachine('details', 'CONTINUE', engine);
} }
}, }
checkSecretCapabilities: maybeQueryRecord( @maybeQueryRecord(
'capabilities', 'capabilities',
(context) => { (context) => {
if (!context.model || context.mode === 'create') { if (!context.args.model || context.args.mode === 'create') {
return; return;
} }
let backend = context.isV2 ? context.get('model.engine.id') : context.model.backend; let backend = context.isV2 ? context.args.model.engine.id : context.args.model.backend;
let id = context.model.id; let id = context.args.model.id;
let path = context.isV2 ? `${backend}/data/${id}` : `${backend}/${id}`; let path = context.isV2 ? `${backend}/data/${id}` : `${backend}/${id}`;
return { return {
id: path, id: path,
@ -88,17 +67,18 @@ export default Component.extend(FocusOnInsertMixin, WithNavToNearestAncestor, {
'model', 'model',
'model.id', 'model.id',
'mode' 'mode'
), )
canUpdateSecretData: alias('checkSecretCapabilities.canUpdate'), checkSecretCapabilities;
canReadSecretData: alias('checkSecretCapabilities.canRead'), @alias('checkSecretCapabilities.canUpdate') canUpdateSecretData;
@alias('checkSecretCapabilities.canRead') canReadSecretData;
checkMetadataCapabilities: maybeQueryRecord( @maybeQueryRecord(
'capabilities', 'capabilities',
(context) => { (context) => {
if (!context.model || !context.isV2) { if (!context.args.model || !context.isV2) {
return; return;
} }
let backend = context.model.backend; let backend = context.args.model.backend;
let path = `${backend}/metadata/`; let path = `${backend}/metadata/`;
return { return {
id: path, id: path,
@ -108,60 +88,59 @@ export default Component.extend(FocusOnInsertMixin, WithNavToNearestAncestor, {
'model', 'model',
'model.id', 'model.id',
'mode' 'mode'
), )
canDeleteSecretMetadata: alias('checkMetadataCapabilities.canDelete'), checkMetadataCapabilities;
canUpdateSecretMetadata: alias('checkMetadataCapabilities.canUpdate'), @alias('checkMetadataCapabilities.canDelete') canDeleteSecretMetadata;
canReadSecretMetadata: alias('checkMetadataCapabilities.canRead'), @alias('checkMetadataCapabilities.canUpdate') canUpdateSecretMetadata;
@alias('checkMetadataCapabilities.canRead') canReadSecretMetadata;
requestInFlight: or('model.isLoading', 'model.isReloading', 'model.isSaving'), @or('model.isLoading', 'model.isReloading', 'model.isSaving') requestInFlight;
@or('requestInFlight', 'model.isFolder', 'model.flagsIsInvalid') buttonDisabled;
buttonDisabled: or('requestInFlight', 'model.isFolder', 'model.flagsIsInvalid'), get modelForData() {
let { model } = this.args;
modelForData: computed('isV2', 'model', function () {
let { model } = this;
if (!model) return null; if (!model) return null;
return this.isV2 ? model.belongsTo('selectedVersion').value() : model; return this.isV2 ? model.belongsTo('selectedVersion').value() : model;
}), }
basicModeDisabled: computed('secretDataIsAdvanced', 'showAdvancedMode', function () { get basicModeDisabled() {
return this.secretDataIsAdvanced || this.showAdvancedMode === false; return this.secretDataIsAdvanced || this.showAdvancedMode === false;
}), }
secretDataAsJSON: computed('secretData', 'secretData.[]', function () { get secretDataAsJSON() {
return this.secretData.toJSON(); return this.secretData.toJSON();
}), }
secretDataIsAdvanced: computed('secretData', 'secretData.[]', function () { get secretDataIsAdvanced() {
return this.secretData.isAdvanced(); return this.secretData.isAdvanced();
}), }
showAdvancedMode: or('secretDataIsAdvanced', 'preferAdvancedEdit'), get showAdvancedMode() {
return this.secretDataIsAdvanced || this.args.preferAdvancedEdit;
}
isWriteWithoutRead: computed( get isWriteWithoutRead() {
'model.failedServerRead', if (!this.args.model) {
'modelForData.failedServerRead', return false;
'isV2', }
function () {
if (!this.model) return;
// if the version couldn't be read from the server // if the version couldn't be read from the server
if (this.isV2 && this.modelForData.failedServerRead) { if (this.isV2 && this.modelForData.failedServerRead) {
return true; return true;
} }
// if the model couldn't be read from the server // if the model couldn't be read from the server
if (!this.isV2 && this.model.failedServerRead) { if (!this.isV2 && this.args.model.failedServerRead) {
return true; return true;
} }
return false; return false;
} }
),
actions: { @action
refresh() { refresh() {
this.onRefresh(); this.args.onRefresh();
}, }
@action
toggleAdvanced(bool) { toggleAdvanced(bool) {
this.onToggleAdvancedEdit(bool); this.args.onToggleAdvancedEdit(bool);
}, }
}, }
});

View File

@ -19,7 +19,6 @@
<SecretDeleteMenu <SecretDeleteMenu
@modelForData={{@modelForData}} @modelForData={{@modelForData}}
@model={{@model}} @model={{@model}}
@navToNearestAncestor={{@navToNearestAncestor}}
@isV2={{@isV2}} @isV2={{@isV2}}
@refresh={{action @editActions.refresh}} @refresh={{action @editActions.refresh}}
@canReadSecretMetadata={{@canReadSecretMetadata}} @canReadSecretMetadata={{@canReadSecretMetadata}}

View File

@ -1,38 +1,38 @@
<PageHeader as |p|> <PageHeader as |p|>
<p.top> <p.top>
<KeyValueHeader <KeyValueHeader
@baseKey={{this.baseKey}} @baseKey={{@baseKey}}
@path="vault.cluster.secrets.backend.list" @path="vault.cluster.secrets.backend.list"
@mode={{this.mode}} @mode={{@mode}}
@root={{this.root}} @root={{@root}}
@showCurrent={{true}} @showCurrent={{true}}
/> />
</p.top> </p.top>
<p.levelLeft> <p.levelLeft>
<h1 class="title is-3"> <h1 class="title is-3">
{{#if (eq this.mode "create")}} {{#if (eq @mode "create")}}
Create secret Create secret
{{else if (and this.isV2 (eq this.mode "edit"))}} {{else if (and this.isV2 (eq @mode "edit"))}}
Create new version Create new version
{{else if (eq this.mode "edit")}} {{else if (eq @mode "edit")}}
Edit secret Edit secret
{{else}} {{else}}
{{this.key.id}} {{@key.id}}
{{/if}} {{/if}}
</h1> </h1>
</p.levelLeft> </p.levelLeft>
</PageHeader> </PageHeader>
{{! tabs for show only }} {{! tabs for show only }}
{{#if (eq this.mode "show")}} {{#if (eq @mode "show")}}
<div class="tabs-container box is-bottomless is-marginless is-fullwidth is-paddingless"> <div class="tabs-container box is-bottomless is-marginless is-fullwidth is-paddingless">
<nav class="tabs"> <nav class="tabs">
<ul> <ul>
<LinkTo @route="vault.cluster.secrets.backend.show" @model={{this.key.id}} data-test-secret-tab> <LinkTo @route="vault.cluster.secrets.backend.show" @model={{@key.id}} data-test-secret-tab>
Secret Secret
</LinkTo> </LinkTo>
{{! must have read access to /metadata see tab or update to update metadata}} {{! must have read access to /metadata see tab or update to update metadata}}
{{#if (or this.canReadSecretMetadata this.canUpdateSecretMetadata)}} {{#if (or this.canReadSecretMetadata this.canUpdateSecretMetadata)}}
<LinkTo @route="vault.cluster.secrets.backend.metadata" @model={{this.key.id}} data-test-secret-metadata-tab> <LinkTo @route="vault.cluster.secrets.backend.metadata" @model={{@key.id}} data-test-secret-metadata-tab>
Metadata Metadata
</LinkTo> </LinkTo>
{{/if}} {{/if}}
@ -42,24 +42,23 @@
{{/if}} {{/if}}
<SecretEditToolbar <SecretEditToolbar
@mode={{this.mode}} @mode={{@mode}}
@model={{this.model}} @model={{@model}}
@isV2={{this.isV2}} @isV2={{this.isV2}}
@isWriteWithoutRead={{this.isWriteWithoutRead}} @isWriteWithoutRead={{this.isWriteWithoutRead}}
@secretDataIsAdvanced={{this.secretDataIsAdvanced}} @secretDataIsAdvanced={{this.secretDataIsAdvanced}}
@showAdvancedMode={{this.showAdvancedMode}} @showAdvancedMode={{this.showAdvancedMode}}
@modelForData={{this.modelForData}} @modelForData={{this.modelForData}}
@navToNearestAncestor={{this.navToNearestAncestor}}
@canUpdateSecretData={{this.canUpdateSecretData}} @canUpdateSecretData={{this.canUpdateSecretData}}
@canReadSecretMetadata={{this.canReadSecretMetadata}} @canReadSecretMetadata={{this.canReadSecretMetadata}}
@codemirrorString={{this.codemirrorString}} @codemirrorString={{this.codemirrorString}}
@editActions={{hash toggleAdvanced=(action "toggleAdvanced") refresh=(action "refresh")}} @editActions={{hash toggleAdvanced=(action "toggleAdvanced") refresh=(action "refresh")}}
/> />
{{#if (or (eq this.mode "create") (eq this.mode "edit"))}} {{#if (or (eq @mode "create") (eq @mode "edit"))}}
<SecretCreateOrUpdate <SecretCreateOrUpdate
@mode={{this.mode}} @mode={{@mode}}
@model={{this.model}} @model={{@model}}
@showAdvancedMode={{this.showAdvancedMode}} @showAdvancedMode={{this.showAdvancedMode}}
@modelForData={{this.modelForData}} @modelForData={{this.modelForData}}
@error={{this.error}} @error={{this.error}}
@ -70,7 +69,7 @@
@canReadSecretData={{this.canReadSecretData}} @canReadSecretData={{this.canReadSecretData}}
@canReadSecretMetadata={{this.canReadSecretMetadata}} @canReadSecretMetadata={{this.canReadSecretMetadata}}
/> />
{{else if (eq this.mode "show")}} {{else if (eq @mode "show")}}
<SecretFormShow <SecretFormShow
@isV2={{this.isV2}} @isV2={{this.isV2}}
@modelForData={{this.modelForData}} @modelForData={{this.modelForData}}