Skip to content
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

Make sure to reject mappings with type _doc when include_type_name is false. #38270

Merged

Conversation

jtibshirani
Copy link
Contributor

@jtibshirani jtibshirani commented Feb 3, 2019

CreateIndexRequest#source(Map<String, Object>, ... ), which is used when deserializing index creation requests, accidentally accepts mappings that are nested twice under the type key (as described in the bug report #38266).

This in turn causes us to be too lenient in parsing typeless mappings. In particular, we accept the following index creation request, even though it should not contain the type key _doc:

PUT index?include_type_name=false
{
  "mappings": {
    "_doc": {
      "properties": { ... }
    }
}

There is a similar issue for both 'put templates' and 'put mappings' requests as well.

This PR makes the minimal changes to detect and reject these typed mappings in requests. It does not address #38266 generally, or attempt a larger refactor around types in these server-side requests, as I think this should be done at a later time.

@jtibshirani jtibshirani added >bug :Search Foundations/Mapping Index mappings, including merging and defining field types :Data Management/Indices APIs APIs to create and manage indices and templates v7.0.0 v6.7.0 labels Feb 3, 2019
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-features

@jtibshirani jtibshirani force-pushed the dummy-type-in-typeless-requests branch from 2bbf871 to 82066cf Compare February 3, 2019 22:52
Copy link
Contributor

@jpountz jpountz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some minor comments but in general it looks good to me. Thanks for extracting the map manipulation code to functions that can be unit-tested.

version: " - 6.99.99"
reason: include_type_name defaults to true before 7.0
- do:
catch: /illegal_argument_exception/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we match a more specific string in the error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

index: test_index

- do:
catch: /illegal_argument_exception/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we match a more specific string in the error message?

reason: include_type_name defaults to true before 7.0

- do:
catch: /illegal_argument_exception/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we match a more specific string in the error message?



static Map<String, Object> prepareMappings(Map<String, Object> source, boolean includeTypeName) {
if (includeTypeName || !source.containsKey("mappings")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use == false for consistency with the rest of the codebase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, this is proving a hard habit to break!

Map<String, Object> newSource = new HashMap<>(source);

@SuppressWarnings("unchecked")
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast is unsafe and would result in a confusing error message if a user created an index with something like below? Should we have an instanceof Map check?

PUT test
{
  "mappings": "foo"
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will fix this.

Copy link
Contributor Author

@jtibshirani jtibshirani Feb 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that I made this method more robust, but after testing I noticed the same confusing error will occur even without these changes. This is because of another unchecked cast: https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java#L381

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking

@jtibshirani
Copy link
Contributor Author

Thank you @jpountz for reviewing, it is ready for another look.

Copy link
Contributor

@jakelandis jakelandis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Map<String, Object> newSource = new HashMap<>(source);

@SuppressWarnings("unchecked")
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking

@jtibshirani jtibshirani merged commit 3ce7d2c into elastic:master Feb 5, 2019
@jtibshirani jtibshirani deleted the dummy-type-in-typeless-requests branch February 5, 2019 18:52
jasontedor added a commit to jasontedor/elasticsearch that referenced this pull request Feb 5, 2019
* master: (23 commits)
  Lift retention lease expiration to index shard (elastic#38380)
  Make Ccr recovery file chunk size configurable (elastic#38370)
  Prevent CCR recovery from missing documents (elastic#38237)
  re-enables awaitsfixed datemath tests (elastic#38376)
  Types removal fix FullClusterRestartIT warnings (elastic#38445)
  Make sure to reject mappings with type _doc when include_type_name is false. (elastic#38270)
  Updates the grok patterns to be consistent with logstash (elastic#27181)
  Ignore type-removal warnings in XPackRestTestHelper (elastic#38431)
  testHlrcFromXContent() should respect assertToXContentEquivalence() (elastic#38232)
  add basic REST test for geohash_grid (elastic#37996)
  Remove DiscoveryPlugin#getDiscoveryTypes (elastic#38414)
  Fix the clock resolution to millis in GetWatchResponseTests (elastic#38405)
  Throw AssertionError when no master (elastic#38432)
  `if_seq_no` and `if_primary_term` parameters aren't wired correctly in REST Client's CRUD API (elastic#38411)
  Enable CronEvalToolTest.testEnsureDateIsShownInRootLocale (elastic#38394)
  Fix failures in BulkProcessorIT#testGlobalParametersAndBulkProcessor. (elastic#38129)
  SQL: Implement CURRENT_DATE (elastic#38175)
  Mute testReadRequestsReturnLatestMappingVersion (elastic#38438)
  [ML] Report index unavailable instead of waiting for lazy node (elastic#38423)
  Update Rollup Caps to allow unknown fields (elastic#38339)
  ...
dliappis added a commit to dliappis/rally-tracks that referenced this pull request Feb 6, 2019
elastic/elasticsearch#38270 made current master
(7.0.0) reject mappings with type `_doc` unless
`?include_type_name=true` is passed during index creation.

Workaround the rejection for now by explicitly setting
`include_type_name` to `true` in every create_index operation.
dliappis added a commit to elastic/rally-tracks that referenced this pull request Feb 6, 2019
elastic/elasticsearch#38270 made current master
(7.0.0) reject mappings with type `_doc` unless
`?include_type_name=true` is passed during index creation.

Workaround the rejection for now by explicitly setting
`include_type_name` to `true` in every create_index operation.

Relates: #64
@danielmitterdorfer danielmitterdorfer removed the :Data Management/Indices APIs APIs to create and manage indices and templates label Feb 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Search Foundations/Mapping Index mappings, including merging and defining field types v6.7.0 v7.0.0-beta1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants