mirror of
https://github.com/google/nomulus.git
synced 2025-07-06 19:23:31 +02:00
Create a Java entity to store ThreatMatch objects in SQL (#617)
* Squash everything together Create SafeBrowsing_Threats table Create LocalDateConverter and add indexes to SafeBrowsingThreats Add indexes to SafeBrowsingThreats and make small style changes Pass in DateTimeFormatter Delete LocalDateConverterTest.java Rebase Make changes to ThreatType comments Create LocalDateConverterTest Add review changes Add SafeBrowsingThreatTest Rename repoId, refactor LocalDateConverterTest/SafeBrowsingThreatTest, add foreign keys Change imports Add foreign keys and rename version number Add new generated db-schema file Clean up null test cases Add changes Add foreign keys into SafeBrowsingThreatTeat and apply style checks Add SafeBrowsingThreatTest into SqlIntegrationTestSuite and change golden file Make small changes to SafeBrowsingThreatTest Add tests for ForeignKeyViolations and remove setId in SafeBrowsingThreat * Change V35 -> V36 * Add a foreign key test for a reference to Registrar * Move some variables around
This commit is contained in:
parent
2c243a7d5f
commit
ec09226baa
10 changed files with 588 additions and 2 deletions
|
@ -0,0 +1,168 @@
|
|||
// Copyright 2020 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.model.reporting;
|
||||
|
||||
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.model.Buildable;
|
||||
import google.registry.model.ImmutableObject;
|
||||
import google.registry.schema.replay.DatastoreEntity;
|
||||
import google.registry.schema.replay.SqlEntity;
|
||||
import google.registry.util.DomainNameUtils;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Index;
|
||||
import javax.persistence.Table;
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
@Table(
|
||||
indexes = {
|
||||
@Index(name = "safebrowsing_threat_registrar_id_idx", columnList = "registrarId"),
|
||||
@Index(name = "safebrowsing_threat_tld_idx", columnList = "tld"),
|
||||
@Index(name = "safebrowsing_threat_check_date_idx", columnList = "checkDate")
|
||||
})
|
||||
public class SafeBrowsingThreat extends ImmutableObject implements Buildable, SqlEntity {
|
||||
|
||||
/** The type of threat detected. */
|
||||
public enum ThreatType {
|
||||
THREAT_TYPE_UNSPECIFIED,
|
||||
MALWARE,
|
||||
SOCIAL_ENGINEERING,
|
||||
UNWANTED_SOFTWARE,
|
||||
POTENTIALLY_HARMFUL_APPLICATION
|
||||
}
|
||||
|
||||
/** An auto-generated identifier and unique primary key for this entity. */
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
Long id;
|
||||
|
||||
/** The name of the offending domain */
|
||||
@Column(nullable = false)
|
||||
String domainName;
|
||||
|
||||
/** The type of threat detected. */
|
||||
@Column(nullable = false)
|
||||
@Enumerated(EnumType.STRING)
|
||||
ThreatType threatType;
|
||||
|
||||
/** Primary key of the domain table and unique identifier for all EPP resources. */
|
||||
@Column(nullable = false)
|
||||
String domainRepoId;
|
||||
|
||||
/** ID of the registrar at the moment of the scan. Domains may change registrars over time */
|
||||
@Column(nullable = false)
|
||||
String registrarId;
|
||||
|
||||
/** Date on which the check was run, on which the domain was flagged as abusive. */
|
||||
@Column(nullable = false)
|
||||
LocalDate checkDate;
|
||||
|
||||
/** The domain's top-level domain. */
|
||||
@Column(nullable = false)
|
||||
String tld;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getDomainName() {
|
||||
return domainName;
|
||||
}
|
||||
|
||||
public ThreatType getThreatType() {
|
||||
return threatType;
|
||||
}
|
||||
|
||||
public String getDomainRepoId() {
|
||||
return domainRepoId;
|
||||
}
|
||||
|
||||
public String getRegistrarId() {
|
||||
return registrarId;
|
||||
}
|
||||
|
||||
public LocalDate getCheckDate() {
|
||||
return checkDate;
|
||||
}
|
||||
|
||||
public String getTld() {
|
||||
return tld;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImmutableList<DatastoreEntity> toDatastoreEntities() {
|
||||
return ImmutableList.of(); // not stored in Datastore
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder asBuilder() {
|
||||
return new Builder(clone(this));
|
||||
}
|
||||
|
||||
/** A builder for constructing {@link SafeBrowsingThreat}, since it is immutable. */
|
||||
public static class Builder extends Buildable.Builder<SafeBrowsingThreat> {
|
||||
public Builder() {}
|
||||
|
||||
private Builder(SafeBrowsingThreat instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SafeBrowsingThreat build() {
|
||||
checkArgumentNotNull(getInstance().domainName, "Domain name cannot be null");
|
||||
checkArgumentNotNull(getInstance().threatType, "Threat type cannot be null");
|
||||
checkArgumentNotNull(getInstance().domainRepoId, "Repo ID cannot be null");
|
||||
checkArgumentNotNull(getInstance().registrarId, "Registrar ID cannot be null");
|
||||
checkArgumentNotNull(getInstance().checkDate, "Check date cannot be null");
|
||||
checkArgumentNotNull(getInstance().tld, "TLD cannot be null");
|
||||
|
||||
return super.build();
|
||||
}
|
||||
|
||||
public Builder setDomainName(String domainName) {
|
||||
getInstance().domainName = domainName;
|
||||
getInstance().tld = DomainNameUtils.getTldFromDomainName(domainName);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setThreatType(ThreatType threatType) {
|
||||
getInstance().threatType = threatType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDomainRepoId(String domainRepoId) {
|
||||
getInstance().domainRepoId = domainRepoId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setRegistrarId(String registrarId) {
|
||||
getInstance().registrarId = registrarId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setCheckDate(LocalDate checkDate) {
|
||||
getInstance().checkDate = checkDate;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2020 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.persistence.converter;
|
||||
|
||||
import javax.persistence.Converter;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
|
||||
/** JPA converter for {@link LocalDate}. */
|
||||
@Converter(autoApply = true)
|
||||
public class LocalDateConverter extends ToStringConverterBase<LocalDate> {
|
||||
|
||||
/** Converts the string (a date in ISO-8601 format) into a LocalDate. */
|
||||
@Override
|
||||
public LocalDate convertToEntityAttribute(String columnValue) {
|
||||
return (columnValue == null) ? null : LocalDate.parse(columnValue, ISODateTimeFormat.date());
|
||||
}
|
||||
}
|
|
@ -28,6 +28,7 @@
|
|||
<class>google.registry.model.host.HostResource</class>
|
||||
<class>google.registry.model.registrar.Registrar</class>
|
||||
<class>google.registry.model.registrar.RegistrarContact</class>
|
||||
<class>google.registry.model.reporting.SafeBrowsingThreat</class>
|
||||
<class>google.registry.schema.domain.RegistryLock</class>
|
||||
<class>google.registry.schema.tmch.ClaimsList</class>
|
||||
<class>google.registry.schema.cursor.Cursor</class>
|
||||
|
@ -52,6 +53,7 @@
|
|||
<class>google.registry.persistence.converter.DateTimeConverter</class>
|
||||
<class>google.registry.persistence.converter.DurationConverter</class>
|
||||
<class>google.registry.persistence.converter.InetAddressSetConverter</class>
|
||||
<class>google.registry.persistence.converter.LocalDateConverter</class>
|
||||
<class>google.registry.persistence.converter.PostalInfoChoiceListConverter</class>
|
||||
<class>google.registry.persistence.converter.RegistrarPocSetConverter</class>
|
||||
<class>google.registry.persistence.converter.StatusValueSetConverter</class>
|
||||
|
|
|
@ -0,0 +1,167 @@
|
|||
// Copyright 2020 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.model.reporting;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.model.reporting.SafeBrowsingThreat.ThreatType.MALWARE;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
|
||||
import static google.registry.testing.DatastoreHelper.createTld;
|
||||
import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation;
|
||||
import static google.registry.testing.SqlHelper.saveRegistrar;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import google.registry.model.EntityTestCase;
|
||||
import google.registry.model.contact.ContactResource;
|
||||
import google.registry.model.domain.DomainBase;
|
||||
import google.registry.model.host.HostResource;
|
||||
import google.registry.model.transfer.ContactTransferData;
|
||||
import google.registry.persistence.VKey;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link SafeBrowsingThreat}. */
|
||||
public class SafeBrowsingThreatTest extends EntityTestCase {
|
||||
|
||||
private static final String REGISTRAR_ID = "registrar";
|
||||
private static final LocalDate DATE = LocalDate.parse("2020-06-10", ISODateTimeFormat.date());
|
||||
|
||||
private SafeBrowsingThreat threat;
|
||||
private DomainBase domain;
|
||||
private HostResource host;
|
||||
private ContactResource registrantContact;
|
||||
|
||||
public SafeBrowsingThreatTest() {
|
||||
super(true);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
VKey<HostResource> hostVKey = VKey.createSql(HostResource.class, "host");
|
||||
VKey<ContactResource> registrantContactVKey =
|
||||
VKey.createSql(ContactResource.class, "contact_id");
|
||||
String domainRepoId = "4-TLD";
|
||||
createTld("tld");
|
||||
|
||||
/** Create a domain for the purpose of testing a foreign key reference in the Threat table. */
|
||||
domain =
|
||||
new DomainBase()
|
||||
.asBuilder()
|
||||
.setCreationClientId(REGISTRAR_ID)
|
||||
.setPersistedCurrentSponsorClientId(REGISTRAR_ID)
|
||||
.setDomainName("foo.tld")
|
||||
.setRepoId(domainRepoId)
|
||||
.setNameservers(hostVKey)
|
||||
.setRegistrant(registrantContactVKey)
|
||||
.setContacts(ImmutableSet.of())
|
||||
.build();
|
||||
|
||||
/** Create a contact for the purpose of testing a foreign key reference in the Domain table. */
|
||||
registrantContact =
|
||||
new ContactResource.Builder()
|
||||
.setRepoId("contact_id")
|
||||
.setCreationClientId(REGISTRAR_ID)
|
||||
.setTransferData(new ContactTransferData.Builder().build())
|
||||
.setPersistedCurrentSponsorClientId(REGISTRAR_ID)
|
||||
.build();
|
||||
|
||||
/** Create a host for the purpose of testing a foreign key reference in the Domain table. */
|
||||
host =
|
||||
new HostResource.Builder()
|
||||
.setRepoId("host")
|
||||
.setHostName("ns1.example.com")
|
||||
.setCreationClientId(REGISTRAR_ID)
|
||||
.setPersistedCurrentSponsorClientId(REGISTRAR_ID)
|
||||
.build();
|
||||
|
||||
threat =
|
||||
new SafeBrowsingThreat.Builder()
|
||||
.setThreatType(MALWARE)
|
||||
.setCheckDate(DATE)
|
||||
.setDomainName("foo.tld")
|
||||
.setDomainRepoId(domainRepoId)
|
||||
.setRegistrarId(REGISTRAR_ID)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersistence() {
|
||||
saveRegistrar(REGISTRAR_ID);
|
||||
|
||||
jpaTm()
|
||||
.transact(
|
||||
() -> {
|
||||
jpaTm().saveNew(registrantContact);
|
||||
jpaTm().saveNew(domain);
|
||||
jpaTm().saveNew(host);
|
||||
jpaTm().saveNew(threat);
|
||||
});
|
||||
|
||||
VKey<SafeBrowsingThreat> threatVKey = VKey.createSql(SafeBrowsingThreat.class, threat.getId());
|
||||
SafeBrowsingThreat persistedThreat = jpaTm().transact(() -> jpaTm().load(threatVKey));
|
||||
threat.id = persistedThreat.id;
|
||||
assertThat(threat).isEqualTo(persistedThreat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThreatForeignKeyConstraints() {
|
||||
assertThrowForeignKeyViolation(
|
||||
() -> {
|
||||
jpaTm()
|
||||
.transact(
|
||||
() -> {
|
||||
// Persist the threat without the associated registrar.
|
||||
jpaTm().saveNew(host);
|
||||
jpaTm().saveNew(registrantContact);
|
||||
jpaTm().saveNew(domain);
|
||||
jpaTm().saveNew(threat);
|
||||
});
|
||||
});
|
||||
|
||||
saveRegistrar(REGISTRAR_ID);
|
||||
|
||||
assertThrowForeignKeyViolation(
|
||||
() -> {
|
||||
jpaTm()
|
||||
.transact(
|
||||
() -> {
|
||||
// Persist the threat without the associated domain.
|
||||
jpaTm().saveNew(registrantContact);
|
||||
jpaTm().saveNew(host);
|
||||
jpaTm().saveNew(threat);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_threatsWithNullFields() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> threat.asBuilder().setRegistrarId(null).build());
|
||||
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> threat.asBuilder().setDomainName(null).build());
|
||||
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> threat.asBuilder().setCheckDate(null).build());
|
||||
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> threat.asBuilder().setThreatType(null).build());
|
||||
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> threat.asBuilder().setDomainRepoId(null).build());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
// Copyright 2020 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.persistence.converter;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
|
||||
|
||||
import google.registry.model.ImmutableObject;
|
||||
import google.registry.persistence.VKey;
|
||||
import google.registry.persistence.transaction.JpaTestRules;
|
||||
import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule;
|
||||
import google.registry.schema.replay.EntityTest;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
/** Unit tests for {@link LocalDateConverter}. */
|
||||
public class LocalDateConverterTest {
|
||||
|
||||
@RegisterExtension
|
||||
public final JpaUnitTestRule jpaRule =
|
||||
new JpaTestRules.Builder()
|
||||
.withEntityClass(LocalDateConverterTestEntity.class)
|
||||
.buildUnitTestRule();
|
||||
|
||||
private final LocalDate exampleDate = LocalDate.parse("2020-06-10", ISODateTimeFormat.date());
|
||||
|
||||
@Test
|
||||
public void testNullInput() {
|
||||
LocalDateConverterTestEntity retrievedEntity = persistAndLoadTestEntity(null);
|
||||
assertThat(retrievedEntity.date).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveAndLoad_success() {
|
||||
LocalDateConverterTestEntity retrievedEntity = persistAndLoadTestEntity(exampleDate);
|
||||
assertThat(retrievedEntity.date).isEqualTo(exampleDate);
|
||||
}
|
||||
|
||||
private LocalDateConverterTestEntity persistAndLoadTestEntity(LocalDate date) {
|
||||
LocalDateConverterTestEntity entity = new LocalDateConverterTestEntity(date);
|
||||
jpaTm().transact(() -> jpaTm().saveNew(entity));
|
||||
LocalDateConverterTestEntity retrievedEntity =
|
||||
jpaTm()
|
||||
.transact(() -> jpaTm().load(VKey.createSql(LocalDateConverterTestEntity.class, "id")));
|
||||
return retrievedEntity;
|
||||
}
|
||||
|
||||
/** Override entity name to avoid the nested class reference. */
|
||||
@Entity(name = "LocalDateConverterTestEntity")
|
||||
@EntityTest.EntityForTesting
|
||||
private static class LocalDateConverterTestEntity extends ImmutableObject {
|
||||
|
||||
@Id String name = "id";
|
||||
|
||||
LocalDate date;
|
||||
|
||||
public LocalDateConverterTestEntity() {}
|
||||
|
||||
LocalDateConverterTestEntity(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,6 +22,7 @@ import google.registry.model.domain.DomainBaseSqlTest;
|
|||
import google.registry.model.history.HostHistoryTest;
|
||||
import google.registry.model.poll.PollMessageTest;
|
||||
import google.registry.model.registry.RegistryLockDaoTest;
|
||||
import google.registry.model.reporting.SafeBrowsingThreatTest;
|
||||
import google.registry.persistence.transaction.JpaEntityCoverage;
|
||||
import google.registry.schema.cursor.CursorDaoTest;
|
||||
import google.registry.schema.integration.SqlIntegrationTestSuite.AfterSuiteTest;
|
||||
|
@ -83,6 +84,7 @@ import org.junit.runner.RunWith;
|
|||
RegistrarDaoTest.class,
|
||||
RegistryLockDaoTest.class,
|
||||
ReservedListDaoTest.class,
|
||||
SafeBrowsingThreatTest.class,
|
||||
// AfterSuiteTest must be the last entry. See class javadoc for details.
|
||||
AfterSuiteTest.class
|
||||
})
|
||||
|
|
|
@ -25,7 +25,7 @@ import google.registry.model.registry.RegistryLockDao;
|
|||
import google.registry.schema.domain.RegistryLock;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
import javax.persistence.RollbackException;
|
||||
import javax.persistence.PersistenceException;
|
||||
import org.junit.function.ThrowingRunnable;
|
||||
|
||||
/** Static utils for setting up and retrieving test resources from the SQL database. */
|
||||
|
@ -66,7 +66,7 @@ public class SqlHelper {
|
|||
}
|
||||
|
||||
public static void assertThrowForeignKeyViolation(ThrowingRunnable runnable) {
|
||||
RollbackException thrown = assertThrows(RollbackException.class, runnable);
|
||||
PersistenceException thrown = assertThrows(PersistenceException.class, runnable);
|
||||
assertThat(Throwables.getRootCause(thrown)).isInstanceOf(SQLException.class);
|
||||
assertThat(Throwables.getRootCause(thrown))
|
||||
.hasMessageThat()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue