Skip to content

Commit

Permalink
cherry-pick changes (#14623)
Browse files Browse the repository at this point in the history
  • Loading branch information
hellobontempo authored Mar 21, 2022
1 parent 904dde3 commit b1d9ed6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
3 changes: 3 additions & 0 deletions changelog/14551.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: Fix issue where UI incorrectly handled API errors when mounting backends
```
11 changes: 2 additions & 9 deletions ui/app/adapters/secret-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,8 @@ export default ApplicationAdapter.extend({
data.id = path;
}
// first create the engine
try {
await this.ajax(this.url(path), 'POST', { data });
} catch (e) {
// if error determine if path duplicate or permissions
if (e.httpStatus === 400) {
throw new Error('samePath');
}
throw new Error('mountIssue');
}
await this.ajax(this.url(path), 'POST', { data });

// second post to config
try {
await this.ajax(this.urlForConfig(path), 'POST', { data: configData });
Expand Down
45 changes: 31 additions & 14 deletions ui/app/components/mount-backend-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,49 @@ export default Component.extend({
}
}

if (!capabilities.get('canUpdate')) {
// if there is no sys/mount issue then error is config endpoint.
this.flashMessages.warning(
'You do not have access to the config endpoint. The secret engine was mounted, but the configuration settings were not saved.'
);
// remove the config data from the model otherwise it will save it even if the network request failed.
[this.mountModel.maxVersions, this.mountModel.casRequired, this.mountModel.deleteVersionAfter] = [
0,
false,
0,
];
}
let changedAttrKeys = Object.keys(mountModel.changedAttributes());
const updatesConfig =
mountModel.isV2KV &&
(changedAttrKeys.includes('casRequired') ||
changedAttrKeys.includes('deleteVersionAfter') ||
changedAttrKeys.includes('maxVersions'));

try {
yield mountModel.save();
} catch (err) {
if (err.message === 'mountIssue') {
if (err.httpStatus === 403) {
this.mountIssue = true;
this.set('isFormInvalid', this.mountIssue);
this.flashMessages.danger(
'You do not have access to the sys/mounts endpoint. The secret engine was not mounted.'
);
return;
}
this.set('errorMessage', 'This mount path already exist.');
if (err.errors) {
let errors = err.errors.map((e) => {
if (typeof e === 'object') return e.title || e.message || JSON.stringify(e);
return e;
});
this.set('errors', errors);
} else if (err.message) {
this.set('errorMessage', err.message);
} else {
this.set('errorMessage', 'An error occurred, check the vault logs.');
}
return;
}
if (updatesConfig && !capabilities.get('canUpdate')) {
// config error is not thrown from secret-engine adapter, so handling here
this.flashMessages.warning(
'You do not have access to the config endpoint. The secret engine was mounted, but the configuration settings were not saved.'
);
// remove the config data from the model otherwise it will save it even if the network request failed.
[this.mountModel.maxVersions, this.mountModel.casRequired, this.mountModel.deleteVersionAfter] = [
0,
false,
0,
];
}
let mountType = this.mountType;
mountType = mountType === 'secret' ? `${mountType}s engine` : `${mountType} method`;
this.flashMessages.success(`Successfully mounted the ${type} ${mountType} at ${path}.`);
Expand Down
2 changes: 1 addition & 1 deletion ui/app/templates/components/mount-backend-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<form {{action (perform this.mountBackend) on="submit"}}>
<div class="box is-sideless is-fullwidth is-marginless">
<NamespaceReminder @mode="enable" @noun={{if (eq this.mountType "auth") "Auth Method" "Secret Engine"}} />
<MessageError @model={{this.model}} @errorMessage={{this.errorMessage}} />
<MessageError @model={{this.model}} @errorMessage={{this.errorMessage}} @errors={{this.errors}} />
{{#if this.showEnable}}
<FormFieldGroups
@model={{this.mountModel}}
Expand Down
2 changes: 1 addition & 1 deletion ui/tests/acceptance/settings/mount-secret-backend-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module('Acceptance | settings/mount-secret-backend', function (hooks) {
await page.enableEngine();
await page.selectType('kv');
await page.next().path(path).submit();
assert.dom('.alert-banner-message-body').hasText('This mount path already exist.');
assert.dom('.alert-banner-message-body').containsText(`path is already in use at ${path}`);
assert.equal(currentRouteName(), 'vault.cluster.settings.mount-secret-backend');

await page.secretList();
Expand Down

0 comments on commit b1d9ed6

Please sign in to comment.