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

Show generate creds for static-roles when you have read permissions #19190

Merged
merged 7 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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/19190.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: show Get credentials button for static roles detail page when a user has the proper permissions.
```
1 change: 1 addition & 0 deletions ui/app/models/database/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export default Model.extend({
credentialPath: lazyCapabilities(apiPath`${'backend'}/creds/${'id'}`, 'backend', 'id'),
staticCredentialPath: lazyCapabilities(apiPath`${'backend'}/static-creds/${'id'}`, 'backend', 'id'),
canGenerateCredentials: alias('credentialPath.canRead'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be a computed instead and true if staticCredentialPath.canRead OR
credentialPath.canRead is true? that way the HBS template could still be just @model.canGenerateCredentials?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. The type matters here. So if type is static then we have to check the static path and if type is dynamic check the dynamic path. We could still do a computed given this logic but I go back and forth on this. I think because this is a backport, and I just noticed (my bad) that the the static param already existed canGetCredentials I'll keep the changes to a minimum. The canGenerateCreds and canGetCredentials are used in another template so to use a computed I would need to amend the logic there as well. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I see that now, good call removing!

canGenerateStaticCredentials: alias('staticCredentialPath.canRead'),
canGetCredentials: alias('staticCredentialPath.canRead'),
databasePath: lazyCapabilities(apiPath`${'backend'}/config/${'database[0]'}`, 'backend', 'database'),
canUpdateDb: alias('databasePath.canUpdate'),
Expand Down
9 changes: 7 additions & 2 deletions ui/app/templates/components/database-role-edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@
Rotate credentials
</button>
{{/if}}
{{#if @model.canGenerateCredentials}}
{{#if
(or
(and (eq @model.type "static") @model.canGenerateStaticCredentials)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be reverted if the above is possible!

(and (eq @model.type "dynamic") @model.canGenerateCredentials)
)
}}
<button
type="button"
class="toolbar-link"
{{on "click" (fn this.generateCreds @model.id @model.type)}}
data-test-database-role-generate-creds
data-test-database-role-creds={{@model.type}}
>
{{if (eq @model.type "static") "Get credentials" "Generate credentials"}}
</button>
Expand Down
45 changes: 45 additions & 0 deletions ui/tests/integration/components/database-role-edit-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { module, test } from 'qunit';
Copy link
Contributor Author

@Monkeychip Monkeychip Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove this file and put in another pr that will not get backported.

import { setupRenderingTest } from 'ember-qunit';
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';

module('Integration | Component | database-role-edit meep', function (hooks) {
setupRenderingTest(hooks);
setupMirage(hooks);

hooks.beforeEach(function () {
this.store = this.owner.lookup('service:store');
this.store.pushPayload('database-role', {
modelName: 'database/role',
database: ['my-mongodb-database'],
backend: 'database',
type: 'static',
name: 'my-static-role',
id: 'my-static-role',
});
this.store.pushPayload('database-role', {
modelName: 'database/role',
database: ['my-mongodb-database'],
backend: 'database',
type: 'dynamic',
name: 'my-dynamic-role',
id: 'my-dynamic-role',
});
this.modelStatic = this.store.peekRecord('database/role', 'my-static-role');
this.modelDynamic = this.store.peekRecord('database/role', 'my-dynamic-role');
});

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"/>`);
assert.dom('[data-test-database-role-creds="static"]').exists('Get credentials button exists');
});

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