在 apparmor 配置文件下加载 Django 应用程序的设置



所以我们有一个django应用程序,它需要一个apprmor配置文件(客户端命令)。它在位于/opt/fact-virtual-environment中的Python虚拟环境中运行。应用程序本身安装到/usr/share/fact

以下是我到目前为止的个人资料(主要使用aa genprof工具构建):

# Last Modified: Tue Jan 20 09:25:09 2015
#include <tunables/global>
/usr/bin/fact flags=(audit) {
  #include <abstractions/apache2-common>
  #include <abstractions/base>
  #include <abstractions/bash>

  capability setgid,
  capability sys_resource,
  /usr/local/lib/python2.7/dist-packages/**/ rm,
  /usr/local/lib/python2.7/dist-packages/** rm,
  /usr/local/lib/python2.7/site-packages/**/ rm,
  /usr/local/lib/python2.7/site-packages/** rm,
  /etc/apparmor.d/** r,
  #/usr/lib/python2.7/**[^/] r,
  /usr/bin/sudo Ux,
  #/usr/bin/strace ux,
  /bin/bash rix,
  /bin/dash rix,
  /bin/uname rix,
  /dev/tty rw,
  /etc/default/locale r,
  #/etc/nsswitch.conf r,
  #/etc/group r,
  #/etc/passwd r,
  /etc/environment r,
  /etc/login.defs r,
  /etc/lsb-release r,
  /etc/fact/fact.ini r,
  /etc/python2.7/sitecustomize.py r,
  /etc/security/pam_env.conf r,
  /lib{,32,64}/** mr,
  /opt/fact-virtual-environment/**/ mr,
  /opt/fact-virtual-environment/lib/python2.7/**/ mr,
  /opt/fact-virtual-environment/lib/python2.7/** mr,
  /opt/fact-virtual-environment/bin/python rix,
  /run/utmp rk,
  /sbin/ldconfig rix,
  /sbin/ldconfig.real rix,
  /usr/bin/fact rix,
  /usr/lib{,32,64}/** mr,
  /usr/share/fact/**/ r,
  /usr/share/fact/** r,
  /usr/share/pyshared/** r,
  /usr/share/pyshared/**/ r,
}

并且/usr/bin/fact命令是一个简单的包装器:

#!/bin/bash
## This script is for running the 'fact' command on staging/prod, it sudos to
## the 'fact' user before executing /opt/fact/bin/fact
## It is installed as '/usr/bin/fact'
PYTHONPATH=/usr/share/fact
PYTHON_BIN=/opt/fact-virtual-environment/bin/python
DJANGO_SETTINGS_MODULE=fact.settings.staging
if [ "$USER" != "fact" ];
then
  sudo -u fact $0 $*;
else
  PYTHONPATH=${PYTHONPATH} DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE}  ${PYTHON_BIN} -m fact.managecommand $*;
fi

我已经启用了很多我知道不需要的读取权限,比如在调试尝试中设置的整个/usr/local/lib/*。这整件事让我非常恼火的是,当我运行service apparmor reload(或重新启动)时,我的配置文件似乎不会被重新加载。如果我重新启动服务,与aa-complain usr.bin.fact然后是aa-enforce usr.bin.fact相比,我似乎会得到不同的结果。

现在,当我运行命令并检查系统日志时,我看到以下内容:

Feb 11 15:08:48 spiffy kernel: [1228245.774660] type=1400 audit(1423620528.359:14142): apparmor="DENIED" operation="open" parent=12884 profile="/usr/bin/fact" name="/usr/local/lib/python2.7/site-packages/" pid=12885 comm="python" requested_mask="r" denied_mask="r" fsuid=111 ouid=0
Feb 11 15:08:48 spiffy kernel: [1228245.774855] type=1400 audit(1423620528.359:14143): apparmor="DENIED" operation="open" parent=12884 profile="/usr/bin/fact" name="/usr/local/lib/python2.7/dist-packages/" pid=12885 comm="python" requested_mask="r" denied_mask="r" fsuid=111 ouid=0
Feb 11 15:08:48 spiffy kernel: [1228245.775829] type=1400 audit(1423620528.359:14144): apparmor="DENIED" operation="open" parent=12884 profile="/usr/bin/fact" name="/usr/local/lib/python2.7/dist-packages/" pid=12885 comm="python" requested_mask="r" denied_mask="r" fsuid=111 ouid=0

和我的python应用程序barfs:

$ fact
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/share/fact/fact/managecommand.py", line 6, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/fact-virtual-environment/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/opt/fact-virtual-environment/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    settings.INSTALLED_APPS
  File "/opt/fact-virtual-environment/local/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/opt/fact-virtual-environment/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/opt/fact-virtual-environment/local/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'fact.settings.staging' (Is it on sys.path? Is there an import error in the settings file?): cannot import name current_app

它要查找的设置文件位于/usr/share/fact/fact/settings/staging.py

我完全不明白为什么我不能让这个应用程序在它的apparmor配置文件下运行。配置文件中的设置应该允许。发生了什么?

您的配置文件缺少读取目录的权限/usr/local/lib/python2.7/site-packages/

您需要添加/usr/local/lib/python2.7/site-packages/r,

当前配置文件具有的权限允许访问/usr/local/lib/python2.7/site-packages/下的所有内容,但不允许实际访问目录本身

所有拒绝者都在寻找这个特定的目录:/usr/local/lib/python2.7/dist-packages/

您的个人资料包括此目录的一些子目录:

/usr/local/lib/python2.7/dist-packages/**/ rm,
/usr/local/lib/python2.7/dist-packages/** rm,
/usr/local/lib/python2.7/site-packages/**/ rm,
/usr/local/lib/python2.7/site-packages/** rm,

但它不包括这个特定的目录。因此,将其添加到您的配置文件中,重新加载配置文件,然后重试:

/usr/local/lib/python2.7/dist-packages/ r,

AppArmor使用尾随的/来确定管理员是否打算允许获取目录列表,而不是允许读取文件。

感谢

相关内容

  • 没有找到相关文章

最新更新