Security Context
概述
Security Context(安全上下文)用来限制容器对宿主节点的可访问范围,以避免容器非法操作宿主节点的系统级别的内容,使得节点的系统或者节点上其他容器组受到影响。
Security Context可以按照如下几种方式设定:
- 访问权限控制:是否可以访问某个对象(例如文件)是基于 userID(UID)和 groupID(GID)的
- Security Enhanced Linux (SELinux):为对象分配Security标签
- 以 privileged(特权)模式运行
- Linux Capabilities:为容器组(或容器)分配一部分特权,而不是 root 用户的所有特权
- AppArmor:自 Kubernetes v1.4 以来,一直处于 beta 状态
- Seccomp:过滤容器中进程的系统调用(system call)
- AllowPrivilegeEscalation(允许特权扩大):此项配置是一个布尔值,定义了一个进程是否可以比其父进程获得更多的特权,直接效果是,容器的进程上是否被设置 no_new_privs标记。当出现如下情况时,AllowPrivilegeEscalation 的值始终为 true:
- 容器以 privileged 模式运行
- 容器拥有 CAP_SYS_ADMIN 的 Linux Capability
为Pod设置Security Context
在 Pod 的定义中增加 securityContext 字段,即可为 Pod 指定 Security 相关的设定。 securityContext 字段是一个 PodSecurityContext 对象。通过该字段指定的内容将对该 Pod 中所有的容器生效
Pod示例
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
volumes:
- name: sec-ctx-vol
emptyDir: {}
containers:
- name: sec-ctx-demo
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
volumeMounts:
- name: sec-ctx-vol
mountPath: /data/demo
securityContext:
allowPrivilegeEscalation: false
- spec.securityContext.runAsUser 字段指定了该 Pod 中所有容器的进程都以UserID 1000 的身份运行,spec.securityContext.runAsGroup 字段指定了该 Pod 中所有容器的进程都以GroupID 3000 的身份运行
- 如果该字段被省略,容器进程的GroupID为 root(0)
- 容器中创建的文件,其所有者为 userID 1000,groupID 3000
- spec.securityContext.fsGroup 字段指定了该 Pod 的 fsGroup 为 2000
- 数据卷 (本例中,对应挂载点 /data/demo 的数据卷为 sec-ctx-demo) 的所有者以及在该数据卷下创建的任何文件,其 GroupID 为 2000
为容器设置Security Context
容器的定义中包含 securityContext 字段,该字段接受 SecurityContext对象。通过指定该字段,可以为容器设定安全相关的配置,当该字段的配置与 Pod 级别的 securityContext 配置相冲突时,容器级别的配置将覆盖 Pod 级别的配置。容器级别的 securityContext 不影响 Pod 中的数据卷。
下面的示例中的 Pod 包含一个 Container,且 Pod 和 Container 都有定义 securityContext 字段:
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo-2
spec:
securityContext:
runAsUser: 1000
containers:
- name: sec-ctx-demo-2
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
securityContext:
runAsUser: 2000
allowPrivilegeEscalation: false
请注意,容器的进程以 userID 2000 的身份运行。该取值由 spec.containers[*].securityContext.runAsUser 容器组中的字段定义。Pod 中定义的 spec.securityContext.runAsUser 取值 1000 被覆盖。
为容器设置Linux Capabilities
使用 Linux Capabilities 可以为容器内的进程授予某些特定的权限(而不是 root 用户的所有权限)。在容器定义的 securityContext 中添加 capabilities 字段,可以向容器添加或删除 Linux Capability。
先运行一个不包含 capabilities 字段的容器,观察容器内进程的 linux capabilities 位图的情况;然后在运行一个包含 capabilities 字段的容器,比较其 linux capabilities 位图与前者的不同。
无capabilities字段时
先确认在没有 capabilities 字段时,容器的行为是怎样的。下面的例子中包含一个容器,我们没有为其添加或删除任何 Linux capability。
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo-3
spec:
containers:
- name: sec-ctx-demo-3
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
在容器的命令行界面中查看 process 1 的状态
# cat /proc/1/status
CapPrm: 00000000a80425fb
CapEff: 00000000a80425fb
有capabilities字段时
运行同样的一个容器,不同的是,这次为其设置了 capabilities 字段。下面是 yaml 配置文件,该配置中为进程添加了两个 Linux Capability: CAP_NET_ADMIN 和 CAP_SYS_TIME:
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo-4
spec:
containers:
- name: sec-ctx-demo-4
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
securityContext:
capabilities:
add: ["NET_ADMIN", "SYS_TIME"]
在容器的命令行界面中查看 process 1 的状态
# cat /proc/1/status
CapPrm: 00000000aa0435fb
CapEff: 00000000aa0435fb
比较两次运行,进程的 Linux Capabilities 位图的差异:
第一次运行:00000000a80425fb
第二次运行:00000000aa0435fb
第一次运行时,位图的 12 位和 25 为是 0。第二次运行时,12 位和 25 位是 1.查看 Linux Capabilities 的常量定义文件 capability.h 可知:12 位代表 CAP_NET_ADMIN,25 位代表 CAP_SYS_TIME。
LinuxCapability常量
Linux Capabilities 常量格式为 CAP_XXX。然而,在容器定义中添加或删除 Linux Capabilities 时,必须去除常量的前缀 CAP_。例如:向容器中添加 CAP_SYS_TIME 时,只需要填写 SYS_TIME。
为容器设置SELinux标签
Pod 或容器定义的 securityContext 中 seLinuxOptions 字段是一个 SELinuxOptions 对象,该字段可用于为容器指定 SELinux 标签。
securityContext:
seLinuxOptions:
level: "s0:c123,c456"
为容器指定 SELinux 标签时,宿主节点的 SELinux 模块必须加载。
Kuboard中Pod的Security Context
通过 Kuboard,可以直接设定 Deployment、StatefulSet、DaemonSet 等中 Pod 模板的 securityContext 的内容。在 Kuboard 工作负载编辑器界面中切换到 高级设置 标签页,并勾选 容器组安全上下文 ,可查看到 Pod 的 Security Context 设置界面,如下图所示:

各个字段的含义逐个解释如下:

Kuboard中容器的Security Context
在 Kuboard 工作负载编辑器界面中切换到 容器信息 标签页,并勾选 容器安全上下文 ,可查看到容器的 Security Context 设置界面,如下图所示:

各个字段的含义逐个解释如下:

Security Context的更多相关文章
- The server principal "sa" is not able to access the database "xxxx" under the current security context
在SQL Server服务器上一个作业执行时,遇到下面错误信息: Message: Executed as user: dbo. The server principal "sa" ...
- SQL Agent Job 报“Access to the remote server is denied because the current security context is not trusted”
SQL Server 2005(Microsoft SQL Server 2005 - 9.00.5000.00)下的一个作业执行一个存储过程,存储过程中动态SQL语句使用链接服务器(Linked S ...
- Page Security
参见开发文档 Overview This document describes how to build applications that grant selected access to indi ...
- openstack context
之前一直不知道context模块中存储的是什么东西,这回看一下代码: 其中最主要的类是:RequestContext: class RequestContext(object): "&q ...
- Unable to get valid context for root
登陆时报以下错误Unable to get valid context for rootLast login: Wed Jul 24 02:06:01 2013 from 10.64.41.3 单机模 ...
- Spring源码解析之:Spring Security启动细节和工作模式--转载
原文地址:http://blog.csdn.net/bluishglc/article/details/12709557 Spring-Security的启动加载细节 Spring-Securit ...
- 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
- WebLogic: The Definitive Guide examined WebLogic's security mechanisms--reference
reference from: http://www.onjava.com/pub/a/onjava/excerpt/weblogic_chap17/index1.html?page=1 ...... ...
- 网络负载均衡环境下wsHttpBinding+Message Security+Windows Authentication的常见异常
提高Windows Communication Foundation (WCF) 应用程序负载能力的方法之一就是通过把它们部署到负载均衡的服务器场中. 其中可以使用标准的负载均衡技术, Windows ...
随机推荐
- 👨💻Mybatis源码我搞透了,面试来问吧!写了134个源码类,1.03万行代码!
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言:手撸一万行! 完结撒花:4个月.20章.134个类.1.03万行代码! 22年3月初 ...
- 从编译器对指令集的要求看API设计原则
摘要:最近看<计算机体系结构:量化研究方法(第五版)>,发现指令集设计中的一些原则,对API设计也同样适用,给大家分享一下. 本文中的所有内容来自工作和学习过程中的心得整理,如需转载请注明 ...
- 彻底理解DDS(信号发生器)的fpga实现(verilog设计代码)
DDS(Direct Digital Synthesis)是一种把一系列数字信号通过D/A转换器转换成模拟信号的数字合成技术. 它有查表法和计算法两种基本合成方法.在这里主要记录DDS查表法的fpga ...
- Java的基础语法01
一. 注释,标识符,关键字 书写注释是一种习惯的养成,当我们一段代码完成后,长时间没有回顾,便会产生遗忘,所以注释是给我们写代码的人看的.1.注释 //单行注释 /*多行注释*/ /**文档注释也叫文 ...
- 算法竞赛进阶指南 0x50 总论
目录 AcWing895. 最长上升子序列 方法一 方法二 当询问最长子序列是哪些的时候 896. 最长上升子序列 II 思路 O(NlogN)做法:贪心+二分 代码 AcWing\897. 最长公共 ...
- C#程序设计的6大原则
设计模式:面向对象语言开发过程中,遇到各种场景和问题,解决方案和思路沉淀下来,就是设计模式.俗称,套路 设计模式的六大原则:理解为面向对象语言开发过程中推荐的一些指导性的原则,通俗的说是套路的 套路 ...
- 闭包类型(Fn,FnMut,FnOnce)和move关键字
move关键字是强制让环境变量的所有权转移到闭包中而不管是不是发生了所有权的转移 move关键字和匿名函数是否是FnOnce没有必然联系,之和匿名函数体有关 当匿名函数体里转移了环境变量的所有权的时候 ...
- Go语言基础二:常用的Go工具命令
常用的Go工具命令 Go附带了一下有用的命令,这些命令可以简化开发的过程.命令通常包含的IDE中,从而使工具在整个开发环境中保持一致. go run 命令 go run命令实在开发过程中执行的最常见的 ...
- linux常见命令搜集
查找根目录下txt和pdf文件 find / \( -name "*.txt" -o -name "*.pdf" \) -print 正则查找根目录下所有的tx ...
- Mybatis的使用(3)
1:动态sql: 意义:可以定义代码片段,可以进行逻辑判断,可以进行循环或批量处理,使条件判断更为简单 1.1:定义代码片段简化代码: 1.2:多条件查询: <where> <if ...