12.3. 安全性上下文约束示例
以下示例演示了如何在集群中定义和使用安全性上下文约束(SCC)。
注解 privileged SCC
allowHostDirVolumePlugin: true
allowHostIPC: true
allowHostNetwork: true
allowHostPID: true
allowHostPorts: true
allowPrivilegedContainer: true
allowedCapabilities:
- '*'
apiVersion: security.openshift.io/v1
defaultAddCapabilities: []
fsGroup:
type: RunAsAny
groups:
- system:cluster-admins
- system:nodes
kind: SecurityContextConstraints
metadata:
annotations:
kubernetes.io/description: 'privileged allows access to all privileged and host
features and the ability to run as any user, any group, any fsGroup, and with
any SELinux context. WARNING: this is the most relaxed SCC and should be used
only for cluster administration. Grant with caution.'
creationTimestamp: null
name: privileged
priority: null
readOnlyRootFilesystem: false
requiredDropCapabilities: null
runAsUser:
type: RunAsAny
seLinuxContext:
type: RunAsAny
seccompProfiles:
- '*'
supplementalGroups:
type: RunAsAny
users:
- system:serviceaccount:default:registry
- system:serviceaccount:default:router
- system:serviceaccount:openshift-infra:build-controller
volumes:
- '*'
- 其中,
allowedCapabilities -
Pod 可以请求的功能列表。空列表表示不允许请求任何功能,而特殊符号
*则允许任何功能。 defaultAddCapabilities- 添加至任何 Pod 的附加功能列表。
fsGroup-
FSGroup策略,指明安全性上下文的允许值。 groups- 可访问此 SCC 的组。
requiredDropCapabilities-
从 pod 丢弃的功能列表。或者,指定
ALL以丢弃所有功能。 runAsUser-
runAsUser策略类型,指明安全上下文的允许值。 seLinuxContext-
seLinuxContext策略类型,指明安全上下文的允许值。 supplementalGroups-
supplementalGroups策略,指明安全上下文的允许补充组。 users- 可访问此 SCC 的用户。
卷-
安全上下文允许的卷类型。在这个示例中,
*允许使用所有卷类型。
SCC 中的 users 和 groups 字段控制能够访问该 SCC 的用户。默认情况下,集群管理员、节点和构建控制器被授予特权 SCC 的访问权限。所有经过身份验证的用户都授予了访问 restricted-v2 的权限。
在验证对 SCC 的访问时,请注意以下命令的行为:
-
oc adm policy who-can use scc <scc_name>和oc auth can-i use scc/<scc_name> 命令仅评估 RBAC 策略(RoleBinding或ClusterRoleBinding资源)。它们的输出不包括直接在 SCC字段中配置的用户或组。用户和组 -
oc describe scc <scc_name> 命令只显示直接在 SCC 对象中配置的用户和组。其输出不包括通过 RBAC 策略授予访问权限。
无显式 runAsUser 设置
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
containers:
- name: sec-ctx-demo
image: gcr.io/google-samples/node-hello:1.0
当容器或 pod 没有请求运行它的用户 ID 时,有效的 UID 取决于发出此 Pod 的 SCC。由于在默认情况下,restricted-v2 SCC 会授权给所有经过身份验证的用户,所以它可供所有用户和服务帐户使用,并在大多数情形中使用。restricted-v2 SCC 使用 MustRunAsRange 策略来约束并默认分配 securityContext.runAsUser 字段的可能值。准入插件会在当前项目上查找 openshift.io/sa.scc.uid-range 注解来填充范围字段,因为它不提供此范围。最后,容器的 runAsUser 值等于这一范围中的第一个值,而这难以预测,因为每个项目都有不同的范围。
带有显式 runAsUser 设置
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
runAsUser: 1000
containers:
- name: sec-ctx-demo
image: gcr.io/google-samples/node-hello:1.0
只有服务帐户或用户被授予对允许某一用户 ID 的 SCC 访问权限时,Red Hat OpenShift Service on AWS 才会接受请求该用户 ID 的容器或 Pod。SCC 允许任意 ID、属于某一范围的 ID,或特定于请求的确切用户 ID。
此配置对 SELinux、fsGroup 和补充组有效。