Icu 5646 serializer (#1257)

* feat: 🎸 Update Credential Serializer

Add support for SSH Private Key credentials

✅ Closes: ICU-5646

* test: 💍 Add normalize test for ssh private key credentials

* refactor: 💡 remove deletion of hmac attributes in serializer
This commit is contained in:
Cameron Perera authored and GitHub committed 2022-08-16 14:35:33 -05:00
1 parent 7386b2c9ed
commit 50038cdf85
2 files changed
+100 -16

No files matched your search

+28 -7
View File
@@ -2,20 +2,41 @@ import ApplicationSerializer from './application';
import { copy } from 'ember-copy';
export default class CredentialSerializer extends ApplicationSerializer {
serialize() {
const serialized = super.serialize(...arguments);
delete serialized['attributes']['password_hmac'];
if (serialized.type === 'username_password') {
delete serialized['attributes']['private_key'];
delete serialized['attributes']['passphrase'];
/**
* @override
* @method serialize
* @param {Snapshot} snapshot
*/
serialize(snapshot) {
switch (snapshot.record.type) {
case 'username_password':
return this.serializeUsernamePassword(...arguments);
case 'ssh_private_key':
default:
return this.serializeSSHPrivateKey(...arguments);
}
}
serializeUsernamePassword() {
const serialized = super.serialize(...arguments);
// Remove non-username_password type attributes
delete serialized['attributes']['private_key'];
delete serialized['attributes']['passphrase'];
return serialized;
}
serializeSSHPrivateKey() {
const serialized = super.serialize(...arguments);
// Remove non-ssh_private_key type attributes
delete serialized['attributes']['password'];
return serialized;
}
normalize(typeClass, hash, ...rest) {
const normalizedHash = copy(hash, true);
const normalized = super.normalize(typeClass, normalizedHash, ...rest);
normalized['data']['attributes']['password'] = '';
// Remove passphrase as we don't track it after being created/updated
delete normalized['data']['attributes']['passphrase'];
return normalized;
}
}
@@ -4,17 +4,18 @@ import { setupTest } from 'ember-qunit';
module('Unit | Serializer | credential', function (hooks) {
setupTest(hooks);
test('it serializes correctly on create', function (assert) {
test('it serializes username_password type correctly on create', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential');
const record = store.createRecord('credential', {
password: 'pass',
username: 'user',
credential_store_id: 'cs_i7p1eu0Nw8',
credential_store_id: 'csst_i7p1eu0Nw8',
type: 'username_password',
name: 'Name',
description: 'Description',
version: 1,
});
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
@@ -24,25 +25,57 @@ module('Unit | Serializer | credential', function (hooks) {
password: 'pass',
username: 'user',
},
credential_store_id: 'cs_i7p1eu0Nw8',
credential_store_id: 'csst_i7p1eu0Nw8',
type: 'username_password',
name: 'Name',
description: 'Description',
version: 1,
});
});
test('it normalizes credential record', async function (assert) {
test('it serializes ssh_private_key type correctly on create', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential');
const record = store.createRecord('credential', {
passphrase: 'passphrasesaresosecure',
private_key: 'superPriveKey',
username: 'user',
credential_store_id: 'csst_i7p1eu0Nw8',
type: 'ssh_private_key',
name: 'Name',
description: 'Description',
version: 1,
});
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
attributes: {
passphrase: 'passphrasesaresosecure',
private_key: 'superPriveKey',
username: 'user',
},
credential_store_id: 'csst_i7p1eu0Nw8',
type: 'ssh_private_key',
name: 'Name',
description: 'Description',
version: 1,
});
});
test('it normalizes "username_password" type credential record', async function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential');
const credentialModelClass = store.createRecord('credential').constructor;
const payload = {
id: 'cred_123',
id: 'credup_123',
version: 1,
type: 'username_password',
attributes: {
username: 'username',
password: '12345678',
password_hmac: 'completenonsense',
},
};
const normalized = serializer.normalize(credentialModelClass, payload);
@@ -50,14 +83,44 @@ module('Unit | Serializer | credential', function (hooks) {
assert.deepEqual(normalized, {
data: {
attributes: {
password: '',
type: 'username_password',
username: 'username',
version: 1,
},
id: 'cred_123',
relationships: {},
type: 'credential',
id: 'credup_123',
relationships: {},
},
});
});
test('it normalizes "ssh_private_key" type credential record', async function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential');
const credentialModelClass = store.createRecord('credential').constructor;
const payload = {
id: 'credspk_123',
version: 1,
type: 'ssh_private_key',
attributes: {
username: 'username',
passphrase: 'random',
private_key_hmac: 'completenonsense',
},
};
const normalized = serializer.normalize(credentialModelClass, payload);
assert.deepEqual(normalized, {
data: {
attributes: {
type: 'ssh_private_key',
username: 'username',
version: 1,
},
type: 'credential',
id: 'credspk_123',
relationships: {},
},
});
});