Types for aws host catalogs (#2514)

assume role dynamic AWS credentials
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2024-10-09 14:49:22 -07:00
1 parent 0d55812f64
commit 965e82473d
11 files changed
+538 -89

No files matched your search

@@ -66,7 +66,6 @@ export default class GeneratedHostCatalogModel extends BaseModel {
// AWS specific
@attr('string', {
for: 'plugin',
description:
'An expression used to filter the workers that have network access to a service that is hosting the external object store.',
})
@@ -78,6 +77,7 @@ export default class GeneratedHostCatalogModel extends BaseModel {
})
region;
// AWS static credentials
@attr('string', {
isNestedSecret: true,
description: '',
@@ -90,6 +90,31 @@ export default class GeneratedHostCatalogModel extends BaseModel {
})
secret_access_key;
// AWS dynamic credentials
@attr('string', {
isNestedAttribute: true,
description: 'The role ARN to use.',
})
role_arn;
@attr('string', {
isNestedAttribute: true,
description: 'The role external ID to use.',
})
role_external_id;
@attr('string', {
isNestedAttribute: true,
description: 'The role session to use.',
})
role_session_name;
@attr('object-as-array', {
isNestedAttribute: true,
description: 'The role tags to use.',
})
role_tags;
// Azure specific
@attr('string', {
+32
View File
@@ -19,9 +19,18 @@ export const TYPES_HOST_CATALOG_PLUGIN = [
TYPE_HOST_CATALOG_PLUGIN_AZURE,
];
export const TYPE_CREDENTIAL_STATIC = 'static-credential';
export const TYPE_CREDENTIAL_DYNAMIC = 'dynamic-credential';
export const TYPES_CREDENTIALS = Object.freeze([
TYPE_CREDENTIAL_STATIC,
TYPE_CREDENTIAL_DYNAMIC,
]);
export default class HostCatalogModel extends GeneratedHostCatalogModel {
// =attributes
#credentialType;
/**
* True if the host catalog is static.
* @type {boolean}
@@ -38,6 +47,29 @@ export default class HostCatalogModel extends GeneratedHostCatalogModel {
return this.type === 'plugin';
}
/**
* Gets a value from credentialType
* @type {string}
*/
get credentialType() {
if (!this.#credentialType) {
if (this.role_arn) {
this.#credentialType = TYPE_CREDENTIAL_DYNAMIC;
} else {
this.#credentialType = TYPE_CREDENTIAL_STATIC;
}
}
return this.#credentialType;
}
/**
* Sets a value to credentialType
* @type {string}
*/
set credentialType(type) {
this.#credentialType = type;
}
/**
* True if the host catalog is an unknown type.
* @type {boolean}
+46 -25
View File
@@ -4,30 +4,53 @@
*/
import ApplicationSerializer from './application';
import {
TYPE_HOST_CATALOG_PLUGIN_AWS,
TYPE_HOST_CATALOG_PLUGIN_AZURE,
} from '../models/host-catalog';
const fieldsByType = {
aws: [
'disable_credential_rotation',
'region',
// 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': [
'access_key_id',
'secret_access_key',
'region',
'disable_credential_rotation',
'worker_filter',
],
azure: [
'dynamic-credential': [
'role_arn',
'role_external_id',
'role_session_name',
'role_tags',
'region',
'disable_credential_rotation',
'tenant_id',
'client_id',
'subscription_id',
'secret_id',
'secret_value',
'worker_filter',
],
};
export default class HostCatalogSerializer extends ApplicationSerializer {
// =methods
serialize(snapshot) {
const { compositeType } = snapshot.record;
const serialized = super.serialize(...arguments);
switch (snapshot.record.compositeType) {
// 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;
}
switch (compositeType) {
case 'static':
return this.serializeStatic(...arguments);
default:
@@ -37,31 +60,28 @@ export default class HostCatalogSerializer extends ApplicationSerializer {
serializeAttribute(snapshot, json, key, attribute) {
const value = super.serializeAttribute(...arguments);
const { isPlugin, compositeType } = snapshot.record;
const { compositeType, credentialType, isPlugin } = snapshot.record;
const { options } = attribute;
// Delete any fields that don't belong to the compositeType
if (
isPlugin &&
json &&
options?.for &&
!options.for.includes(compositeType) &&
!fieldsByType[compositeType].includes(key)
) {
delete json[key];
}
const fields =
compositeType === TYPE_HOST_CATALOG_PLUGIN_AZURE
? azureFields
: AWSfieldsWithCredentialType[credentialType];
// Delete any nested attribute fields that don't belong to the record type
if (isPlugin && options.isNestedAttribute && json.attributes) {
if (!fieldsByType[compositeType].includes(key)) {
if (!fields.includes(key)) {
delete json.attributes[key];
}
}
// Delete any secret fields that don't belong to the record type
if (isPlugin && options.isNestedSecret && json.secrets) {
if (!fieldsByType[compositeType].includes(key)) {
if (!fields.includes(key)) {
delete json.secrets[key];
if (json['secrets'] && Object.keys(json.secrets).length === 0) {
delete json.secrets;
}
}
}
return value;
@@ -72,6 +92,7 @@ export default class HostCatalogSerializer extends ApplicationSerializer {
// Delete unnecessary fields for static host-catalog
delete serialized.attributes;
delete serialized.secrets;
delete serialized.worker_filter;
return serialized;
}
}