[
https://issues.apache.org/jira/browse/NIFI-2661?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15984620#comment-15984620
]
ASF GitHub Bot commented on NIFI-2661:
--------------------------------------
Github user trixpan commented on a diff in the pull request:
https://github.com/apache/nifi/pull/1650#discussion_r113428044
--- Diff:
nifi-nar-bundles/nifi-enrich-bundle/nifi-enrich-processors/src/main/java/org/apache/nifi/processors/ISPEnrichIP.java
---
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.nifi.processors;
+
+import com.maxmind.geoip2.exception.GeoIp2Exception;
+import com.maxmind.geoip2.model.IspResponse;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.nifi.annotation.behavior.EventDriven;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.SupportsBatching;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.behavior.WritesAttributes;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processors.maxmind.DatabaseReader;
+import org.apache.nifi.util.StopWatch;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+@EventDriven
+@SideEffectFree
+@SupportsBatching
+@Tags({"ISP", "enrich", "ip", "maxmind"})
+@InputRequirement(Requirement.INPUT_REQUIRED)
+@CapabilityDescription("Looks up ISP information for an IP address and
adds the information to FlowFile attributes. The "
+ + "ISP data is provided as a MaxMind database. The attribute that
contains the IP address to lookup is provided by the "
+ + "'IP Address Attribute' property. If the name of the attribute
provided is 'X', then the the attributes added by enrichment "
+ + "will take the form X.isp.<fieldName>")
+@WritesAttributes({
+ @WritesAttribute(attribute = "X.isp.lookup.micros", description = "The
number of microseconds that the geo lookup took"),
+ @WritesAttribute(attribute = "X.isp.asn", description = "The
Autonomous System Number (ASN) identified for the IP address"),
+ @WritesAttribute(attribute = "X.isp.asn.organization", description =
"The Organization Associated with the ASN identified"),
+ @WritesAttribute(attribute = "X.isp.name", description = "The name of
the ISP associated with the IP address provided"),
+ @WritesAttribute(attribute = "X.isp.organization", description = "The
Organization associated with the IP address provided"),})
+public class ISPEnrichIP extends AbstractEnrichIP {
+
+ @Override
+ public void onTrigger(final ProcessContext context, final
ProcessSession session) throws ProcessException {
+ FlowFile flowFile = session.get();
+ if (flowFile == null) {
+ return;
+ }
+
+ final DatabaseReader dbReader = databaseReaderRef.get();
+ final String ipAttributeName =
context.getProperty(IP_ADDRESS_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
+ final String ipAttributeValue =
flowFile.getAttribute(ipAttributeName);
+ if (StringUtils.isEmpty(ipAttributeName)) { //TODO need to add
additional validation - should look like an IPv4 or IPv6 addr for instance
--- End diff --
I was revisiting the code and to be honest that TODO should be simply
removed. The validation is done just following that section when we call:
```
try {
inetAddress = InetAddress.getByName(ipAttributeValue);
} catch (final IOException ioe) {
session.transfer(flowFile, REL_NOT_FOUND);
getLogger().warn("Could not resolve {} to ip address for {}",
new Object[]{ipAttributeValue, flowFile}, ioe);
return;
}
```
IMNSHO relying on `InetAddress.getByName(String)` to validate is far better
than using something like Apache Commons InetAddressValidator as the very same
object used for validation is then re-used within the processor logic itself.
```
try {
response = dbReader.city(inetAddress);
stopWatch.stop();
}
```
I would guess that anything else would be validating the same input twice??
So instead I did instead update the log messages raised by triggering the
condition you pointed / the exception above.
let me know what you think
> Create enrichment processor supporting GeoLite ASN
> --------------------------------------------------
>
> Key: NIFI-2661
> URL: https://issues.apache.org/jira/browse/NIFI-2661
> Project: Apache NiFi
> Issue Type: Improvement
> Reporter: Andre F de Miranda
> Assignee: Andre F de Miranda
>
> Current EnrichGeoIP does not support MaxMind's GeoLite ASN API and database.
> It would be great to have a processor capable of doing so.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)