diff --git a/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/errors/entity_definition_id_too_long_error.ts b/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/errors/entity_definition_id_invalid.ts similarity index 75% rename from x-pack/plugins/observability_solution/entity_manager/server/lib/entities/errors/entity_definition_id_too_long_error.ts rename to x-pack/plugins/observability_solution/entity_manager/server/lib/entities/errors/entity_definition_id_invalid.ts index fb78f237bee37..4d6110f661aa0 100644 --- a/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/errors/entity_definition_id_too_long_error.ts +++ b/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/errors/entity_definition_id_invalid.ts @@ -5,9 +5,9 @@ * 2.0. */ -export class EntityDefinitionIdTooLong extends Error { +export class EntityDefinitionIdInvalid extends Error { constructor(message: string) { super(message); - this.name = 'EntityDefinitionIdTooLong'; + this.name = 'EntityDefinitionIdInvalid'; } } diff --git a/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/transform/validate_transform_ids.test.ts b/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/transform/validate_transform_ids.test.ts index 9a8d23b16c973..9320cd1324b75 100644 --- a/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/transform/validate_transform_ids.test.ts +++ b/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/transform/validate_transform_ids.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { EntityDefinitionIdTooLong } from '../errors/entity_definition_id_too_long_error'; +import { EntityDefinitionIdInvalid } from '../errors/entity_definition_id_invalid'; import { entityDefinition } from '../helpers/fixtures/entity_definition'; import { validateDefinitionCanCreateValidTransformIds } from './validate_transform_ids'; @@ -20,7 +20,25 @@ describe('validateDefinitionCanCreateValidTransformIds(definition)', () => { 'a-really-really-really-really-really-really-really-really-really-really-long-id'; expect(() => { - validateDefinitionCanCreateValidTransformIds(entityDefinition); - }).toThrow(EntityDefinitionIdTooLong); + validateDefinitionCanCreateValidTransformIds(entityDefinitionWithLongID); + }).toThrow(EntityDefinitionIdInvalid); + }); + + it('should throw an error for a definition ID which contains invalid characters', () => { + const entityDefinitionWithDots = entityDefinition; + entityDefinitionWithDots.id = 'dots.are.not.allowed'; + + expect(() => { + validateDefinitionCanCreateValidTransformIds(entityDefinitionWithDots); + }).toThrow(EntityDefinitionIdInvalid); + }); + + it('should throw an error for a definition ID which ends with dash or underscore', () => { + const entityDefinitionEndingInUnderscore = entityDefinition; + entityDefinitionEndingInUnderscore.id = 'looking-good-but_'; + + expect(() => { + validateDefinitionCanCreateValidTransformIds(entityDefinitionEndingInUnderscore); + }).toThrow(EntityDefinitionIdInvalid); }); }); diff --git a/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/transform/validate_transform_ids.ts b/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/transform/validate_transform_ids.ts index 320505134dd9b..baf010f2dc2df 100644 --- a/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/transform/validate_transform_ids.ts +++ b/x-pack/plugins/observability_solution/entity_manager/server/lib/entities/transform/validate_transform_ids.ts @@ -5,13 +5,13 @@ * 2.0. */ -const TRANSFORM_ID_MAX_LENGTH = 64; - import { EntityDefinition } from '@kbn/entities-schema'; -import { EntityDefinitionIdTooLong } from '../errors/entity_definition_id_too_long_error'; +import { EntityDefinitionIdInvalid } from '../errors/entity_definition_id_invalid'; import { generateHistoryTransformId } from './generate_history_transform_id'; import { generateLatestTransformId } from './generate_latest_transform_id'; +const TRANSFORM_ID_MAX_LENGTH = 64; + export function validateDefinitionCanCreateValidTransformIds(definition: EntityDefinition) { const historyTransformId = generateHistoryTransformId(definition); const latestTransformId = generateLatestTransformId(definition); @@ -20,10 +20,17 @@ export function validateDefinitionCanCreateValidTransformIds(definition: EntityD TRANSFORM_ID_MAX_LENGTH - Math.max(historyTransformId.length, latestTransformId.length); if (spareChars < 0) { - throw new EntityDefinitionIdTooLong( + throw new EntityDefinitionIdInvalid( `Entity definition ID is too long (max = ${ definition.id.length + spareChars }); the resulting transform ID will be invalid` ); } + + const transformIdRegex = /^[a-z0-9][a-z0-9_-]*[a-z0-9]$/; + if (!transformIdRegex.test(definition.id)) { + throw new EntityDefinitionIdInvalid( + 'Entity definition ID must contain only lowercase alphanumeric characters (a-z and 0-9), hyphens, and underscores. It must also start and end with alphanumeric characters.' + ); + } }