diff --git a/addons/api/addon/serializers/credential-library.js b/addons/api/addon/serializers/credential-library.js index 8a466ae97..b5cc40bff 100644 --- a/addons/api/addon/serializers/credential-library.js +++ b/addons/api/addon/serializers/credential-library.js @@ -28,6 +28,7 @@ export default class CredentialLibrarySerializer extends ApplicationSerializer { case TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC: return this.serializeVaultGeneric(...arguments); case TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE: + return this.serializeVaultSshCertificate(...arguments); default: return super.serialize(...arguments); } @@ -61,4 +62,16 @@ export default class CredentialLibrarySerializer extends ApplicationSerializer { return serialized; } } + + serializeVaultSshCertificate() { + const serialized = super.serialize(...arguments); + if (serialized.attributes) { + const { key_type } = serialized.attributes; + if (key_type !== 'rsa' && key_type !== 'ecdsa') { + // If the key type is not RSA or ECDSA, set key_bits to null + serialized.attributes.key_bits = null; + } + return serialized; + } + } } diff --git a/addons/api/tests/unit/serializers/credential-library-test.js b/addons/api/tests/unit/serializers/credential-library-test.js index 61e936535..94a645870 100644 --- a/addons/api/tests/unit/serializers/credential-library-test.js +++ b/addons/api/tests/unit/serializers/credential-library-test.js @@ -231,4 +231,52 @@ module('Unit | Serializer | credential library', function (hooks) { version: 1, }); }); + + test('it serializes vault_ssh_certificate correctly when changing type to ed25519', function (assert) { + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('credential-library'); + store.push({ + data: { + id: '1', + type: 'credential-library', + attributes: { + type: TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE, + version: 1, + name: 'Name', + description: 'Description', + path: '/vault/path', + username: 'user', + key_type: 'rsa', + key_bits: 100, + ttl: '100', + key_id: 'id', + extensions: [{ key: 'key', value: 'value' }], + critical_options: [{ key: 'key', value: 'value' }], + }, + }, + }); + const record = store.peekRecord('credential-library', '1'); + record.key_type = 'ed25519'; + + const snapshot = record._createSnapshot(); + const serializedRecord = serializer.serialize(snapshot); + + assert.deepEqual(serializedRecord, { + type: TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE, + credential_store_id: null, + name: 'Name', + description: 'Description', + attributes: { + path: '/vault/path', + username: 'user', + key_type: 'ed25519', + key_bits: null, + ttl: '100', + key_id: 'id', + extensions: { key: 'value' }, + critical_options: { key: 'value' }, + }, + version: 1, + }); + }); });