使用 MITRE 政策限制条件

政策控制器附带默认的限制条件模板库,您可以将该库与 MITRE 政策包搭配使用,以根据实际观察结果,根据 MITRE 对手策略和技术的 MITRE 知识库的某些方面评估集群资源的合规性。

MITRE 政策包限制条件

限制条件名称 限制条件说明
mitre-v2024-apparmor 限制 Pod 的 AppArmor 配置文件。
mitre-v2024-block-all-ingress 限制 Ingress 对象的创建。
mitre-v2024-cronjob-restrict-repos CronJob 的容器映像限制在允许的“repos”列表中。
mitre-v2024-no-anonymous 禁止将 ClusterRoleRole 资源与 system:anonymous 用户和 system:unauthenticated 组相关联。
mitre-v2024-host-namespaces-host-pid-ipc hostPIDhostIPC 设置为 true 的情况下,容器无法运行。
mitre-v2024-host-namespaces-hostnetwork hostNetwork 设置为 true 的情况下,容器无法运行。
mitre-v2024-host-ports 应禁止使用 HostPorts,或至少将其限制为已知列表。
mitre-v2024-no-secrets-as-env-vars 限制将 Secret 用作容器定义中的环境变量。
mitre-v2024-privileged-containers 禁止容器的 securityContext.privileged 设置为 true
mitre-v2024-proc-mount-type 默认的 /proc 掩码可以减少受攻击面,因此是必需的。
mitre-v2024-require-binauthz 需要 Binary Authorization 验证准入 Webhook。
mitre-v2024-require-namespace-networkpolicy 要求集群中定义的每个命名空间都必须具有 NetworkPolicy。
mitre-v2024-restrict-admission-controller 限制使用动态准入控制器和网络钩子。
mitre-v2024-restrict-automountserviceaccounttoken 限制服务帐号令牌的使用。
mitre-v2024-restrict-capabilities 除了所列功能之外,不允许添加其他功能。
mitre-v2024-restrict-cluster-admin-role 限制 cluster-admin 角色的使用。
mitre-v2024-restrict-hostpath-volumes 限制使用 HostPath 卷。
mitre-v2024-restrict-kubernetes-dashboard-namespace 限制使用 kubernetes-dashboard 命名空间。
mitre-v2024-restrict-pods-exec 禁止在 RolesClusterRoles 中使用 pods/exec
mitre-v2024-restrict-rbac-subjects 将 RBAC 主题中名称的使用限制为允许的值。
mitre-v2024-restrict-repos 将容器映像限制为允许的 repos 列表。
mitre-v2024-restrict-role-secrets 限制 RolesClusterRoles 中的 Secret 使用。
mitre-v2024-restrict-windows-hostprocess 限制 Windows HostProcess 容器 / Pod 的运行
mitre-v2024-seccomp Seccomp 配置文件不得设置为 Unconfined
mitre-v2024-selinux 无法设置 SELinux 类型或设置自定义 SELinux 用户或角色选项。
mitre-v2024-sysctls 容器只能设置 allowedSysctls 字段中列出的 sysctl。

准备工作

  1. 安装并初始化 Google Cloud CLI,它提供了以下说明中使用的 gcloudkubectl 命令。如果您使用 Cloud Shell,则系统会预安装 Google Cloud CLI。
  2. 在使用默认限制条件模板库的集群上安装 Policy Controller v1.17.2 或更高版本。您还必须启用对参照限制条件的支持,因为此捆绑包包含参照限制条件。

为参照限制条件配置政策控制器

  1. 在文件中,将以下 YAML 清单保存为 policycontroller-config.yaml。清单将政策控制器配置为监视特定种类的对象。

    apiVersion: config.gatekeeper.sh/v1alpha1
    kind: Config
    metadata:
      name: config
      namespace: "gatekeeper-system"
    spec:
      sync:
        syncOnly:
          - group: "networking.k8s.io"
            version: "v1"
            kind: "NetworkPolicy"
          - group: "admissionregistration.k8s.io"
            version: "v1"
            kind: "ValidatingWebhookConfiguration"
    
  2. 应用 policycontroller-config.yaml 清单:

    kubectl apply -f policycontroller-config.yaml
    

审核 MITRE 政策包

政策控制器允许您为 Kubernetes 集群强制执行政策。为了帮助测试您的工作负载及其与上表中列出的 MITRE 政策的合规性,您可以在“审核”模式下部署这些限制条件,以发现违规行为;更重要的是,更重要的是,让您有机会在对 Kubernetes 集群强制执行之前进行修正。

您可以在 spec.enforcementAction 设置为 dryrun 的情况下,使用 kubectl、kptConfig Sync 应用这些政策。

kubectl

  1. (可选)使用 kubectl 预览政策限制条件:

    kubectl kustomize https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/mitre-v2024
    
  2. 使用 kubectl 应用政策限制条件:

    kubectl apply -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/mitre-v2024
    

    输出如下所示:

    k8sallowedrepos.constraints.gatekeeper.sh/mitre-v2024-restrict-repos created
    k8sblockallingress.constraints.gatekeeper.sh/mitre-v2024-block-all-ingress created
    k8scronjoballowedrepos.constraints.gatekeeper.sh/mitre-v2024-cronjob-restrict-repos created
    k8sdisallowanonymous.constraints.gatekeeper.sh/mitre-v2024-no-anonymous created
    k8snoenvvarsecrets.constraints.gatekeeper.sh/mitre-v2024-no-secrets-as-env-vars created
    k8spspapparmor.constraints.gatekeeper.sh/mitre-v2024-apparmor created
    k8spspcapabilities.constraints.gatekeeper.sh/mitre-v2024-restrict-capabilities created
    k8spspforbiddensysctls.constraints.gatekeeper.sh/mitre-v2024-sysctls created
    k8spsphostfilesystem.constraints.gatekeeper.sh/mitre-v2024-restrict-hostpath-volumes created
    k8spsphostnamespace.constraints.gatekeeper.sh/mitre-v2024-host-namespaces-host-pid-ipc created
    k8spsphostnetworkingports.constraints.gatekeeper.sh/mitre-v2024-host-namespaces-hostnetwork created
    k8spsphostnetworkingports.constraints.gatekeeper.sh/mitre-v2024-host-ports created
    k8spspprivilegedcontainer.constraints.gatekeeper.sh/mitre-v2024-privileged-containers created
    k8spspprocmount.constraints.gatekeeper.sh/mitre-v2024-proc-mount-type created
    k8spspselinuxv2.constraints.gatekeeper.sh/mitre-v2024-selinux created
    k8spspseccomp.constraints.gatekeeper.sh/mitre-v2024-seccomp created
    k8spspwindowshostprocess.constraints.gatekeeper.sh/mitre-v2024-restrict-windows-hostprocess created
    k8srequirebinauthz.constraints.gatekeeper.sh/mitre-v2024-require-binauthz created
    k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/mitre-v2024-require-namespace-networkpolicy created
    k8srestrictadmissioncontroller.constraints.gatekeeper.sh/mitre-v2024-restrict-admission-controller created
    k8srestrictautomountserviceaccounttokens.constraints.gatekeeper.sh/mitre-v2024-restrict-automountserviceaccounttoken created
    k8srestrictnamespaces.constraints.gatekeeper.sh/mitre-v2024-restrict-kubernetes-dashboard-namespace created
    k8srestrictrbacsubjects.constraints.gatekeeper.sh/mitre-v2024-restrict-rbac-subjects created
    k8srestrictrolebindings.constraints.gatekeeper.sh/mitre-v2024-restrict-cluster-admin-role created
    k8srestrictrolerules.constraints.gatekeeper.sh/mitre-v2024-restrict-pods-exec created
    k8srestrictrolerules.constraints.gatekeeper.sh/mitre-v2024-restrict-role-secrets created
    
  3. 验证政策限制条件是否已安装,并检查集群中是否存在违规行为:

    kubectl get constraints -l policycontroller.gke.io/bundleName=mitre-v2024
    

    输出类似于以下内容:

    NAME                                                                   ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8sallowedrepos.constraints.gatekeeper.sh/mitre-v2024-restrict-repos   dryrun               0
    
    NAME                                                                         ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8sblockallingress.constraints.gatekeeper.sh/mitre-v2024-block-all-ingress   dryrun               0
    
    NAME                                                                                  ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8scronjoballowedrepos.constraints.gatekeeper.sh/mitre-v2024-cronjob-restrict-repos   dryrun               0
    
    NAME                                                                      ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8sdisallowanonymous.constraints.gatekeeper.sh/mitre-v2024-no-anonymous   dryrun               0
    
    NAME                                                                              ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8snoenvvarsecrets.constraints.gatekeeper.sh/mitre-v2024-no-secrets-as-env-vars   dryrun               0
    
    NAME                                                            ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspapparmor.constraints.gatekeeper.sh/mitre-v2024-apparmor   dryrun               0
    
    NAME                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspcapabilities.constraints.gatekeeper.sh/mitre-v2024-restrict-capabilities   dryrun               0
    
    NAME                                                                   ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspforbiddensysctls.constraints.gatekeeper.sh/mitre-v2024-sysctls   dryrun               0
    
    NAME                                                                                   ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spsphostfilesystem.constraints.gatekeeper.sh/mitre-v2024-restrict-hostpath-volumes   dryrun               0
    
    NAME                                                                                     ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spsphostnamespace.constraints.gatekeeper.sh/mitre-v2024-host-namespaces-host-pid-ipc   dryrun               0
    
    NAME                                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spsphostnetworkingports.constraints.gatekeeper.sh/mitre-v2024-host-namespaces-hostnetwork   dryrun               0
    k8spsphostnetworkingports.constraints.gatekeeper.sh/mitre-v2024-host-ports                    dryrun               0
    
    NAME                                                                                    ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspprivilegedcontainer.constraints.gatekeeper.sh/mitre-v2024-privileged-containers   dryrun               0
    
    NAME                                                                    ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspprocmount.constraints.gatekeeper.sh/mitre-v2024-proc-mount-type   dryrun               0
    
    NAME                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspseccomp.constraints.gatekeeper.sh/mitre-v2024-seccomp   dryrun               0
    
    NAME                                                            ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspselinuxv2.constraints.gatekeeper.sh/mitre-v2024-selinux   dryrun               0
    
    NAME                                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspwindowshostprocess.constraints.gatekeeper.sh/mitre-v2024-restrict-windows-hostprocess   dryrun               0
    
    NAME                                                                        ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srequirebinauthz.constraints.gatekeeper.sh/mitre-v2024-require-binauthz   dryrun               0
    
    NAME                                                                                                       ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/mitre-v2024-require-namespace-networkpolicy   dryrun               0
    
    NAME                                                                                                 ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictadmissioncontroller.constraints.gatekeeper.sh/mitre-v2024-restrict-admission-controller   dryrun               0
    
    NAME                                                                                                                   ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictautomountserviceaccounttokens.constraints.gatekeeper.sh/mitre-v2024-restrict-automountserviceaccounttoken   dryrun               0
    
    NAME                                                                                                  ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictnamespaces.constraints.gatekeeper.sh/mitre-v2024-restrict-kubernetes-dashboard-namespace   dryrun               0
    
    NAME                                                                                   ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictrbacsubjects.constraints.gatekeeper.sh/mitre-v2024-restrict-rbac-subjects   dryrun               0
    
    NAME                                                                                        ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictrolebindings.constraints.gatekeeper.sh/mitre-v2024-restrict-cluster-admin-role   dryrun               0
    
    NAME                                                                               ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictrolerules.constraints.gatekeeper.sh/mitre-v2024-restrict-pods-exec      dryrun               0
    k8srestrictrolerules.constraints.gatekeeper.sh/mitre-v2024-restrict-role-secrets   dryrun               0
    

kpt

  1. 安装并设置 kpt。在这些说明中,kpt 用于自定义和部署 Kubernetes 资源。

  2. 使用 kpt 从 GitHub 下载 MITRE 政策包:

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/mitre-v2024
    
  3. 运行 set-enforcement-action kpt 函数将政策的强制执行操作设置为 dryrun

    kpt fn eval mitre-v2024 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 \
      -- enforcementAction=dryrun
    
  4. 使用 kpt 初始化工作目录,此操作会创建一个资源来跟踪更改:

    cd mitre-v2024
    kpt live init
    
  5. 使用 kpt 应用政策限制条件:

    kpt live apply
    
  6. 验证政策限制条件是否已安装,并检查集群中是否存在违规行为:

    kpt live status --output table --poll-until current
    

    状态为 CURRENT,即确认限制条件成功安装。

Config Sync

  1. 安装并设置 kpt。在这些说明中,kpt 用于自定义和部署 Kubernetes 资源。

使用 Config Sync 将政策部署到其集群的运维人员可以按照以下说明操作:

  1. 更改为 Config Sync 的同步目录:

    cd SYNC_ROOT_DIR
    

    创建 resourcegroup.yaml 或将其附加到 .gitignore

    echo resourcegroup.yaml >> .gitignore
    

  2. 创建一个专用的 policies 目录:

    mkdir -p policies
    
  3. 使用 kpt 从 GitHub 下载 MITRE 政策包:

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/mitre-v2024 policies/mitre-v2024
    
  4. 运行 set-enforcement-action kpt 函数将政策的强制执行操作设置为 dryrun

    kpt fn eval policies/mitre-v2024 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=dryrun
    
  5. (可选)预览待创建的政策限制条件:

    kpt live init policies/mitre-v2024
    kpt live apply --dry-run policies/mitre-v2024
    
  6. 如果 Config Sync 的同步目录使用 Kustomize,请将 policies/mitre-v2024 添加到根 kustomization.yaml。 否则,请移除 policies/mitre-v2024/kustomization.yaml 文件:

    rm SYNC_ROOT_DIR/policies/mitre-v2024/kustomization.yaml
    
  7. 将更改推送到 Config Sync 代码库:

    git add SYNC_ROOT_DIR/policies/mitre-v2024
    git commit -m 'Adding MITRE policy audit enforcement'
    git push
    
  8. 验证安装状态:

    watch gcloud beta container fleet config-management status --project PROJECT_ID
    

    状态 SYNCED 用于确认政策的安装情况。

查看违规问题

在审核模式下安装政策限制条件后,您可以使用政策控制器信息中心在界面中查看集群的违规问题。

您还可以使用 kubectl 来通过以下命令查看针对集群的违规行为:

kubectl get constraint -l policycontroller.gke.io/bundleName=mitre-v2024 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]'

如果存在违规行为,您可以使用以下命令查看每个限制条件的违规消息列表:

kubectl get constraint -l policycontroller.gke.io/bundleName=mitre-v2024 -o json | jq -C '.items[]| select(.status.totalViolations>0)| [.metadata.name,.status.violations[]?]'

更改 MITRE 政策包违规处置操作

查看集群上的政策违规情况后,您可以考虑更改强制执行模式,以便准入控制器发出 warn,甚至 deny 屏蔽不合规资源应用于集群。

kubectl

  1. 使用 kubectl 将政策的强制执行操作设置为 warn

    kubectl get constraint -l policycontroller.gke.io/bundleName=mitre-v2024 -o name | xargs -I {} kubectl patch {} --type='json' -p='[{"op":"replace","path":"/spec/enforcementAction","value":"warn"}]'
    
  2. 验证政策限制条件强制执行措施是否已更新:

    kubectl get constraint -l policycontroller.gke.io/bundleName=mitre-v2024
    

kpt

  1. 运行 set-enforcement-action kpt 函数将政策的强制执行操作设置为 warn

    kpt fn eval -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  2. 应用政策限制条件:

    kpt live apply
    

Config Sync

使用 Config Sync 将政策部署到其集群的运维人员可以按照以下说明操作:

  1. 更改为 Config Sync 的同步目录:

    cd SYNC_ROOT_DIR
    
  2. 运行 set-enforcement-action kpt 函数将政策的强制执行操作设置为 warn

    kpt fn eval policies/mitre-v2024 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  3. 将更改推送到 Config Sync 代码库:

    git add SYNC_ROOT_DIR/policies/mitre-v2024
    git commit -m 'Adding MITRE policy bundle warn enforcement'
    git push
    
  4. 验证安装状态:

    gcloud alpha anthos config sync repo list --project PROJECT_ID
    

    SYNCED 列中显示的代码库会确认政策已安装。

测试政策执行情况

使用以下命令在集群上创建不合规的资源:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  namespace: default
  name: wp-non-compliant
  labels:
    app: wordpress
spec:
  containers:
    - image: wordpress
      name: wordpress
      ports:
      - containerPort: 80
        name: wordpress
EOF

准入控制器应生成一个警告列表,其中列出了此资源违反政策的行为,如以下示例所示:

Warning: [mitre-v2024-restrict-repos] container <wordpress> has an invalid image repo <wordpress>, allowed repos are ["gcr.io/gke-release/", "gcr.io/anthos-baremetal-release/", "gcr.io/gke-on-prem-release/", "gcr.io/gke-multi-cloud-release/", "gcr.io/config-management-release/", "gcr.io/kubebuilder/", "gcr.io/gkeconnect/", "gke.gcr.io/"]
pod/wp-non-compliant created

移除 MITRE 政策包

如果需要,可以从集群中移除 MITRE 政策包。

kubectl

  • 使用 kubectl 移除政策:

    kubectl delete constraint -l policycontroller.gke.io/bundleName=mitre-v2024
    

kpt

  • 移除政策:

    kpt live destroy
    

Config Sync

使用 Config Sync 将政策部署到其集群的运维人员可以按照以下说明操作:

  1. 将更改推送到 Config Sync 代码库:

    git rm -r SYNC_ROOT_DIR/policies/mitre-v2024
    git commit -m 'Removing MITRE policies'
    git push
    
  2. 验证状态:

    gcloud alpha anthos config sync repo list --project PROJECT_ID
    

    SYNCED 列中显示您的代码库即表示政策移除成功。