Skip to content

Commit

Permalink
apply team repos
Browse files Browse the repository at this point in the history
  • Loading branch information
WafaaNasr committed Feb 1, 2023
1 parent 67b4b2f commit f192914
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 218 deletions.
44 changes: 24 additions & 20 deletions x-pack/plugins/alerting/server/rules_client/lib/validate_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { parseDuration } from '../../lib';
export async function validateActions(
context: RulesClientContext,
alertType: UntypedNormalizedRuleType,
data: Pick<RawRule, 'notifyWhen' | 'throttle' | 'schedule'> & { actions: NormalizedAlertAction[] }
data: Pick<RawRule, 'notifyWhen' | 'throttle' | 'schedule'> & {
actions: NormalizedAlertAction[];
},
skipMissingSecretsValidation?: boolean
): Promise<void> {
const { actions, notifyWhen, throttle } = data;
const hasRuleLevelNotifyWhen = typeof notifyWhen !== 'undefined';
Expand All @@ -28,27 +31,28 @@ export async function validateActions(

const errors = [];

// check for actions using connectors with missing secrets
const actionsClient = await context.getActionsClient();
const actionIds = [...new Set(actions.map((action) => action.id))];
const actionResults = (await actionsClient.getBulk(actionIds)) || [];
const actionsUsingConnectorsWithMissingSecrets = actionResults.filter(
(result) => result.isMissingSecrets
);

if (actionsUsingConnectorsWithMissingSecrets.length) {
errors.push(
i18n.translate('xpack.alerting.rulesClient.validateActions.misconfiguredConnector', {
defaultMessage: 'Invalid connectors: {groups}',
values: {
groups: actionsUsingConnectorsWithMissingSecrets
.map((connector) => connector.name)
.join(', '),
},
})
if (!skipMissingSecretsValidation) {
// check for actions using connectors with missing secrets
const actionsClient = await context.getActionsClient();
const actionIds = [...new Set(actions.map((action) => action.id))];
const actionResults = (await actionsClient.getBulk(actionIds)) || [];
const actionsUsingConnectorsWithMissingSecrets = actionResults.filter(
(result) => result.isMissingSecrets
);
}

if (actionsUsingConnectorsWithMissingSecrets.length) {
errors.push(
i18n.translate('xpack.alerting.rulesClient.validateActions.misconfiguredConnector', {
defaultMessage: 'Invalid connectors: {groups}',
values: {
groups: actionsUsingConnectorsWithMissingSecrets
.map((connector) => connector.name)
.join(', '),
},
})
);
}
}
// check for actions with invalid action groups
const { actionGroups: alertTypeActionGroups } = alertType;
const usedAlertActionGroups = actions.map((action) => action.group);
Expand Down
15 changes: 7 additions & 8 deletions x-pack/plugins/alerting/server/rules_client/methods/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ export interface CreateOptions<Params extends RuleTypeParams> {
| 'nextRun'
> & { actions: NormalizedAlertAction[] };
options?: SavedObjectOptions;
skipActionConnectorsValidations?: boolean;
skipMissingSecretsValidation?: boolean;
}

export async function create<Params extends RuleTypeParams = never>(
context: RulesClientContext,
{ data, options, skipActionConnectorsValidations }: CreateOptions<Params>
{ data, options, skipMissingSecretsValidation }: CreateOptions<Params>
): Promise<SanitizedRule<Params>> {
const id = options?.id || SavedObjectsUtils.generateId();

Expand Down Expand Up @@ -105,12 +105,11 @@ export async function create<Params extends RuleTypeParams = never>(
}
}

if (!skipActionConnectorsValidations) {
await validateActions(context, ruleType, data);
await withSpan({ name: 'validateActions', type: 'rules' }, () =>
validateActions(context, ruleType, data)
);
}
await validateActions(context, ruleType, data, skipMissingSecretsValidation);
await withSpan({ name: 'validateActions', type: 'rules' }, () =>
validateActions(context, ruleType, data, skipMissingSecretsValidation)
);

// Throw error if schedule interval is less than the minimum and we are enforcing it
const intervalInMs = parseDuration(data.schedule.interval);
if (
Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/alerting/server/rules_client/methods/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ export interface UpdateOptions<Params extends RuleTypeParams> {
throttle?: string | null;
notifyWhen?: RuleNotifyWhenType | null;
};
skipActionConnectorsValidations?: boolean;
skipMissingSecretsValidation?: boolean;
}

export async function update<Params extends RuleTypeParams = never>(
context: RulesClientContext,
{ id, data, skipActionConnectorsValidations }: UpdateOptions<Params>
{ id, data, skipMissingSecretsValidation }: UpdateOptions<Params>
): Promise<PartialRule<Params>> {
return await retryIfConflicts(
context.logger,
`rulesClient.update('${id}')`,
async () => await updateWithOCC<Params>(context, { id, data, skipActionConnectorsValidations })
async () => await updateWithOCC<Params>(context, { id, data, skipMissingSecretsValidation })
);
}

async function updateWithOCC<Params extends RuleTypeParams>(
context: RulesClientContext,
{ id, data, skipActionConnectorsValidations }: UpdateOptions<Params>
{ id, data, skipMissingSecretsValidation }: UpdateOptions<Params>
): Promise<PartialRule<Params>> {
let alertSavedObject: SavedObject<RawRule>;

Expand Down Expand Up @@ -102,7 +102,7 @@ async function updateWithOCC<Params extends RuleTypeParams>(

const updateResult = await updateAlert<Params>(
context,
{ id, data, skipActionConnectorsValidations },
{ id, data, skipMissingSecretsValidation },
alertSavedObject
);

Expand Down Expand Up @@ -143,7 +143,7 @@ async function updateWithOCC<Params extends RuleTypeParams>(

async function updateAlert<Params extends RuleTypeParams>(
context: RulesClientContext,
{ id, data, skipActionConnectorsValidations }: UpdateOptions<Params>,
{ id, data, skipMissingSecretsValidation }: UpdateOptions<Params>,
{ attributes, version }: SavedObject<RawRule>
): Promise<PartialRule<Params>> {
const ruleType = context.ruleTypeRegistry.get(attributes.alertTypeId);
Expand All @@ -161,7 +161,7 @@ async function updateAlert<Params extends RuleTypeParams>(

// Validate
const validatedAlertTypeParams = validateRuleTypeParams(data.params, ruleType.validate?.params);
if (!skipActionConnectorsValidations) await validateActions(context, ruleType, data);
await validateActions(context, ruleType, data, skipMissingSecretsValidation);

// Throw error if schedule interval is less than the minimum and we are enforcing it
const intervalInMs = parseDuration(data.schedule.interval);
Expand Down
69 changes: 7 additions & 62 deletions x-pack/plugins/alerting/server/rules_client/tests/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3047,7 +3047,7 @@ describe('create()', () => {
expect(unsecuredSavedObjectsClient.create).not.toHaveBeenCalled();
expect(taskManager.schedule).not.toHaveBeenCalled();
});
test('should create a rule and not throw an error when skipActionConnectorsValidations is true even when some actions are missing frequency params', async () => {
test('should create a rule even if action is missing secret when skipMissingSecretsValidation is true', async () => {
const data = getMockData({
actions: [
{
Expand Down Expand Up @@ -3077,33 +3077,10 @@ describe('create()', () => {
actionsClient.getBulk.mockResolvedValue([
{
id: '1',
actionTypeId: 'test',
config: {
from: '[email protected]',
hasAuth: false,
host: 'hello',
port: 22,
secure: null,
service: null,
},
actionTypeId: '.slack',
config: {},
isMissingSecrets: true,
name: 'email connector',
isPreconfigured: false,
isDeprecated: false,
},
{
id: '2',
actionTypeId: 'test',
config: {
from: '[email protected]',
hasAuth: false,
host: 'hello',
port: 22,
secure: null,
service: null,
},
isMissingSecrets: false,
name: 'email connector',
name: 'Slack connector',
isPreconfigured: false,
isDeprecated: false,
},
Expand All @@ -3124,23 +3101,7 @@ describe('create()', () => {
{
group: 'default',
actionRef: 'action_0',
actionTypeId: 'test',
params: {
foo: true,
},
},
{
group: 'default',
actionRef: 'action_1',
actionTypeId: 'test',
params: {
foo: true,
},
},
{
group: 'default',
actionRef: 'action_2',
actionTypeId: 'test2',
actionTypeId: '.slack',
params: {
foo: true,
},
Expand Down Expand Up @@ -3174,34 +3135,18 @@ describe('create()', () => {
},
references: [],
});
const result = await rulesClient.create({ data, skipActionConnectorsValidations: true });
const result = await rulesClient.create({ data, skipMissingSecretsValidation: true });
expect(result).toMatchInlineSnapshot(`
Object {
"actions": Array [
Object {
"actionTypeId": "test",
"group": "default",
"id": "1",
"params": Object {
"foo": true,
},
},
Object {
"actionTypeId": "test",
"actionTypeId": ".slack",
"group": "default",
"id": "1",
"params": Object {
"foo": true,
},
},
Object {
"actionTypeId": "test2",
"group": "default",
"id": "2",
"params": Object {
"foo": true,
},
},
],
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
Expand Down
Loading

0 comments on commit f192914

Please sign in to comment.