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.06k stars 746 forks source link

broken API endpoint PUT/api/v1/rules/{rule_ref_or_id} #3863

Closed divgwd closed 6 years ago

divgwd commented 6 years ago

Hi, I am trying to update a rule in st2 using the /api/v1/rule/id I get the below error

"faultstring": "Internal Server Error"

when I check the logs this is the output:

2017-11-15 19:16:07,091 78737232 INFO logging [-] 07a98006-2f35-4705-b120-1847b1cbe2e7 - PUT /v1/rules/5a0c418540c0a027a2e113bb with query={} (remote_addr='127.0.0.1',method='PUT',request_id='07a98006-2f35-4705-b120-1847b1cbe2e7',query={},path='/v1/rules/5a0c418540c0a027a2e113bb')
2017-11-15 19:16:07,103 78737232 ERROR router [-] Failed to call controller function "put" for operation "st2api.controllers.v1.rules:rule_controller.put": 'RuleAPI' object has no attribute 'id'
Traceback (most recent call last):
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/router.py", line 414, in __call__
    resp = func(**kw)
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2api/controllers/v1/rules.py", line 141, in put
    if rule.id is not None and rule.id is not '' and rule.id != rule_ref_or_id:
AttributeError: 'RuleAPI' object has no attribute 'id'
2017-11-15 19:16:07,104 78737232 ERROR error_handling [-] API call failed: 'RuleAPI' object has no attribute 'id'
Traceback (most recent call last):
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/middleware/error_handling.py", line 46, in __call__
    return self.app(environ, start_response)
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/router.py", line 445, in as_wsgi
    resp = self(req)
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/router.py", line 418, in __call__
    raise e
AttributeError: 'RuleAPI' object has no attribute 'id' (_exception_data={},_exception_class='AttributeError',_exception_message="'RuleAPI' object has no attribute 'id'")
2017-11-15 19:16:07,105 78737232 INFO logging [-] 07a98006-2f35-4705-b120-1847b1cbe2e7 - 500 46 14.448ms
{
    "faultstring": "Internal Server Error"
} (result='{\n    "faultstring": "Internal Server Error"\n}',content_length=46,request_id='07a98006-2f35-4705-b120-1847b1cbe2e7',runtime=14.448,remote_addr='127.0.0.1',status=500,method='PUT',path='/v1/rules/5a0c418540c0a027a2e113bb')

my update query is below:

url :https://172.16.23.20/api/v1/rules/5a0c418540c0a027a2e113bb

body :
{
    "name": "sreepad&sreepad&newrule_now3",
    "description": "testing phase1",
    "enabled": true,
    "criteria": {
        "trigger.results.ruleName": {
            "pattern": "sreepad&sreepad&newrule_now3",
            "type": "equals"
        }
    },
    "pack": "elasticsearch",
    "trigger": {
        "ref": "elasticsearch.count_event",
        "type": "elasticsearch.count_event",
        "parameters": {}
    },
    "action": {
        "ref": "linux.file_touch",
        "parameters": {
            "hosts": "172.1",
            "file": "/home/stanley/test19\\n"
        }
    }
}

data in mongoDB

> db.rule_d_b.find({"_id" : ObjectId("5a0c418540c0a027a2e113bb")}).pretty()
{
        "_id" : ObjectId("5a0c418540c0a027a2e113bb"),
        "tags" : [ ],
        "uid" : "rule:elasticsearch:sreepad&newrule_now3",
        "name" : "sreepad&newrule_now3",
        "ref" : "elasticsearch.sreepad&newrule_now3",
        "description" : "testing phase1",
        "pack" : "elasticsearch",
        "type" : {
                "ref" : "standard",
                "parameters" : {

                }
        },
        "trigger" : "elasticsearch.count_event",
        "criteria" : {
                "triggerï¼resultsï¼ruleName" : {
                        "pattern" : "sreepad&newrule_now3",
                        "type" : "equals"
                }
        },
        "action" : {
                "ref" : "linux.file_touch",
                "parameters" : {
                        "hosts" : "172.16.23.27",
                        "file" : "/home/stanley/test19\n"
                }
        },
        "enabled" : false
}
>

I would like to know if there is a workaround for this ? or is there a problem with the body of the put data. Thanks in advance!

LindsayHill commented 6 years ago

Possibly something related to URL encoding? Are you trying to put a space in the name, or is this really your rule name: sreepad&newrule_now3?

Kami commented 6 years ago

It looks like data you are posting (body) is missing id attribute in the payload.

I will also make sure to update the code to correctly return 400 "invalid data" instead of 500 in such case - thanks for reporting this.

Kami commented 6 years ago

In the mean time, as a work-around, you can simply put "id" attribute in the payload body which matches the object id (aka the one already provided in the url) and it should work.