StackStorm / st2

StackStorm (aka "IFTTT for Ops") is event-driven automation for auto-remediation, incident responses, troubleshooting, deployments, and more for DevOps and SREs. Includes rules engine, workflow, 160 integration packs with 6000+ actions (see https://exchange.stackstorm.org) and ChatOps. Installer at https://docs.stackstorm.com/install/index.html
https://stackstorm.com/
Apache License 2.0
6.07k stars 749 forks source link

Unicode issue occurs from webhook invoke #3499

Closed leoxhj closed 7 years ago

leoxhj commented 7 years ago

Dear support,

Seems if we put some Chinese characters, and the action execution will end up with following errors:

{
  "traceback": "  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2actions/container/base.py\", line 99, in _do_run\n    (status, result, context) = runner.run(action_params)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 49, in wrapped_f\n    return Retrying(*dargs, **dkw).call(f, *args, **kw)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 206, in call\n    return attempt.get(self._wrap_exception)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 247, in get\n    six.reraise(self.value[0], self.value[1], self.value[2])\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 200, in call\n    attempt = Attempt(fn(*args, **kwargs), attempt_number, False)\n  File \"/opt/stackstorm/runners/mistral_v2/mistral_v2.py\", line 218, in run\n    result = self.start(action_parameters=action_parameters)\n  File \"/opt/stackstorm/runners/mistral_v2/mistral_v2.py\", line 255, in start\n    **options)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/mistralclient/api/v2/executions.py\", line 56, in create\n    return self._create('/executions', data)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/mistralclient/api/base.py\", line 95, in _create\n    self._raise_api_exception(resp)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/mistralclient/api/base.py\", line 143, in _raise_api_exception\n    error_message=error_data)\n",
  "error": "RemoteError: Remote error: UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)\n[u'Traceback (most recent call last):\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/oslo_messaging/rpc/server.py\", line 155, in _process_incoming\\n    res = self.dispatcher.dispatch(message)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/oslo_messaging/rpc/dispatcher.py\", line 222, in dispatch\\n    return self._do_dispatch(endpoint, method, ctxt, args)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/oslo_messaging/rpc/dispatcher.py\", line 192, in _do_dispatch\\n    result = func(ctxt, **new_args)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/mistral/engine/engine_server.py\", line 98, in start_workflow\\n    (rpc_ctx, workflow_identifier, utils.cut(workflow_input),\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/mistral/utils/__init__.py\", line 283, in cut\\n    return cut_dict(data, length=length)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/mistral/utils/__init__.py\", line 197, in cut_dict\\n    v = str(value)\\n', u\"UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)\\n\"]."
}

Body in webhook: {"pool_id": "9300", "assigned_group": "上海-OPS-APP", "assignee": "cjie2"}

LindsayHill commented 7 years ago

Which version of ST2 are you running? There were several Unicode-related fixes in version 2.3, which has just been released.

Also, what does your action look like? From the traceback, the error is occurring when running the action, not at the webhook stage.

leoxhj commented 7 years ago

ST2 version is 2.2.1, I remembered last time, I input some Chinese characters in the payload body via Webhook, the action was print the trigger.body to local file, and the whole body result is not as expected, means Chinese characters were transfered as strange characters like "-----OPS-APP" from its original which should be "上海-OPS-APP", anyway maybe you can test it out at your side. Just copy these characters and put it to body, print it or save to local file seem what happens.

LindsayHill commented 7 years ago

If I use a simple rule like the one in the examples pack, that writes the body to file using core.local, I get this written to file: {u'foo': u'\u4e0a\u6d77-OPS-APP', u'name': u'st2'}

That is with 2.3.0.

You would need to test it with your full action though.

leoxhj commented 7 years ago

Yes, this seems correct. I will test our tomorrow on both 2.2.1 and 2.3, will come back once tested, thanks Lindsay.

LindsayHill commented 7 years ago

Thanks @leoxhj - I don't know if we've fixed all of the Unicode issues, but it is something we want to do.

leoxhj commented 7 years ago

@LindsayHill I have tested on 2.2.1, same result as yours:

root@1eabe73d2837:/opt/stackstorm/packs/examples/rules# cat /home/st/st2out {'foo': 'bar', 'name': 'st2'} {'you': 'too', 'name': 'st2'} {'you': 'too', 'name': 'st2'} {'you': 'too', 'name': 'st2'} {'you': u'\u4e0a\u6d77-OPS-APP', 'name': 'st2'}

LindsayHill commented 7 years ago

@leoxhj can you please provide a rule + workflow/action that reproduces the problem, since using a simple core.local action works?

jeremycharlesgillespie commented 7 years ago

UnicodeEncodeError: 'ascii' codec can't encode character u'\\\\xb7' in position 791: ordinal not in range(128) I have a 3rd party that is sending a webhook with the middle dot character https://en.wikipedia.org/wiki/Interpunct and I can receive the webhook just fine, but stackstorm/mistral seems to crash when processing the middle dot. Is there a reasonable workaround for this? Or something that I can slip into the existing code where I replace all the middle dots with normal periods? Here is python stack trace:

{
  "traceback": "  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2actions/container/base.py\", line 99, in _do_run\n    (status, result, context) = runner.run(action_params)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 49, in wrapped_f\n    return Retrying(*dargs, **dkw).call(f, *args, **kw)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 206, in call\n    return attempt.get(self._wrap_exception)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 247, in get\n    six.reraise(self.value[0], self.value[1], self.value[2])\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 200, in call\n    attempt = Attempt(fn(*args, **kwargs), attempt_number, False)\n  File \"/opt/stackstorm/runners/mistral_v2/mistral_v2.py\", line 218, in run\n    result = self.start(action_parameters=action_parameters)\n  File \"/opt/stackstorm/runners/mistral_v2/mistral_v2.py\", line 260, in start\n    **options)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/mistralclient/api/v2/executions.py\", line 56, in create\n    return self._create('/executions', data)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/mistralclient/api/base.py\", line 95, in _create\n    self._raise_api_exception(resp)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/mistralclient/api/base.py\", line 143, in _raise_api_exception\n    error_message=error_data)\n",
  "error": "RemoteError: Remote error: UnicodeEncodeError 'ascii' codec can't encode character u'\\xb7' in position 791: ordinal not in range(128)\n[u'Traceback (most recent call last):\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/oslo_messaging/rpc/server.py\", line 155, in _process_incoming\\n    res = self.dispatcher.dispatch(message)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/oslo_messaging/rpc/dispatcher.py\", line 222, in dispatch\\n    return self._do_dispatch(endpoint, method, ctxt, args)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/oslo_messaging/rpc/dispatcher.py\", line 192, in _do_dispatch\\n    result = func(ctxt, **new_args)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/mistral/engine/engine_server.py\", line 98, in start_workflow\\n    (rpc_ctx, workflow_identifier, utils.cut(workflow_input),\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/mistral/utils/__init__.py\", line 283, in cut\\n    return cut_dict(data, length=length)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/mistral/utils/__init__.py\", line 197, in cut_dict\\n    v = str(value)\\n', u\"UnicodeEncodeError: 'ascii' codec can't encode character u'\\\\xb7' in position 791: ordinal not in range(128)\\n\"]."
}

The "$TEXT_ONLY_MSG" portion from Datadog is what I think is adding the middle dot character. Here is my webhook payload information:

{"trigger":
    "linux.DataDog",
    "payload": { 
        "body": {
        "id": "$ID",
        "hostname" : "$HOSTNAME",
        "user": "$USER",
        "priority" : "$PRIORITY",
        "last_updated" : "$LAST_UPDATED",
        "date" : "$DATE",
        "event_type" : "$EVENT_TYPE",
        "body" : "$TEXT_ONLY_MSG",
        "org_id" : "$ORG_ID",
        "name" : "$ORG_NAME",
        "plain_text" : "$EVENT_TITLE",
        "aggregate_key" : "$AGGREG_KEY",
        "alert_scope" : "$ALERT_SCOPE",
        "picture" : "$SNAPSHOT",
        "link" : "$LINK",
        "tags" : "$TAGS",
        "alert_id" : "$ALERT_ID",
        "alert_metric" : "$ALERT_METRIC",
        "alert_transition" : "$ALERT_TRANSITION",
        "alert_status" : "$ALERT_STATUS",
        "alert_title" : "$ALERT_TITLE"}
    }
}
bigmstone commented 7 years ago

Would need to investigate further, but based on what I'm seeing it looks like the mistral runner's handling Unicode characters is the culprit.

jeremycharlesgillespie commented 7 years ago

I think you are 100% correct. I'm working on a change to just find and replace those characters within the JSON before the resp object gets created here: /opt/stackstorm/st2/local/lib/python2.7/site-packages/mistralclient/api/base.py

def _create(self, url, data, response_key=None, dump_json=True):
        if dump_json:
            data = json.dumps(data)
---->>insert terrible hack
        resp = self.http_client.post(url, data)

        if resp.status_code != 201:
            self._raise_api_exception(resp)

        return self.resource_class(self, extract_json(resp, response_key))
bigmstone commented 7 years ago

Could be wrong:

ubuntu@ubuntu-xenial:~$ st2 run test.mistral action_input="æ"
............
id: 594c21b9962d740859a6455b
action.ref: test.mistral
parameters:
  action_input: "æ"
status: succeeded
result_task: task1
result:
  exit_code: 0
  result: '"æ"'
  stderr: ''
  stdout: ''
start_timestamp: 2017-06-22T19:59:53.493320Z
end_timestamp: 2017-06-22T20:00:16.298231Z
+--------------------------+------------------------+-------+-------------+-------------------------------+
| id                       | status                 | task  | action      | start_timestamp               |
+--------------------------+------------------------+-------+-------------+-------------------------------+
| 594c21b9962d740859a6455e | succeeded (1s elapsed) | task1 | test.echoer | Thu, 22 Jun 2017 19:59:53 UTC |
+--------------------------+------------------------+-------+-------------+-------------------------------+
ubuntu@ubuntu-xenial:~$

Wonder if it's related to the action being executed? Not willing to rule out the runner yet, but this passed through the runner just fine. As well as the YAQL evaluator.

leoxhj commented 7 years ago

@LindsayHill

the output should be fine, and this can be handled decode in python script: {'you': u'\u4e0a\u6d77-OPS-APP', 'name': 'st2'}, you can close this issue, thanks a lot for your support.

LindsayHill commented 7 years ago

@leoxhj it still seems to be a problem for other users when running Mistral workflows, as seen above. I'll leave it open a little longer - hopefully someone will be able to provide a complete workflow that reproduces this. No-one has given us a full workflow that triggers it yet. If we can get that, we can work on fixing it.

leoxhj commented 7 years ago

@LindsayHill Today, I tested our code, it's indeed a problem of unicode, please find attached rules, workflows. liberty.zip

you can test with below payload data: {"pool_id": "8883", "assigned_group": "上海-OPS-APP", "assignee": "cjie2"}

code structure:

rules\pool-decommission.yaml actions\pool-decommission.yaml actions\workflows\pool-decommission.yaml

traceback as follows:

{
  "traceback": "  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2actions/container/base.py\", line 99, in _do_run\n    (status, result, context) = runner.run(action_params)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 49, in wrapped_f\n    return Retrying(*dargs, **dkw).call(f, *args, **kw)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 206, in call\n    return attempt.get(self._wrap_exception)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 247, in get\n    six.reraise(self.value[0], self.value[1], self.value[2])\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/retrying.py\", line 200, in call\n    attempt = Attempt(fn(*args, **kwargs), attempt_number, False)\n  File \"/opt/stackstorm/runners/mistral_v2/mistral_v2.py\", line 218, in run\n    result = self.start(action_parameters=action_parameters)\n  File \"/opt/stackstorm/runners/mistral_v2/mistral_v2.py\", line 255, in start\n    **options)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/mistralclient/api/v2/executions.py\", line 56, in create\n    return self._create('/executions', data)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/mistralclient/api/base.py\", line 95, in _create\n    self._raise_api_exception(resp)\n  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/mistralclient/api/base.py\", line 143, in _raise_api_exception\n    error_message=error_data)\n",
  "error": "RemoteError: Remote error: UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)\n[u'Traceback (most recent call last):\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/oslo_messaging/rpc/server.py\", line 155, in _process_incoming\\n    res = self.dispatcher.dispatch(message)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/oslo_messaging/rpc/dispatcher.py\", line 222, in dispatch\\n    return self._do_dispatch(endpoint, method, ctxt, args)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/oslo_messaging/rpc/dispatcher.py\", line 192, in _do_dispatch\\n    result = func(ctxt, **new_args)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/mistral/engine/engine_server.py\", line 98, in start_workflow\\n    (rpc_ctx, workflow_identifier, utils.cut(workflow_input),\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/mistral/utils/__init__.py\", line 283, in cut\\n    return cut_dict(data, length=length)\\n', u'  File \"/opt/stackstorm/mistral/local/lib/python2.7/site-packages/mistral/utils/__init__.py\", line 197, *in cut_dict\\n    v = str(value)\\n', u\"UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)\\n\"]."*
leoxhj commented 7 years ago

@LindsayHill just verified in st2vagrant (st2 version 2.3), no error. so I assume it fixed in 2.3, you can double confirm this.

LindsayHill commented 7 years ago

Thanks for the additional info @leoxhj. I will check it is fixed in both 2.3 (current stable) and the latest master.

LindsayHill commented 7 years ago

In my tests, this worked on 2.3. So I think it has been fixed. Please do continue to let us know if you find any more Unicode issues. We do want to fix them.

Thanks!