Skip to content

Commit

Permalink
Backport 1.13.x: UI: fixes pki role editing changing to default key p…
Browse files Browse the repository at this point in the history
…arameter values (hashicorp#20921)
  • Loading branch information
hellobontempo authored Jun 1, 2023
1 parent 9f18485 commit 2881445
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 3 deletions.
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, '']));
});
});

0 comments on commit 2881445

Please sign in to comment.