Skip to content

Commit 6fbb10e

Browse files
committed
CP-308800: Dynamic control http firewall service
Signed-off-by: Bengang Yuan <[email protected]>
1 parent 64da201 commit 6fbb10e

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

ocaml/xapi/dbsync_slave.ml

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,33 @@ let refresh_localhost_info ~__context info =
134134
) else
135135
Db.Host.remove_from_other_config ~__context ~self:host
136136
~key:Xapi_globs.host_no_local_storage ;
137-
let script_output =
138-
Helpers.call_script !Xapi_globs.firewall_port_config_script ["check"; "80"]
137+
let update_https_only =
138+
match !Xapi_globs.firewall_backend with
139+
| "firewalld" ->
140+
let enabled =
141+
Firewall.is_firewall_service_enabled ~service:Firewall.Xapi_insecure
142+
in
143+
Db.Host.set_https_only ~__context ~self:host ~value:(not enabled)
144+
| "iptables" -> (
145+
let script_output =
146+
Helpers.call_script
147+
!Xapi_globs.firewall_port_config_script
148+
["check"; "80"]
149+
in
150+
try
151+
let network_state =
152+
Scanf.sscanf script_output "Port 80 open: %B" Fun.id
153+
in
154+
Db.Host.set_https_only ~__context ~self:host ~value:network_state
155+
with _ ->
156+
Helpers.internal_error
157+
"unexpected output from /etc/xapi.d/plugins/firewall-port: %s"
158+
script_output
159+
)
160+
| backend ->
161+
warn "Unknown firewall backend %s, ignore it." backend
139162
in
140-
try
141-
let network_state = Scanf.sscanf script_output "Port 80 open: %B" Fun.id in
142-
Db.Host.set_https_only ~__context ~self:host ~value:network_state
143-
with _ ->
144-
Helpers.internal_error
145-
"unexpected output from /etc/xapi.d/plugins/firewall-port: %s"
146-
script_output
163+
update_https_only
147164
(*************** update database tools ******************)
148165

149166
(** Record host memory properties in database *)

ocaml/xapi/xapi_host.ml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,13 +3116,28 @@ let cc_prep () =
31163116
true
31173117

31183118
let set_https_only ~__context ~self ~value =
3119-
let state = match value with true -> "close" | false -> "open" in
31203119
match cc_prep () with
31213120
| false ->
3122-
ignore
3123-
@@ Helpers.call_script
3124-
!Xapi_globs.firewall_port_config_script
3125-
[state; "80"] ;
3121+
( match !Xapi_globs.firewall_backend with
3122+
| "firewalld" ->
3123+
let status =
3124+
match value with
3125+
| true ->
3126+
Firewall.Disabled
3127+
| false ->
3128+
Firewall.Enabled
3129+
in
3130+
Firewall.update_firewall_status ~service:Firewall.Xapi_insecure
3131+
~status
3132+
| "iptables" ->
3133+
let state = match value with true -> "close" | false -> "open" in
3134+
ignore
3135+
@@ Helpers.call_script
3136+
!Xapi_globs.firewall_port_config_script
3137+
[state; "80"]
3138+
| backend ->
3139+
warn "Unknown firewall backend %s, ignore it." backend
3140+
) ;
31263141
Db.Host.set_https_only ~__context ~self ~value
31273142
| true when value = Db.Host.get_https_only ~__context ~self ->
31283143
(* the new value is the same as the old value *)

0 commit comments

Comments
 (0)