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

API: Failed Object Creation due to Validation methods blocks Object Name #10166

Closed
oxzi opened this issue Sep 19, 2024 · 2 comments
Closed

API: Failed Object Creation due to Validation methods blocks Object Name #10166

oxzi opened this issue Sep 19, 2024 · 2 comments
Labels
area/api REST API bug Something isn't working duplicate This issue or pull request already exists

Comments

@oxzi
Copy link
Member

oxzi commented Sep 19, 2024

Describe the bug

After a failed attempt to create some Object via the Icinga 2 API due to a validation exception, the chosen Object name is blocked and Icinga 2 aborts further attempts as they are re-creations. Please take a look at the To Reproduce section below, which should make it more obvious.

I stumbled about this issue while debugging changes for #10102, where I wanted to verify my Validate methods via the Icinga 2 API.

To Reproduce

To make sure that this has nothing to do with my code, my example uses TimePeriod and its validation method.

void TimePeriod::ValidateRanges(const Lazy<Dictionary::Ptr>& lvalue, const ValidationUtils& utils)

  1. Try to create a TimePeriod Object named "oops" with invalid ranges values, resulting in TimePeriod::ValidateRanges to throw an exception.
    $ curl -k -s -S -u root:icinga -H 'Accept: application/json' -X PUT 'https://localhost:5665/v1/objects/timeperiods/oops' -d '{"attrs": {"ranges": {"F": "F"}}, "pretty": true}'
    {
        "results": [
            {
                "code": 500,
                "errors": [
                    "Error: Validation failed for object 'oops' of type 'TimePeriod'; Attribute 'ranges': Invalid time specification 'F': Invalid time specification: F\nLocation: in /var/lib/icinga2/api/packages/_api/8e2362bd-b6a8-415d-85fe-33074907fcd8/conf.d/timeperiods/oops.conf: 2:2-4:2"
                ],
                "status": "Object could not be created."
            }
        ]
    }
    
  2. Verify no such "oops" Object exists.
    $ curl -k -s -S -u root:icinga -H 'Accept: application/json' -X GET 'https://localhost:5665/v1/objects/timeperiods/oops' -d '{"pretty": true}'
    {
        "error": 404,
        "status": "No objects found."
    }
    
  3. Retry to create a TimePeriod Object named "oops":
    1. Retry by creating the same faulty TimePeriod:
      $ curl -k -s -S -u root:icinga -H 'Accept: application/json' -X PUT 'https://localhost:5665/v1/objects/timeperiods/oops' -d '{"attrs": {"ranges": {"F": "F"}}, "pretty": true}'
      {
          "results": [
              {
                  "code": 500,
                  "errors": [
                      "Error: Object 'oops' of type 'TimePeriod' re-defined: in /var/lib/icinga2/api/packages/_api/8e2362bd-b6a8-415d-85fe-33074907fcd8/conf.d/timeperiods/oops.conf: 1:0-1:23; previous definition: in /var/lib/icinga2/api/packages/_api/8e2362bd-b6a8-415d-85fe-33074907fcd8/conf.d/timeperiods/oops.conf: 1:0-1:23\nLocation: in /var/lib/icinga2/api/packages/_api/8e2362bd-b6a8-415d-85fe-33074907fcd8/conf.d/timeperiods/oops.conf: 1:0-1:23"
                  ],
                  "status": "Object could not be created."
              }
          ]
      }
      
    2. Retry by creating a valid TimePeriod:
      $ curl -k -s -S -u root:icinga -H 'Accept: application/json' -X PUT 'https://localhost:5665/v1/objects/timeperiods/oops' -d '{"attrs": {"ranges": {"2025-05-25": "00:00-24:00"}}, "pretty": true}'
      {
          "results": [
              {
                  "code": 500,
                  "errors": [
                      "Error: Object 'oops' of type 'TimePeriod' re-defined: in /var/lib/icinga2/api/packages/_api/8e2362bd-b6a8-415d-85fe-33074907fcd8/conf.d/timeperiods/oops.conf: 1:0-1:23; previous definition: in /var/lib/icinga2/api/packages/_api/8e2362bd-b6a8-415d-85fe-33074907fcd8/conf.d/timeperiods/oops.conf: 1:0-1:23\nLocation: in /var/lib/icinga2/api/packages/_api/8e2362bd-b6a8-415d-85fe-33074907fcd8/conf.d/timeperiods/oops.conf: 1:0-1:23"
                  ],
                  "status": "Object could not be created."
              }
          ]
      }
      

On further inspection, the referenced file in the re-definition error does not exist. There is even no mention of such an object.

icinga@a1e262125edf:/$ cat /var/lib/icinga2/api/packages/_api/8e2362bd-b6a8-415d-85fe-33074907fcd8/conf.d/timeperiods/oops.conf
cat: /var/lib/icinga2/api/packages/_api/8e2362bd-b6a8-415d-85fe-33074907fcd8/conf.d/timeperiods/oops.conf: No such file or directory
icinga@a1e262125edf:/$ fgrep -ri oops /var/lib/icinga2; echo $?
1

Btw, I am also unable to delete it via the API:

$ curl -k -s -S -u root:icinga -H 'Accept: application/json' -X DELETE 'https://localhost:5665/v1/objects/timeperiods/oops' -d '{"pretty": true}'
{
    "error": 404,
    "status": "No objects found."
}

A specter is haunting Icinga 2 - the specter of TimePeriod.

Expected behavior

If Icinga 2 states that an "Object could not be created", one should be able to retry with the same name.

Screenshots

N/A

Your Environment

Include as many relevant details about the environment you experienced the problem in

  • Version used (icinga2 --version): Icinga DB: Config no_user_modify, Extract ValidateCertPath, and Support Redis username authentication #10102 which is a bit ahead of master
  • Operating System and version: NixOS "it's complicated"
  • Enabled features (icinga2 feature list): api checker icingadb ido-mysql ido-pgsql notification
  • Icinga Web 2 version and modules (System - About): N/A
  • Config validation (icinga2 daemon -C): ☑️
  • If you run multiple Icinga 2 instances, the zones.conf file (or icinga2 object list --type Endpoint and icinga2 object list --type Zone) from all affected nodes.

Additional context

N/A

@oxzi oxzi added bug Something isn't working area/api REST API labels Sep 19, 2024
@yhabteab
Copy link
Member

GitHub says otherwise :-)!
Bildschirmfoto 2024-09-19 um 14 58 57

See #10110!

@oxzi oxzi added the duplicate This issue or pull request already exists label Sep 19, 2024
@oxzi
Copy link
Member Author

oxzi commented Sep 19, 2024

Damn. I tried searching for other issues, but failed to find any. Next time, I rebase first, but honestly, I haven't expected a fix in the meantime.

Edit: Verified that the bug was resolved after rebasing.

@oxzi oxzi closed this as completed Sep 19, 2024
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 duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants