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

Updates the ipv4 and ipv6 command line options #185

Merged
merged 4 commits into from
Jan 20, 2021

Conversation

matsduf
Copy link
Contributor

@matsduf matsduf commented Jan 19, 2021

Makes the ipv4 and ipv6 options undefined unless explicitly set on the command line. Changes so that the ip setting is only changed if the option is explicitly set on the command line.

This resolves a bug that appeared after #182 was fixed by #183.

The bug is described in #182 (comment).

Makes the ipv4 and ipv6 options undefined unless explicitly set on
the command line. Changes so that the ip setting is only changed
if the option is explicitly set on the command line.
@matsduf matsduf added P-High Priority: Issue to be solved before other A-Code labels Jan 19, 2021
@matsduf matsduf added this to the v2020.2 milestone Jan 19, 2021
@matsduf matsduf requested review from mattias-p and a user January 19, 2021 15:16
Comment on lines 360 to 361
Zonemaster::Engine::Profile->effective->set( q{net.ipv4}, 0+$self->ipv4 ) if defined ($self->ipv4);
Zonemaster::Engine::Profile->effective->set( q{net.ipv6}, 0+$self->ipv6 ) if defined ($self->ipv6);
Copy link
Member

Choose a reason for hiding this comment

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

Could you update this to use if (...) { ... } syntax? This is in line with the rest of the code. Also, the structure of the code is more apparent to the reader that way.

I do realize there are a couple of instances of trailing if statements in this file, but adding more of them is a mistake from a readability point of view.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is that only your personal opinion that trailing "if" is always less readable, or do you know of any studies of that supports that? In that case we get three lines, instead of one, and more separators. And the condition is short.

# to override the profile setting
Zonemaster::Engine::Profile->effective->set( q{net.ipv4}, 0+$self->ipv4 );
Zonemaster::Engine::Profile->effective->set( q{net.ipv6}, 0+$self->ipv6 );
# to override the profile setting, but only if selected.
Copy link
Member

Choose a reason for hiding this comment

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

If the control flow is apparent from the syntax (like I suggest below), this added clarification is unnecessary and distracting. And it's better to remove it.

@@ -147,15 +147,13 @@ has 'restore' => (
has 'ipv4' => (
is => 'ro',
isa => 'Bool',
default => 1,
Copy link

Choose a reason for hiding this comment

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

This default behavior is not moved into the code. I mean, before the change if you do not have the net stanza in the profile file and you just run the CLI, IPv4 and IPv6 will be enabled by default.

Here with the current changes, removing the stanza from the profile file gives a CRITICAL message :

   0.01 CRITICAL  Both IPv4 and IPv6 are disabled.

I feel an else statement is needed in the code to enable IPv4 and IPv6 by default.

Copy link
Contributor Author

@matsduf matsduf Jan 20, 2021

Choose a reason for hiding this comment

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

Good catch. I had to remove that "default" to make it possible to detect if the option had been actively set or not. It is more complex than I thought.

Copy link
Contributor Author

@matsduf matsduf Jan 20, 2021

Choose a reason for hiding this comment

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

Now I see what you mean. If the default profile.json does not have any IPv4 or IPv6 enabled, then there will no network enabled. -- In Backend both are enabled if both are disabled.

You will get the same effect if you run

# zonemaster-cli --level INFO --test basic iis.se --no-ipv6 --no-ipv4

Should CLI be helpful in that case?

Zonemaster::Engine::Profile->effective->set( q{net.ipv4}, 0+$self->ipv4 );
Zonemaster::Engine::Profile->effective->set( q{net.ipv6}, 0+$self->ipv6 );
# to override the profile setting, but only if selected.
Zonemaster::Engine::Profile->effective->set( q{net.ipv4}, 0+$self->ipv4 ) if defined ($self->ipv4);
Copy link

Choose a reason for hiding this comment

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

The else statement I see :

else {
    Zonemaster::Engine::Profile->effective->set( q{net.ipv4}, 1 );
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That "else" should first check if any of ipv4/ipv6 is enabled, if not set both. Otherwise we will, again, override the setting in any selected config file.

I will check and come with an updated version.

Copy link

Choose a reason for hiding this comment

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

Oh yes I didn't see that it was as twisted as that.

I think this cumbersome code covers every cases (maybe some perl magic might make it lighter)

    if ( defined ($self->ipv4) ) {
        Zonemaster::Engine::Profile->effective->set( q{net.ipv4}, $self->ipv4 );
    } else {
        if ( ! defined ($self->ipv6) ) {
            Zonemaster::Engine::Profile->effective->set( q{net.ipv4}, 1 );
        }
    }

    if ( defined ($self->ipv6) ) {
        Zonemaster::Engine::Profile->effective->set( q{net.ipv6}, $self->ipv6 );
    } else {
        if ( ! defined ($self->ipv4) ) {
            Zonemaster::Engine::Profile->effective->set( q{net.ipv6}, 1 );
        }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will also be wrong, because if you have selected a profile where IPv6 has been disabled, then your suggestion will enable IPv6 unless you explicitly have selected --no-ipv6.

I will proved an update.

Copy link

Choose a reason for hiding this comment

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

This is really twisted ^^. I'll let you find a solution ;)

@matsduf
Copy link
Contributor Author

matsduf commented Jan 20, 2021

@mattias-p and @PNAX, should CLI be "helpful" and enable both IPv4 and IPv6 if both are disabled? I do not think so. I think it is better that the user gets the message rather than adding that magic.

When could that happen?

  1. The user has disabled both in the default profile.
  2. The user has loaded a profile which in combination with the default profile disables both.
  3. The user has given --no-ipvX option or options that in combination with the profiles disables both.

@matsduf matsduf requested review from mattias-p and a user January 20, 2021 10:28
@mattias-p
Copy link
Member

I don't think we should support modifying the default profile.

If a user specifies a profile with both IPv4 and IPv6 disabled but forgets to override either one on the command line, it seems reasonable to just let them know that nothing can be done because both IPv4 and IPv6 are disabled. Maybe it would be better to give the user feedback in the form of an input validation error than in the form of a message tag.

@matsduf
Copy link
Contributor Author

matsduf commented Jan 20, 2021

I don't think we should support modifying the default profile.

I agree.

Maybe it would be better to give the user feedback in the form of an input validation error than in the form of a message tag.

That would be beyond the scope of this PR for fixing the bug late in the release cycle. I suggest a issue to do that update in next release.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

From my point of view, if IPv4 and/or IPv6 are explicitly disabled, then the CLI should comply with this choice.

However, I think this does not solve a specific case. From my understanding the net stanza in the profile is not mandatory. Hence if the profile.json file do not have the net stanza (because the user forgot to add it, because it was shipped without it...) then there is no default fallback. I think the CLI should use IPv4 and IPv6 by default in this specific case.
Or maybe this stanza is already mandatory and I missed this.

Is there any documentation about the precedence between the CLI --(no)ipvx options and the profile.json entries ?

}
if defined ($self->ipv6) {
Zonemaster::Engine::Profile->effective->set( q{net.ipv6}, 0+$self->ipv6 );
};
Copy link

Choose a reason for hiding this comment

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

unnecessary trailing semicolon

And I think that the 0+ can be removed as well.

Copy link
Member

Choose a reason for hiding this comment

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

And I think that the 0+ can be removed as well.

That's correct. It's in the contract of the Profile properties to always return the correct data type.

Copy link
Contributor Author

@matsduf matsduf Jan 20, 2021

Choose a reason for hiding this comment

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

Concerning "0+" I also looked at that, but I wanted to change as little as possible since this is a bug fix late in the cycle.

The trailing semicolon it not needed. When I started with Perl 1996 I think it was recommended, or maybe even mandatory in Perl 4, so I have a habit of writing them. It does not harm anything.

Copy link
Member

Choose a reason for hiding this comment

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

I appreciate the historical note :)

@matsduf
Copy link
Contributor Author

matsduf commented Jan 20, 2021

From my point of view, if IPv4 and/or IPv6 are explicitly disabled, then the CLI should comply with this choice.

If you select a profile with "--profile" and that has disabled both or you have disable by "--no-ipv4 --no-ipv6" then I think we can say that the user has explicitly disabled both.

However, I think this does not solve a specific case. From my understanding the net stanza in the profile is not mandatory. Hence if the profile.json file do not have the net stanza (because the user forgot to add it, because it was shipped without it...) then there is no default fallback. I think the CLI should use IPv4 and IPv6 by default in this specific case.

The profile.json is part of the installation, and that is always loaded. We should not support editing of that. That profile has both ipv4 and ipv6 enabled.

If you choose another profile by "--profile" then first the default profile.json will be loaded and then your profile, which will override all parameters that are defined in your profile.

Or maybe this stanza is already mandatory and I missed this.

It is not mandatory, but is is included in the default profile.json that is provided by the installation and always loaded.

Is there any documentation about the precedence between the CLI --(no)ipvx options and the profile.json entries ?

No, not explicitly.

@mattias-p
Copy link
Member

@PNAX I feel there's a misunderstanding here.

The Profile module was designed to never have any undefined properties in the default profile, nor in the effective profile. They always start out with a valid default value - in particular the default value that is specified in the documentation (i.e. true for both net.ipv4 and net.ipv6). If users modify the default profile, they're breaking the Profile contract and all bets are off.

Users should be able to override the default profile with --profile and other arguments. So the only way to get to no-IPv4 and no-IPv6 should be to specify that on the command line.

Lastly, no, I don't think we have any documentation on precedence. We should have that.

@ghost
Copy link

ghost commented Jan 20, 2021

Indeed this is not the logic I had in mind. Thank you for your insights.

@matsduf
Copy link
Contributor Author

matsduf commented Jan 20, 2021

@mattias-p and @PNAX, is this good enough? Can you approve?

@matsduf matsduf requested a review from a user January 20, 2021 11:27
Comment on lines 360 to 365
if defined ($self->ipv4) {
Zonemaster::Engine::Profile->effective->set( q{net.ipv4}, 0+$self->ipv4 );
}
if defined ($self->ipv6) {
Zonemaster::Engine::Profile->effective->set( q{net.ipv6}, 0+$self->ipv6 );
}
Copy link

@ghost ghost Jan 20, 2021

Choose a reason for hiding this comment

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

I tested it, and it looks like the parentheses usage in the if statement makes perl complain.

the following solves the complaint

if ( defined (...) ) {
...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should have kept the first version... Fix in a minute.

@mattias-p
Copy link
Member

@matsduf Yeah, just fix the syntax errors and I'll approve.

@matsduf matsduf requested a review from a user January 20, 2021 12:33
@matsduf
Copy link
Contributor Author

matsduf commented Jan 20, 2021

I have verified that the code works after my last commit. @mattias-p and @PNAX, please review.

@matsduf matsduf merged commit 47314d8 into zonemaster:develop Jan 20, 2021
@matsduf matsduf deleted the update-cli.pm-issue-182bis branch January 20, 2021 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-High Priority: Issue to be solved before other
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants