|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.ingest.common; |
| 10 | + |
| 11 | +import org.apache.http.conn.util.PublicSuffixMatcher; |
| 12 | +import org.apache.http.conn.util.PublicSuffixMatcherLoader; |
| 13 | +import org.elasticsearch.ingest.AbstractProcessor; |
| 14 | +import org.elasticsearch.ingest.ConfigurationUtils; |
| 15 | +import org.elasticsearch.ingest.IngestDocument; |
| 16 | +import org.elasticsearch.ingest.Processor; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +public class RegisteredDomainProcessor extends AbstractProcessor { |
| 21 | + private static final PublicSuffixMatcher SUFFIX_MATCHER = PublicSuffixMatcherLoader.getDefault(); |
| 22 | + |
| 23 | + public static final String TYPE = "registered_domain"; |
| 24 | + |
| 25 | + private final String field; |
| 26 | + private final String targetField; |
| 27 | + private final boolean ignoreMissing; |
| 28 | + |
| 29 | + RegisteredDomainProcessor( |
| 30 | + String tag, |
| 31 | + String description, |
| 32 | + String field, |
| 33 | + String targetField, |
| 34 | + boolean ignoreMissing |
| 35 | + ) { |
| 36 | + super(tag, description); |
| 37 | + this.field = field; |
| 38 | + this.targetField = targetField; |
| 39 | + this.ignoreMissing = ignoreMissing; |
| 40 | + } |
| 41 | + |
| 42 | + public String getField() { |
| 43 | + return field; |
| 44 | + } |
| 45 | + |
| 46 | + public String getTargetField() { |
| 47 | + return targetField; |
| 48 | + } |
| 49 | + |
| 50 | + public boolean getIgnoreMissing() { |
| 51 | + return ignoreMissing; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public IngestDocument execute(IngestDocument ingestDocument) throws Exception { |
| 56 | + DomainInfo info = getRegisteredDomain(ingestDocument); |
| 57 | + if (info == null) { |
| 58 | + if (ignoreMissing) { |
| 59 | + return ingestDocument; |
| 60 | + } else { |
| 61 | + throw new IllegalArgumentException("unable to set domain information for document"); |
| 62 | + } |
| 63 | + } |
| 64 | + String fieldPrefix = targetField; |
| 65 | + if (fieldPrefix.equals("") == false) { |
| 66 | + fieldPrefix += "."; |
| 67 | + } |
| 68 | + String domainTarget = fieldPrefix + "domain"; |
| 69 | + String registeredDomainTarget = fieldPrefix + "registered_domain"; |
| 70 | + String subdomainTarget = fieldPrefix + "subdomain"; |
| 71 | + String topLevelDomainTarget = fieldPrefix + "top_level_domain"; |
| 72 | + |
| 73 | + if (info.getDomain() != null) { |
| 74 | + ingestDocument.setFieldValue(domainTarget, info.getDomain()); |
| 75 | + } |
| 76 | + if (info.getRegisteredDomain() != null) { |
| 77 | + ingestDocument.setFieldValue(registeredDomainTarget, info.getRegisteredDomain()); |
| 78 | + } |
| 79 | + if (info.getETLD() != null) { |
| 80 | + ingestDocument.setFieldValue(topLevelDomainTarget, info.getETLD()); |
| 81 | + } |
| 82 | + if (info.getSubdomain() != null) { |
| 83 | + ingestDocument.setFieldValue(subdomainTarget, info.getSubdomain()); |
| 84 | + } |
| 85 | + return ingestDocument; |
| 86 | + } |
| 87 | + |
| 88 | + private DomainInfo getRegisteredDomain(IngestDocument d) { |
| 89 | + String fieldString = d.getFieldValue(field, String.class, ignoreMissing); |
| 90 | + if (fieldString == null) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + String registeredDomain = SUFFIX_MATCHER.getDomainRoot(fieldString); |
| 94 | + if (registeredDomain == null) { |
| 95 | + if (SUFFIX_MATCHER.matches(fieldString)) { |
| 96 | + return new DomainInfo(fieldString); |
| 97 | + } |
| 98 | + return null; |
| 99 | + } |
| 100 | + if (registeredDomain.indexOf(".") == -1) { |
| 101 | + // we have domain with no matching public suffix, but "." in it |
| 102 | + return null; |
| 103 | + } |
| 104 | + return new DomainInfo(registeredDomain, fieldString); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String getType() { |
| 109 | + return TYPE; |
| 110 | + } |
| 111 | + |
| 112 | + private class DomainInfo { |
| 113 | + private final String domain; |
| 114 | + private final String registeredDomain; |
| 115 | + private final String eTLD; |
| 116 | + private final String subdomain; |
| 117 | + |
| 118 | + private DomainInfo(String eTLD) { |
| 119 | + this.domain = eTLD; |
| 120 | + this.eTLD = eTLD; |
| 121 | + this.registeredDomain = null; |
| 122 | + this.subdomain = null; |
| 123 | + } |
| 124 | + |
| 125 | + private DomainInfo(String registeredDomain, String domain) { |
| 126 | + int index = registeredDomain.indexOf(".") + 1; |
| 127 | + if (index > 0 && index < registeredDomain.length()) { |
| 128 | + this.domain = domain; |
| 129 | + this.eTLD = registeredDomain.substring(index); |
| 130 | + this.registeredDomain = registeredDomain; |
| 131 | + int subdomainIndex = domain.lastIndexOf("." + registeredDomain); |
| 132 | + if (subdomainIndex > 0) { |
| 133 | + this.subdomain = domain.substring(0, subdomainIndex); |
| 134 | + } else { |
| 135 | + this.subdomain = null; |
| 136 | + } |
| 137 | + } else { |
| 138 | + this.domain = null; |
| 139 | + this.eTLD = null; |
| 140 | + this.registeredDomain = null; |
| 141 | + this.subdomain = null; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public String getDomain() { |
| 146 | + return domain; |
| 147 | + } |
| 148 | + |
| 149 | + public String getSubdomain() { |
| 150 | + return subdomain; |
| 151 | + } |
| 152 | + |
| 153 | + public String getRegisteredDomain() { |
| 154 | + return registeredDomain; |
| 155 | + } |
| 156 | + |
| 157 | + public String getETLD() { |
| 158 | + return eTLD; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + public static final class Factory implements Processor.Factory { |
| 163 | + |
| 164 | + static final String DEFAULT_TARGET_FIELD = ""; |
| 165 | + |
| 166 | + @Override |
| 167 | + public RegisteredDomainProcessor create( |
| 168 | + Map<String, Processor.Factory> registry, |
| 169 | + String processorTag, |
| 170 | + String description, |
| 171 | + Map<String, Object> config |
| 172 | + ) throws Exception { |
| 173 | + String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); |
| 174 | + String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", DEFAULT_TARGET_FIELD); |
| 175 | + boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", true); |
| 176 | + |
| 177 | + return new RegisteredDomainProcessor( |
| 178 | + processorTag, |
| 179 | + description, |
| 180 | + field, |
| 181 | + targetField, |
| 182 | + ignoreMissing |
| 183 | + ); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments