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

Backports KV Key Whitespace Warning (#23702) to 1.15 #23773

Merged
merged 1 commit into from
Oct 20, 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/23702.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
ui: Adds a warning when whitespace is detected in a key of a KV secret
```
10 changes: 10 additions & 0 deletions ui/lib/core/addon/components/kv-object-editor.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
@value={{row.name}}
placeholder={{this.placeholders.key}}
{{on "change" (fn this.updateRow row index)}}
{{on "input" (fn this.validateKey index)}}
class="input"
/>
</div>
Expand Down Expand Up @@ -70,6 +71,15 @@
{{/if}}
</div>
</div>
{{#if (includes index this.whitespaceWarningRows)}}
<div class="has-bottom-margin-s">
<AlertInline
@type="warning"
@message="Key contains whitespace. If this is desired, you'll need to encode it with %20 in API requests."
data-test-kv-whitespace-warning={{index}}
/>
</div>
{{/if}}
{{/each}}
{{#if this.hasDuplicateKeys}}
<Hds::Alert data-test-duplicate-keys-warning @type="inline" @color="warning" as |A|>
Expand Down
15 changes: 15 additions & 0 deletions ui/lib/core/addon/components/kv-object-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isNone } from '@ember/utils';
import { assert } from '@ember/debug';
import { action } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import { A } from '@ember/array';
import KVObject from 'vault/lib/kv-object';

/**
Expand Down Expand Up @@ -38,6 +39,7 @@ import KVObject from 'vault/lib/kv-object';

export default class KvObjectEditor extends Component {
@tracked kvData;
whitespaceWarningRows = A();

get placeholders() {
return {
Expand Down Expand Up @@ -73,6 +75,7 @@ export default class KvObjectEditor extends Component {
const oldObj = this.kvData.objectAt(index);
assert('object guids match', guidFor(oldObj) === guidFor(object));
this.kvData.removeAt(index);
this.whitespaceWarningRows.removeObject(index);
this.args.onChange(this.kvData.toJSON());
}
@action
Expand All @@ -81,4 +84,16 @@ export default class KvObjectEditor extends Component {
this.args.onKeyUp(event.target.value);
}
}
@action
validateKey(rowIndex, event) {
const { value } = event.target;
const keyHasWhitespace = new RegExp('\\s', 'g').test(value);
const rows = [...this.whitespaceWarningRows];
const rowHasWarning = rows.includes(rowIndex);
if (!keyHasWhitespace && rowHasWarning) {
this.whitespaceWarningRows.removeObject(rowIndex);
} else if (keyHasWhitespace && !rowHasWarning) {
this.whitespaceWarningRows.addObject(rowIndex);
}
}
}
25 changes: 19 additions & 6 deletions ui/tests/integration/components/kv-object-editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { render, fillIn, click } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

import { create } from 'ember-cli-page-object';
Expand All @@ -22,14 +22,14 @@ module('Integration | Component | kv-object-editor', function (hooks) {
});

test('it renders with no initial value', async function (assert) {
await render(hbs`{{kv-object-editor onChange=this.spy}}`);
await render(hbs`<KvObjectEditor @onChange={{this.spy}} />`);
assert.strictEqual(component.rows.length, 1, 'renders a single row');
await component.addRow();
assert.strictEqual(component.rows.length, 1, 'will only render row with a blank key');
});

test('it calls onChange when the val changes', async function (assert) {
await render(hbs`{{kv-object-editor onChange=this.spy}}`);
await render(hbs`<KvObjectEditor @onChange={{this.spy}} />`);
await component.rows.objectAt(0).kvKey('foo').kvVal('bar');
assert.strictEqual(this.spy.callCount, 2, 'calls onChange each time change is triggered');
assert.deepEqual(
Expand All @@ -50,7 +50,7 @@ module('Integration | Component | kv-object-editor', function (hooks) {
test('it renders passed data', async function (assert) {
const metadata = { foo: 'bar', baz: 'bop' };
this.set('value', metadata);
await render(hbs`{{kv-object-editor value=this.value}}`);
await render(hbs`<KvObjectEditor @value={{this.value}} />`);
assert.strictEqual(
component.rows.length,
Object.keys(metadata).length + 1,
Expand All @@ -59,7 +59,7 @@ module('Integration | Component | kv-object-editor', function (hooks) {
});

test('it deletes a row', async function (assert) {
await render(hbs`{{kv-object-editor onChange=this.spy}}`);
await render(hbs`<KvObjectEditor @onChange={{this.spy}} />`);
await component.rows.objectAt(0).kvKey('foo').kvVal('bar');
await component.addRow();
assert.strictEqual(component.rows.length, 2);
Expand All @@ -74,7 +74,7 @@ module('Integration | Component | kv-object-editor', function (hooks) {
test('it shows a warning if there are duplicate keys', async function (assert) {
const metadata = { foo: 'bar', baz: 'bop' };
this.set('value', metadata);
await render(hbs`{{kv-object-editor value=this.value onChange=this.spy}}`);
await render(hbs`<KvObjectEditor @value={{this.value}} @onChange={{this.spy}} />`);
await component.rows.objectAt(0).kvKey('foo');

assert.ok(component.showsDuplicateError, 'duplicate keys are allowed but an error message is shown');
Expand All @@ -97,4 +97,17 @@ module('Integration | Component | kv-object-editor', function (hooks) {
assert.dom('textarea').doesNotExist('Value input hidden when block is provided');
assert.dom('[data-test-yield]').exists('Component yields block');
});

test('it should display whitespace warning for keys', async function (assert) {
await render(hbs`<KvObjectEditor @onChange={{this.spy}} />`);
await fillIn('[data-test-kv-key="0"]', 'test ');
assert.dom('[data-test-kv-whitespace-warning="0"]').exists();
await fillIn('[data-test-kv-key="0"]', 'test');
assert.dom('[data-test-kv-whitespace-warning="0"]').doesNotExist();
await fillIn('[data-test-kv-key="0"]', 'test ');
await click('[data-test-kv-add-row="0"]');
assert.dom('[data-test-kv-whitespace-warning="0"]').exists();
await click('[data-test-kv-delete-row="0"]');
assert.dom('[data-test-kv-whitespace-warning="0"]').doesNotExist();
});
});
Loading