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

CP-40754 Update host.https_only field in dbsync based on firewall state #4811

Conversation

jameshensmancitrix
Copy link
Contributor

The firewall-port script returns true if port 80 is blocked … and false if it is closed, this is captured in set_https_only to update the DB based on the state of the network, not the requested setting should there be a failure

Signed-off-by: jameshensmancitrix james.hensman@citrix.com

let network_state =
Helpers.call_script !Xapi_globs.firewall_port_config_script [state; "80"]
in
Db.Host.set_https_only ~__context ~self ~value:(bool_of_string network_state)
Copy link
Member

@robhoes robhoes Oct 5, 2022

Choose a reason for hiding this comment

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

This change should not be done here, but in refresh_localhost_info in dbsync_slave.ml, which is a function that runs when xapi starts up. At that point we want to query the state of the firewall and sync the DB.

@@ -52,5 +52,13 @@ case "${OP}" in
exit 1
;;
esac

if [[ -z `iptables -S $CHAIN | grep " $PORT "` ]]
Copy link
Member

Choose a reason for hiding this comment

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

This query should go as a new operation in the case statement above, e.g. check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I put the query under each of the case statements?

Copy link
Member

Choose a reason for hiding this comment

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

No, only under one new case. So the script will have three distinct ops: open, close, check.

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 makes more sense now, have since adjusted it

Copy link
Member

@robhoes robhoes left a comment

Choose a reason for hiding this comment

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

See above

if [[ -z `iptables -S $CHAIN | grep " $PORT "` ]]
then
echo true
else
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation needs a fix. Maybe this is part of the design, but I think a more descriptive output would be better unless the output is directly used somewhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the stdout feeds into OCaml where it is fed into bool_of_string. This tells Xapi what the state of https_only should be.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would still use a more descriptive output and use sscanf to parse it on the OCaml side. That is safer than using bool_of_string anyway. Something like: port 80: open which can be easily parsed.

else
echo false
fi
;;
*)
echo $"Usage: $0 {open|close} {port} {protocol}" 1>&2
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs an update

@jameshensmancitrix jameshensmancitrix force-pushed the private/jameshen/host_https_only branch 3 times, most recently from f5f3602 to 89945a0 Compare October 5, 2022 15:20
(Scanf.bscanf
(Scanf.Scanning.from_string network_state)
"Port 80 open: %B" Fun.id
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Scanf can fail with an exception. This needs to be caught and raised as internal error. Obviously we expect that the script returns the expected output. You might want to experiment with this in an OCaml shell like utop

Copy link
Member

Choose a reason for hiding this comment

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

ksscanf takes an additional parameter that is a handler in case the string cannot be parsed:

ocaml/xapi/dbsync_slave.ml Outdated Show resolved Hide resolved
…and false if it is closed, this is captured in set_https_only to update the DB based on the tate of the network not the requested setting should there be a failure

Signed-off-by: jameshensmancitrix <james.hensman@citrix.com>
Copy link
Contributor

@lindig lindig left a comment

Choose a reason for hiding this comment

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

Nice improvements!

@robhoes
Copy link
Member

robhoes commented Oct 6, 2022

This looks good now. But please merge only after testing.

@jameshensmancitrix
Copy link
Contributor Author

Am currently testing, I will compare with both network states switched when Xapi is not running

@jameshensmancitrix
Copy link
Contributor Author

Testing complete, xapi correctly sets https_only on the host on the basis of the port after systemctl restart xapi

@jameshensmancitrix jameshensmancitrix merged commit 42d4de9 into xapi-project:master Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants