使用Kubesec檢查YAML檔案安全

人生的哲理發表於2024-06-22

目錄
  • 一.系統環境
  • 二.前言
  • 三.Kubesec簡介
  • 四.使用Kubesec檢查YAML檔案安全
  • 五.總結

一.系統環境

本文主要基於Kubernetes1.22.2和Linux作業系統Ubuntu 18.04。

伺服器版本 docker軟體版本 Kubernetes(k8s)叢集版本 CPU架構
Ubuntu 18.04.5 LTS Docker version 20.10.14 v1.22.2 x86_64

Kubernetes叢集架構:k8scludes1作為master節點,k8scludes2,k8scludes3作為worker節點。

伺服器 作業系統版本 CPU架構 程序 功能描述
k8scludes1/192.168.110.128 Ubuntu 18.04.5 LTS x86_64 docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico k8s master節點
k8scludes2/192.168.110.129 Ubuntu 18.04.5 LTS x86_64 docker,kubelet,kube-proxy,calico k8s worker節點
k8scludes3/192.168.110.130 Ubuntu 18.04.5 LTS x86_64 docker,kubelet,kube-proxy,calico k8s worker節點

二.前言

在當今的雲端計算時代,Kubernetes已經成為容器化應用部署和管理的事實標準。然而,隨著Kubernetes叢集的規模不斷擴大,如何確保叢集的安全性成為了一個重要的問題。YAML檔案是Kubernetes配置的主要載體,因此,檢查YAML檔案的安全性對於確保Kubernetes叢集的安全至關重要。本文將介紹如何使用Kubesec工具來檢查YAML檔案的安全性。

使用Kubesec檢查YAML檔案安全的前提是已經有一套可以正常執行的Kubernetes叢集,關於Kubernetes(k8s)叢集的安裝部署,可以檢視部落格《Ubuntu 安裝部署Kubernetes(k8s)叢集》https://www.cnblogs.com/renshengdezheli/p/17632858.html。

三.Kubesec簡介

Kubesec是一個開源的Kubernetes安全評估工具,由controlplaneio公司開發。它可以幫助使用者發現和修復Kubernetes配置檔案中的安全問題,從而提高叢集的安全性。Kubesec透過分析YAML檔案中的配置資訊,識別潛在的安全風險,並提供修復建議。Kubesec官網為:https://kubesec.io/ ,在github上的網址為:https://github.com/controlplaneio/kubesec ,Kubesec安裝包下載網址為:https://github.com/controlplaneio/kubesec/releases
Kubesec基於一組安全最佳實踐和規則,對YAML檔案進行深度分析。它採用了多種技術手段,包括正規表示式、字串匹配和模式匹配等,來識別不安全配置。Kubesec還對Kubernetes API進行監控,以確保配置檔案與API的相容性。

四.使用Kubesec檢查YAML檔案安全

使用--dry-run=client生成一份建立pod的yaml檔案,--dry-run 表示模擬執行,並不會真的建立一個pod , --dry-run=client輸出資訊少 ,--dry-run=server輸出資訊多, -o yaml表示以yaml檔案的格式輸出。

生成的pod yaml檔案是最基本的yaml檔案,很多安全設定並沒有設定。

root@k8scludes1:~# kubectl run podyamlsafe --image=hub.c.163.com/library/centos:latest --image-pull-policy=IfNotPresent --dry-run=client -o yaml >podyamlsafe.yaml

下載好kubesec安裝包。

root@k8scludes1:~# ll -h kubesec_linux_amd64.tar.gz 
-rw-r--r-- 1 root root 3.9M Jun 18 22:27 kubesec_linux_amd64.tar.gz

解壓安裝包。

root@k8scludes1:~# tar xf kubesec_linux_amd64.tar.gz 

root@k8scludes1:~# ll -h kubesec*
-rwxr-xr-x 1 1001 docker  11M Sep 22  2021 kubesec*
-rw-r--r-- 1 root root   3.9M Jun 18 22:27 kubesec_linux_amd64.tar.gz

移動kubesec可執行檔案到/usr/local/bin/目錄下。

root@k8scludes1:~# mv kubesec /usr/local/bin/

使用kubesec掃描yaml檔案,顯示出來的就是提高pod安全性的意見。

可以看到給出了很多安全建議,比如設定AppArmor策略,配置ServiceAccount,還有Seccomp系統呼叫等等。

root@k8scludes1:~# kubesec scan podyamlsafe.yaml 
[
  {
    "object": "Pod/podyamlsafe.default",
    "valid": true,
    "fileName": "podyamlsafe.yaml",
    "message": "Passed with a score of 0 points",
    "score": 0,
    "scoring": {
      "advise": [
        {
          "id": "ApparmorAny",
          "selector": ".metadata .annotations .\"container.apparmor.security.beta.kubernetes.io/nginx\"",
          "reason": "Well defined AppArmor policies may provide greater protection from unknown threats. WARNING: NOT PRODUCTION READY",
          "points": 3
        },
        {
          "id": "ServiceAccountName",
          "selector": ".spec .serviceAccountName",
          "reason": "Service accounts restrict Kubernetes API access and should be configured with least privilege",
          "points": 3
        },
        {
          "id": "SeccompAny",
          "selector": ".metadata .annotations .\"container.seccomp.security.alpha.kubernetes.io/pod\"",
          "reason": "Seccomp profiles set minimum privilege and secure against unknown threats",
          "points": 1
        },
        {
          "id": "LimitsCPU",
          "selector": "containers[] .resources .limits .cpu",
          "reason": "Enforcing CPU limits prevents DOS via resource exhaustion",
          "points": 1
        },
        {
          "id": "LimitsMemory",
          "selector": "containers[] .resources .limits .memory",
          "reason": "Enforcing memory limits prevents DOS via resource exhaustion",
          "points": 1
        },
        {
          "id": "RequestsCPU",
          "selector": "containers[] .resources .requests .cpu",
          "reason": "Enforcing CPU requests aids a fair balancing of resources across the cluster",
          "points": 1
        },
        {
          "id": "RequestsMemory",
          "selector": "containers[] .resources .requests .memory",
          "reason": "Enforcing memory requests aids a fair balancing of resources across the cluster",
          "points": 1
        },
        {
          "id": "CapDropAny",
          "selector": "containers[] .securityContext .capabilities .drop",
          "reason": "Reducing kernel capabilities available to a container limits its attack surface",
          "points": 1
        },
        {
          "id": "CapDropAll",
          "selector": "containers[] .securityContext .capabilities .drop | index(\"ALL\")",
          "reason": "Drop all capabilities and add only those required to reduce syscall attack surface",
          "points": 1
        },
        {
          "id": "ReadOnlyRootFilesystem",
          "selector": "containers[] .securityContext .readOnlyRootFilesystem == true",
          "reason": "An immutable root filesystem can prevent malicious binaries being added to PATH and increase attack cost",
          "points": 1
        },
        {
          "id": "RunAsNonRoot",
          "selector": "containers[] .securityContext .runAsNonRoot == true",
          "reason": "Force the running image to run as a non-root user to ensure least privilege",
          "points": 1
        },
        {
          "id": "RunAsUser",
          "selector": "containers[] .securityContext .runAsUser -gt 10000",
          "reason": "Run as a high-UID user to avoid conflicts with the host's user table",
          "points": 1
        }
      ]
    }
  }
]

五.總結

使用Kubesec檢查YAML檔案安全性是一個簡單而有效的方法,可以幫助識別和修復潛在的安全問題。透過遵循Kubesec的修復建議,可以顯著提高Kubernetes叢集的安全性。

相關文章