Skip to content

Commit

Permalink
KV alert banner for white space in KV path (hashicorp#12921)
Browse files Browse the repository at this point in the history
* alert banner

* changelog

* test coverage

* amend message

* address pr comments

* whoops

* Revert "whoops"

This reverts commit ac83254.

* whoops again
  • Loading branch information
Monkeychip authored and Artem Alexandrov committed Feb 4, 2022
1 parent dcffbc3 commit c905163
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelog/12921.txt
@@ -0,0 +1,3 @@
```release-note:improvement
ui: Adds warning about white space in KV secret engine.
```
7 changes: 7 additions & 0 deletions ui/app/components/secret-create-or-update.js
Expand Up @@ -44,6 +44,7 @@ export default class SecretCreateOrUpdate extends Component {
@tracked codemirrorString = null;
@tracked error = null;
@tracked secretPaths = null;
@tracked pathWhiteSpaceWarning = false;
@tracked validationErrorCount = 0;
@tracked validationMessages = null;

Expand Down Expand Up @@ -82,6 +83,8 @@ export default class SecretCreateOrUpdate extends Component {
}
checkValidation(name, value) {
if (name === 'path') {
// check for whitespace
this.pathHasWhiteSpace(value);
!value
? set(this.validationMessages, name, `${name} can't be blank.`)
: set(this.validationMessages, name, '');
Expand All @@ -106,6 +109,10 @@ export default class SecretCreateOrUpdate extends Component {
this.transitionToRoute(LIST_ROOT_ROUTE);
}
}
pathHasWhiteSpace(value) {
let validation = new RegExp('\\s', 'g'); // search for whitespace
this.pathWhiteSpaceWarning = validation.test(value);
}
// successCallback is called in the context of the component
persistKey(successCallback) {
let secret = this.args.model;
Expand Down
2 changes: 1 addition & 1 deletion ui/app/models/secret-engine.js
Expand Up @@ -52,7 +52,7 @@ export default Model.extend(Validations, {
defaultValue: 0,
label: 'Maximum number of versions',
subText:
'The number of versions to keep per key. Once the number of keys exceeds the maximum number set here, the oldest version will be permanently deleted. This value applies to all keys, but a key’s metadata settings can overwrite this value.',
'The number of versions to keep per key. Once the number of keys exceeds the maximum number set here, the oldest version will be permanently deleted. This value applies to all keys, but a key’s metadata settings can overwrite this value. When 0 is used or the value is unset, Vault will keep 10 versions.',
}),
casRequired: attr('boolean', {
defaultValue: false,
Expand Down
10 changes: 10 additions & 0 deletions ui/app/templates/components/secret-create-or-update.hbs
Expand Up @@ -28,6 +28,16 @@
The secret path may not end in <code>/</code>
</p>
{{/if}}
{{#if this.pathWhiteSpaceWarning}}
<div class="has-top-margin-m">
<AlertBanner
@type="warning"
@message="Your secret path contains whitespace. If this is desired, you'll need to encode it with %20 in API calls."
@marginTop=true
data-test-whitespace-warning
/>
</div>
{{/if}}
</div>
{{#if @showAdvancedMode}}
<div class="form-section">
Expand Down
10 changes: 8 additions & 2 deletions ui/tests/acceptance/secrets/backend/kv/secret-test.js
Expand Up @@ -440,7 +440,7 @@ module('Acceptance | secrets/secret/create', function(hooks) {
}
});

test('create secret with space shows version data', async function(assert) {
test('create secret with space shows version data and shows space warning', async function(assert) {
let enginePath = `kv-${new Date().getTime()}`;
let secretPath = 'space space';
// mount version 2
Expand All @@ -452,7 +452,13 @@ module('Acceptance | secrets/secret/create', function(hooks) {
.submit();
await settled();
await listPage.create();
await editPage.createSecret(secretPath, 'foo', 'bar');
await editPage.createSecretDontSave(secretPath, 'foo', 'bar');
// to trigger warning need to hit keyup on the secret path
await triggerKeyEvent('[data-test-secret-path="true"]', 'keyup', 65);
await settled();
assert.dom('[data-test-whitespace-warning]').exists('renders warning about their being a space');
await settled();
await click('[data-test-secret-save="true"]');
await settled();
await click('[data-test-popup-menu-trigger="version"]');
await settled();
Expand Down
5 changes: 5 additions & 0 deletions ui/tests/pages/secrets/backend/kv/edit-secret.js
Expand Up @@ -29,6 +29,11 @@ export default create({
.secretValue(value)
.save();
},
createSecretDontSave: async function(path, key, value) {
return this.path(path)
.secretKey(key)
.secretValue(value);
},
createSecretWithMetadata: async function(path, key, value, maxVersion) {
return this.path(path)
.secretKey(key)
Expand Down

0 comments on commit c905163

Please sign in to comment.