Add Spec11 registrar emailing mechanism

This adds the terminal step of the Spec11 pipeline- processing the output of
the Beam pipeline to send an e-mail to each registrar informing them of
identified 'bad urls.'

This also factors out methods common between invoicing (which uses similar beam pipeline tools) and spec11 to the common superpackage ReportingModule + ReportingUtils classes.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=210932496
This commit is contained in:
larryruili 2018-08-30 10:16:20 -07:00 committed by jianglai
parent e4bb1c281c
commit c5e6eae555
26 changed files with 816 additions and 93 deletions

View file

@ -84,7 +84,7 @@ public class Spec11PipelineTest {
public void initializePipeline() throws IOException {
spec11Pipeline = new Spec11Pipeline();
spec11Pipeline.projectId = "test-project";
spec11Pipeline.spec11BucketUrl = tempFolder.getRoot().getAbsolutePath();
spec11Pipeline.reportingBucketUrl = tempFolder.getRoot().getAbsolutePath();
File beamTempFolder = tempFolder.newFolder();
spec11Pipeline.beamStagingUrl = beamTempFolder.getAbsolutePath() + "/staging";
spec11Pipeline.spec11TemplateUrl = beamTempFolder.getAbsolutePath() + "/templates/invoicing";
@ -175,10 +175,14 @@ public class Spec11PipelineTest {
new JSONObject()
.put("fullyQualifiedDomainName", "111.com")
.put("threatType", "MALWARE")
.put("threatEntryMetadata", "NONE")
.put("platformType", "WINDOWS")
.toString(),
new JSONObject()
.put("fullyQualifiedDomainName", "222.com")
.put("threatType", "MALWARE")
.put("threatEntryMetadata", "NONE")
.put("platformType", "WINDOWS")
.toString());
}
@ -273,7 +277,8 @@ public class Spec11PipelineTest {
File resultFile =
new File(
String.format(
"%s/2018-06/2018-06-monthly-report", tempFolder.getRoot().getAbsolutePath()));
"%s/icann/spec11/2018-06/SPEC11_MONTHLY_REPORT",
tempFolder.getRoot().getAbsolutePath()));
return ImmutableList.copyOf(
ResourceUtils.readResourceUtf8(resultFile.toURI().toURL()).split("\n"));
}

View file

@ -32,6 +32,7 @@ PATH CLASS METHOD
/_dr/task/pollBigqueryJob BigqueryPollJobAction GET,POST y INTERNAL APP IGNORED
/_dr/task/publishDnsUpdates PublishDnsUpdatesAction POST y INTERNAL APP IGNORED
/_dr/task/publishInvoices PublishInvoicesAction POST n INTERNAL,API APP ADMIN
/_dr/task/publishSpec11 PublishSpec11ReportAction POST n INTERNAL,API APP ADMIN
/_dr/task/rdeReport RdeReportAction POST n INTERNAL APP IGNORED
/_dr/task/rdeStaging RdeStagingAction GET,POST n INTERNAL APP IGNORED
/_dr/task/rdeUpload RdeUploadAction POST n INTERNAL APP IGNORED

View file

@ -109,7 +109,7 @@ public class GenerateInvoicesActionTest {
.method("POST")
.param("jobId", "12345")
.param("yearMonth", "2017-10");
assertTasksEnqueued("billing", matcher);
assertTasksEnqueued("beam-reporting", matcher);
}
@Test

View file

@ -41,6 +41,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link PublishInvoicesAction}. */
@RunWith(JUnit4.class)
public class PublishInvoicesActionTest {

View file

@ -10,8 +10,11 @@ load("//java/com/google/testing/builddefs:GenTestRules.bzl", "GenTestRules")
java_library(
name = "spec11",
srcs = glob(["*.java"]),
resources = glob(["testdata/*"]),
deps = [
"//java/google/registry/gcs",
"//java/google/registry/reporting/spec11",
"//java/google/registry/util",
"//javatests/google/registry/testing",
"@com_google_apis_google_api_services_dataflow",
"@com_google_appengine_api_1_0_sdk",
@ -27,6 +30,7 @@ java_library(
"@org_apache_beam_runners_google_cloud_dataflow_java",
"@org_apache_beam_sdks_java_core",
"@org_apache_beam_sdks_java_io_google_cloud_platform",
"@org_json",
"@org_mockito_all",
],
)

View file

@ -15,6 +15,7 @@
package google.registry.reporting.spec11;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@ -30,9 +31,13 @@ import com.google.api.services.dataflow.model.LaunchTemplateResponse;
import com.google.api.services.dataflow.model.RuntimeEnvironment;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeResponse;
import google.registry.testing.TaskQueueHelper.TaskMatcher;
import java.io.IOException;
import org.joda.time.YearMonth;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -41,6 +46,9 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class GenerateSpec11ReportActionTest {
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder().withTaskQueue().build();
private FakeResponse response;
private Dataflow dataflow;
private Projects dataflowProjects;
@ -61,7 +69,7 @@ public class GenerateSpec11ReportActionTest {
dataflowLaunch = mock(Launch.class);
LaunchTemplateResponse launchTemplateResponse = new LaunchTemplateResponse();
// Ultimately we get back this job response with a given id.
launchTemplateResponse.setJob(new Job().setReplaceJobId("jobid"));
launchTemplateResponse.setJob(new Job().setId("jobid"));
when(dataflow.projects()).thenReturn(dataflowProjects);
when(dataflowProjects.templates()).thenReturn(dataflowTemplates);
when(dataflowTemplates.launch(any(String.class), any(LaunchTemplateParameters.class)))
@ -79,22 +87,32 @@ public class GenerateSpec11ReportActionTest {
"gs://template",
"us-east1-c",
"api_key/a",
YearMonth.parse("2018-06"),
response,
dataflow);
action.run();
LaunchTemplateParameters expectedLaunchTemplateParameters =
new LaunchTemplateParameters()
.setJobName("spec11_action")
.setJobName("spec11_2018-06")
.setEnvironment(
new RuntimeEnvironment()
.setZone("us-east1-c")
.setTempLocation("gs://my-bucket-beam/temporary"))
.setParameters(ImmutableMap.of("safeBrowsingApiKey", "api_key/a"));
.setParameters(
ImmutableMap.of("safeBrowsingApiKey", "api_key/a", "yearMonth", "2018-06"));
verify(dataflowTemplates).launch("test", expectedLaunchTemplateParameters);
verify(dataflowLaunch).setGcsPath("gs://template");
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
assertThat(response.getPayload()).isEqualTo("Launched Spec11 dataflow template.");
TaskMatcher matcher =
new TaskMatcher()
.url("/_dr/task/publishSpec11")
.method("POST")
.param("jobId", "jobid")
.param("yearMonth", "2018-06");
assertTasksEnqueued("beam-reporting", matcher);
}
}

View file

@ -0,0 +1,107 @@
// Copyright 2018 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.reporting.spec11;
import static com.google.common.truth.Truth.assertThat;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import com.google.api.services.dataflow.Dataflow;
import com.google.api.services.dataflow.Dataflow.Projects;
import com.google.api.services.dataflow.Dataflow.Projects.Jobs;
import com.google.api.services.dataflow.Dataflow.Projects.Jobs.Get;
import com.google.api.services.dataflow.model.Job;
import com.google.common.net.MediaType;
import google.registry.testing.FakeResponse;
import java.io.IOException;
import org.joda.time.YearMonth;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link PublishSpec11ReportAction}. */
@RunWith(JUnit4.class)
public class PublishSpec11ReportActionTest {
private Dataflow dataflow;
private Projects projects;
private Jobs jobs;
private Get get;
private Spec11EmailUtils emailUtils;
private Job expectedJob;
private FakeResponse response;
private PublishSpec11ReportAction publishAction;
@Before
public void setUp() throws IOException {
dataflow = mock(Dataflow.class);
projects = mock(Projects.class);
jobs = mock(Jobs.class);
get = mock(Get.class);
when(dataflow.projects()).thenReturn(projects);
when(projects.jobs()).thenReturn(jobs);
when(jobs.get("test-project", "12345")).thenReturn(get);
expectedJob = new Job();
when(get.execute()).thenReturn(expectedJob);
emailUtils = mock(Spec11EmailUtils.class);
response = new FakeResponse();
publishAction =
new PublishSpec11ReportAction(
"test-project", "12345", emailUtils, dataflow, response, new YearMonth(2018, 6));
}
@Test
public void testJobDone_emailsResults() {
expectedJob.setCurrentState("JOB_STATE_DONE");
publishAction.run();
assertThat(response.getStatus()).isEqualTo(SC_OK);
verify(emailUtils).emailSpec11Reports();
}
@Test
public void testJobFailed_returnsNonRetriableResponse() {
expectedJob.setCurrentState("JOB_STATE_FAILED");
publishAction.run();
assertThat(response.getStatus()).isEqualTo(SC_NO_CONTENT);
verify(emailUtils).sendFailureAlertEmail("Spec11 2018-06 job 12345 ended in status failure.");
}
@Test
public void testJobIndeterminate_returnsRetriableResponse() {
expectedJob.setCurrentState("JOB_STATE_RUNNING");
publishAction.run();
assertThat(response.getStatus()).isEqualTo(SC_NOT_MODIFIED);
verifyNoMoreInteractions(emailUtils);
}
@Test
public void testIOException_returnsFailureMessage() throws IOException {
when(get.execute()).thenThrow(new IOException("expected"));
publishAction.run();
assertThat(response.getStatus()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
assertThat(response.getPayload()).isEqualTo("Template launch failed: expected");
verify(emailUtils)
.sendFailureAlertEmail("Spec11 2018-06 publish action failed due to expected");
}
}

View file

@ -0,0 +1,192 @@
// Copyright 2018 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.reporting.spec11;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.testing.JUnitBackports.assertThrows;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.appengine.tools.cloudstorage.GcsFilename;
import google.registry.gcs.GcsUtils;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeSleeper;
import google.registry.testing.TestDataHelper;
import google.registry.util.Retrier;
import google.registry.util.SendEmailService;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.joda.time.YearMonth;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
/** Unit tests for {@link Spec11EmailUtils}. */
@RunWith(JUnit4.class)
public class Spec11EmailUtilsTest {
private static final int RETRY_COUNT = 2;
private SendEmailService emailService;
private Spec11EmailUtils emailUtils;
private GcsUtils gcsUtils;
private ArgumentCaptor<Message> gotMessage;
@Before
public void setUp() {
emailService = mock(SendEmailService.class);
when(emailService.createMessage())
.thenAnswer((args) -> new MimeMessage(Session.getInstance(new Properties(), null)));
gcsUtils = mock(GcsUtils.class);
when(gcsUtils.openInputStream(
new GcsFilename("test-bucket", "icann/spec11/2018-06/SPEC11_MONTHLY_REPORT")))
.thenAnswer(
(args) ->
new ByteArrayInputStream(
loadFile("spec11_fake_report").getBytes(StandardCharsets.UTF_8)));
gotMessage = ArgumentCaptor.forClass(Message.class);
emailUtils =
new Spec11EmailUtils(
emailService,
new YearMonth(2018, 6),
"my-sender@test.com",
"my-receiver@test.com",
"test-bucket",
"icann/spec11/2018-06/SPEC11_MONTHLY_REPORT",
gcsUtils,
new Retrier(new FakeSleeper(new FakeClock()), RETRY_COUNT));
}
@Test
public void testSuccess_emailSpec11Reports() throws MessagingException, IOException {
emailUtils.emailSpec11Reports();
// We inspect individual parameters because Message doesn't implement equals().
verify(emailService, times(2)).sendMessage(gotMessage.capture());
List<Message> capturedMessages = gotMessage.getAllValues();
validateMessage(
capturedMessages.get(0),
"my-sender@test.com",
"a@fake.com",
"Spec11 Monthly Threat Detector [2018-06]",
"Hello registrar partner,\n"
+ "The SafeBrowsing API has detected problems with the following domains:\n"
+ "a.com - MALWARE\n"
+ "At the moment, no action is required. This is purely informatory."
+ "Regards,\nGoogle Registry\n");
validateMessage(
capturedMessages.get(1),
"my-sender@test.com",
"b@fake.com",
"Spec11 Monthly Threat Detector [2018-06]",
"Hello registrar partner,\n"
+ "The SafeBrowsing API has detected problems with the following domains:\n"
+ "b.com - MALWARE\n"
+ "c.com - MALWARE\n"
+ "At the moment, no action is required. This is purely informatory."
+ "Regards,\nGoogle Registry\n");
}
@Test
public void testFailure_tooManyRetries_emailsAlert() throws MessagingException, IOException {
Message throwingMessage = mock(Message.class);
doThrow(new MessagingException("expected")).when(throwingMessage).setSubject(any(String.class));
// Only return the throwingMessage enough times to force failure. The last invocation will
// be for the alert e-mail we're looking to verify.
when(emailService.createMessage())
.thenAnswer(
new Answer<Message>() {
private int count = 0;
@Override
public Message answer(InvocationOnMock invocation) {
if (count < RETRY_COUNT) {
count++;
return throwingMessage;
} else if (count == RETRY_COUNT) {
return new MimeMessage(Session.getDefaultInstance(new Properties(), null));
} else {
assertWithMessage("Attempted to generate too many messages!").fail();
return null;
}
}
});
RuntimeException thrown =
assertThrows(RuntimeException.class, () -> emailUtils.emailSpec11Reports());
assertThat(thrown).hasMessageThat().isEqualTo("Emailing spec11 report failed");
assertThat(thrown)
.hasCauseThat()
.hasMessageThat()
.isEqualTo("javax.mail.MessagingException: expected");
// We should have created RETRY_COUNT failing messages and one final alert message
verify(emailService, times(RETRY_COUNT + 1)).createMessage();
// Verify we sent an e-mail alert
verify(emailService).sendMessage(gotMessage.capture());
validateMessage(
gotMessage.getValue(),
"my-sender@test.com",
"my-receiver@test.com",
"Spec11 Pipeline Alert: 2018-06",
"Emailing spec11 reports failed due to expected");
}
@Test
public void testSuccess_sendAlertEmail() throws MessagingException, IOException {
emailUtils.sendFailureAlertEmail("Alert!");
verify(emailService).sendMessage(gotMessage.capture());
validateMessage(
gotMessage.getValue(),
"my-sender@test.com",
"my-receiver@test.com",
"Spec11 Pipeline Alert: 2018-06",
"Alert!");
}
private void validateMessage(
Message message, String from, String recipient, String subject, String body)
throws MessagingException, IOException {
assertThat(message.getFrom()).asList().containsExactly(new InternetAddress(from));
assertThat(message.getAllRecipients())
.asList()
.containsExactly(new InternetAddress(recipient));
assertThat(message.getSubject()).isEqualTo(subject);
assertThat(message.getContentType()).isEqualTo("text/plain");
assertThat(message.getContent().toString()).isEqualTo(body);
}
/** Returns a {@link String} from a file in the {@code spec11/testdata/} directory. */
public static String loadFile(String filename) {
return TestDataHelper.loadFile(Spec11EmailUtils.class, filename);
}
}

View file

@ -0,0 +1,3 @@
Map from registrar email to detected subdomain threats:
{"threatMatches":[{"threatEntryMetadata":"NONE","threatType":"MALWARE","fullyQualifiedDomainName":"a.com","platformType":"ANY_PLATFORM"}],"registrarEmailAddress":"a@fake.com"}
{"threatMatches":[{"threatEntryMetadata":"NONE","threatType":"MALWARE","fullyQualifiedDomainName":"b.com","platformType":"ANY_PLATFORM"},{"threatEntryMetadata":"NONE","threatType":"MALWARE","fullyQualifiedDomainName":"c.com","platformType":"ANY_PLATFORM"}],"registrarEmailAddress":"b@fake.com"}