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

ClassCompiler: Don't allow to define an int as a group name in groups attr #9057

Merged
merged 3 commits into from
Nov 22, 2021

Conversation

yhabteab
Copy link
Member

@yhabteab yhabteab commented Nov 2, 2021

No description provided.

@yhabteab yhabteab added the bug Something isn't working label Nov 2, 2021
@yhabteab yhabteab requested a review from Al2Klimov November 2, 2021 09:55
@yhabteab yhabteab self-assigned this Nov 2, 2021
@cla-bot cla-bot bot added the cla/signed label Nov 2, 2021
@yhabteab yhabteab force-pushed the bugfix/fix-sorting-an-array-with-different-types branch from 9fdbc8a to 99308ce Compare November 2, 2021 09:57
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.

Post auto-generated code before/after.

@yhabteab
Copy link
Member Author

yhabteab commented Nov 2, 2021

Before:

if (avalue()) {
		ObjectLock olock(avalue());
		for (const Value& value : avalue()) {
	if (value.IsEmpty() || !utils.ValidateName("HostGroup", value))
		BOOST_THROW_EXCEPTION(ValidationError(dynamic_cast<ConfigObject *>(this), { "groups" }, "Object '" + value + "' of type 'HostGroup' does not exist."));
		}
	}

After:

if (avalue()) {
		ObjectLock olock(avalue());
		for (const Value& value : avalue()) {
	                if (!value.IsEmpty() && !value.IsString())
				BOOST_THROW_EXCEPTION(ValidationError(dynamic_cast<ConfigObject *>(this), { "groups" }, "Object '" + value + "' of type 'HostGroup' has an invalid name."));
	                if (value.IsEmpty() || !utils.ValidateName("HostGroup", value))
		                BOOST_THROW_EXCEPTION(ValidationError(dynamic_cast<ConfigObject *>(this), { "groups" }, "Object '" + value + "' of type 'HostGroup' does not exist."));
		}
	}

@yhabteab yhabteab requested a review from Al2Klimov November 2, 2021 14:03
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.

Share an example Icinga2 complaint.

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.

An Icinga 2 complaint output, not the code that triggers the complaint.

@yhabteab
Copy link
Member Author

yhabteab commented Nov 2, 2021

object User:

[2021-11-02 16:17:57 +0100] critical/config: Error: Validation failed for object 'icingaadmin' of type 'User'; Attribute 'groups': It is not allowed to specify '12' of type 'Number' as 'UserGroup' name. Expected type of 'UserGroup' name 'String'.
Location: in /prefix/etc/icinga2/conf.d/custom-host.conf: 73:3-73:33
/prefix/etc/icinga2/conf.d/custom-host.conf(71): 
/prefix/etc/icinga2/conf.d/custom-host.conf(72):   display_name = "Icinga 2 Admin"
/prefix/etc/icinga2/conf.d/custom-host.conf(73):   groups = [ 12, "icingaadmins" ]
                                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

object Host:

[2021-11-02 16:20:42 +0100] critical/config: Error: Validation failed for object '49' of type 'Host'; Attribute 'groups': It is not allowed to specify '9' of type 'Number' as 'HostGroup' name. Expected type of 'HostGroup' name 'String'.
Location: in /prefix/etc/icinga2/conf.d/custom-host.conf: 54:9-54:30
/prefix/etc/icinga2/conf.d/custom-host.conf(52):         check_command = "dummy"
/prefix/etc/icinga2/conf.d/custom-host.conf(53):         enable_active_checks = 0
/prefix/etc/icinga2/conf.d/custom-host.conf(54):         groups = [ name % hg ]
                                                         ^^^^^^^^^^^^^^^^^^^^^^

object Service:

[2021-11-02 16:24:06 +0100] critical/config: Error: Validation failed for object '9!4' of type 'Service'; Attribute 'groups': It is not allowed to specify '9' of type 'Number' as 'ServiceGroup' name. Expected type of 'ServiceGroup' name 'String'.
Location: in /prefix/etc/icinga2/conf.d/custom-host.conf: 63:9-63:35
/prefix/etc/icinga2/conf.d/custom-host.conf(61):         check_command = "dummy"
/prefix/etc/icinga2/conf.d/custom-host.conf(62):         enable_active_checks = 0
/prefix/etc/icinga2/conf.d/custom-host.conf(63):         groups = [ host_name % sg ]
                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^

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.

Doesn’t Icinga 2 already show you the code location along with the wrong value and its type?

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.

Share an example Icinga2 complaint.

And with the group being null, not a number?

@yhabteab
Copy link
Member Author

yhabteab commented Nov 2, 2021

And with the group being null, not a number?

Nothing happens. But if you explicitly define null as a value, the following will be raised.

[2021-11-02 17:01:59 +0100] critical/config: Error: Validation failed for object '27!1' of type 'Service'; Attribute 'groups': Object '' of type 'ServiceGroup' does not exist.
Location: in /prefix/etc/icinga2/conf.d/custom-host.conf: 63:9-63:25
/prefix/etc/icinga2/conf.d/custom-host.conf(61):         check_command = "dummy"
/prefix/etc/icinga2/conf.d/custom-host.conf(62):         enable_active_checks = 0
/prefix/etc/icinga2/conf.d/custom-host.conf(63):         groups = [ null ]
                                                         ^^^^^^^^^^^^^^^^^

Note:

This check applies also to notification's users and user_groups attrs.

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.

Once done, let Julian review. (No need for squashing at all, will be done while merging.)

tools/mkclass/classcompiler.cpp Outdated Show resolved Hide resolved
tools/mkclass/classcompiler.cpp Outdated Show resolved Hide resolved
@yhabteab yhabteab requested a review from julianbrost November 2, 2021 16:19
@julianbrost
Copy link
Contributor

What's the issue this is supposed to solve? If I try the current master, I get the following which looks okay as well:

[2021-11-22 09:53:28 +0100] critical/config: Error: Validation failed for object 'foobar' of type 'Host'; Attribute 'groups': Object '42' of type 'HostGroup' does not exist.
Location: in data/etc/icinga2/conf.d/objects.conf: 70:5-70:17
data/etc/icinga2/conf.d/objects.conf(68): object Host "foobar" {
data/etc/icinga2/conf.d/objects.conf(69):     check_command = "dummy"
data/etc/icinga2/conf.d/objects.conf(70):     groups = [42]
                                              ^^^^^^^^^^^^^
data/etc/icinga2/conf.d/objects.conf(71): }

@yhabteab
Copy link
Member Author

Generally for Icinga2 this doesn't matter, as the integers are implicitly cast to string, but when icinga2 writes to the IDO, this groups attribute is sorted with std::sort, which was introduced with this PR #8367. And there these cannot be implicitly cast, if you have e.g. in the groups attr [2, "1"] and falls simply through.

@julianbrost
Copy link
Contributor

I see, so with this config

object HostGroup 9057 {}
object HostGroup "9057-str" {}

object Host "9057-host" {
	check_command = "dummy"
	groups = [9057, "9057-str"]
}

you eventually run into this error:

[2021-11-22 11:40:19 +0100] critical/IdoMysqlConnection: Exception during database operation: Verify that your database is operational!
[2021-11-22 11:40:19 +0100] debug/IdoMysqlConnection: Exception during database operation: Error: Operator < cannot be applied to values of type 'String' and 'Number'

Have you considered normalizing all array elements to be of type string, so that the config would just work? I think it's a bit inconsistent that you can declare a group as object HostGroup 9057 {} (without the string quotes) and then not being able to use it that way. On the other hand, I hope that no non-test config just uses integer values for their group names, so the exact behavior in that case is not too important I guess (as long as it's not totally broken as it is at the moment).

@Al2Klimov
Copy link
Member

normalizing all array elements to be of type string, so that the config would just work

What for? After all:

I hope that no non-test config just uses integer values for their group names, so the exact behavior in that case is not too important

@julianbrost
Copy link
Contributor

I'm just thinking out loud. It's not a request for changes. As I said, it would probably be more consistent but not practically relevant.

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.

Just noticed strange behavior with empty strings as object names, but that not related to this PR so I created #9097 for it.

Apart from this, looks fine for me, just needs to be squashed on merge.

@Al2Klimov Al2Klimov merged commit a64089f into master Nov 22, 2021
@yhabteab yhabteab deleted the bugfix/fix-sorting-an-array-with-different-types branch November 22, 2021 13:25
@Al2Klimov Al2Klimov added this to the 2.14.0 milestone Dec 3, 2021
@julianbrost
Copy link
Contributor

Won't be backported. Rationale: the current fix is slightly backwards-incompatible and most likely users don't have group names that can be represented by other types than string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cla/signed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants