Skip to content

Commit

Permalink
Checking for undefined config and secrets during connector validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Jan 11, 2022
1 parent 33af3fc commit ee5f1db
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
57 changes: 53 additions & 4 deletions x-pack/plugins/actions/server/lib/validate_with_schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ test('should validate when validators return incoming value', () => {
params: selfValidator,
config: selfValidator,
secrets: selfValidator,
connector: () => null,
},
};

Expand All @@ -83,7 +84,7 @@ test('should validate when validators return incoming value', () => {
result = validateSecrets(actionType, testValue);
expect(result).toEqual(testValue);

result = validateConnector(actionType, { config: testValue });
result = validateConnector(actionType, { config: testValue, secrets: { user: 'test' } });
expect(result).toBeNull();
});

Expand All @@ -99,6 +100,7 @@ test('should validate when validators return different values', () => {
params: selfValidator,
config: selfValidator,
secrets: selfValidator,
connector: () => null,
},
};

Expand Down Expand Up @@ -133,9 +135,7 @@ test('should throw with expected error when validators fail', () => {
params: erroringValidator,
config: erroringValidator,
secrets: erroringValidator,
connector: () => {
return 'test error';
},
connector: () => 'test error',
},
};

Expand Down Expand Up @@ -180,3 +180,52 @@ test('should work with @kbn/config-schema', () => {
`"error validating action params: [foo]: expected value of type [string] but got [undefined]"`
);
});

describe('validateConnectors', () => {
const testValue = { any: ['old', 'thing'] };
const selfValidator = { validate: (value: Record<string, unknown>) => value };
const actionType: ActionType = {
id: 'foo',
name: 'bar',
minimumLicenseRequired: 'basic',
executor,
validate: {
params: selfValidator,
config: selfValidator,
secrets: selfValidator,
connector: () => null,
},
};

test('should throw error when connector config is null', () => {
expect(() =>
validateConnector(actionType, { config: null, secrets: { user: 'test' } })
).toThrowErrorMatchingInlineSnapshot(
`"error validating action type connector: config must be defined"`
);
});

test('should throw error when connector config is undefined', () => {
expect(() =>
validateConnector(actionType, { config: undefined, secrets: { user: 'test' } })
).toThrowErrorMatchingInlineSnapshot(
`"error validating action type connector: config must be defined"`
);
});

test('should throw error when connector secrets is null', () => {
expect(() =>
validateConnector(actionType, { config: testValue, secrets: null })
).toThrowErrorMatchingInlineSnapshot(
`"error validating action type connector: secrets must be defined"`
);
});

test('should throw error when connector secrets is undefined', () => {
expect(() =>
validateConnector(actionType, { config: testValue, secrets: undefined })
).toThrowErrorMatchingInlineSnapshot(
`"error validating action type connector: secrets must be defined"`
);
});
});
20 changes: 16 additions & 4 deletions x-pack/plugins/actions/server/lib/validate_with_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,22 @@ export function validateConnector<
ExecutorResultData = void
>(actionType: ActionType<Config, Secrets, Params, ExecutorResultData>, value: unknown) {
if (actionType.validate && actionType.validate.connector) {
const connectorValue = value as { config: Config; secrets: Secrets };
const result = actionType.validate.connector(connectorValue.config, connectorValue.secrets);
if (result !== null) {
throw Boom.badRequest(`error validating action type connector: ${result}`);
try {
const connectorValue = value as { config: Config; secrets: Secrets };

// Connector config and secrets should be defined
if (connectorValue.config == null) {
throw new Error(`config must be defined`);
}
if (connectorValue.secrets == null) {
throw new Error(`secrets must be defined`);
}
const result = actionType.validate.connector(connectorValue.config, connectorValue.secrets);
if (result !== null) {
throw new Error(result);
}
} catch (err) {
throw Boom.badRequest(`error validating action type connector: ${err.message}`);
}
}
return null;
Expand Down

0 comments on commit ee5f1db

Please sign in to comment.