Merge pull request elastic/elasticsearch#2685 from rjernst/uninject_htmlsanitizer
Uninject HtmlSanitizer Original commit: elastic/x-pack-elasticsearch@4a6c8cdee6
This commit is contained in:
commit
d6ff6211eb
|
@ -41,7 +41,6 @@ public class NotificationModule extends AbstractModule {
|
||||||
@Override
|
@Override
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
// email
|
// email
|
||||||
bind(HtmlSanitizer.class).asEagerSingleton();
|
|
||||||
bind(InternalEmailService.class).asEagerSingleton();
|
bind(InternalEmailService.class).asEagerSingleton();
|
||||||
bind(EmailService.class).to(InternalEmailService.class).asEagerSingleton();
|
bind(EmailService.class).to(InternalEmailService.class).asEagerSingleton();
|
||||||
|
|
||||||
|
|
|
@ -41,8 +41,7 @@ public class HtmlSanitizer {
|
||||||
private final boolean enabled;
|
private final boolean enabled;
|
||||||
@SuppressForbidden( reason = "PolicyFactory uses guava Function")
|
@SuppressForbidden( reason = "PolicyFactory uses guava Function")
|
||||||
private final PolicyFactory policy;
|
private final PolicyFactory policy;
|
||||||
|
|
||||||
@Inject
|
|
||||||
public HtmlSanitizer(Settings settings) {
|
public HtmlSanitizer(Settings settings) {
|
||||||
enabled = settings.getAsBoolean("xpack.notification.email.html.sanitization.enabled", true);
|
enabled = settings.getAsBoolean("xpack.notification.email.html.sanitization.enabled", true);
|
||||||
String[] allow = settings.getAsArray("xpack.notification.email.html.sanitization.allow", DEFAULT_ALLOWED);
|
String[] allow = settings.getAsArray("xpack.notification.email.html.sanitization.allow", DEFAULT_ALLOWED);
|
||||||
|
|
|
@ -28,12 +28,12 @@ public class EmailActionFactory extends ActionFactory<EmailAction, ExecutableEma
|
||||||
private final EmailAttachmentsParser emailAttachmentsParser;
|
private final EmailAttachmentsParser emailAttachmentsParser;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public EmailActionFactory(Settings settings, EmailService emailService, TextTemplateEngine templateEngine, HtmlSanitizer htmlSanitizer,
|
public EmailActionFactory(Settings settings, EmailService emailService, TextTemplateEngine templateEngine,
|
||||||
EmailAttachmentsParser emailAttachmentsParser) {
|
EmailAttachmentsParser emailAttachmentsParser) {
|
||||||
super(Loggers.getLogger(ExecutableEmailAction.class, settings));
|
super(Loggers.getLogger(ExecutableEmailAction.class, settings));
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
this.templateEngine = templateEngine;
|
this.templateEngine = templateEngine;
|
||||||
this.htmlSanitizer = htmlSanitizer;
|
this.htmlSanitizer = new HtmlSanitizer(settings);
|
||||||
this.emailAttachmentsParser = emailAttachmentsParser;
|
this.emailAttachmentsParser = emailAttachmentsParser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -197,7 +197,6 @@ public class EmailActionTests extends ESTestCase {
|
||||||
|
|
||||||
public void testParser() throws Exception {
|
public void testParser() throws Exception {
|
||||||
TextTemplateEngine engine = mock(TextTemplateEngine.class);
|
TextTemplateEngine engine = mock(TextTemplateEngine.class);
|
||||||
HtmlSanitizer htmlSanitizer = mock(HtmlSanitizer.class);
|
|
||||||
EmailService emailService = mock(EmailService.class);
|
EmailService emailService = mock(EmailService.class);
|
||||||
Profile profile = randomFrom(Profile.values());
|
Profile profile = randomFrom(Profile.values());
|
||||||
Email.Priority priority = randomFrom(Email.Priority.values());
|
Email.Priority priority = randomFrom(Email.Priority.values());
|
||||||
|
@ -301,7 +300,7 @@ public class EmailActionTests extends ESTestCase {
|
||||||
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
|
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
|
||||||
parser.nextToken();
|
parser.nextToken();
|
||||||
|
|
||||||
ExecutableEmailAction executable = new EmailActionFactory(Settings.EMPTY, emailService, engine, htmlSanitizer,
|
ExecutableEmailAction executable = new EmailActionFactory(Settings.EMPTY, emailService, engine,
|
||||||
emailAttachmentParser)
|
emailAttachmentParser)
|
||||||
.parseExecutable(randomAsciiOfLength(8), randomAsciiOfLength(3), parser);
|
.parseExecutable(randomAsciiOfLength(8), randomAsciiOfLength(3), parser);
|
||||||
|
|
||||||
|
@ -396,7 +395,7 @@ public class EmailActionTests extends ESTestCase {
|
||||||
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
|
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
|
||||||
parser.nextToken();
|
parser.nextToken();
|
||||||
|
|
||||||
ExecutableEmailAction parsed = new EmailActionFactory(Settings.EMPTY, service, engine, htmlSanitizer, emailAttachmentParser)
|
ExecutableEmailAction parsed = new EmailActionFactory(Settings.EMPTY, service, engine, emailAttachmentParser)
|
||||||
.parseExecutable(randomAsciiOfLength(4), randomAsciiOfLength(10), parser);
|
.parseExecutable(randomAsciiOfLength(4), randomAsciiOfLength(10), parser);
|
||||||
|
|
||||||
if (!hideSecrets) {
|
if (!hideSecrets) {
|
||||||
|
@ -420,14 +419,13 @@ public class EmailActionTests extends ESTestCase {
|
||||||
public void testParserInvalid() throws Exception {
|
public void testParserInvalid() throws Exception {
|
||||||
EmailService emailService = mock(EmailService.class);
|
EmailService emailService = mock(EmailService.class);
|
||||||
TextTemplateEngine engine = mock(TextTemplateEngine.class);
|
TextTemplateEngine engine = mock(TextTemplateEngine.class);
|
||||||
HtmlSanitizer htmlSanitizer = mock(HtmlSanitizer.class);
|
|
||||||
EmailAttachmentsParser emailAttachmentsParser = mock(EmailAttachmentsParser.class);
|
EmailAttachmentsParser emailAttachmentsParser = mock(EmailAttachmentsParser.class);
|
||||||
|
|
||||||
XContentBuilder builder = jsonBuilder().startObject().field("unknown_field", "value").endObject();
|
XContentBuilder builder = jsonBuilder().startObject().field("unknown_field", "value").endObject();
|
||||||
XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes());
|
XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes());
|
||||||
parser.nextToken();
|
parser.nextToken();
|
||||||
try {
|
try {
|
||||||
new EmailActionFactory(Settings.EMPTY, emailService, engine, htmlSanitizer, emailAttachmentsParser)
|
new EmailActionFactory(Settings.EMPTY, emailService, engine, emailAttachmentsParser)
|
||||||
.parseExecutable(randomAsciiOfLength(3), randomAsciiOfLength(7), parser);
|
.parseExecutable(randomAsciiOfLength(3), randomAsciiOfLength(7), parser);
|
||||||
} catch (ElasticsearchParseException e) {
|
} catch (ElasticsearchParseException e) {
|
||||||
assertThat(e.getMessage(), containsString("unexpected string field [unknown_field]"));
|
assertThat(e.getMessage(), containsString("unexpected string field [unknown_field]"));
|
||||||
|
@ -518,7 +516,6 @@ public class EmailActionTests extends ESTestCase {
|
||||||
public void testThatOneFailedEmailAttachmentResultsInActionFailure() throws Exception {
|
public void testThatOneFailedEmailAttachmentResultsInActionFailure() throws Exception {
|
||||||
EmailService emailService = new AbstractWatcherIntegrationTestCase.NoopEmailService();
|
EmailService emailService = new AbstractWatcherIntegrationTestCase.NoopEmailService();
|
||||||
TextTemplateEngine engine = mock(TextTemplateEngine.class);
|
TextTemplateEngine engine = mock(TextTemplateEngine.class);
|
||||||
HtmlSanitizer htmlSanitizer = mock(HtmlSanitizer.class);
|
|
||||||
HttpClient httpClient = mock(HttpClient.class);
|
HttpClient httpClient = mock(HttpClient.class);
|
||||||
|
|
||||||
// setup mock response, second one is an error
|
// setup mock response, second one is an error
|
||||||
|
@ -554,7 +551,7 @@ public class EmailActionTests extends ESTestCase {
|
||||||
|
|
||||||
parser.nextToken();
|
parser.nextToken();
|
||||||
|
|
||||||
ExecutableEmailAction executableEmailAction = new EmailActionFactory(Settings.EMPTY, emailService, engine, htmlSanitizer,
|
ExecutableEmailAction executableEmailAction = new EmailActionFactory(Settings.EMPTY, emailService, engine,
|
||||||
emailAttachmentsParser).parseExecutable(randomAsciiOfLength(3), randomAsciiOfLength(7), parser);
|
emailAttachmentsParser).parseExecutable(randomAsciiOfLength(3), randomAsciiOfLength(7), parser);
|
||||||
|
|
||||||
DateTime now = DateTime.now(DateTimeZone.UTC);
|
DateTime now = DateTime.now(DateTimeZone.UTC);
|
||||||
|
@ -577,9 +574,8 @@ public class EmailActionTests extends ESTestCase {
|
||||||
private EmailActionFactory createEmailActionFactory() {
|
private EmailActionFactory createEmailActionFactory() {
|
||||||
EmailService emailService = new AbstractWatcherIntegrationTestCase.NoopEmailService();
|
EmailService emailService = new AbstractWatcherIntegrationTestCase.NoopEmailService();
|
||||||
TextTemplateEngine engine = mock(TextTemplateEngine.class);
|
TextTemplateEngine engine = mock(TextTemplateEngine.class);
|
||||||
HtmlSanitizer htmlSanitizer = mock(HtmlSanitizer.class);
|
|
||||||
|
|
||||||
return new EmailActionFactory(Settings.EMPTY, emailService, engine, htmlSanitizer, emailAttachmentParser);
|
return new EmailActionFactory(Settings.EMPTY, emailService, engine, emailAttachmentParser);
|
||||||
}
|
}
|
||||||
|
|
||||||
private WatchExecutionContext createWatchExecutionContext() {
|
private WatchExecutionContext createWatchExecutionContext() {
|
||||||
|
|
|
@ -469,7 +469,7 @@ public class WatchTests extends ESTestCase {
|
||||||
for (ActionWrapper action : actions) {
|
for (ActionWrapper action : actions) {
|
||||||
switch (action.action().type()) {
|
switch (action.action().type()) {
|
||||||
case EmailAction.TYPE:
|
case EmailAction.TYPE:
|
||||||
parsers.put(EmailAction.TYPE, new EmailActionFactory(settings, emailService, templateEngine, htmlSanitizer,
|
parsers.put(EmailAction.TYPE, new EmailActionFactory(settings, emailService, templateEngine,
|
||||||
new EmailAttachmentsParser(Collections.emptyMap())));
|
new EmailAttachmentsParser(Collections.emptyMap())));
|
||||||
break;
|
break;
|
||||||
case IndexAction.TYPE:
|
case IndexAction.TYPE:
|
||||||
|
|
Loading…
Reference in New Issue