---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
---
Test put redact pipeline:
- do:
    ingest.put_pipeline:
      id: "pipeline-using-a-redact-processor"
      body: "{\n  \"processors\": [\n    {\n      \"redact\": {\n        \"field\"\
        : \"to_redact\",\n        \"patterns\": [\"%{EMAILADDRESS:EMAIL}\", \"%{IP:IP_ADDRESS}\"\
        , \"%{CREDIT_CARD:CREDIT_CARD}\"],\n        \"pattern_definitions\": {\n \
        \         \"CREDIT_CARD\": \"\\\\d{4}[ -]\\\\d{4}[ -]\\\\d{4}[ -]\\\\d{4}\"\
        \n        }\n      }\n    }\n  ]\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    acknowledged: true
- do:
    index:
      index: "test"
      id: "1"
      pipeline: "pipeline-using-a-redact-processor"
      body:
        to_redact: "0.0.0.1 is my secret IP to redact"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    get:
      index: "test"
      id: "1"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    _source.to_redact: "<IP_ADDRESS> is my secret IP to redact"
---
Test redact:
- do:
    ingest.simulate:
      body: "{\n  \"pipeline\": {\n    \"processors\": [\n      {\n        \"redact\"\
        : {\n          \"field\": \"to_redact\",\n          \"patterns\": [\"%{EMAILADDRESS:EMAIL}\"\
        , \"%{IP:IP_ADDRESS}\"]\n        }\n      }\n    ]\n  },\n  \"docs\": [{\"\
        _source\": {\"to_redact\": \"this-email@address.com will be redacted\"}}]\n\
        }\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    docs: 1
- match:
    docs.0.doc._source.to_redact: "<EMAIL> will be redacted"
---
Test ignore missing:
- do:
    ingest.simulate:
      body: "{\n  \"pipeline\": {\n    \"processors\": [\n      {\n        \"redact\"\
        : {\n          \"field\": \"to_redact\",\n          \"ignore_missing\": false,\n\
        \          \"patterns\": [\"%{EMAILADDRESS:EMAIL}\", \"%{IP:IP_ADDRESS}\"\
        ]\n        }\n      }\n    ]\n  },\n  \"docs\": [{\"_source\": {\"wrong_field\"\
        : \"will error\"}}]\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    docs.0.error.reason: "field [to_redact] is null or missing"
---
Test custom start and end tokens:
- do:
    ingest.simulate:
      body: "{\n  \"pipeline\": {\n    \"processors\": [\n      {\n        \"redact\"\
        : {\n          \"prefix\": \"==*\",\n          \"suffix\": \"*==\",\n    \
        \      \"field\": \"to_redact\",\n          \"patterns\": [\"%{EMAILADDRESS:EMAIL}\"\
        , \"%{IP:IP_ADDRESS}\"]\n        }\n      }\n    ]\n  },\n  \"docs\": [{\"\
        _source\": {\"to_redact\": \"this-email@address.com will be redacted\"}}]\n\
        }\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    docs: 1
- match:
    docs.0.doc._source.to_redact: "==*EMAIL*== will be redacted"
---
Test redact with trace_redact:
- do:
    ingest.simulate:
      body: "{\n  \"pipeline\": {\n    \"processors\": [\n      {\n        \"redact\"\
        : {\n          \"field\": \"to_redact\",\n          \"patterns\": [\"%{EMAILADDRESS:EMAIL}\"\
        , \"%{IP:IP_ADDRESS}\"],\n          \"trace_redact\": true\n        }\n  \
        \    }\n    ]\n  },\n  \"docs\": [{\"_source\": {\"to_redact\": \"this-email@address.com\
        \ will be redacted\"}}]\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    docs: 1
- match:
    docs.0.doc._source.to_redact: "<EMAIL> will be redacted"
- match:
    docs.0.doc._ingest._redact._is_redacted: true
