POD 安全策略评估顺序(多个角色)



在 AWS EKS 上运行 1.15。

默认情况下,AWS 提供eks.privilegedPSP(在此处记录:https://docs.aws.amazon.com/eks/latest/userguide/pod-security-policy.html)。这将分配给所有经过身份验证的用户。

然后我创建了一个限制性更强的PSPeks.restricted

---
# restricted pod security policy
apiVersion: extensions/v1beta1
kind: PodSecurityPolicy
metadata:
creationTimestamp: null
labels:
kubernetes.io/cluster-service: "true"
eks.amazonaws.com/component: pod-security-policy
name: eks.restricted
spec:
allowPrivilegeEscalation: false
allowedCapabilities:
- '*'
fsGroup:
rule: RunAsAny
hostPorts:
- max: 65535
min: 0
runAsUser:
rule: MustRunAsNonRoot
seLinux:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
volumes:
- '*'

以上是一个非变异的PSP。我还修改了PSP的默认eks.privilged,通过添加以下注释使其修改

seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default

最后,我更新集群角色以添加我创建的新 PSP:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: eks:podsecuritypolicy:privileged
labels:
kubernetes.io/cluster-service: "true"
eks.amazonaws.com/component: pod-security-policy
rules:
- apiGroups:
- policy
resourceNames:
- eks.privileged
- eks.restricted
resources:
- podsecuritypolicies
verbs:
- use

这样做的目的是eks.restricted成为默认的PSP对它是非变异的事实所做的(https://v1-15.docs.kubernetes.io/docs/concepts/policy/pod-security-policy/#policy-order 并且列表的顺序无关紧要)。

真棒。但是我要完成的是创建一个默认为eks.restricted而所有其他命名空间默认为eks.privileged的命名空间。

我试图这样做。

首先,我从 ClusterRole 中删除了eks.restrictedeks:podsecuritypolicy:privileged以便 eks.privileged 现在是集群范围的默认值。在我的命名空间中,我创建了一个新角色

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
annotations:
labels:
eks.amazonaws.com/component: pod-security-policy
kubernetes.io/cluster-service: "true"
name: eks:podsecuritypolicy:restricted
rules:
- apiGroups:
- policy
resourceNames:
- eks.restricted
resources:
- podsecuritypolicies
verbs:
- use

此角色授予对 PSPeks.restricted的使用权。然后,我将这个新角色绑定到示例命名空间中的服务帐户。

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: psp-restricted
namespace: psp-example
roleRef:
kind: Role
name: eks:podsecuritypolicy:restricted
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: privileged-sa
namespace: psp-example

最后我创建了一个部署,它违反了使用此服务帐户的PSPeks.restricted

apiVersion: apps/v1
kind: Deployment
metadata:
name: centos-deployment
namespace: psp-example
labels:
app: centos
spec:
replicas: 3
selector:
matchLabels:
app: centos
template:
metadata:
labels:
app: centos
spec:
serviceAccountName: privileged-sa
containers:
- name: centos
#image: centos:centos7
image: datinc/permtest:0
command:
- '/bin/sleep'
- '60000'

我的假设是,这将像我在本文开头的初始示例/测试中一样起作用。我的合并访问权限是两个eks.privileged,因为它绑定到系统:经过身份验证的组,eks.restricted绑定到运行我的部署的服务帐户。由于eks.restricted是非变异的,因此它应该是适用的,因此它应该无法创建 POD。但事实并非如此。POD 启动得很好。

作为进一步的测试,我向 SA 角色(上面列出)添加了eks.privileged,希望它像我的原始示例一样运行。它没有,POD 创建得很好。

试图弄清楚这是为什么。

在 AWS 上,您的部署使用 命名空间kube-system中的 ServiceAccountreplicaset-controller,因此您需要将其从 ClusterRoleBindingeks:podsecuritypolicy:authenticated中删除或删除。

请查看本文以了解详细信息: https://dev.to/anupamncsu/pod-security-policy-on-eks-mp9

最新更新