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

Backport 1.13.x: UI: fixes pki role editing changing to default key parameter values #20921

Merged
merged 8 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/20907.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: fixes key_bits and signature_bits reverting to default values when editing a pki role
```
2 changes: 1 addition & 1 deletion ui/app/models/pki/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default class PkiRoleModel extends Model {
})
keyBits; // no possibleValues because options are dependent on selected key type

@attr('number', {
@attr('string', {
label: 'Signature bits',
subText: `Only applicable for key_type 'RSA'. Ignore for other key types.`,
defaultValue: '0',
Expand Down
2 changes: 1 addition & 1 deletion ui/lib/core/addon/components/form-field.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</option>
{{/if}}
{{#each (path-or-array @attr.options.possibleValues @model) as |val|}}
<option selected={{eq (get @model this.valuePath) (or val.value val)}} value={{or val.value val}}>
<option selected={{loose-equal (get @model this.valuePath) (or val.value val)}} value={{or val.value val}}>
{{or val.displayName val}}
</option>
{{/each}}
Expand Down
17 changes: 17 additions & 0 deletions ui/lib/core/addon/helpers/loose-equal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { helper } from '@ember/component/helper';

/*
* use sparingly *
ex: logic for an HTML element's selected boolean because <select> values are strings
strict equal (===) will fail if the API param is a number
<option selected={{loose-equal model.someAttr someOption)}} value={{someOption}}>
*/
export function looseEqual([a, b]) {
// loose equal 0 == '' returns true, we don't want that
if ((a === 0 && b === '') || (a === '' && b === 0)) {
return false;
}
return a == b;
}

export default helper(looseEqual);
1 change: 1 addition & 0 deletions ui/lib/core/app/helpers/loose-equal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'core/helpers/loose-equal';
2 changes: 1 addition & 1 deletion ui/lib/pki/addon/components/pki-key-parameters.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</option>
{{/if}}
{{#each this.keyBitOptions as |val|}}
<option selected={{eq (get @model "keyBits") val}} value={{val}}>
<option selected={{loose-equal (get @model "keyBits") val}} value={{val}}>
{{val}}
</option>
{{/each}}
Expand Down
64 changes: 64 additions & 0 deletions ui/tests/integration/components/pki/pki-role-form-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,68 @@ module('Integration | Component | pki-role-form', function (hooks) {
await click(SELECTORS.roleCreateButton);
assert.strictEqual(this.model.issuerRef, 'not-default', 'Issuer Ref correctly saved on create');
});

test('it should edit a role', async function (assert) {
assert.expect(8);
this.server.post(`/pki-test/roles/test-role`, (schema, req) => {
assert.ok(true, 'Request made to correct endpoint to update role');
const request = JSON.parse(req.requestBody);
assert.propEqual(
request,
{
allow_ip_sans: true,
issuer_ref: 'default',
key_bits: '224',
key_type: 'ec',
key_usage: ['DigitalSignature', 'KeyAgreement', 'KeyEncipherment'],
not_before_duration: '30s',
require_cn: true,
signature_bits: '384',
use_csr_common_name: true,
use_csr_sans: true,
},
'sends role params in correct type'
);
return {};
});

this.store.pushPayload('pki/role', {
modelName: 'pki/role',
name: 'test-role',
backend: 'pki-test',
id: 'role-id',
key_type: 'rsa',
});
this.model = this.store.peekRecord('pki/role', 'role-id');

// setting these params manually because ember transforms to whatever type is defined on the model
// but in the 'real' app the model uses openApi and the API returns both attrs as numbers types
this.model.keyBits = 3072;
this.model.signatureBits = 512;

await render(
hbs`
<PkiRoleForm
@model={{this.model}}
@onCancel={{this.onCancel}}
@onSave={{this.onSave}}
/>
`,
{ owner: this.engine }
);

await click(SELECTORS.keyParams);
assert.dom(SELECTORS.keyType).hasValue('rsa');
assert.dom(SELECTORS.keyBits).hasValue('3072', 'dropdown has model value, not default value (2048)');
assert.dom(SELECTORS.signatureBits).hasValue('512', 'dropdown has model value, not default value (0)');

await fillIn(SELECTORS.keyType, 'ec');
await fillIn(SELECTORS.keyBits, '224');
assert.dom(SELECTORS.keyBits).hasValue('224', 'dropdown has selected value, not default value (256)');
await fillIn(SELECTORS.signatureBits, '384');
assert.dom(SELECTORS.signatureBits).hasValue('384', 'dropdown has selected value, not default value (0)');

await click(SELECTORS.roleCreateButton);
assert.strictEqual(this.model.issuerRef, 'default', 'Issuer Ref correctly saved on create');
});
});
34 changes: 34 additions & 0 deletions ui/tests/integration/helpers/loose-equal-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'vault/tests/helpers';
import { render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { looseEqual } from 'core/helpers/loose-equal';

module('Integration | Helper | loose-equal', function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
this.inputValue = 1234;
await render(hbs`{{if (loose-equal "1234" 1234) "true" "false"}}`);
assert.dom(this.element).hasText('true');

this.inputValue = '4567';
await render(hbs`{{if (loose-equal "1234" "4567") "true" "false"}}`);
assert.dom(this.element).hasText('false');
});

test('it compares values as expected', async function (assert) {
assert.true(looseEqual([0, '0']));
assert.true(looseEqual([0, 0]));
assert.true(looseEqual(['0', '0']));
assert.true(looseEqual(['1234', 1234]));
assert.true(looseEqual(['1234', '1234']));
assert.true(looseEqual([1234, 1234]));
assert.true(looseEqual(['abc', 'abc']));
assert.true(looseEqual(['', '']));

// == normally returns true for this comparison, we intercept and return false
assert.false(looseEqual(['', 0]));
assert.false(looseEqual([0, '']));
});
});