Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Fix KV v2 json editor #24224

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/24224.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: Fix JSON editor in KV V2 unable to handle pasted values
```
2 changes: 1 addition & 1 deletion ui/lib/kv/addon/components/kv-data-fields.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{{#if @showJson}}
<JsonEditor
@title="{{if @isEdit 'Version' 'Secret'}} data"
@value={{or (stringify @secret.secretData) this.emptyJson}}
@value={{this.codeMirrorString}}
@valueUpdated={{this.handleJson}}
/>
{{#if (or @modelValidations.secretData.errors this.lintingErrors)}}
Expand Down
12 changes: 8 additions & 4 deletions ui/lib/kv/addon/components/kv-data-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import KVObject from 'vault/lib/kv-object';
import { stringify } from 'core/helpers/stringify';

/**
* @module KvDataFields is used for rendering the fields associated with kv secret data, it hides/shows a json editor and renders validation errors for the json editor
Expand All @@ -28,10 +28,13 @@ import KVObject from 'vault/lib/kv-object';

export default class KvDataFields extends Component {
@tracked lintingErrors;
@tracked codeMirrorString;

get emptyJson() {
// if secretData is null, this specially formats a blank object and renders a nice initial state for the json editor
return KVObject.create({ content: [{ name: '', value: '' }] }).toJSONString(true);
constructor() {
super(...arguments);
this.codeMirrorString = this.args.secret?.secretData
? stringify([this.args.secret.secretData], {})
: '{ "": "" }';
}

@action
Expand All @@ -41,5 +44,6 @@ export default class KvDataFields extends Component {
if (!this.lintingErrors) {
this.args.secret.secretData = JSON.parse(value);
}
this.codeMirrorString = value;
}
}
9 changes: 7 additions & 2 deletions ui/lib/kv/addon/components/page/secret/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

<KvPageHeader @breadcrumbs={{@breadcrumbs}} @pageTitle="Create New Version">
<:toolbarFilters>
<Toggle @name="json" @checked={{this.showJsonView}} @onChange={{fn (mut this.showJsonView)}}>
<Toggle
@name="json"
@checked={{or this.showJsonView this.secretDataIsAdvanced}}
@onChange={{fn (mut this.showJsonView)}}
@disabled={{this.secretDataIsAdvanced}}
>
<span class="has-text-grey">JSON</span>
</Toggle>
</:toolbarFilters>
Expand Down Expand Up @@ -38,7 +43,7 @@
<MessageError @model={{@secret}} @errorMessage={{this.errorMessage}} />

<KvDataFields
@showJson={{this.showJsonView}}
@showJson={{or this.showJsonView this.secretDataIsAdvanced}}
@secret={{@secret}}
@modelValidations={{this.modelValidations}}
@isEdit={{true}}
Expand Down
5 changes: 5 additions & 0 deletions ui/lib/kv/addon/components/page/secret/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ export default class KvSecretEdit extends Component {
@tracked modelValidations;
@tracked invalidFormAlert;
originalSecret;
secretDataIsAdvanced;

constructor() {
super(...arguments);
this.originalSecret = JSON.stringify(this.args.secret.secretData || {});
if (this.originalSecret.lastIndexOf('{') > 0) {
// Dumb way to check if there's a nested object in the secret
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂

this.secretDataIsAdvanced = true;
}
}

get showOldVersionAlert() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from 'vault/tests/helpers/policy-generator/kv';
import { clearRecords, writeSecret, writeVersionedSecret } from 'vault/tests/helpers/kv/kv-run-commands';
import { FORM, PAGE } from 'vault/tests/helpers/kv/kv-selectors';
import codemirror from 'vault/tests/helpers/codemirror';

/**
* This test set is for testing edge cases, such as specific bug fixes or reported user workflows
Expand Down Expand Up @@ -269,6 +270,20 @@ module('Acceptance | kv-v2 workflow | edge cases', function (hooks) {
await click(PAGE.breadcrumbAtIdx(2));
assert.dom(PAGE.list.item()).exists({ count: 2 }, 'two secrets are listed');
});

test('complex values default to JSON display', async function (assert) {
await visit(`/vault/secrets/${this.backend}/kv/create`);
await fillIn(FORM.inputByAttr('path'), 'complex');

await click(FORM.toggleJson);
assert.strictEqual(codemirror().getValue(), '{ "": "" }');
codemirror().setValue('{ "foo3": { "name": "bar3" } }');
await click(FORM.saveBtn);
// Future: test that json is automatic on details too
await click(PAGE.detail.createNewVersion);
assert.dom(FORM.toggleJson).isDisabled();
assert.dom(FORM.toggleJson).isChecked();
});
});

// NAMESPACE TESTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module('Integration | Component | kv-v2 | KvDataFields', function (hooks) {

assert.strictEqual(
codemirror().getValue(' '),
`{ \"\": \"\" }`, // eslint-disable-line no-useless-escape
`{ \"\": \"\" }`, // eslint-disable-line no-useless-escape
'json editor initializes with empty object'
);
await fillIn(`${FORM.jsonEditor} textarea`, 'blah');
Expand Down
Loading