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

Scripting: Augment String with sha1 and sha256 #59671

Merged

Conversation

stu-elastic
Copy link
Contributor

@stu-elastic stu-elastic commented Jul 15, 2020

Only available in the ingest context for use in ingest pipelines.

Digests are computed on the UTF-8 encoding of the String and are
returned as hex strings.

sha1() return hex strings of length 40, sha256() returns length 64

Fixes: #59633

Only available in the ingest context for use in ingest pipelines.

Digests are computed on the UTF-8 encoding of the String and are
returned as hex strings.

sha1() return hex strings of length 40, sha256() returns length 64

Fixes: elastic#59647
@stu-elastic stu-elastic added >enhancement :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache v8.0.0 v7.10.0 labels Jul 15, 2020
@stu-elastic stu-elastic requested review from rjernst and jdconrad July 15, 2020 21:04
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (:Core/Infra/Scripting)

@elasticmachine elasticmachine added the Team:Core/Infra Meta label for core/infra team label Jul 15, 2020
Copy link
Contributor

@jdconrad jdconrad left a comment

Choose a reason for hiding this comment

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

Great change!

@stu-elastic
Copy link
Contributor Author

stu-elastic commented Jul 15, 2020

We're adding sha1 and sha256 because they, along with md5, must be implemented by the MessageDigest SPI since at least Java 8.

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

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

LGTM

@stu-elastic
Copy link
Contributor Author

Manual integration testing to ensure this feature can be used as expected:

# Create pipeline
curl --user 'elastic-admin:elastic-password' -H 'Content-Type: application/json' -XPUT localhost:9200/_ingest/pipeline/foo_id -d '{
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": "ctx._id = ctx.bar.sha256()"
      }
    }
  ]
}'
{
  "acknowledged": true
}
# Create index and index document using pipeline
curl --user 'elastic-admin:elastic-password' -H 'Content-Type: application/json' localhost:9200/foo/_doc?pipeline=foo_id -d '{ "bar": "baz", "qux": 123 }'
{
  "_index": "foo",
  "_id": "baa5a0964d3320fbc0c6a922140453c8513ea24ab8fd0577034804a967248096",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
# Fetch document
curl --user 'elastic-admin:elastic-password' localhost:9200/foo/_search?q=bar:baz
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "foo",
        "_id": "baa5a0964d3320fbc0c6a922140453c8513ea24ab8fd0577034804a967248096",
        "_score": 0.2876821,
        "_source": {
          "bar": "baz",
          "qux": 123
        }
      }
    ]
  }
}
# Attempt indexing different document with the same `bar` value
curl --user 'elastic-admin:elastic-password' -H 'Content-Type: application/json' localhost:9200/foo/_doc?pipeline=foo_id -d '{ "bar": "baz", "qux": 451, "other": 123 }'
{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[baa5a0964d3320fbc0c6a922140453c8513ea24ab8fd0577034804a967248096]: version conflict, document already exists (current version [1])",
        "index_uuid": "Nt3z3wMaS7-bfiC2OD_EPQ",
        "shard": "0",
        "index": "foo"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[baa5a0964d3320fbc0c6a922140453c8513ea24ab8fd0577034804a967248096]: version conflict, document already exists (current version [1])",
    "index_uuid": "Nt3z3wMaS7-bfiC2OD_EPQ",
    "shard": "0",
    "index": "foo"
  },
  "status": 409
}
# Ensure document not updated
curl --user 'elastic-admin:elastic-password' localhost:9200/foo/_search?q=bar:baz
{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "foo",
        "_id": "baa5a0964d3320fbc0c6a922140453c8513ea24ab8fd0577034804a967248096",
        "_score": 0.2876821,
        "_source": {
          "bar": "baz",
          "qux": 123
        }
      }
    ]
  }
}
# Index different document
curl --user 'elastic-admin:elastic-password' -H 'Content-Type: application/json' localhost:9200/foo/_doc?pipeline=foo_id -d '{ "bar": "foo", "qux": 789 }'
{
  "_index": "foo",
  "_id": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}
# Ensure document indexed
curl --user 'elastic-admin:elastic-password' localhost:9200/foo/_search?q=*
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "foo",
        "_id": "baa5a0964d3320fbc0c6a922140453c8513ea24ab8fd0577034804a967248096",
        "_score": 1,
        "_source": {
          "bar": "baz",
          "qux": 123
        }
      },
      {
        "_index": "foo",
        "_id": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
        "_score": 1,
        "_source": {
          "bar": "foo",
          "qux": 789
        }
      }
    ]
  }
}

@stu-elastic stu-elastic merged commit 3c85272 into elastic:master Jul 16, 2020
stu-elastic added a commit to stu-elastic/elasticsearch that referenced this pull request Jul 16, 2020
Only available in the ingest context for use in ingest pipelines.

Digests are computed on the UTF-8 encoding of the String and are
returned as hex strings.

sha1() return hex strings of length 40, sha256() returns length 64

Fixes: elastic#59647
Backport: 3c85272
stu-elastic added a commit that referenced this pull request Jul 16, 2020
Only available in the ingest context for use in ingest pipelines.

Digests are computed on the UTF-8 encoding of the String and are
returned as hex strings.

sha1() return hex strings of length 40, sha256() returns length 64

Fixes: #59647
Backport: 3c85272
@stu-elastic
Copy link
Contributor Author

master: 3c85272
7.x (7.10): 8fdaed0

stu-elastic added a commit to stu-elastic/elasticsearch that referenced this pull request Aug 18, 2020
Strings in the watcher context may use the `.sha1()` and `.sha256()`
augmentation added for ingest.

Ref: elastic#59633, elastic#59671
Fixes: elastic#61244
stu-elastic added a commit that referenced this pull request Oct 7, 2020
Strings in the watcher context may use the `.sha1()` and `.sha256()`
augmentation added for ingest.

Ref: #59633, #59671
Fixes: #61244
stu-elastic added a commit to stu-elastic/elasticsearch that referenced this pull request Oct 7, 2020
Strings in the watcher context may use the `.sha1()` and `.sha256()`
augmentation added for ingest.

Ref: elastic#59633, elastic#59671
Fixes: elastic#61244
stu-elastic added a commit that referenced this pull request Oct 7, 2020
Strings in the watcher context may use the `.sha1()` and `.sha256()`
augmentation added for ingest.

Ref: #59633, #59671
Fixes: #61244
 Backport of: 380ee6f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement Team:Core/Infra Meta label for core/infra team v7.10.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Whitelist SHA2 for use in ingest pipelines
5 participants