-
Notifications
You must be signed in to change notification settings - Fork 292
CP-308800: Dynamic control of firewalld service - part 1 #6629
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
Open
BengangY
wants to merge
2
commits into
xapi-project:feature/dynamic-firewalld-control
Choose a base branch
from
BengangY:private/bengangy/CP-308800
base: feature/dynamic-firewalld-control
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
(* | ||
* Copyright (c) Cloud Software Group, Inc. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published | ||
* by the Free Software Foundation; version 2.1 only. with the special | ||
* exception on linking described in file LICENSE. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*) | ||
|
||
module D = Debug.Make (struct let name = __MODULE__ end) | ||
|
||
open D | ||
|
||
type status = Enabled | Disabled | ||
|
||
type service_type = Dlm | Nbd | Ssh | Vxlan | Http | Xenha | ||
|
||
type service_info = {name: string; port: int; protocol: string} | ||
|
||
let status_to_string = function Enabled -> "enabled" | Disabled -> "disabled" | ||
|
||
let service_type_to_service_info = function | ||
| Dlm -> | ||
{name= "dlm"; port= !Xapi_globs.xapi_clusterd_port; protocol= "TCP"} | ||
| Nbd -> | ||
{name= "nbd"; port= 10809; protocol= "TCP"} | ||
| Ssh -> | ||
{name= "ssh"; port= 22; protocol= "TCP"} | ||
| Vxlan -> | ||
{name= "vxlan"; port= 4789; protocol= "UDP"} | ||
| Http -> | ||
{name= "xapi-insecure"; port= Constants.http_port; protocol= "TCP"} | ||
| Xenha -> | ||
{name= "xenha"; port= Xapi_globs.xha_udp_port; protocol= "UDP"} | ||
|
||
module type FIREWALL = sig | ||
val update_firewall_status : service:service_type -> status:status -> unit | ||
|
||
val is_firewall_service_enabled : service:service_type -> bool | ||
end | ||
|
||
module Firewalld : FIREWALL = struct | ||
let update_firewall_status ~service ~status = | ||
if !Xapi_globs.dynamic_control_firewalld_service then | ||
let service_option = | ||
match status with | ||
| Enabled -> | ||
"--add-service" | ||
| Disabled -> | ||
"--remove-service" | ||
in | ||
let service_info = service_type_to_service_info service in | ||
try | ||
Helpers.call_script !Xapi_globs.firewall_cmd | ||
[service_option; service_info.name] | ||
|> ignore | ||
with e -> | ||
error | ||
"%s: Failed to update firewall service (%s) to status (%s) with \ | ||
error: %s" | ||
__FUNCTION__ service_info.name (status_to_string status) | ||
(Printexc.to_string e) | ||
|
||
let is_firewall_service_enabled ~service = | ||
let service_info = service_type_to_service_info service in | ||
try | ||
let output = | ||
Helpers.call_script !Xapi_globs.firewall_cmd | ||
["--query-service"; service_info.name] | ||
|> String.trim | ||
|> String.lowercase_ascii | ||
in | ||
debug "%s: Check firewall service (%s) return: %s" __FUNCTION__ | ||
service_info.name output ; | ||
let status = Scanf.sscanf output "%s" Fun.id in | ||
match status with "yes" -> true | _ -> false | ||
with e -> | ||
error "%s: Failed to check firewall service (%s) with error: %s" | ||
__FUNCTION__ service_info.name (Printexc.to_string e) ; | ||
false | ||
end | ||
|
||
module Iptables : FIREWALL = struct | ||
let update_firewall_status ~service ~status = | ||
let op = match status with Enabled -> "open" | Disabled -> "close" in | ||
let service_info = service_type_to_service_info service in | ||
try | ||
Helpers.call_script | ||
!Xapi_globs.firewall_port_config_script | ||
[op; string_of_int service_info.port; service_info.protocol] | ||
|> ignore | ||
with e -> | ||
error | ||
"%s: Failed to update firewall service (%s) to status (%s) with error: \ | ||
%s" | ||
__FUNCTION__ service_info.name (status_to_string status) | ||
(Printexc.to_string e) | ||
|
||
let is_firewall_service_enabled ~service = | ||
let service_info = service_type_to_service_info service in | ||
try | ||
let output = | ||
Helpers.call_script | ||
!Xapi_globs.firewall_port_config_script | ||
["check"; string_of_int service_info.port; service_info.protocol] | ||
in | ||
debug "%s: Check firewall service (%s) return: %s" __FUNCTION__ | ||
service_info.name output ; | ||
let enabled = | ||
(* The firewall-port script returns true if port 80 is blocked and false | ||
if it is not. *) | ||
Scanf.sscanf output "Port %d open: %B" (fun _ is_blocked -> | ||
not is_blocked | ||
) | ||
in | ||
enabled | ||
with e -> | ||
error "%s: Failed to check firewall service (%s) with error: %s" | ||
__FUNCTION__ service_info.name (Printexc.to_string e) ; | ||
false | ||
end | ||
|
||
let firewall_provider (backend : string) : (module FIREWALL) = | ||
match backend with | ||
| "firewalld" -> | ||
(module Firewalld) | ||
| "iptables" -> | ||
(module Iptables) | ||
| _ -> | ||
Helpers.internal_error "unknown firewall backend: %s" backend |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
(* | ||
* Copyright (c) Cloud Software Group, Inc. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published | ||
* by the Free Software Foundation; version 2.1 only. with the special | ||
* exception on linking described in file LICENSE. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*) | ||
|
||
type status = Enabled | Disabled | ||
|
||
type service_type = Dlm | Nbd | Ssh | Vxlan | Http | Xenha | ||
|
||
type service_info = {name: string; port: int; protocol: string} | ||
|
||
module type FIREWALL = sig | ||
val update_firewall_status : service:service_type -> status:status -> unit | ||
|
||
val is_firewall_service_enabled : service:service_type -> bool | ||
end | ||
|
||
module Firewalld : FIREWALL | ||
|
||
module Iptables : FIREWALL | ||
|
||
val firewall_provider : string -> (module FIREWALL) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -861,6 +861,10 @@ let nbd_firewall_config_script = | |
|
||
let firewall_port_config_script = ref "/etc/xapi.d/plugins/firewall-port" | ||
|
||
let firewall_cmd = ref "/usr/bin/firewall-cmd" | ||
|
||
let firewall_cmd_wrapper = ref "/usr/bin/firewall-cmd-wrapper" | ||
|
||
let nbd_client_manager_script = | ||
ref "/opt/xensource/libexec/nbd_client_manager.py" | ||
|
||
|
@@ -1317,6 +1321,12 @@ let ssh_monitor_service = ref "xapi-ssh-monitor" | |
|
||
let ssh_auto_mode_default = ref true | ||
|
||
(* Firewall backend to use. iptables in XS 8, firewalld in XS 9. *) | ||
let firewall_backend = ref "firewalld" | ||
|
||
(* For firewalld, if dynamic control firewalld service. *) | ||
let dynamic_control_firewalld_service = ref true | ||
|
||
(* Fingerprint of default patch key *) | ||
let citrix_patch_key = | ||
"NERDNTUzMDMwRUMwNDFFNDI4N0M4OEVCRUFEMzlGOTJEOEE5REUyNg==" | ||
|
@@ -1762,12 +1772,6 @@ let other_options = | |
, (fun () -> string_of_bool !validate_reusable_pool_session) | ||
, "Enable validation of reusable pool sessions before use" | ||
) | ||
; ( "ssh-auto-mode" | ||
, Arg.Bool (fun b -> ssh_auto_mode_default := b) | ||
, (fun () -> string_of_bool !ssh_auto_mode_default) | ||
, "Defaults to true; overridden to false via \ | ||
/etc/xapi.conf.d/ssh-auto-mode.conf(e.g., in XenServer 8)" | ||
) | ||
; ( "vm-sysprep-enabled" | ||
, Arg.Set vm_sysprep_enabled | ||
, (fun () -> string_of_bool !vm_sysprep_enabled) | ||
|
@@ -1778,6 +1782,17 @@ let other_options = | |
, (fun () -> string_of_float !vm_sysprep_wait) | ||
, "Time in seconds to wait for VM to recognise inserted CD" | ||
) | ||
; ( "firewall-backend" | ||
, Arg.Set_string firewall_backend | ||
, (fun () -> !firewall_backend) | ||
, "Firewall backend. iptables (in XS 8) or firewalld (in XS 9 or later XS \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we would not accept arbitratry strings here and reject illegal strings already. But this is the easiest solution and we have to reject illegal backends later and report them as internal error - which is not entirely true. |
||
version)" | ||
) | ||
; ( "dynamic-control-firewalld-service" | ||
, Arg.Bool (fun b -> dynamic_control_firewalld_service := b) | ||
, (fun () -> string_of_bool !dynamic_control_firewalld_service) | ||
, "Enable dynamic control firewalld service" | ||
) | ||
] | ||
|
||
(* The options can be set with the variable xapiflags in /etc/sysconfig/xapi. | ||
|
@@ -1912,10 +1927,14 @@ module Resources = struct | |
, "Executed after NBD-related networking changes to configure the \ | ||
firewall for NBD" | ||
) | ||
; ( "firewall-port-config" | ||
, firewall_port_config_script | ||
, "Executed when starting/stopping xapi-clusterd to configure firewall \ | ||
port" | ||
; ( "firewall-cmd" | ||
, firewall_cmd | ||
, "Executed when enable/disable a service on a firewalld zone" | ||
) | ||
; ( "firewall-cmd-wrapper" | ||
, firewall_cmd_wrapper | ||
, "Executed when enable/disable a service on a firewalld zone and \ | ||
interface" | ||
) | ||
; ( "nbd_client_manager" | ||
, nbd_client_manager_script | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if this was iptables by default, with the configuration file for XS9 overriding the value to firewalld for the time being. And changing the default once XS9 is forked.