使用 CIS Kubernetes 基准政策限制条件
政策控制器附带默认的限制条件模板库,您可以将该库与 CIS 软件包搭配使用,从而根据CIS Kubernetes 基准来审核集群的合规性。此基准是一系列关于配置 Kubernetes 以支持可靠的安全状况的建议。
此限制条件软件包在以下网域中处理并强制执行政策:
- RBAC 和服务帐号
- Pod 安全政策
- 网络政策和 CNI
- 密文管理
- 常规政策
这些政策尚未通过 CIS 认证。
该软件包包含以下限制条件,以满足 CIS 基准中相应控制措施的要求:
限制条件 | 对照组 ID | 对照组说明 | 参照 |
---|---|---|---|
restrict-clusteradmin-rolebindings | 5.1.1 | 限制 cluster-admin 角色的使用 | 否 |
prohibit-role-wildcard-access | 5.1.3 | 限制在“Roles”和“ClusterRoles”中使用通配符 | 否 |
psp-privileged-container | 5.2.1 | 尽可能减少特权容器的准许 | 否 |
psp-host-namespace | 5.2.2 | 尽可能减少希望共享主机进程 ID 命名空间的容器的准许 | 否 |
5.2.3 | 尽可能减少希望共享主机 IPC 命名空间的容器的准许 | 否 | |
psp-host-network-ports | 5.2.4 | 尽可能减少希望共享主机网络命名空间的容器的准许 | 否 |
psp-allow-privilege-escalation-container | 5.2.5 | 尽可能减少具有 allowPrivilegeEscalation 的容器的准许 | 否 |
psp-pods-must-run-as-nonroot | 5.2.6 | 尽可能减少根容器的准许 | 否 |
psp-capabilities | 5.2.7 | 尽可能减少具有 NET_RAW 功能的容器的准许 | 否 |
5.2.8 | 尽可能减少具有附加功能的容器的准许 | 否 | |
5.2.9 | 尽可能减少具有分配的功能的容器的准许 | 否 | |
require-namespace-network-policies | 5.3.2 | 确保所有命名空间都定义了网络政策 | 是 |
no-secrets-as-env-vars | 5.4.1 | 优先将 Secret 用作文件而不是用作环境变量 | 否 |
psp-seccomp-default | 5.7.2 | 确保在 pod 定义中将 seccomp 配置文件设置为 docker/default | 否 |
pods-require-security-context | 5.7.3 | 将安全上下文应用到您的 Pod 和容器 | 否 |
准备工作
- 安装并初始化 Google Cloud CLI,它提供了以下说明中使用的
gcloud
和kubectl
命令。如果您使用 Cloud Shell,则系统会预安装 Google Cloud CLI。 - 在集群上安装政策控制器以及默认的限制条件模板库。您还必须启用对参照限制条件的支持,因为此捆绑包包含一览表中列出的参照限制条件。
为参照限制条件配置政策控制器
在文件中,将以下 YAML 清单保存为
policycontroller-config.yaml
。清单将政策控制器配置为监视特定种类的对象。apiVersion: config.gatekeeper.sh/v1alpha1 kind: Config metadata: name: config namespace: "gatekeeper-system" spec: sync: syncOnly: - group: "" version: "v1" kind: "Namespace" - group: "networking.k8s.io" version: "v1" kind: "NetworkPolicy"
应用
policycontroller-config.yaml
清单:kubectl apply -f policycontroller-config.yaml
使用政策控制器审核 CIS 政策
政策控制器允许您为 Kubernetes 集群强制执行政策。为了帮助测试您的工作负载及其对上表中列出的 CIS 政策的合规性,您可以在“审核”模式下部署这些限制条件以发现违规行为,更重要的是让您有机会在 Kubernetes 集群上强制执行之前处理这些违规行为。
您可以在 spec.enforcementAction
设置为 dryrun
的情况下,使用 kubectl、kpt 或 Config Sync 应用这些政策。
kubectl
(可选)使用 kubectl 预览政策限制条件:
kubectl kustomize https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1
使用 kubectl 应用政策限制条件:
kubectl apply -k https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1
输出如下所示:
k8snoenvvarsecrets.constraints.gatekeeper.sh/cis-k8s-v1.5.1-no-secrets-as-env-vars created k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-allow-privilege-escalation created k8spspallowedusers.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-pods-must-run-as-nonroot created k8spspcapabilities.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-capabilities created k8spsphostnamespace.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-namespace created k8spsphostnetworkingports.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-network-ports created k8spspprivilegedcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-privileged-container created k8spspseccomp.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-seccomp-default created k8spodsrequiresecuritycontext.constraints.gatekeeper.sh/cis-k8s-v1.5.1-pods-require-security-context created k8sprohibitrolewildcardaccess.constraints.gatekeeper.sh/cis-k8s-v1.5.1-prohibit-role-wildcard-access created k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/cis-k8s-v1.5.1-require-namespace-network-policies created k8srestrictrolebindings.constraints.gatekeeper.sh/cis-k8s-v1.5.1-restrict-clusteradmin-rolebindings created
验证政策限制条件是否已安装,并检查集群中是否存在违规行为:
kubectl get -k https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1
输出类似于以下内容:
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8snoenvvarsecrets.constraints.gatekeeper.sh/cis-k8s-v1.5.1-no-secrets-as-env-vars dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-allow-privilege-escalation dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspallowedusers.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-pods-must-run-as-nonroot dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspcapabilities.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-capabilities dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spsphostnamespace.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-namespace dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spsphostnetworkingports.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-network-ports dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspprivilegedcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-privileged-container dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspseccomp.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-seccomp-default dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spodsrequiresecuritycontext.constraints.gatekeeper.sh/cis-k8s-v1.5.1-pods-require-security-context dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8sprohibitrolewildcardaccess.constraints.gatekeeper.sh/cis-k8s-v1.5.1-prohibit-role-wildcard-access dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/cis-k8s-v1.5.1-require-namespace-network-policies dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8srestrictrolebindings.constraints.gatekeeper.sh/cis-k8s-v1.5.1-restrict-clusteradmin-rolebindings dryrun 0
kpt
安装并设置 kpt。在这些说明中,kpt 用于自定义和部署 Kubernetes 资源。
使用 kpt 从 GitHub 下载 CIS 政策软件包:
kpt pkg get https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1
运行
set-enforcement-action
kpt 函数将政策的强制执行操作设置为dryrun
:kpt fn eval cis-k8s-v1.5.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 \ -- enforcementAction=dryrun
使用 kpt 初始化工作目录,此操作会创建一个资源来跟踪更改:
cd cis-k8s-v1.5.1 kpt live init
使用 kpt 应用政策限制条件:
kpt live apply
输出如下所示:
k8spspseccomp.constraints.gatekeeper.sh/psp-seccomp-docker-default created k8spodsrequiresecuritycontext.constraints.gatekeeper.sh/pods-require-security-context created k8sprohibitrolewildcardaccess.constraints.gatekeeper.sh/prohibit-role-wildcard-access created k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/require-namespace-network-policies created k8snoenvvarsecrets.constraints.gatekeeper.sh/no-secrets-as-env-vars created k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/psp-allow-privilege-escalation-container created k8spspallowedusers.constraints.gatekeeper.sh/psp-pods-must-run-as-nonroot created k8spsphostnetworkingports.constraints.gatekeeper.sh/psp-host-network-ports created k8srestrictrolebindings.constraints.gatekeeper.sh/restrict-clusteradmin-rolebindings created k8spspcapabilities.constraints.gatekeeper.sh/psp-capabilities created k8spsphostnamespace.constraints.gatekeeper.sh/psp-host-namespace created k8spspprivilegedcontainer.constraints.gatekeeper.sh/psp-privileged-container created 13 resource(s) applied. 13 created, 0 unchanged, 0 configured, 0 failed
验证政策限制条件是否已安装,并检查集群中是否存在违规行为:
kpt live status --output table --poll-until current
状态为
CURRENT
,即确认限制条件成功安装。
Config Sync
安装并设置 kpt。在这些说明中,kpt 用于自定义和部署 Kubernetes 资源。
如果您是第一次使用 Anthos Config Management,请启用 Anthos Config Management。
使用 Config Sync 将政策部署到其集群的运维人员可以按照以下说明操作:
更改为 Config Sync 的同步目录:
cd SYNC_ROOT_DIR
如需使用
resourcegroup.yaml
创建或附加.gitignore
,请执行以下操作:echo resourcegroup.yaml >> .gitignore
创建一个专用的
policies
目录:mkdir -p policies
使用 kpt 从 GitHub 下载 CIS 政策软件包:
kpt pkg get https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1 policies/cis-k8s-v1.5.1
运行
set-enforcement-action
kpt 函数将政策的强制执行操作设置为dryrun
:kpt fn eval policies/cis-k8s-v1.5.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=dryrun
(可选)预览将创建的政策限制条件:
kpt live init policies/cis-k8s-v1.5.1 kpt live apply --dry-run policies/cis-k8s-v1.5.1
输出如下所示:
Dry-run strategy: client inventory update started inventory update finished apply phase started k8snoenvvarsecrets.constraints.gatekeeper.sh/cis-k8s-v1.5.1-no-secrets-as-env-vars apply successful k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-allow-privilege-escalation apply successful k8spspallowedusers.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-pods-must-run-as-nonroot apply successful k8spspcapabilities.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-capabilities apply successful k8spsphostnamespace.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-namespace apply successful k8spsphostnetworkingports.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-network-ports apply successful k8spspprivilegedcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-privileged-container apply successful k8spspseccomp.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-seccomp-default apply successful k8spodsrequiresecuritycontext.constraints.gatekeeper.sh/cis-k8s-v1.5.1-pods-require-security-context apply successful k8sprohibitrolewildcardaccess.constraints.gatekeeper.sh/cis-k8s-v1.5.1-prohibit-role-wildcard-access apply successful k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/cis-k8s-v1.5.1-require-namespace-network-policies apply successful k8srestrictrolebindings.constraints.gatekeeper.sh/cis-k8s-v1.5.1-restrict-clusteradmin-rolebindings apply successful apply phase finished inventory update started inventory update finished apply result: 12 attempted, 12 successful, 0 skipped, 0 failed
如果 Config Sync 的同步目录使用 Kustomize,请将
policies/cis-k8s-v1.5.1
添加到根kustomization.yaml
。否则,请移除policies/cis-k8s-v1.5.1/kustomization.yaml
文件:rm SYNC_ROOT_DIR/policies/cis-k8s-v1.5.1/kustomization.yaml
将更改推送到 Config Sync 代码库:
git add SYNC_ROOT_DIR/policies/cis-k8s-v1.5.1 git commit -m 'Adding CIS policy audit enforcement' git push
验证安装状态:
watch gcloud beta container fleet config-management status --project PROJECT_ID
状态
SYNCED
用于确认政策的安装情况。
查看违规问题
在审核模式下安装政策限制条件后,您可以使用政策控制器信息中心在界面中查看集群的违规问题。
您还可以使用 kubectl
来通过以下命令查看针对集群的违规行为:
kubectl get constraint -l policycontroller.gke.io/bundleName=cis-k8s-v1.5.1 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]'
如果存在违规行为,您可以使用以下命令查看每个限制条件的违规消息列表:
kubectl get constraint -l policycontroller.gke.io/bundleName=cis-k8s-v1.5.1 -o json | jq -C '.items[]| select(.status.totalViolations>0)| [.metadata.name,.status.violations[]?]'
实施 CIS 政策
查看集群中的政策违规后,下一步就是强制执行这些政策,以使准入控制器阻止任何不合规的资源应用于集群。
kubectl
使用 kubectl 将政策的强制执行操作设置为
deny
:kubectl get constraint -l policycontroller.gke.io/bundleName=cis-k8s-v1.5.1 -o json | jq '.items[].spec.enforcementAction="deny"' | kubectl apply -f -
验证政策限制条件强制执行措施是否已更新:
kubectl get -k https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1
kpt
运行
set-enforcement-action
kpt 函数将政策的强制执行操作设置为deny
:kpt fn eval -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=deny
应用政策限制条件:
kpt live apply
Config Sync
使用 Config Sync 将政策部署到其集群的运维人员可以按照以下说明操作:
更改为 Config Sync 的同步目录:
cd SYNC_ROOT_DIR
运行
set-enforcement-action
kpt 函数将政策的强制执行操作设置为deny
:kpt fn eval policies/cis-k8s-v1.5.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=deny
将更改推送到 Config Sync 代码库:
git add SYNC_ROOT_DIR/policies/cis-k8s-v1.5.1 git commit -m 'Enforcing CIS policies for GKE' git push
验证安装状态:
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
准入控制器应生成一个错误,其中列出了此资源违反政策的行为。 例如:
Error from server (Forbidden): error when creating "STDIN": admission webhook "validation.gatekeeper.sh" denied the request: [cis-k8s-v1.5.1-psp-pods-must-run-as-nonroot] Container wordpress is attempting to run without a required securityContext/runAsNonRoot or securityContext/runAsUser != 0 [cis-k8s-v1.5.1-psp-allow-privilege-escalation] Privilege escalation container is not allowed: wordpress [cis-k8s-v1.5.1-psp-seccomp-default] Seccomp profile 'not configured' is not allowed for container 'wordpress'. Found at: no explicit profile found. Allowed profiles: {"RuntimeDefault", "docker/default", "runtime/default"} [cis-k8s-v1.5.1-psp-capabilities] containeris not dropping all required capabilities. Container must drop all of ["NET_RAW"] or "ALL" [cis-k8s-v1.5.1-pods-require-security-context] securityContext must be defined for all Pod containers