refactor: 💡sb dynamic credential type secret fields and how we serialize (#1907)
* refactor: 💡 secret fields and how we serialize
This commit is contained in:
1 parent
1b84a70ca0
commit
d0a505558b
10 files changed
+85
-36
No files matched your search
@@ -73,6 +73,13 @@ export default class GeneratedStorageBucketModel extends BaseModel {
|
||||
})
|
||||
region;
|
||||
|
||||
//this field is not used in the UI, but this is kept here to replicate the API
|
||||
@attr('string', {
|
||||
readOnly: true,
|
||||
isNestedAttribute: true,
|
||||
})
|
||||
secrets_hmac;
|
||||
|
||||
@attr('boolean', {
|
||||
isNestedAttribute: true,
|
||||
description: 'If set to `true`, credential rotation is not performed.',
|
||||
|
||||
@@ -22,6 +22,9 @@ export const TYPES_CREDENTIALS = Object.freeze([
|
||||
]);
|
||||
|
||||
export default class StorageBucketModel extends GeneratedStorageBucketModel {
|
||||
// attributes
|
||||
|
||||
#credentialType;
|
||||
/**
|
||||
* True if the storage bucket is a plugin.
|
||||
* @type {boolean}
|
||||
@@ -31,18 +34,21 @@ export default class StorageBucketModel extends GeneratedStorageBucketModel {
|
||||
}
|
||||
|
||||
/**
|
||||
* If the storage bucket has role_arn, return dynamic
|
||||
* otherwise return the default static type.
|
||||
* Gets a value from credentialType
|
||||
* @type {string}
|
||||
*/
|
||||
get credentialType() {
|
||||
if (this.role_arn && !this.access_key_id) {
|
||||
return TYPE_CREDENTIAL_DYNAMIC;
|
||||
} else {
|
||||
return TYPE_CREDENTIAL_STATIC;
|
||||
}
|
||||
if (!this.#credentialType) return 'static';
|
||||
return this.#credentialType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a value to credentialType
|
||||
* @type {string}
|
||||
*/
|
||||
set credentialType(type) {
|
||||
this.#credentialType = type;
|
||||
}
|
||||
/**
|
||||
* True if the storage bucket is an unknown type.
|
||||
* @type {boolean}
|
||||
|
||||
@@ -49,17 +49,22 @@ export default class StorageBucketSerializer extends ApplicationSerializer {
|
||||
json.attributes[key] = null;
|
||||
}
|
||||
|
||||
//json.secrets is only present, if there are any updates to the secret fields
|
||||
if (options.isNestedSecret && json.secrets) {
|
||||
if (!fieldsByCredentialType[credentialType].includes(key)) {
|
||||
delete json.secrets;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
normalize(typeClass, hash, ...rest) {
|
||||
const normalizedHash = structuredClone(hash);
|
||||
const normalized = super.normalize(typeClass, normalizedHash, ...rest);
|
||||
|
||||
// Remove secret fields so we don't track them after creating/updating
|
||||
if (normalizedHash?.secrets?.access_key_id) {
|
||||
delete normalizedHash.secrets.access_key_id;
|
||||
delete normalizedHash.secrets.secret_access_key;
|
||||
}
|
||||
return super.normalize(typeClass, normalizedHash, ...rest);
|
||||
normalized.data.attributes.access_key_id = null;
|
||||
normalized.data.attributes.secret_access_key = null;
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user