Refactor key value comp (#2180)

* refactor: 💡 keyvalue comp
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2024-06-03 13:38:42 -07:00
1 parent 9b403b3159
commit 6397cd80a6
22 files changed
+531 -405

No files matched your search

@@ -81,7 +81,7 @@ export default class GeneratedCredentialLibraryModel extends BaseModel {
})
credential_type;
@attr('object', {
@attr('object-as-array', {
for: 'vault-generic',
description: 'It indicates the credential mapping overrides.',
})
@@ -13,15 +13,12 @@ export const options = {
key_type: ['ed25519', 'ecdsa', 'rsa'],
credential_types: ['ssh_private_key', 'username_password'],
mapping_overrides: {
username_password: {
username_attribute: 'Username',
password_attribute: 'Password',
},
ssh_private_key: {
private_key_attribute: 'Private Key',
private_key_passphrase_attribute: 'Private Key Passphrase',
username_attribute: 'Username',
},
username_password: ['username_attribute', 'password_attribute'],
ssh_private_key: [
'private_key_attribute',
'private_key_passphrase_attribute',
'username_attribute',
],
},
};
@@ -32,32 +32,9 @@ export default class CredentialLibrarySerializer extends ApplicationSerializer {
return super.serialize(...arguments);
}
}
serializeAttribute(snapshot, json) {
const value = super.serializeAttribute(...arguments);
const { credential_type, type, credential_mapping_overrides, isNew } =
snapshot.record;
if (
type === TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC &&
credential_mapping_overrides &&
credential_type &&
!isNew
) {
// API expects to send null to fields if it is undefined or deleted
json.credential_mapping_overrides = Object.keys(
options.mapping_overrides[credential_type],
).reduce((obj, key) => {
if (credential_mapping_overrides[key]) {
obj[key] = credential_mapping_overrides[key];
} else {
obj[key] = null;
}
return obj;
}, {});
return value;
}
}
serializeVaultGeneric() {
serializeVaultGeneric(snapshot) {
const { isNew } = snapshot;
const serialized = super.serialize(...arguments);
if (serialized.attributes) {
@@ -66,7 +43,22 @@ export default class CredentialLibrarySerializer extends ApplicationSerializer {
if (!serialized.attributes?.http_method?.match(/post/i)) {
serialized.attributes.http_request_body = null;
}
const { credential_type, credential_mapping_overrides } = serialized;
// API expects to send null to fields if it is undefined or deleted
if (credential_mapping_overrides && !isNew) {
serialized.credential_mapping_overrides = options.mapping_overrides[
credential_type
].reduce((obj, key) => {
if (credential_mapping_overrides[key]) {
obj[key] = credential_mapping_overrides[key];
} else {
obj[key] = null;
}
return obj;
}, {});
}
return serialized;
}
return serialized;
}
}
@@ -9,13 +9,13 @@ export default class AccountValueMapArrayTransform extends Transform {
deserialize(serialized) {
const accountValues = serialized || [];
return accountValues.map((accountValueString) => ({
from: accountValueString.split('=')[0],
to: accountValueString.split('=')[1],
key: accountValueString.split('=')[0],
value: accountValueString.split('=')[1],
}));
}
serialize(deserialized) {
const accountValues = deserialized || [];
return accountValues.map(({ from, to }) => `${from}=${to}`);
return accountValues.map(({ key, value }) => `${key}=${value}`);
}
}
@@ -139,7 +139,7 @@ module('Unit | Serializer | application', function (hooks) {
type: TYPE_AUTH_METHOD_OIDC, //has both string and array `for` option
name: 'OIDC Auth Method',
state: 'foo',
account_claim_maps: [{ from: 'foo', to: 'bar' }],
account_claim_maps: [{ key: 'foo', value: 'bar' }],
claims_scopes: [{ value: 'profile' }, { value: 'email' }],
signing_algorithms: [{ value: 'RS256' }, { value: 'RS384' }],
allowed_audiences: [
@@ -45,7 +45,7 @@ module('Unit | Serializer | auth method', function (hooks) {
type: TYPE_AUTH_METHOD_OIDC,
name: 'OIDC Auth Method',
state: 'foo',
account_claim_maps: [{ from: 'foo', to: 'bar' }],
account_claim_maps: [{ key: 'foo', value: 'bar' }],
claims_scopes: [{ value: 'profile' }, { value: 'email' }],
signing_algorithms: [{ value: 'RS256' }, { value: 'RS384' }],
allowed_audiences: [
@@ -96,7 +96,7 @@ module('Unit | Serializer | auth method', function (hooks) {
type: TYPE_AUTH_METHOD_LDAP,
name: 'LDAP Auth Method',
state: 'baz',
account_attribute_maps: [{ from: 'foo', to: 'bar' }],
account_attribute_maps: [{ key: 'foo', value: 'bar' }],
urls: [{ value: 'ldap://ldap.example.com' }],
use_token_groups: false,
start_tls: true,
@@ -160,7 +160,7 @@ module('Unit | Serializer | auth method', function (hooks) {
type: TYPE_AUTH_METHOD_OIDC,
name: 'OIDC Auth Method',
state: 'foo',
account_claim_maps: [{ from: 'foo', to: 'bar' }],
account_claim_maps: [{ key: 'foo', value: 'bar' }],
claims_scopes: [{ value: 'profile' }, { value: 'email' }],
signing_algorithms: [{ value: 'RS256' }, { value: 'RS384' }],
allowed_audiences: [
@@ -210,7 +210,7 @@ module('Unit | Serializer | auth method', function (hooks) {
type: TYPE_AUTH_METHOD_LDAP,
name: 'LDAP Auth Method',
state: 'baz',
account_attribute_maps: [{ from: 'foo', to: 'bar' }],
account_attribute_maps: [{ key: 'foo', value: 'bar' }],
urls: [{ value: 'ldap://ldap.example.com' }],
use_token_groups: false,
start_tls: true,
@@ -410,8 +410,8 @@ module('Unit | Serializer | auth method', function (hooks) {
prompts,
} = record;
assert.deepEqual(account_claim_maps, [
{ from: 'from', to: 'to' },
{ from: 'foo', to: 'bar' },
{ key: 'from', value: 'to' },
{ key: 'foo', value: 'bar' },
]);
assert.deepEqual(claims_scopes, [{ value: 'profile' }, { value: 'email' }]);
assert.deepEqual(allowed_audiences, [
@@ -481,8 +481,8 @@ module('Unit | Serializer | auth method', function (hooks) {
certificates: recordCertificates,
} = record;
assert.deepEqual(account_attribute_maps, [
{ from: 'from', to: 'to' },
{ from: 'foo', to: 'bar' },
{ key: 'from', value: 'to' },
{ key: 'foo', value: 'bar' },
]);
assert.deepEqual(urls, [{ value: ldapUrl }]);
assert.deepEqual(recordCertificates, [
@@ -24,7 +24,9 @@ module('Unit | Serializer | credential library', function (hooks) {
http_method: 'GET',
version: 1,
credential_type: 'ssh_private_key',
credential_mapping_overrides: { username_attribute: 'user' },
credential_mapping_overrides: [
{ key: 'username_attribute', value: 'user' },
],
});
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
@@ -61,7 +63,9 @@ module('Unit | Serializer | credential library', function (hooks) {
http_method: 'GET',
version: 1,
credential_type: 'ssh_private_key',
credential_mapping_overrides: {},
credential_mapping_overrides: [
{ key: 'private_key_attribute', value: 'test' },
],
},
},
});
@@ -75,7 +79,7 @@ module('Unit | Serializer | credential library', function (hooks) {
description: 'Description',
credential_type: 'ssh_private_key',
credential_mapping_overrides: {
private_key_attribute: null,
private_key_attribute: 'test',
private_key_passphrase_attribute: null,
username_attribute: null,
},
@@ -116,7 +120,7 @@ module('Unit | Serializer | credential library', function (hooks) {
name: null,
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
credential_type: null,
credential_mapping_overrides: {},
credential_mapping_overrides: null,
});
assert.deepEqual(serializedVaultSSHCertificateRecord, {
attributes: {
@@ -157,7 +161,7 @@ module('Unit | Serializer | credential library', function (hooks) {
name: null,
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
credential_type: null,
credential_mapping_overrides: {},
credential_mapping_overrides: null,
},
'http_request_body attribute is not expected',
);
@@ -177,7 +181,7 @@ module('Unit | Serializer | credential library', function (hooks) {
name: null,
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
credential_type: null,
credential_mapping_overrides: {},
credential_mapping_overrides: null,
},
'http_request_body attribute is expected',
);
@@ -13,16 +13,16 @@ module('Unit | Transform | account value map array', function (hooks) {
const transform = this.owner.lookup('transform:account-value-map-array');
const deserialized = transform.deserialize(['oid=sub', 'full_name=name']);
assert.deepEqual(deserialized, [
{ from: 'oid', to: 'sub' },
{ from: 'full_name', to: 'name' },
{ key: 'oid', value: 'sub' },
{ key: 'full_name', value: 'name' },
]);
});
test('it serializes an array of account claims', function (assert) {
const transform = this.owner.lookup('transform:account-value-map-array');
const serialized = transform.serialize([
{ from: 'oid', to: 'sub' },
{ from: 'full_name', to: 'name' },
{ key: 'oid', value: 'sub' },
{ key: 'full_name', value: 'name' },
]);
assert.deepEqual(serialized, ['oid=sub', 'full_name=name']);
});