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 updating static roles via role edit page on UI #29498

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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/29498.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: Fixes 'cannot update static username' error when updating a static role's rotation period via the UI
```
11 changes: 10 additions & 1 deletion ui/app/adapters/database/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,19 @@ export default ApplicationAdapter.extend({

async updateRecord(store, type, snapshot) {
const serializer = store.serializerFor(type.modelName);
const data = serializer.serialize(snapshot);
const serializedData = serializer.serialize(snapshot);
const roleType = snapshot.attr('type');
const backend = snapshot.attr('backend');
const id = snapshot.attr('name');
let data = {};
if (roleType === 'static') {
data = {
...serializedData,
username: snapshot.attr('username'), // username is required for updating a static role
};
} else {
data = serializedData;
}

return this.ajax(this.urlFor(backend, id, roleType), 'POST', { data }).then(() => data);
},
Expand Down
22 changes: 22 additions & 0 deletions ui/tests/integration/components/database-role-edit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { capabilitiesStub } from 'vault/tests/helpers/stubs';
import { click, fillIn } from '@ember/test-helpers';

module('Integration | Component | database-role-edit', function (hooks) {
setupRenderingTest(hooks);
Expand All @@ -20,6 +21,7 @@ module('Integration | Component | database-role-edit', function (hooks) {
modelName: 'database/role',
database: ['my-mongodb-database'],
backend: 'database',
username: 'staticTestUser',
type: 'static',
name: 'my-static-role',
id: 'my-static-role',
Expand All @@ -36,6 +38,26 @@ module('Integration | Component | database-role-edit', function (hooks) {
this.modelDynamic = this.store.peekRecord('database/role', 'my-dynamic-role');
});

test('it should let user edit a static role when given update capability', async function (assert) {
this.server.post('/sys/capabilities-self', capabilitiesStub('database/static-creds/my-role', ['update']));

this.server.post(`/database/static-roles/my-static-role`, (schema, req) => {
assert.true(true, 'request made to update static role');
assert.propEqual(
JSON.parse(req.requestBody),
{
username: 'staticTestUser',
rotation_period: '1728000s', // 20 days in seconds
},
'it updates static role with correct payload'
);
});

await render(hbs`<DatabaseRoleEdit @model={{this.modelStatic}} @mode="edit"/>`);
await fillIn('[data-test-ttl-value="Rotation period"]', '20');
await click('[data-test-secret-save]');
});

test('it should show Get credentials button when a user has the correct policy', async function (assert) {
this.server.post('/sys/capabilities-self', capabilitiesStub('database/static-creds/my-role', ['read']));
await render(hbs`<DatabaseRoleEdit @model={{this.modelStatic}} @mode="show"/>`);
Expand Down
Loading