2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-01 09:39:41 -06:00
|
|
|
*/
|
|
|
|
|
|
2021-07-08 11:38:41 -04:00
|
|
|
import ApplicationSerializer from './application';
|
2023-02-24 13:11:17 -05:00
|
|
|
import {
|
|
|
|
|
TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
|
|
|
|
|
TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
|
2024-02-23 10:07:52 -08:00
|
|
|
options,
|
2023-02-24 13:11:17 -05:00
|
|
|
} from '../models/credential-library';
|
2021-07-08 11:38:41 -04:00
|
|
|
|
2022-06-28 19:30:26 -04:00
|
|
|
export default class CredentialLibrarySerializer extends ApplicationSerializer {
|
2022-09-20 18:24:18 -04:00
|
|
|
// =properties
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
*/
|
|
|
|
|
serializeScopeID = false;
|
|
|
|
|
|
2022-06-28 19:30:26 -04:00
|
|
|
/**
|
|
|
|
|
* @override
|
|
|
|
|
* @method serialize
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
*/
|
2023-02-24 13:11:17 -05:00
|
|
|
serialize(snapshot) {
|
|
|
|
|
switch (snapshot.record.type) {
|
|
|
|
|
case TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC:
|
|
|
|
|
return this.serializeVaultGeneric(...arguments);
|
|
|
|
|
case TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE:
|
2025-04-25 10:23:51 -05:00
|
|
|
return this.serializeVaultSshCertificate(...arguments);
|
2023-02-24 13:11:17 -05:00
|
|
|
default:
|
|
|
|
|
return super.serialize(...arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 13:38:42 -07:00
|
|
|
serializeVaultGeneric(snapshot) {
|
|
|
|
|
const { isNew } = snapshot;
|
2022-06-28 19:30:26 -04:00
|
|
|
const serialized = super.serialize(...arguments);
|
2024-02-23 10:07:52 -08:00
|
|
|
|
2022-06-28 19:30:26 -04:00
|
|
|
if (serialized.attributes) {
|
2023-02-24 13:11:17 -05:00
|
|
|
// Set `http_request_body` only if `http_method` is POST
|
|
|
|
|
// otherwise set to null to have the field removed
|
|
|
|
|
if (!serialized.attributes?.http_method?.match(/post/i)) {
|
|
|
|
|
serialized.attributes.http_request_body = null;
|
2023-02-01 13:46:23 -05:00
|
|
|
}
|
2025-07-25 10:57:56 -05:00
|
|
|
this.handleCredentialMappingOverrides(serialized, isNew);
|
2024-06-03 13:38:42 -07:00
|
|
|
return serialized;
|
2022-06-28 19:30:26 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-25 10:23:51 -05:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-25 10:57:56 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle credential mapping overrides for serialization.
|
|
|
|
|
* @param {Object} serialized - The serialized object.
|
|
|
|
|
* @param {boolean} isNew - Indicates if the record is new.
|
|
|
|
|
*/
|
|
|
|
|
handleCredentialMappingOverrides(serialized, isNew) {
|
|
|
|
|
const { credential_type, credential_mapping_overrides } = serialized;
|
2025-11-14 15:49:32 -08:00
|
|
|
if (Object.keys(credential_mapping_overrides).length === 0) {
|
|
|
|
|
serialized.credential_mapping_overrides = null;
|
|
|
|
|
}
|
2025-07-25 10:57:56 -05:00
|
|
|
// 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;
|
|
|
|
|
}, {});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-28 19:30:26 -04:00
|
|
|
}
|