-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
URI parts ingest processor #65150
URI parts ingest processor #65150
Changes from 4 commits
df786f8
3d76502
828914e
caf2715
0c9163f
30b9eff
44bb394
6b052cb
c128522
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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. | ||
*/ | ||
|
||
apply plugin: 'elasticsearch.esplugin' | ||
apply plugin: 'elasticsearch.internal-cluster-test' | ||
esplugin { | ||
name 'x-pack-ingest' | ||
description 'Elasticsearch Expanded Pack Plugin - Ingest' | ||
classname 'org.elasticsearch.xpack.ingest.IngestPlugin' | ||
extendedPlugins = ['x-pack-core'] | ||
} | ||
archivesBaseName = 'x-pack-ingest' | ||
|
||
dependencies { | ||
compileOnly project(path: xpackModule('core'), configuration: 'default') | ||
testImplementation project(path: xpackModule('core'), configuration: 'testArtifacts') | ||
testImplementation project(path: ':modules:ingest-common') | ||
testImplementation project(path: ':modules:lang-mustache') | ||
testImplementation project(path: ':modules:geo') | ||
testImplementation project(path: xpackModule('monitoring'), configuration: 'testArtifacts') | ||
} | ||
|
||
addQaCheckDependencies() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ingest; | ||
|
||
import org.elasticsearch.ingest.Processor; | ||
import org.elasticsearch.plugins.Plugin; | ||
|
||
import java.util.Map; | ||
|
||
public class IngestPlugin extends Plugin implements org.elasticsearch.plugins.IngestPlugin { | ||
|
||
@Override | ||
public Map<String, Processor.Factory> getProcessors(Processor.Parameters parameters) { | ||
return Map.of(UrlPartsProcessor.TYPE, new UrlPartsProcessor.Factory()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ingest; | ||
|
||
import org.elasticsearch.ingest.AbstractProcessor; | ||
import org.elasticsearch.ingest.ConfigurationUtils; | ||
import org.elasticsearch.ingest.IngestDocument; | ||
import org.elasticsearch.ingest.Processor; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class UrlPartsProcessor extends AbstractProcessor { | ||
|
||
public static final String TYPE = "url_parts"; | ||
|
||
private final String field; | ||
private final String targetField; | ||
private final boolean removeIfSuccessful; | ||
private final boolean keepOriginal; | ||
|
||
UrlPartsProcessor(String tag, String description, String field, String targetField, boolean removeIfSuccessful, boolean keepOriginal) { | ||
super(tag, description); | ||
this.field = field; | ||
this.targetField = targetField; | ||
this.removeIfSuccessful = removeIfSuccessful; | ||
this.keepOriginal = keepOriginal; | ||
} | ||
|
||
public String getField() { | ||
return field; | ||
} | ||
|
||
public String getTargetField() { | ||
return targetField; | ||
} | ||
|
||
public boolean getRemoveIfSuccessful() { | ||
return removeIfSuccessful; | ||
} | ||
|
||
public boolean getKeepOriginal() { | ||
return keepOriginal; | ||
} | ||
|
||
@Override | ||
public IngestDocument execute(IngestDocument ingestDocument) throws Exception { | ||
String value = ingestDocument.getFieldValue(field, String.class); | ||
|
||
URL url; | ||
try { | ||
url = new URL(value); | ||
} catch (MalformedURLException e) { | ||
throw new IllegalArgumentException("unable to parse URL [" + value + "]"); | ||
} | ||
var urlParts = new HashMap<String, Object>(); | ||
urlParts.put("domain", url.getHost()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ECS isn't clear on what's correct here, but what does @elastic/ecs Should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per this source, Since brackets are required with a literal IPv6 address, |
||
if (url.getRef() != null) { | ||
urlParts.put("fragment", url.getRef()); | ||
} | ||
if (keepOriginal) { | ||
urlParts.put("original", value); | ||
} | ||
final String path = url.toURI().getPath(); | ||
if (path != null) { | ||
urlParts.put("path", path); | ||
if (path.contains(".")) { | ||
int periodIndex = path.lastIndexOf('.'); | ||
urlParts.put("extension", periodIndex < path.length() ? path.substring(periodIndex + 1) : ""); | ||
} | ||
} | ||
if (url.getPort() != -1) { | ||
urlParts.put("port", url.getPort()); | ||
} | ||
if (url.getQuery() != null) { | ||
urlParts.put("query", url.getQuery()); | ||
} | ||
urlParts.put("scheme", url.getProtocol()); | ||
final String userInfo = url.getUserInfo(); | ||
if (userInfo != null) { | ||
urlParts.put("user_info", userInfo); | ||
if (userInfo.contains(":")) { | ||
int colonIndex = userInfo.indexOf(":"); | ||
urlParts.put("username", userInfo.substring(0, colonIndex)); | ||
urlParts.put("password", colonIndex < userInfo.length() ? userInfo.substring(colonIndex + 1) : ""); | ||
} | ||
} | ||
|
||
if (removeIfSuccessful && targetField.equals(field) == false) { | ||
ingestDocument.removeField(field); | ||
} | ||
ingestDocument.setFieldValue(targetField, urlParts); | ||
return ingestDocument; | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return TYPE; | ||
} | ||
|
||
public static final class Factory implements Processor.Factory { | ||
|
||
@Override | ||
public UrlPartsProcessor create( | ||
Map<String, Processor.Factory> registry, | ||
String processorTag, | ||
String description, | ||
Map<String, Object> config | ||
) throws Exception { | ||
String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); | ||
String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", "url"); | ||
boolean removeIfSuccessful = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "remove_if_successful", false); | ||
boolean keepOriginal = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "keep_original", true); | ||
return new UrlPartsProcessor(processorTag, description, field, targetField, removeIfSuccessful, keepOriginal); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ingest; | ||
|
||
import org.elasticsearch.ElasticsearchParseException; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.junit.Before; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
public class UrlPartsProcessorFactoryTests extends ESTestCase { | ||
|
||
private UrlPartsProcessor.Factory factory; | ||
|
||
@Before | ||
public void init() { | ||
factory = new UrlPartsProcessor.Factory(); | ||
} | ||
|
||
public void testCreate() throws Exception { | ||
Map<String, Object> config = new HashMap<>(); | ||
String field = randomAlphaOfLength(6); | ||
config.put("field", field); | ||
String targetField = "url"; | ||
if (randomBoolean()) { | ||
targetField = randomAlphaOfLength(6); | ||
config.put("target_field", targetField); | ||
} | ||
boolean removeIfSuccessful = randomBoolean(); | ||
config.put("remove_if_successful", removeIfSuccessful); | ||
boolean keepOriginal = randomBoolean(); | ||
config.put("keep_original", keepOriginal); | ||
|
||
String processorTag = randomAlphaOfLength(10); | ||
UrlPartsProcessor urlPartsProcessor = factory.create(null, processorTag, null, config); | ||
assertThat(urlPartsProcessor.getTag(), equalTo(processorTag)); | ||
assertThat(urlPartsProcessor.getField(), equalTo(field)); | ||
assertThat(urlPartsProcessor.getTargetField(), equalTo(targetField)); | ||
assertThat(urlPartsProcessor.getRemoveIfSuccessful(), equalTo(removeIfSuccessful)); | ||
assertThat(urlPartsProcessor.getKeepOriginal(), equalTo(keepOriginal)); | ||
} | ||
|
||
public void testCreateNoFieldPresent() throws Exception { | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("value", "value1"); | ||
try { | ||
factory.create(null, null, null, config); | ||
fail("factory create should have failed"); | ||
} catch (ElasticsearchParseException e) { | ||
assertThat(e.getMessage(), equalTo("[field] required property is missing")); | ||
} | ||
} | ||
|
||
public void testCreateNullField() throws Exception { | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("field", null); | ||
try { | ||
factory.create(null, null, null, config); | ||
fail("factory create should have failed"); | ||
} catch (ElasticsearchParseException e) { | ||
assertThat(e.getMessage(), equalTo("[field] required property is missing")); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there limitations on which schemes that this can parse? Would these parse?
Perhaps using java.net.URI would be more forgiving and not require a URLStreamHandler to be loaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly parts of the URL are required for parsing to work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrewkroh, thanks for looking it over and commenting. I switched over to
java.net.URI
which does support more schemes including all three of the examples you list above.Right now, no parts of a URI are required beyond what
java.net.URI
needs to construct an instance. Is that what you would prefer?