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

CreateObjectHandler: Fix ignore_on_error api flag doesn't work properly #8819

Closed

Conversation

yhabteab
Copy link
Member

@yhabteab yhabteab commented Jun 1, 2021

The API ignore_on_error flag doesn't quite work yet, as it should. For example, if you don't set the check_command attribute when creating a host object via API, the action will fail during config validation and it won't even go out from the ConfigObjectUtility::CreateObject() method. After that internal server error is sent, even though you have set the flag.

@icinga-probot icinga-probot bot added this to the 2.14.0 milestone Jun 1, 2021
@yhabteab
Copy link
Member Author

yhabteab commented Jun 1, 2021

Before:

curl -k -s -u root:icinga -H 'Accept: application/json' \                      
 -X PUT 'https://localhost:5665/v1/objects/hosts/example' \
 -d '{ "attrs": { "address": "192.168.1.1", "vars.os" : "Linux" }, "ignore_on_error": true, "pretty": true }'
{
    "results": [
        {
            "code": 500,
            "errors": [
                "Error: Validation failed for object 'example' of type 'Host'; Attribute 'check_command': Attribute must not be empty.\nLocation: in /Users/yhabteab/Workspace/icinga2/prefix/var/lib/icinga2/api/packages/_api/ca7c22f6-3eed-426c-bd2e-04b9cf04c267/conf.d/hosts/example.conf: 1:0-1:32"
            ],
            "status": "Object could not be created."
        }
    ]
}

After:

{
    "results": [
        {
            "code": 200,
            "status": "Object was not created but 'ignore_on_error' was set to true."
        }
    ]
}

@yhabteab yhabteab requested a review from Al2Klimov June 1, 2021 16:49
Copy link
Member

@Al2Klimov Al2Klimov left a comment

Choose a reason for hiding this comment

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

Seems quite logical to me and the code below confirms this.

Apropos: couldn’t you just fix the condition instead of duplicating the mentioned code?

@Al2Klimov Al2Klimov added area/api REST API bug Something isn't working labels Jun 1, 2021
@yhabteab yhabteab force-pushed the bugfix/fix-ignore-on-error-flag-does-not-as-expected branch from 3e93a6d to 8a5ebbb Compare June 1, 2021 17:30
lib/remote/createobjecthandler.cpp Outdated Show resolved Hide resolved
The API ``ignore_on_error`` flag doesn't quite work yet, as it should. For example, if you don't set the ``command`` attribute when creating a host object via API, the action will fail during config validation and it won't even go out from the ``ConfigObjectUtility::CreateObject()`` method. After that internal server error is sent, even though you have set the flag.
@yhabteab yhabteab force-pushed the bugfix/fix-ignore-on-error-flag-does-not-as-expected branch from 8a5ebbb to b0cd962 Compare June 2, 2021 09:26
@Al2Klimov
Copy link
Member

Have you re-tested it (all cases)?

Copy link
Contributor

@julianbrost julianbrost left a comment

Choose a reason for hiding this comment

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

Given the description of #8820, it sound like this PR would introduce a crash. This has to be investigated/fixed before we can merge this. (Posting this "request changes" review so that it's not forgotten.)

@Al2Klimov
Copy link
Member

@julianbrost julianbrost self-requested a review October 13, 2022 08:19
@julianbrost
Copy link
Contributor

Before:

curl -k -s -u root:icinga -H 'Accept: application/json' \                      
 -X PUT 'https://localhost:5665/v1/objects/hosts/example' \
 -d '{ "attrs": { "address": "192.168.1.1", "vars.os" : "Linux" }, "ignore_on_error": true, "pretty": true }'
{
    "results": [
        {
            "code": 500,
            "errors": [
                "Error: Validation failed for object 'example' of type 'Host'; Attribute 'check_command': Attribute must not be empty.\nLocation: in /Users/yhabteab/Workspace/icinga2/prefix/var/lib/icinga2/api/packages/_api/ca7c22f6-3eed-426c-bd2e-04b9cf04c267/conf.d/hosts/example.conf: 1:0-1:32"
            ],
            "status": "Object could not be created."
        }
    ]
}

Can't reproduce this with the current master (dd7009d):

$ curl -i -k -s -u root:icinga -H 'Accept: application/json' -X PUT 'https://localhost:5665/v1/objects/hosts/example' -d '{ "attrs": { "address": "192.168.1.1", "vars.os" : "Linux" }, "ignore_on_error": true, "pretty": true }'
HTTP/1.1 200 OK
Server: Icinga/v2.13.0-458-gdd7009dc6
Content-Type: application/json
Content-Length: 157

{
    "results": [
        {
            "code": 200,
            "status": "Object was not created but 'ignore_on_error' was set to true"
        }
    ]
}

Also, with your PR rebased onto the current master, the following happens:

$ curl -i -k -s -u root:icinga -H 'Accept: application/json' -X PUT 'https://localhost:5665/v1/objects/hosts/example' -d '{ "attrs": {"invalid":0}, "ignore_on_error": true, "pretty": true }'                                    
HTTP/1.1 500 Internal Server Error
Server: Icinga/v2.13.0-459-g20d705740
Content-Type: application/json
Content-Length: 228

{
    "results": [
        {
            "code": 500,
            "errors": [
                "Error: Invalid attribute specified: invalid\n"
            ],
            "status": "Object could not be created."
        }
    ]
}

I find the documentation for the ignore_on_error parameter inconsistent with what it does. To me this reads like this only changes error reporting, but that's not actually what the implementation does. What really happens is that the object is written to a config file in the _api package with the ignore_on_error attribute set, which means that if the error is gone on the next reload (for example because the error was that it referred to a non-existing host that was created in the meantime), the object will become active at that time. Otherwise, if the error is still present at a reload, the config file is deleted entirely.

To be honest, I don't see many use cases for this flag where you actually want that behavior rather than just ignore error responses. So another option would be to make the documentation more clear on what this parameter actually does.

Copy link
Member

@Al2Klimov Al2Klimov left a comment

Choose a reason for hiding this comment

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

Works for me as well, why exactly do we still need this?

@Al2Klimov Al2Klimov removed the request for review from julianbrost January 20, 2023 16:23
@yhabteab
Copy link
Member Author

Fixed by #9406 implicitly.

@yhabteab yhabteab closed this Jan 24, 2023
@yhabteab yhabteab deleted the bugfix/fix-ignore-on-error-flag-does-not-as-expected branch January 24, 2023 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api REST API bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants