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
*/
2022-01-20 08:32:13 -08:00
import ApplicationSerializer from './application' ;
2024-10-09 14:49:22 -07:00
import {
TYPE_HOST_CATALOG_PLUGIN_AWS ,
TYPE_HOST_CATALOG_PLUGIN_AZURE ,
2024-10-10 12:44:12 -07:00
TYPE_CREDENTIAL_DYNAMIC ,
2024-10-09 14:49:22 -07:00
} from '../models/host-catalog' ;
2022-01-20 08:32:13 -08:00
2024-10-09 14:49:22 -07:00
// These fields are Azure specific
const azureFields = [
'disable_credential_rotation' ,
'tenant_id' ,
'client_id' ,
'subscription_id' ,
'secret_id' ,
'secret_value' ,
];
// These fields are AWS specific, AWS provider now has two credentialTypes: static and dynamic [not to be confused with static and dynamic host-catalogs]
const AWSfieldsWithCredentialType = {
'static-credential' : [
2022-01-20 16:47:46 -05:00
'access_key_id' ,
'secret_access_key' ,
2024-10-09 14:49:22 -07:00
'region' ,
'disable_credential_rotation' ,
2024-10-08 10:58:52 -07:00
'worker_filter' ,
2022-01-20 16:47:46 -05:00
],
2024-10-09 14:49:22 -07:00
'dynamic-credential' : [
'role_arn' ,
'role_external_id' ,
'role_session_name' ,
'role_tags' ,
'region' ,
2022-01-20 16:47:46 -05:00
'disable_credential_rotation' ,
2024-10-09 14:49:22 -07:00
'worker_filter' ,
2022-01-20 16:47:46 -05:00
],
};
2022-01-20 08:32:13 -08:00
export default class HostCatalogSerializer extends ApplicationSerializer {
serialize ( snapshot ) {
2024-10-10 12:44:12 -07:00
const { compositeType , credentialType } = snapshot . record ;
2022-01-20 08:32:13 -08:00
const serialized = super . serialize (... arguments );
2024-10-09 14:49:22 -07:00
// This is a hack to remove the worker_filter field from the serialized data if the compositeType is not AWS
// Ideally we should refactor the model to include the compositeType using `for` attribute
if ( compositeType !== TYPE_HOST_CATALOG_PLUGIN_AWS ) {
delete serialized . worker_filter ;
}
2024-10-10 12:44:12 -07:00
// By default, disable credential rotation for dynamic credentials
if ( credentialType === TYPE_CREDENTIAL_DYNAMIC ) {
serialized . attributes . disable_credential_rotation = true ;
}
2024-10-09 14:49:22 -07:00
switch ( compositeType ) {
2022-01-20 08:32:13 -08:00
case 'static' :
return this . serializeStatic (... arguments );
default :
return serialized ;
}
}
2022-01-20 16:47:46 -05:00
serializeAttribute ( snapshot , json , key , attribute ) {
const value = super . serializeAttribute (... arguments );
2024-10-09 14:49:22 -07:00
const { compositeType , credentialType , isPlugin } = snapshot . record ;
2022-01-20 16:47:46 -05:00
const { options } = attribute ;
2024-10-09 14:49:22 -07:00
const fields =
compositeType === TYPE_HOST_CATALOG_PLUGIN_AZURE
? azureFields
: AWSfieldsWithCredentialType [ credentialType ];
2024-10-08 10:58:52 -07:00
2024-10-18 14:33:14 -07:00
// Delete any nested attribute fields that don't belong to the record's compositeType
2022-01-20 16:47:46 -05:00
if ( isPlugin && options . isNestedAttribute && json . attributes ) {
2024-10-18 14:33:14 -07:00
// If compositeType is AWS, delete azure fields from the json, and set the rest that don't belong to respective compositeType to null
if ( compositeType === TYPE_HOST_CATALOG_PLUGIN_AWS ) {
if ( ! AWSfieldsWithCredentialType [ credentialType ]. includes ( key )) {
if ( azureFields . includes ( key )) {
delete json . attributes [ key ];
} else {
json . attributes [ key ] = null ;
}
}
}
// If compositeType is Azure, delete AWS fields from the json
if (
compositeType === TYPE_HOST_CATALOG_PLUGIN_AZURE &&
! azureFields . includes ( key )
) {
2022-01-20 16:47:46 -05:00
delete json . attributes [ key ];
2024-10-08 10:58:52 -07:00
}
2022-01-20 16:47:46 -05:00
}
2024-10-08 10:58:52 -07:00
// Delete any secret fields that don't belong to the record type
2022-01-21 12:55:04 -08:00
if ( isPlugin && options . isNestedSecret && json . secrets ) {
2024-10-09 14:49:22 -07:00
if ( ! fields . includes ( key )) {
2024-10-08 10:58:52 -07:00
delete json . secrets [ key ];
2024-10-09 14:49:22 -07:00
if ( json [ 'secrets' ] && Object . keys ( json . secrets ). length === 0 ) {
delete json . secrets ;
}
2024-10-08 10:58:52 -07:00
}
2022-01-21 12:55:04 -08:00
}
2022-01-20 16:47:46 -05:00
return value ;
}
2022-01-20 08:32:13 -08:00
serializeStatic () {
const serialized = super . serialize (... arguments );
// Delete unnecessary fields for static host-catalog
delete serialized . attributes ;
2022-01-21 12:55:04 -08:00
delete serialized . secrets ;
2024-10-18 14:33:14 -07:00
2022-01-20 08:32:13 -08:00
return serialized ;
}
}