diff --git a/changelog/29498.txt b/changelog/29498.txt
new file mode 100644
index 0000000000..b2cad9629a
--- /dev/null
+++ b/changelog/29498.txt
@@ -0,0 +1,3 @@
+```release-note:bug
+ui/database: Fixes 'cannot update static username' error when updating static role's rotation period
+```
\ No newline at end of file
diff --git a/ui/app/adapters/database/role.js b/ui/app/adapters/database/role.js
index 964d716edb..c46a733655 100644
--- a/ui/app/adapters/database/role.js
+++ b/ui/app/adapters/database/role.js
@@ -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);
},
diff --git a/ui/tests/integration/components/database-role-edit-test.js b/ui/tests/integration/components/database-role-edit-test.js
index 15cfe3c74d..113bd818bc 100644
--- a/ui/tests/integration/components/database-role-edit-test.js
+++ b/ui/tests/integration/components/database-role-edit-test.js
@@ -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);
@@ -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',
@@ -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``);
+ 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``);