Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CM] Soften response validation #166919

Merged
merged 15 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
SavedObjectsUpdateOptions,
SavedObjectsFindResult,
} from '@kbn/core-saved-objects-api-server';
import type { Logger } from '@kbn/logging';
import { pick } from 'lodash';
import type {
CMCrudTypes,
Expand Down Expand Up @@ -136,6 +137,9 @@ export interface SOContentStorageConstrutorParams<Types extends CMCrudTypes> {
searchArgsToSOFindOptions?: SearchArgsToSOFindOptions<Types>;
enableMSearch?: boolean;
mSearchAdditionalSearchFields?: string[];

logger: Logger;
throwOnResultValidationError?: boolean;
}

export abstract class SOContentStorage<Types extends CMCrudTypes>
Expand All @@ -155,7 +159,11 @@ export abstract class SOContentStorage<Types extends CMCrudTypes>
enableMSearch,
allowedSavedObjectAttributes,
mSearchAdditionalSearchFields,
logger,
throwOnResultValidationError,
}: SOContentStorageConstrutorParams<Types>) {
this.logger = logger;
this.throwOnResultValidationError = throwOnResultValidationError ?? false;
this.savedObjectType = savedObjectType;
this.cmServicesDefinition = cmServicesDefinition;
this.createArgsToSoCreateOptions =
Expand Down Expand Up @@ -194,6 +202,8 @@ export abstract class SOContentStorage<Types extends CMCrudTypes>
}
}

private throwOnResultValidationError: boolean;
private logger: Logger;
private savedObjectType: SOContentStorageConstrutorParams<Types>['savedObjectType'];
private cmServicesDefinition: SOContentStorageConstrutorParams<Types>['cmServicesDefinition'];
private createArgsToSoCreateOptions: CreateArgsToSoCreateOptions<Types>;
Expand Down Expand Up @@ -280,13 +290,28 @@ export abstract class SOContentStorage<Types extends CMCrudTypes>
createOptions
);

const result = {
item: savedObjectToItem(savedObject, this.allowedSavedObjectAttributes, false),
};

const validationError = transforms.create.out.result.validate(result);
if (validationError) {
if (this.throwOnResultValidationError) {
Dosant marked this conversation as resolved.
Show resolved Hide resolved
throw Boom.badRequest(`Invalid response. ${validationError.message}`);
} else {
this.logger.warn(`Invalid response. ${validationError.message}`);
}
}

// Validate DB response and DOWN transform to the request version
const { value, error: resultError } = transforms.create.out.result.down<
Types['CreateOut'],
Types['CreateOut']
>({
item: savedObjectToItem(savedObject, this.allowedSavedObjectAttributes, false),
});
>(
Dosant marked this conversation as resolved.
Show resolved Hide resolved
result,
undefined, // do not override version
{ validate: false } // validation is done above
);

if (resultError) {
throw Boom.badRequest(`Invalid response. ${resultError.message}`);
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-content-management-utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"@kbn/core-saved-objects-api-server",
"@kbn/config-schema",
"@kbn/object-versioning",
"@kbn/logging",
]
}