-
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
Merged
Merged
URI parts ingest processor #65150
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
df786f8
URL parts processor with new ingest module
danhermann 3d76502
spotless making code less readable :(
danhermann 828914e
forbidden APIs
danhermann caf2715
add tests, don't remove target field
danhermann 0c9163f
switch to java.net.URI and add additional test cases
danhermann 30b9eff
disable testing conventions check
danhermann 44bb394
spotless. again.
danhermann 6b052cb
rename URL to URI
danhermann c128522
test case for blank password in user info
danhermann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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() | ||
|
||
testingConventions.enabled = false |
20 changes: 20 additions & 0 deletions
20
x-pack/plugin/ingest/src/main/java/org/elasticsearch/xpack/ingest/IngestPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(UriPartsProcessor.TYPE, new UriPartsProcessor.Factory()); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
x-pack/plugin/ingest/src/main/java/org/elasticsearch/xpack/ingest/UriPartsProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class UriPartsProcessor extends AbstractProcessor { | ||
|
||
public static final String TYPE = "uri_parts"; | ||
|
||
private final String field; | ||
private final String targetField; | ||
private final boolean removeIfSuccessful; | ||
private final boolean keepOriginal; | ||
|
||
UriPartsProcessor(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); | ||
|
||
URI uri; | ||
try { | ||
uri = new URI(value); | ||
} catch (URISyntaxException e) { | ||
throw new IllegalArgumentException("unable to parse URI [" + value + "]"); | ||
} | ||
var uriParts = new HashMap<String, Object>(); | ||
uriParts.put("domain", uri.getHost()); | ||
if (uri.getFragment() != null) { | ||
uriParts.put("fragment", uri.getFragment()); | ||
} | ||
if (keepOriginal) { | ||
uriParts.put("original", value); | ||
} | ||
final String path = uri.getPath(); | ||
if (path != null) { | ||
uriParts.put("path", path); | ||
if (path.contains(".")) { | ||
int periodIndex = path.lastIndexOf('.'); | ||
uriParts.put("extension", periodIndex < path.length() ? path.substring(periodIndex + 1) : ""); | ||
} | ||
} | ||
if (uri.getPort() != -1) { | ||
uriParts.put("port", uri.getPort()); | ||
} | ||
if (uri.getQuery() != null) { | ||
uriParts.put("query", uri.getQuery()); | ||
} | ||
uriParts.put("scheme", uri.getScheme()); | ||
final String userInfo = uri.getUserInfo(); | ||
if (userInfo != null) { | ||
uriParts.put("user_info", userInfo); | ||
if (userInfo.contains(":")) { | ||
int colonIndex = userInfo.indexOf(":"); | ||
uriParts.put("username", userInfo.substring(0, colonIndex)); | ||
uriParts.put("password", colonIndex < userInfo.length() ? userInfo.substring(colonIndex + 1) : ""); | ||
} | ||
} | ||
|
||
if (removeIfSuccessful && targetField.equals(field) == false) { | ||
ingestDocument.removeField(field); | ||
} | ||
ingestDocument.setFieldValue(targetField, uriParts); | ||
return ingestDocument; | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return TYPE; | ||
} | ||
|
||
public static final class Factory implements Processor.Factory { | ||
|
||
@Override | ||
public UriPartsProcessor 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 UriPartsProcessor(processorTag, description, field, targetField, removeIfSuccessful, keepOriginal); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...in/ingest/src/test/java/org/elasticsearch/xpack/ingest/UriPartsProcessorFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 UriPartsProcessorFactoryTests extends ESTestCase { | ||
|
||
private UriPartsProcessor.Factory factory; | ||
|
||
@Before | ||
public void init() { | ||
factory = new UriPartsProcessor.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); | ||
UriPartsProcessor uriPartsProcessor = factory.create(null, processorTag, null, config); | ||
assertThat(uriPartsProcessor.getTag(), equalTo(processorTag)); | ||
assertThat(uriPartsProcessor.getField(), equalTo(field)); | ||
assertThat(uriPartsProcessor.getTargetField(), equalTo(targetField)); | ||
assertThat(uriPartsProcessor.getRemoveIfSuccessful(), equalTo(removeIfSuccessful)); | ||
assertThat(uriPartsProcessor.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")); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
would this fail with
IndexOutOfBounds
forhttp://user:@www.google.com:80/blarg.gif#ref
? (no password)Shall we add a test for this?
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.
In that case, the password is set to an empty string. I'll add another test case to make that clear.
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.
Thanks for the review, @andreidan!