Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.elasticsearch.common.Strings;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.ConfigurationUtils;
Expand Down Expand Up @@ -85,7 +86,7 @@ public IngestDocument execute(IngestDocument document) throws Exception {
@Nullable
// visible for testing
static DomainInfo getRegisteredDomain(@Nullable String fqdn) {
if (fqdn == null) {
if (Strings.hasText(fqdn) == false) {
return null;
}
String registeredDomain = SUFFIX_MATCHER.getDomainRoot(fqdn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@

import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.TestIngestDocument;
import org.elasticsearch.ingest.common.RegisteredDomainProcessor.DomainInfo;
import org.elasticsearch.test.ESTestCase;

import java.util.Collections;
import java.util.Map;

import static java.util.Map.entry;
import static org.elasticsearch.ingest.common.RegisteredDomainProcessor.getRegisteredDomain;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

/**
* Test parsing of an eTLD from a FQDN. The list of eTLDs is maintained here:
Expand All @@ -29,36 +32,102 @@
*/
public class RegisteredDomainProcessorTests extends ESTestCase {

public void testBasic() throws Exception {
testRegisteredDomainProcessor("www.google.com", "www.google.com", "google.com", "com", "www");
testRegisteredDomainProcessor("google.com", "google.com", "google.com", "com", null);
testRegisteredDomainProcessor("", null, null, null, null);
testRegisteredDomainProcessor(".", null, null, null, null);
testRegisteredDomainProcessor("$", null, null, null, null);
testRegisteredDomainProcessor("foo.bar.baz", null, null, null, null);
testRegisteredDomainProcessor("www.books.amazon.co.uk", "www.books.amazon.co.uk", "amazon.co.uk", "co.uk", "www.books");
public void testGetRegisteredDomain() {
assertThat(getRegisteredDomain("www.google.com"), is(new DomainInfo("www.google.com", "google.com", "com", "www")));
assertThat(getRegisteredDomain("google.com"), is(new DomainInfo("google.com", "google.com", "com", null)));
assertThat(getRegisteredDomain(null), nullValue());
assertThat(getRegisteredDomain(""), nullValue());
assertThat(getRegisteredDomain(" "), nullValue());
assertThat(getRegisteredDomain("."), nullValue());
assertThat(getRegisteredDomain("$"), nullValue());
assertThat(getRegisteredDomain("foo.bar.baz"), nullValue());
assertThat(
getRegisteredDomain("www.books.amazon.co.uk"),
is(new DomainInfo("www.books.amazon.co.uk", "amazon.co.uk", "co.uk", "www.books"))
);
// Verify "com" is returned as the eTLD, for that FQDN or subdomain
testRegisteredDomainProcessor("com", "com", null, "com", null);
testRegisteredDomainProcessor("example.com", "example.com", "example.com", "com", null);
testRegisteredDomainProcessor("googleapis.com", "googleapis.com", "googleapis.com", "com", null);
testRegisteredDomainProcessor(
"content-autofill.googleapis.com",
"content-autofill.googleapis.com",
"googleapis.com",
"com",
"content-autofill"
assertThat(getRegisteredDomain("com"), is(new DomainInfo("com", null, "com", null)));
assertThat(getRegisteredDomain("example.com"), is(new DomainInfo("example.com", "example.com", "com", null)));
assertThat(getRegisteredDomain("googleapis.com"), is(new DomainInfo("googleapis.com", "googleapis.com", "com", null)));
assertThat(
getRegisteredDomain("content-autofill.googleapis.com"),
is(new DomainInfo("content-autofill.googleapis.com", "googleapis.com", "com", "content-autofill"))
);
// Verify "ssl.fastly.net" is returned as the eTLD, for that FQDN or subdomain
testRegisteredDomainProcessor("global.ssl.fastly.net", "global.ssl.fastly.net", "global.ssl.fastly.net", "ssl.fastly.net", null);
testRegisteredDomainProcessor(
"1.www.global.ssl.fastly.net",
"1.www.global.ssl.fastly.net",
"global.ssl.fastly.net",
"ssl.fastly.net",
"1.www"
assertThat(
getRegisteredDomain("global.ssl.fastly.net"),
is(new DomainInfo("global.ssl.fastly.net", "global.ssl.fastly.net", "ssl.fastly.net", null))
);
assertThat(
getRegisteredDomain("1.www.global.ssl.fastly.net"),
is(new DomainInfo("1.www.global.ssl.fastly.net", "global.ssl.fastly.net", "ssl.fastly.net", "1.www"))
);
}

public void testBasic() throws Exception {
var processor = new RegisteredDomainProcessor(null, null, "input", "output", false);
{
IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("input", "www.google.co.uk"));
processor.execute(document);
assertThat(
document.getSource(),
is(
Map.ofEntries(
entry("input", "www.google.co.uk"),
entry(
"output",
Map.ofEntries(
entry("domain", "www.google.co.uk"),
entry("registered_domain", "google.co.uk"),
entry("top_level_domain", "co.uk"),
entry("subdomain", "www")
)
)
)
)
);
}
{
IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("input", "example.com"));
processor.execute(document);
assertThat(
document.getSource(),
is(
Map.ofEntries(
entry("input", "example.com"),
entry(
"output",
Map.ofEntries(
entry("domain", "example.com"),
entry("registered_domain", "example.com"),
entry("top_level_domain", "com")
)
)
)
)
);
}
{
IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("input", "com"));
processor.execute(document);
assertThat(
document.getSource(),
is(
Map.ofEntries(
entry("input", "com"),
entry(
"output",
Map.ofEntries(
entry("domain", "com"), //
entry("top_level_domain", "com")
)
)
)
)
);
}
}

public void testUseRoot() throws Exception {
var processor = new RegisteredDomainProcessor(null, null, "domain", "", false);
IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("domain", "www.google.co.uk"));
Expand Down Expand Up @@ -110,31 +179,4 @@ public void testIgnoreMissing() throws Exception {
assertThat(document.getSource(), is(Collections.singletonMap("domain", null)));
}
}

private void testRegisteredDomainProcessor(
String fqdn,
String expectedDomain,
String expectedRegisteredDomain,
String expectedETLD,
String expectedSubdomain
) throws Exception {
String domainField = "url.domain";
String registeredDomainField = "url.registered_domain";
String topLevelDomainField = "url.top_level_domain";
String subdomainField = "url.subdomain";

var processor = new RegisteredDomainProcessor(null, null, "domain", "url", true);

IngestDocument document = TestIngestDocument.withDefaultVersion(Map.of("domain", fqdn));
processor.execute(document);

String domain = document.getFieldValue(domainField, String.class, expectedDomain == null);
assertThat(domain, is(expectedDomain));
String registeredDomain = document.getFieldValue(registeredDomainField, String.class, expectedRegisteredDomain == null);
assertThat(registeredDomain, is(expectedRegisteredDomain));
String eTLD = document.getFieldValue(topLevelDomainField, String.class, expectedETLD == null);
assertThat(eTLD, is(expectedETLD));
String subdomain = document.getFieldValue(subdomainField, String.class, expectedSubdomain == null);
assertThat(subdomain, is(expectedSubdomain));
}
}