Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -5,6 +5,8 @@
* 2.0.
*/

export * from './src';

export const ALERTING_V2_RULE_API_PATH = '/api/alerting/v2/rules' as const;
export const ALERTING_V2_ALERT_API_PATH = '/api/alerting/v2/alerts' as const;
export const ALERTING_V2_NOTIFICATION_POLICY_API_PATH =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

/** Artifact type identifier for runbooks */
export const RUNBOOK_ARTIFACT_TYPE = 'runbook';

/** Default maximum character length for artifact values (applies when no type-specific override exists) */
export const DEFAULT_ARTIFACT_VALUE_LIMIT = 1024;

/**
* Type-specific artifact value length limits.
*
* To raise or add a limit for a new artifact type, add an entry here.
* The framework schema resolves: `ARTIFACT_VALUE_LIMITS[type] ?? DEFAULT_ARTIFACT_VALUE_LIMIT`.
* No framework code changes are needed — only this map.
*/
export const ARTIFACT_VALUE_LIMITS: Readonly<Record<string, number>> = {
[RUNBOOK_ARTIFACT_TYPE]: 50_000,
};

/** The highest value in ARTIFACT_VALUE_LIMITS (used as the Zod base .max()) */
export const MAX_ARTIFACT_VALUE_LIMIT = Math.max(
DEFAULT_ARTIFACT_VALUE_LIMIT,
...Object.values(ARTIFACT_VALUE_LIMITS)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export * from './artifacts';
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import {
EuiTitle,
} from '@elastic/eui';
import { useController, useFormContext } from 'react-hook-form';
import { RUNBOOK_ARTIFACT_TYPE } from '@kbn/alerting-v2-constants';
import { RunbookField } from '../fields/runbook_field';
import type { FormValues } from '../types';
import { FieldGroup } from './field_group';

const RUNBOOK_ARTIFACT_TYPE = 'runbook';

export const AttachmentRunbookFieldGroup: React.FC = () => {
const { setValue, control } = useFormContext<FormValues>();
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ import {
EuiButtonEmpty,
} from '@elastic/eui';
import { useController, useFormContext } from 'react-hook-form';
import {
RUNBOOK_ARTIFACT_TYPE,
ARTIFACT_VALUE_LIMITS,
DEFAULT_ARTIFACT_VALUE_LIMIT,
} from '@kbn/alerting-v2-constants';
import type { FormValues } from '../types';

const RUNBOOK_ROW_ID = 'ruleV2FormRunbookField';
const RUNBOOK_ARTIFACT_TYPE = 'runbook';
const RUNBOOK_MAX_LENGTH =
ARTIFACT_VALUE_LIMITS[RUNBOOK_ARTIFACT_TYPE] ?? DEFAULT_ARTIFACT_VALUE_LIMIT;
const createRunbookArtifactId = () =>
`runbook-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;

Expand All @@ -45,6 +51,8 @@ export const RunbookField: React.FC<RunbookFieldProps> = ({ isOpen, onClose }) =
const runbookArtifact = artifacts.find((artifact) => artifact.type === RUNBOOK_ARTIFACT_TYPE);
const runbookValue = runbookArtifact?.value ?? '';
const [draftRunbook, setDraftRunbook] = useState(runbookValue);
const trimmedLength = draftRunbook.trim().length;
const isOverLimit = trimmedLength > RUNBOOK_MAX_LENGTH;

useEffect(() => {
if (isOpen) {
Expand All @@ -53,6 +61,9 @@ export const RunbookField: React.FC<RunbookFieldProps> = ({ isOpen, onClose }) =
}, [isOpen, runbookValue]);

const handleSaveRunbook = () => {
if (isOverLimit) {
return;
}
const trimmedRunbook = draftRunbook.trim();
const nonRunbookArtifacts = artifacts.filter(
(artifact) => artifact.type !== RUNBOOK_ARTIFACT_TYPE
Expand Down Expand Up @@ -97,7 +108,19 @@ export const RunbookField: React.FC<RunbookFieldProps> = ({ isOpen, onClose }) =
})}
</EuiFormLabel>
<EuiSpacer size="s" />
<EuiFormRow fullWidth>
<EuiFormRow
fullWidth
isInvalid={isOverLimit}
error={
isOverLimit
? i18n.translate('xpack.alertingV2.ruleForm.runbookMaxLengthError', {
defaultMessage:
'Runbook must be at most {maxLength} characters. Current length: {currentLength}.',
values: { maxLength: RUNBOOK_MAX_LENGTH, currentLength: trimmedLength },
})
: undefined
}
>
<EuiMarkdownEditor
style={{ width: '100%' }}
value={draftRunbook}
Expand All @@ -117,7 +140,7 @@ export const RunbookField: React.FC<RunbookFieldProps> = ({ isOpen, onClose }) =
defaultMessage: 'Cancel',
})}
</EuiButtonEmpty>
<EuiButton onClick={handleSaveRunbook} size="s" color="primary" fill>
<EuiButton onClick={handleSaveRunbook} size="s" color="primary" fill disabled={isOverLimit}>
{i18n.translate('xpack.alertingV2.ruleForm.runbookSaveButton', {
defaultMessage: 'Add Runbook',
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import type {
CreateRuleData,
UpdateRuleData,
} from '@kbn/alerting-v2-schemas';
import { RUNBOOK_ARTIFACT_TYPE } from '@kbn/alerting-v2-constants';
import type { FormValues, StateTransition } from '../types';

const RUNBOOK_ARTIFACT_TYPE = 'runbook';
const createRunbookArtifactId = () =>
`runbook-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
type RuleArtifactPayload = Array<{ id: string; type: string; value: string }>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependsOn:
- '@kbn/react-query'
- '@kbn/search-types'
- '@kbn/alerting-v2-schemas'
- '@kbn/alerting-v2-constants'
- '@kbn/yaml-rule-editor'
- '@kbn/code-editor'
- '@kbn/monaco'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@kbn/react-query",
"@kbn/search-types",
"@kbn/alerting-v2-schemas",
"@kbn/alerting-v2-constants",
"@kbn/yaml-rule-editor",
"@kbn/code-editor",
"@kbn/monaco",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ project:
sourceRoot: x-pack/platform/packages/shared/response-ops/alerting-v2-schemas
dependsOn:
- '@kbn/zod'
- '@kbn/alerting-v2-constants'
tags:
- shared-common
- package
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* 2.0.
*/

import {
DEFAULT_ARTIFACT_VALUE_LIMIT,
ARTIFACT_VALUE_LIMITS,
RUNBOOK_ARTIFACT_TYPE,
} from '@kbn/alerting-v2-constants';
import { createRuleDataSchema, updateRuleDataSchema } from './rule_data_schema';

const validCreateData = {
Expand Down Expand Up @@ -638,6 +643,84 @@ describe('createRuleDataSchema', () => {
});
});

describe('artifacts value length', () => {
it('accepts a runbook artifact at the maximum allowed length', () => {
const result = createRuleDataSchema.safeParse({
...validCreateData,
artifacts: [
{
id: 'runbook-1',
type: RUNBOOK_ARTIFACT_TYPE,
value: 'a'.repeat(ARTIFACT_VALUE_LIMITS[RUNBOOK_ARTIFACT_TYPE]),
},
],
});
expect(result.success).toBe(true);
});

it('rejects a runbook artifact exceeding the maximum allowed length', () => {
const result = createRuleDataSchema.safeParse({
...validCreateData,
artifacts: [
{
id: 'runbook-1',
type: RUNBOOK_ARTIFACT_TYPE,
value: 'a'.repeat(ARTIFACT_VALUE_LIMITS[RUNBOOK_ARTIFACT_TYPE] + 1),
},
],
});
expect(result.success).toBe(false);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test the rejection message as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in here

if (!result.success) {
expect(result.error.issues).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: `Artifact value must be at most ${ARTIFACT_VALUE_LIMITS[RUNBOOK_ARTIFACT_TYPE]} characters for type "${RUNBOOK_ARTIFACT_TYPE}".`,
}),
])
);
}
});

it('accepts a non-runbook artifact at the default maximum length', () => {
const result = createRuleDataSchema.safeParse({
...validCreateData,
artifacts: [
{
id: 'artifact-1',
type: 'host',
value: 'a'.repeat(DEFAULT_ARTIFACT_VALUE_LIMIT),
},
],
});
expect(result.success).toBe(true);
});

it('rejects a non-runbook artifact exceeding the default maximum length', () => {
const result = createRuleDataSchema.safeParse({
...validCreateData,
artifacts: [
{
id: 'artifact-1',
type: 'host',
value: 'a'.repeat(DEFAULT_ARTIFACT_VALUE_LIMIT + 1),
},
],
});
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining(
`Artifact value must be at most ${DEFAULT_ARTIFACT_VALUE_LIMIT} characters`
),
}),
])
);
}
});
});

describe('required fields', () => {
it.each(['kind', 'metadata', 'schedule', 'evaluation'] as const)(
'rejects when required field "%s" is missing',
Expand Down Expand Up @@ -787,6 +870,80 @@ describe('updateRuleDataSchema', () => {
});
});

describe('artifacts value length', () => {
it('accepts a runbook artifact at the maximum allowed length', () => {
const result = updateRuleDataSchema.safeParse({
artifacts: [
{
id: 'runbook-1',
type: RUNBOOK_ARTIFACT_TYPE,
value: 'a'.repeat(ARTIFACT_VALUE_LIMITS[RUNBOOK_ARTIFACT_TYPE]),
},
],
});
expect(result.success).toBe(true);
});

it('rejects a runbook artifact exceeding the maximum allowed length', () => {
const result = updateRuleDataSchema.safeParse({
artifacts: [
{
id: 'runbook-1',
type: RUNBOOK_ARTIFACT_TYPE,
value: 'a'.repeat(ARTIFACT_VALUE_LIMITS[RUNBOOK_ARTIFACT_TYPE] + 1),
},
],
});
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: `Artifact value must be at most ${ARTIFACT_VALUE_LIMITS[RUNBOOK_ARTIFACT_TYPE]} characters for type "${RUNBOOK_ARTIFACT_TYPE}".`,
}),
])
);
}
});

it('accepts a non-runbook artifact at the default maximum length', () => {
const result = updateRuleDataSchema.safeParse({
artifacts: [
{
id: 'artifact-1',
type: 'host',
value: 'a'.repeat(DEFAULT_ARTIFACT_VALUE_LIMIT),
},
],
});
expect(result.success).toBe(true);
});

it('rejects a non-runbook artifact exceeding the default maximum length', () => {
const result = updateRuleDataSchema.safeParse({
artifacts: [
{
id: 'artifact-1',
type: 'host',
value: 'a'.repeat(DEFAULT_ARTIFACT_VALUE_LIMIT + 1),
},
],
});
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining(
`Artifact value must be at most ${DEFAULT_ARTIFACT_VALUE_LIMIT} characters`
),
}),
])
);
}
});
});

describe('state_transition constraints', () => {
it('rejects an invalid pending_operator', () => {
const result = updateRuleDataSchema.safeParse({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
*/

import { z } from '@kbn/zod/v4';
import {
DEFAULT_ARTIFACT_VALUE_LIMIT,
ARTIFACT_VALUE_LIMITS,
MAX_ARTIFACT_VALUE_LIMIT,
} from '@kbn/alerting-v2-constants';
import { validateEsqlQuery, validateMinDuration } from './validation';
import { durationSchema } from './common';
import { MAX_CONSECUTIVE_BREACHES, MIN_SCHEDULE_INTERVAL } from './constants';
Expand Down Expand Up @@ -178,9 +183,20 @@ const artifactSchema = z
.object({
id: z.string().min(1).max(256).describe('Artifact identifier.'),
type: z.string().min(1).max(128).describe('Artifact type.'),
value: z.string().min(1).max(1024).describe('Artifact value.'),
value: z.string().min(1).max(MAX_ARTIFACT_VALUE_LIMIT).describe('Artifact value.'),
})
.strict();
.strict()
.check((ctx) => {
const limit = ARTIFACT_VALUE_LIMITS[ctx.value.type] ?? DEFAULT_ARTIFACT_VALUE_LIMIT;
if (ctx.value.value.length > limit) {
ctx.issues.push({
code: 'custom',
path: ['value'],
message: `Artifact value must be at most ${limit} characters for type "${ctx.value.type}".`,
input: ctx.value.value,
});
}
});

/** Create rule API schema */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"target/**/*"
],
"kbn_references": [
"@kbn/zod"
"@kbn/zod",
"@kbn/alerting-v2-constants"
]
}
Loading