2.8. 配置 SELinux
SELinux 默认限制对 HTTP 服务器的非标准访问。如果我们定义了自定义端口,则需要添加允许 SELinux 授予访问权限的配置。
Puppet 包含用于管理某些 SELinux 功能的资源类型,如布尔值和模块。但是,我们需要执行 semanage 命令来管理端口设置。这个工具是 policycoreutils-python 软件包的一部分,默认情况下未在 Red Hat Enterprise Linux 系统上安装。
在您的 mymodule::httpd 类中添加以下代码:
exec { 'semanage-port':
command => "semanage port -a -t http_port_t -p tcp ${httpd_port}",
path => "/usr/sbin",
require => Package['policycoreutils-python'],
before => Service['httpd'],
subscribe => Package['httpd'],
refreshonly => true,
}
package { 'policycoreutils-python':
ensure => installed,
}
这个代码执行以下操作:
-
require 时间为 package['policycoreutils-python']属性,确保在执行此命令前确保已安装了 policycoreutils-python。 -
Puppet 使用
httpd_port作为变量执行semanage,将自定义端口添加到 Apache 允许侦听的 TCP 端口列表中。 -
之前 InventoryService Service ['httpd']确保在httpd服务启动前执行此命令。如果在 SELinux 命令前启动httpd,SELinux 会拒绝访问端口,服务无法启动。 -
SELinux 可执行文件的代码包含
仅刷新 10.10.10.2 true并订阅 abrt Package['httpd']属性。这样可确保 SELinux 命令仅在 httpd 安装之后运行。如果没有这些属性,后续运行会失败。这是因为 SELinux 检测到端口已经启用并报告错误。
再次运行 puppet apply 命令,以测试对模块的更改。
# puppet apply mymodule/tests/init.pp --noop
...
Notice: /Stage[main]/Mymodule::Httpd/Package[policycoreutils-python]/ensure: current_value absent, should be present (noop)
...
Notice: /Stage[main]/Mymodule::Httpd/Exec[semanage-port]/returns: current_value notrun, should be 0 (noop)
...
Notice: /Stage[main]/Mymodule::Httpd/Service[httpd]/ensure: current_value stopped, should be running (noop)
...
Puppet 首先安装 policycoreutils-python,然后在启动 httpd 服务前配置端口访问权限。