Skip to content

Commit 9dcc8c7

Browse files
committed
[Alerting] allow email action to not require auth
resolves #57143 Currently, the built-in email action requires user/password properties to be set in it's secrets parameters. This PR changes that requirement, so they are no longer required.
1 parent cf9b64e commit 9dcc8c7

3 files changed

Lines changed: 78 additions & 17 deletions

File tree

  • x-pack
    • plugins/actions/server/builtin_action_types
    • test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types

x-pack/plugins/actions/server/builtin_action_types/email.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,14 @@ describe('secrets validation', () => {
184184
expect(validateSecrets(actionType, secrets)).toEqual(secrets);
185185
});
186186

187-
test('secrets validation fails when secrets is not valid', () => {
188-
expect(() => {
189-
validateSecrets(actionType, {});
190-
}).toThrowErrorMatchingInlineSnapshot(
191-
`"error validating action type secrets: [user]: expected value of type [string] but got [undefined]"`
192-
);
187+
test('secrets validation succeeds when secrets props are null/undefined', () => {
188+
const secrets: Record<string, any> = {
189+
user: null,
190+
password: null,
191+
};
192+
expect(validateSecrets(actionType, {})).toEqual(secrets);
193+
expect(validateSecrets(actionType, { user: null })).toEqual(secrets);
194+
expect(validateSecrets(actionType, { password: null })).toEqual(secrets);
193195
});
194196
});
195197

x-pack/plugins/actions/server/builtin_action_types/email.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { schema, TypeOf } from '@kbn/config-schema';
1010
import nodemailerGetService from 'nodemailer/lib/well-known';
1111

1212
import { sendEmail, JSON_TRANSPORT_SERVICE } from './lib/send_email';
13-
import { nullableType } from './lib/nullable';
1413
import { portSchema } from './lib/schemas';
1514
import { Logger } from '../../../../../src/core/server';
1615
import { ActionType, ActionTypeExecutorOptions, ActionTypeExecutorResult } from '../types';
@@ -20,10 +19,10 @@ import { ActionsConfigurationUtilities } from '../actions_config';
2019
export type ActionTypeConfigType = TypeOf<typeof ConfigSchema>;
2120

2221
const ConfigSchemaProps = {
23-
service: nullableType(schema.string()),
24-
host: nullableType(schema.string()),
25-
port: nullableType(portSchema()),
26-
secure: nullableType(schema.boolean()),
22+
service: schema.nullable(schema.string()),
23+
host: schema.nullable(schema.string()),
24+
port: schema.nullable(portSchema()),
25+
secure: schema.nullable(schema.boolean()),
2726
from: schema.string(),
2827
};
2928

@@ -75,8 +74,8 @@ function validateConfig(
7574
export type ActionTypeSecretsType = TypeOf<typeof SecretsSchema>;
7675

7776
const SecretsSchema = schema.object({
78-
user: schema.string(),
79-
password: schema.string(),
77+
user: schema.nullable(schema.string()),
78+
password: schema.nullable(schema.string()),
8079
});
8180

8281
// params definition
@@ -144,10 +143,14 @@ async function executor(
144143
const secrets = execOptions.secrets as ActionTypeSecretsType;
145144
const params = execOptions.params as ActionParamsType;
146145

147-
const transport: any = {
148-
user: secrets.user,
149-
password: secrets.password,
150-
};
146+
const transport: any = {};
147+
148+
if (secrets.user != null) {
149+
transport.user = secrets.user;
150+
}
151+
if (secrets.password != null) {
152+
transport.password = secrets.password;
153+
}
151154

152155
if (config.service !== null) {
153156
transport.service = config.service;

x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/email.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,61 @@ export default function emailTest({ getService }: FtrProviderContext) {
227227
.expect(200);
228228
expect(typeof createdAction.id).to.be('string');
229229
});
230+
231+
it('should handle an email action with no auth', async () => {
232+
const { body: createdAction } = await supertest
233+
.post('/api/action')
234+
.set('kbn-xsrf', 'foo')
235+
.send({
236+
name: 'An email action with no auth',
237+
actionTypeId: '.email',
238+
config: {
239+
service: '__json',
240+
from: 'jim@example.com',
241+
},
242+
})
243+
.expect(200);
244+
245+
await supertest
246+
.post(`/api/action/${createdAction.id}/_execute`)
247+
.set('kbn-xsrf', 'foo')
248+
.send({
249+
params: {
250+
to: ['kibana-action-test@elastic.co'],
251+
subject: 'email-subject',
252+
message: 'email-message',
253+
},
254+
})
255+
.expect(200)
256+
.then((resp: any) => {
257+
expect(resp.body.data.message.messageId).to.be.a('string');
258+
expect(resp.body.data.messageId).to.be.a('string');
259+
260+
delete resp.body.data.message.messageId;
261+
delete resp.body.data.messageId;
262+
263+
expect(resp.body.data).to.eql({
264+
envelope: {
265+
from: 'jim@example.com',
266+
to: ['kibana-action-test@elastic.co'],
267+
},
268+
message: {
269+
from: { address: 'jim@example.com', name: '' },
270+
to: [
271+
{
272+
address: 'kibana-action-test@elastic.co',
273+
name: '',
274+
},
275+
],
276+
cc: null,
277+
bcc: null,
278+
subject: 'email-subject',
279+
html: '<p>email-message</p>\n',
280+
text: 'email-message',
281+
headers: {},
282+
},
283+
});
284+
});
285+
});
230286
});
231287
}

0 commit comments

Comments
 (0)