From e3d44eb8133f8c1025637bc674a69f05dd3ff5ea Mon Sep 17 00:00:00 2001 From: Devon Anderson Date: Mon, 21 Nov 2022 16:28:10 -0500 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20JSON=20credential=20dat?= =?UTF-8?q?a=20modeling=20(#1485)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: 🤖 JSON credential data modeling Added json type to credential model, created `json_object` attribute for `json` type, updated credential serializer to handle `json_object` attribute data being sent to backend and updated normalizer to handlle secret untracked 'json_object'. ✅ Closes: ICU-6653, ICU-6684, ICU-6685, ICU-6802, ICU-6803 * Update en-us.yaml fixed spelling * Update addons/api/addon/models/credential.js Co-authored-by: Cameron Perera * refactor: 💡 refactor credential serializer and .yaml Refactored credential serializer to check for `json_object` existence before serializing and changed help variable in `.yaml` file for credentials. * refactor: 💡 refactor for credential serializer Refactor credential serializer removed redundant code, Co-authored-by: Cameron Perera --- .../api/addon/generated/models/credential.js | 8 +++ addons/api/addon/serializers/credential.js | 15 +++++ addons/api/mirage/serializers/credential.js | 2 +- .../tests/unit/serializers/credential-test.js | 62 +++++++++++++++++++ addons/core/translations/resources/en-us.yaml | 6 +- 5 files changed, 90 insertions(+), 3 deletions(-) diff --git a/addons/api/addon/generated/models/credential.js b/addons/api/addon/generated/models/credential.js index fb51ad5e9..bc1872820 100644 --- a/addons/api/addon/generated/models/credential.js +++ b/addons/api/addon/generated/models/credential.js @@ -79,4 +79,12 @@ export default class GeneratedCredentialModel extends BaseModel { description: 'The private key passphrase for credential.', }) private_key_passphrase; + + @attr('string', { + for: 'json', + isNestedAttribute: true, + isSecret: true, + description: 'The secret associated with credential.', + }) + json_object; } diff --git a/addons/api/addon/serializers/credential.js b/addons/api/addon/serializers/credential.js index d2e3eed20..5c61d93dc 100644 --- a/addons/api/addon/serializers/credential.js +++ b/addons/api/addon/serializers/credential.js @@ -13,6 +13,20 @@ export default class CredentialSerializer extends ApplicationSerializer { */ serializeScopeID = false; + serialize() { + const serialized = super.serialize(...arguments); + + if (serialized.type === 'json') { + delete serialized.attributes.username; + + if (serialized?.attributes?.json_object) { + serialized.attributes.object = JSON.parse(serialized.attributes.json_object); + delete serialized.attributes.json_object; + } + } + + return serialized; + } normalize(typeClass, hash, ...rest) { const normalizedHash = copy(hash, true); @@ -21,6 +35,7 @@ export default class CredentialSerializer extends ApplicationSerializer { normalized.data.attributes.password = ''; normalized.data.attributes.private_key = ''; normalized.data.attributes.private_key_passphrase = ''; + normalized.data.attributes.json_object = ''; return normalized; } } diff --git a/addons/api/mirage/serializers/credential.js b/addons/api/mirage/serializers/credential.js index cd46766a4..5f4040870 100644 --- a/addons/api/mirage/serializers/credential.js +++ b/addons/api/mirage/serializers/credential.js @@ -15,7 +15,7 @@ export default ApplicationSerializer.extend({ delete json.attributes?.password; delete json.attributes?.private_key; delete json.attributes?.private_key_passphrase; - + delete json.attributes?.json_object; return json; }, }); diff --git a/addons/api/tests/unit/serializers/credential-test.js b/addons/api/tests/unit/serializers/credential-test.js index af930ff35..ca492c280 100644 --- a/addons/api/tests/unit/serializers/credential-test.js +++ b/addons/api/tests/unit/serializers/credential-test.js @@ -92,6 +92,37 @@ module('Unit | Serializer | credential', function (hooks) { }); }); + test('it serializes json_object type correctly on create', async function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('credential'); + const record = store.createRecord('credential', { + credential_store_id: 'csst_i7p1eu0Nw8', + json_object: + '{"secret_key": "QWERTYUIOP", "secret_access_key": "QWERT.YUIOP234567890"}', + type: 'json', + name: 'Name', + description: 'Description', + version: 1, + }); + const snapshot = record._createSnapshot(); + const serializedRecord = serializer.serialize(snapshot); + + assert.deepEqual(serializedRecord, { + attributes: { + object: { + secret_key: 'QWERTYUIOP', + secret_access_key: 'QWERT.YUIOP234567890', + }, + }, + credential_store_id: 'csst_i7p1eu0Nw8', + type: 'json', + name: 'Name', + description: 'Description', + version: 1, + }); + }); + test('it serializes ssh_private_key type correctly when only the username is updated', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); @@ -146,6 +177,7 @@ module('Unit | Serializer | credential', function (hooks) { private_key_passphrase: '', private_key: '', password: '', + json_object: '', }, type: 'credential', id: 'credup_123', @@ -180,6 +212,7 @@ module('Unit | Serializer | credential', function (hooks) { private_key_passphrase: '', private_key: '', password: '', + json_object: '', }, type: 'credential', id: 'credspk_123', @@ -187,4 +220,33 @@ module('Unit | Serializer | credential', function (hooks) { }, }); }); + + test('it normalizes "json" 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: 'credjson_123', + version: 1, + type: 'json', + }; + const normalized = serializer.normalize(credentialModelClass, payload); + + assert.deepEqual(normalized, { + data: { + attributes: { + type: 'json', + version: 1, + private_key_passphrase: '', + private_key: '', + password: '', + json_object: '', + }, + type: 'credential', + id: 'credjson_123', + relationships: {}, + }, + }); + }); }); diff --git a/addons/core/translations/resources/en-us.yaml b/addons/core/translations/resources/en-us.yaml index eca53bbed..32f934c43 100644 --- a/addons/core/translations/resources/en-us.yaml +++ b/addons/core/translations/resources/en-us.yaml @@ -436,10 +436,12 @@ credential: types: username_password: Username & Password ssh_private_key: Username & Key Pair + json: JSON unknown: Unknown help: - username_password: This host requires a username and password to connect. - ssh_private_key: This host requires a username, public key, and private key to connect. + username_password: Connect using the provided username and password. + ssh_private_key: Connect using a username, public key, and private key. + json: Connect using a JSON blob containing the credentials. form: username: label: Username