加固指南旨在与特定版本的 CIS Kubernetes Benchmark,Kubernetes 和 Rancher 一起使用:

下面的安全加固指南是针对在生产环境的 Rancher v2.1.x 中使用 Kubernetes 1.11 版本的集群。它概述了如何满足互联网安全中心(CIS)提出的 Kubernetes 安全标准。

有关如果根据官方 CIS 基准评估集群的更多详细信息,请参阅CIS Benchmark Rancher 自测指南 - Rancher v2.1

The following profile definitions agree with the CIS benchmarks for Kubernetes.

A profile is a set of configurations that provide a certain amount of hardening. Generally, the more hardened an environment is, the more it affects performance.

Items in this profile intend to:

  • offer practical advice appropriate for the environment;
  • deliver an obvious security benefit; and
  • not alter the functionality or utility of the environment beyond an acceptable margin

Level 2

Items in this profile extend the “Level 1” profile and exhibit one or more of the following characteristics:

  • are intended for use in environments or use cases where security is paramount
  • act as a defense in depth measure
  • may negatively impact the utility or performance of the technology

1.1 - Rancher HA Kubernetes cluster host configuration

1.1.1 - Configure default sysctl settings on all hosts

Profile Applicability

  • Level 1

Description

Configure sysctl settings to match what the kubelet would set if allowed.

Rationale

We recommend that users launch the kubelet with the option. The settings that the kubelet initially attempts to change can be set manually.

This supports the following control:

  • 2.1.7 - Ensure that the --protect-kernel-defaults argument is set to true (Scored)

Audit

  • Verify vm.overcommit_memory = 1
  • Verify kernel.panic = 10
  1. sysctl kernel.panic
  • Verify kernel.panic_on_oops = 1
  1. sysctl kernel.panic_on_oops

Remediation

  • Set the following parameters in /etc/sysctl.conf on all nodes:
  1. vm.overcommit_memory=1
  2. kernel.panic=10
  3. kernel.panic_on_oops=1
  • Run sysctl -p to enable the settings.

1.1.2 - Install the encryption provider configuration on all control plane nodes

Profile Applicability

  • Level 1

Description

Create a Kubernetes encryption configuration file on each of the RKE nodes that will be provisioned with the controlplane role:

Rationale

This configuration file will ensure that the Rancher RKE cluster encrypts secrets at rest, which Kubernetes does not do by default.

This supports the following controls:

  • 1.1.34 - Ensure that the --experimental-encryption-provider-config argument is set as appropriate (Scored)
  • 1.1.35 - Ensure that the encryption provider is set to aescbc (Scored)

Audit

On the control plane hosts for the Rancher HA cluster run:

  1. stat /etc/kubernetes/encryption.yaml

Ensure that:

  • The file is present
  • The file mode is 0600
  • The file owner is root:root
  • The file contains:
  1. apiVersion: v1
  2. kind: EncryptionConfig
  3. resources:
  4. - resources:
  5. - secrets
  6. providers:
  7. - aescbc:
  8. keys:
  9. - name: key1
  10. secret: <32-byte base64 encoded string>
  11. - identity: {}

Where aescbc is the key type, and secret is populated with a 32-byte base64 encoded string.

Remediation

  • Generate a key and an empty configuration file:
  1. head -c 32 /dev/urandom | base64 -i -
  2. touch /etc/kubernetes/encryption.yaml
  • Set the file ownership to root:root and the permissions to 0600
  1. chown root:root /etc/kubernetes/encryption.yaml
  2. chmod 0600 /etc/kubernetes/encryption.yaml
  • Set the contents to:
  1. apiVersion: v1
  2. kind: EncryptionConfig
  3. resources:
  4. - resources:
  5. - secrets
  6. providers:
  7. - aescbc:
  8. keys:
  9. - name: key1
  10. secret: <32-byte base64 encoded string>
  11. - identity: {}

Where secret is the 32-byte base64-encoded string generated in the first step.

1.1.3 - Install the audit log configuration on all control plane nodes.

Profile Applicability

  • Level 1

Description

Place the configuration file for Kubernetes audit logging on each of the control plane nodes in the cluster.

Rationale

The Kubernetes API has audit logging capability that is the best way to track actions in the cluster.

This supports the following controls:

  • 1.1.15 - Ensure that the --audit-log-path argument is set as appropriate (Scored)
  • 1.1.16 - Ensure that the --audit-log-maxage argument is as appropriate (Scored)
  • 1.1.17 - Ensure that the --audit-log-maxbackup argument is set as appropriate (Scored)
  • 1.1.18 - Ensure that the --audit-log-maxsize argument is set as appropriate (Scored)
  • 1.1.37 - Ensure that the AdvancedAuditing argument is not set to false (Scored)

Audit

On each control plane node, run:

  1. stat /etc/kubernetes/audit.yaml

Ensure that:

  • The file is present
  • The file mode is 0600
  • The file owner is root:root
  • The file contains:
  1. apiVersion: audit.k8s.io/v1beta1
  2. kind: Policy
  3. rules:
  4. - level: Metadata

Remediation

On nodes with the controlplane role:

  • Generate an empty configuration file:
  1. touch /etc/kubernetes/audit.yaml
  • Set the file ownership to root:root and the permissions to 0600
  1. chown root:root /etc/kubernetes/audit.yaml
  2. chmod 0600 /etc/kubernetes/audit.yaml
  • Set the contents to:
  1. apiVersion: audit.k8s.io/v1beta1
  2. kind: Policy
  3. rules:
  4. - level: Metadata

Profile Applicability

  • Level 1

Description

Place the configuration file for Kubernetes event limit configuration on each of the control plane nodes in the cluster.

Rationale

Set up the EventRateLimit admission control plugin to prevent clients from overwhelming the API server. The settings below are intended as an initial value and may need to be adjusted for larger clusters.

This supports the following control:

  • 1.1.36 - Ensure that the admission control plugin EventRateLimit is set (Scored)

Audit

On nodes with the controlplane role run:

  1. stat /etc/kubernetes/admission.yaml
  2. stat /etc/kubernetes/event.yaml

For each file, ensure that:

  • The file is present
  • The file mode is 0600
  • The file owner is root:root

For admission.yaml ensure that the file contains:

  1. apiVersion: apiserver.k8s.io/v1alpha1
  2. kind: AdmissionConfiguration
  3. plugins:
  4. - name: EventRateLimit
  5. path: /etc/kubernetes/event.yaml

Remediation

On nodes with the controlplane role:

  • Generate an empty configuration file:
  1. touch /etc/kubernetes/admission.yaml
  2. touch /etc/kubernetes/event.yaml
  • Set the file ownership to root:root and the permissions to 0600
  1. chown root:root /etc/kubernetes/admission.yaml
  2. chown root:root /etc/kubernetes/event.yaml
  3. chmod 0600 /etc/kubernetes/admission.yaml
  4. chmod 0600 /etc/kubernetes/event.yaml
  • For admission.yaml set the contents to:
  1. apiVersion: apiserver.k8s.io/v1alpha1
  2. kind: AdmissionConfiguration
  3. plugins:
  4. - name: EventRateLimit
  5. path: /etc/kubernetes/event.yaml
  • For event.yaml set the contents to:
  1. apiVersion: eventratelimit.admission.k8s.io/v1alpha1
  2. kind: Configuration
  3. limits:
  4. - type: Server
  5. qps: 500
  6. burst: 5000

(See Appendix A. for full RKE cluster.yml example)

2.1.1 - Configure kubelet options

Profile Applicability

  • Level 1

Description

Ensure Kubelet options are configured to match CIS controls.

Rationale

To pass the following controls in the CIS benchmark, ensure the appropriate flags are passed to the Kubelet.

  • 2.1.6 - Ensure that the --streaming-connection-idle-timeout argument is not set to 0 (Scored)
  • 2.1.7 - Ensure that the --protect-kernel-defaults argument is set to true (Scored)
  • 2.1.8 - Ensure that the --make-iptables-util-chains argument is set to true (Scored)
  • 2.1.10 - Ensure that the --event-qps argument is set to 0 (Scored)

Audit

Inspect the Kubelet containers on all hosts and verify that they are running with the following options:

  • --protect-kernel-defaults=true
  • --make-iptables-util-chains=true
  • --event-qps=0

Remediation

  • Add the following to the RKE cluster.yml kubelet section under services:
  1. services:
  2. kubelet:
  3. extra_args:
  4. streaming-connection-idle-timeout: "<duration>"
  5. protect-kernel-defaults: "true"
  6. make-iptables-util-chains: "true"
  7. event-qps: "0"

Where <duration> is in a form like 1800s.

  • Reconfigure the cluster:

2.1.2 - Configure kube-api options

Profile Applicability

  • Level 1

Description

Ensure the RKE configuration is set to deploy the kube-api service with the options required for controls.

Rationale

To pass the following controls for the kube-api server ensure RKE configuration passes the appropriate options.

  • 1.1.1 - Ensure that the --anonymous-auth argument is set to false (Scored)
  • 1.1.8 - Ensure that the --profiling argument is set to false (Scored)
  • 1.1.9 - Ensure that the --repair-malformed-updates argument is set to false (Scored)
  • 1.1.11 - Ensure that the admission control plugin AlwaysPullImages is set (Scored)
  • 1.1.12 - Ensure that the admission control plugin DenyEscalatingExec is set (Scored)
  • 1.1.14 - Ensure that the admission control plugin NamespaceLifecycle is set (Scored)
  • 1.1.15 - Ensure that the --audit-log-path argument is set as appropriate (Scored)
  • 1.1.16 - Ensure that the --audit-log-maxage argument is set as appropriate (Scored)
  • 1.1.17 - Ensure that the --audit-log-maxbackup argument is set as appropriate (Scored)
  • 1.1.18 - Ensure that the --audit-log-maxsize argument is set as appropriate (Scored)
  • 1.1.23 - Ensure that the --service-account-lookup argument is set to true (Scored)
  • 1.1.24 - Ensure that the admission control plugin PodSecurityPolicy is set (Scored)
  • 1.1.34 - Ensure that the --experimental-encryption-provider-config argument is set as appropriate (Scored)
  • 1.1.35 - Ensure that the encryption provider is set to aescbc (Scored)
  • 1.1.36 - Ensure that the admission control plugin EventRateLimit is set (Scored)
  • 1.1.37 - Ensure that the AdvancedAuditing argument is not set to false (Scored)

Audit

  • On nodes with the controlplane role inspect the kube-apiserver containers:

    1. docker inspect kube-apiserver
  • Look for the following options in the command section of the output:

  1. --anonymous-auth=false
  2. --profiling=false
  3. --repair-malformed-updates=false
  4. --service-account-lookup=true
  5. --enable-admission-plugins= "ServiceAccount,NamespaceLifecycle,LimitRanger,PersistentVolumeLabel,DefaultStorageClass,ResourceQuota,DefaultTolerationSeconds,AlwaysPullImages,DenyEscalatingExec,NodeRestriction,EventRateLimit,PodSecurityPolicy"
  6. --experimental-encryption-provider-config=/etc/kubernetes/encryption.yaml
  7. --admission-control-config-file=/etc/kubernetes/admission.yaml
  8. --audit-log-path=/var/log/kube-audit/audit-log.json
  9. --audit-log-maxage=5
  10. --audit-log-maxbackup=5
  11. --audit-log-maxsize=100
  12. --audit-log-format=json
  13. --audit-policy-file=/etc/kubernetes/audit.yaml
  • In the volume section of the output ensure the bind mount is present:
  1. /var/log/kube-audit:/var/log/kube-audit

Remediation

  • In the RKE cluster.yml add the following directives to the kube-api section under services:
  1. services:
  2. kube-api:
  3. pod_security_policy: true
  4. extra_args:
  5. anonymous-auth: "false"
  6. profiling: "false"
  7. repair-malformed-updates: "false"
  8. service-account-lookup: "true"
  9. enable-admission-plugins: "ServiceAccount,NamespaceLifecycle,LimitRanger,PersistentVolumeLabel,DefaultStorageClass,ResourceQuota,DefaultTolerationSeconds,AlwaysPullImages,DenyEscalatingExec,NodeRestriction,EventRateLimit,PodSecurityPolicy"
  10. experimental-encryption-provider-config: /etc/kubernetes/encryption.yaml
  11. admission-control-config-file: "/etc/kubernetes/admission.yaml"
  12. audit-log-path: "/var/log/kube-audit/audit-log.json"
  13. audit-log-maxage: "5"
  14. audit-log-maxbackup: "5"
  15. audit-log-maxsize: "100"
  16. audit-log-format: "json"
  17. audit-policy-file: /etc/kubernetes/audit.yaml
  18. extra_binds:
  19. - "/var/log/kube-audit:/var/log/kube-audit"
  • Reconfigure the cluster:
  1. rke up --config cluster.yml

2.1.3 - Configure scheduler options

Profile Applicability

  • Level 1

Description

Set the appropriate options for the Kubernetes scheduling service.

Rationale

To address the following controls on the CIS benchmark, the command line options should be set on the Kubernetes scheduler.

  • 1.2.1 - Ensure that the --profiling argument is set to false (Scored)
  • 1.2.2 - Ensure that the --address argument is set to 127.0.0.1 (Scored)

Audit

  • On nodes with the controlplane role: inspect the kube-scheduler containers:
  1. docker inspect kube-scheduler
  • Verify the following options are set in the command section.
  1. --profiling=false
  2. --address=127.0.0.1

Remediation

  • In the RKE cluster.yml file ensure the following options are set:
  1. services:
  2. scheduler:
  3. extra_args:
  4. profiling: "false"
  5. address: "127.0.0.1"
  • Reconfigure the cluster:
  1. rke up --config cluster.yml

2.1.4 - Configure controller options

Profile Applicability

  • Level 1

Description

Set the appropriate arguments on the Kubernetes controller manager.

Rationale

To address the following controls the options need to be passed to the Kubernetes controller manager.

  • 1.3.1 - Ensure that the --terminated-pod-gc-threshold argument is set as appropriate (Scored)
  • 1.3.2 - Ensure that the --profiling argument is set to false (Scored)
  • 1.3.7 - Ensure that the --address argument is set to 127.0.0.1 (Scored)

Audit

  • On nodes with the controlplane role inspect the kube-controller-manager container:
  • Verify the following options are set in the command section:
  1. --terminated-pod-gc-threshold=1000
  2. --profiling=false
  3. --address=127.0.0.1

Remediation

  • In the RKE cluster.yml file ensure the following options are set:
  1. services:
  2. kube-controller:
  3. extra_args:
  4. profiling: "false"
  5. address: "127.0.0.1"
  6. terminated-pod-gc-threshold: "1000"
  • Reconfigure the cluster:
  1. rke up --config cluster.yml

Profile Applicability

  • Level 1

Description

Configure a restrictive PodSecurityPolicy (PSP) as the default and create role bindings for system level services to use the less restrictive default PSP.

Rationale

To address the following controls, a restrictive default PSP needs to be applied as the default. Role bindings need to be in place to allow system services to still function.

  • 1.7.1 - Do not admit privileged containers (Not Scored)
  • 1.7.2 - Do not admit containers wishing to share the host process ID namespace (Not Scored)
  • 1.7.3 - Do not admit containers wishing to share the host IPC namespace (Not Scored)
  • 1.7.4 - Do not admit containers wishing to share the host network namespace (Not Scored)
  • 1.7.5 - Do not admit containers with allowPrivilegeEscalation (Not Scored)
  • 1.7.6 - Do not admit root containers (Not Scored)
  • 1.7.7 - Do not admit containers with dangerous capabilities (Not Scored)

Audit

  • Verify that the cattle-system namespace exists:
  1. kubectl get ns |grep cattle
  • Verify that the roles exist:
  1. kubectl get role default-psp-role -n ingress-nginx
  2. kubectl get role default-psp-role -n cattle-system
  3. kubectl get clusterrole psp:restricted
  • Verify the bindings are set correctly:
  1. kubectl get rolebinding -n ingress-nginx default-psp-rolebinding
  2. kubectl get rolebinding -n cattle-system default-psp-rolebinding
  3. kubectl get clusterrolebinding psp:restricted
  • Verify the restricted PSP is present.
  1. kubectl get psp restricted

Remediation

  • In the RKE cluster.yml file ensure the following options are set:
  1. addons: |
  2. apiVersion: rbac.authorization.k8s.io/v1
  3. kind: Role
  4. metadata:
  5. name: default-psp-role
  6. namespace: ingress-nginx
  7. rules:
  8. - apiGroups:
  9. - extensions
  10. resourceNames:
  11. - default-psp
  12. resources:
  13. - podsecuritypolicies
  14. verbs:
  15. - use
  16. ---
  17. apiVersion: rbac.authorization.k8s.io/v1
  18. kind: RoleBinding
  19. name: default-psp-rolebinding
  20. namespace: ingress-nginx
  21. apiGroup: rbac.authorization.k8s.io
  22. kind: Role
  23. name: default-psp-role
  24. subjects:
  25. - apiGroup: rbac.authorization.k8s.io
  26. kind: Group
  27. name: system:serviceaccounts
  28. - apiGroup: rbac.authorization.k8s.io
  29. kind: Group
  30. name: system:authenticated
  31. ---
  32. apiVersion: v1
  33. kind: Namespace
  34. metadata:
  35. name: cattle-system
  36. ---
  37. apiVersion: rbac.authorization.k8s.io/v1
  38. kind: Role
  39. metadata:
  40. name: default-psp-role
  41. namespace: cattle-system
  42. rules:
  43. - apiGroups:
  44. - extensions
  45. resourceNames:
  46. - default-psp
  47. resources:
  48. - podsecuritypolicies
  49. verbs:
  50. - use
  51. ---
  52. apiVersion: rbac.authorization.k8s.io/v1
  53. kind: RoleBinding
  54. metadata:
  55. name: default-psp-rolebinding
  56. namespace: cattle-system
  57. roleRef:
  58. apiGroup: rbac.authorization.k8s.io
  59. kind: Role
  60. name: default-psp-role
  61. subjects:
  62. - apiGroup: rbac.authorization.k8s.io
  63. kind: Group
  64. name: system:serviceaccounts
  65. - apiGroup: rbac.authorization.k8s.io
  66. kind: Group
  67. name: system:authenticated
  68. ---
  69. apiVersion: extensions/v1beta1
  70. kind: PodSecurityPolicy
  71. metadata:
  72. name: restricted
  73. spec:
  74. requiredDropCapabilities:
  75. - NET_RAW
  76. privileged: false
  77. allowPrivilegeEscalation: false
  78. defaultAllowPrivilegeEscalation: false
  79. fsGroup:
  80. rule: RunAsAny
  81. runAsUser:
  82. rule: MustRunAsNonRoot
  83. seLinux:
  84. rule: RunAsAny
  85. supplementalGroups:
  86. rule: RunAsAny
  87. volumes:
  88. - emptyDir
  89. - secret
  90. - persistentVolumeClaim
  91. - downwardAPI
  92. - configMap
  93. - projected
  94. ---
  95. apiVersion: rbac.authorization.k8s.io/v1
  96. kind: ClusterRole
  97. metadata:
  98. name: psp:restricted
  99. rules:
  100. - apiGroups:
  101. - extensions
  102. resourceNames:
  103. - restricted
  104. resources:
  105. - podsecuritypolicies
  106. verbs:
  107. - use
  108. ---
  109. apiVersion: rbac.authorization.k8s.io/v1
  110. kind: ClusterRoleBinding
  111. metadata:
  112. name: psp:restricted
  113. roleRef:
  114. apiGroup: rbac.authorization.k8s.io
  115. kind: ClusterRole
  116. name: psp:restricted
  117. subjects:
  118. - apiGroup: rbac.authorization.k8s.io
  119. kind: Group
  120. name: system:serviceaccounts
  121. - apiGroup: rbac.authorization.k8s.io
  122. kind: Group
  123. name: system:authenticated
  • Reconfigure the cluster:
  1. rke up --config cluster.yml

3.1 - Rancher Management Control Plane Installation

3.1.1 - Disable the local cluster option

Profile Applicability

  • Level 2

Description

When deploying Rancher, disable the local cluster option on the Rancher Server.

NOTE: This requires Rancher v2.1.2 or above.

Rationale

Having access to the local cluster from the Rancher UI is convenient for troubleshooting and debugging; however, if the local cluster is enabled in the Rancher UI, a user has access to all elements of the system, including the Rancher management server itself. Disabling the local cluster is a defense in depth measure and removes the possible attack vector from the Rancher UI and API.

Audit

  • Verify the Rancher deployment has the --add-local=false option set.
  1. kubectl get deployment rancher -n cattle-system -o yaml |grep 'add-local'
  • In the Rancher UI go to Clusters in the Global view and verify that no local cluster is present.
  • Upgrade to Rancher v2.1.2 via the Helm chart. While performing the upgrade, provide the following installation flag:
  1. --set addLocal="false"

3.1.2 - Enable Rancher Audit logging

Profile Applicability

  • Level 1

Description

Enable Rancher’s built-in audit logging capability.

Rationale

Tracking down what actions were performed by users in Rancher can provide insight during post mortems, and if monitored proactively can be used to quickly detect malicious actions.

Audit

  • Verify that the audit log parameters were passed into the Rancher deployment.
  1. kubectl get deployment rancher -n cattle-system -o yaml | grep auditLog
  • Verify that the log is going to the appropriate destination, as set by auditLog.destination

    • sidecar:

      1. List pods:

        1. kubectl get pods -n cattle-system
      2. Tail logs:

        1. kubectl logs <pod> -n cattle-system -c rancher-audit-log
    • hostPath

      1. On the worker nodes running the Rancher pods, verify that the log files are being written to the destination indicated in auditlog.hostPath.

Remediation

Upgrade the Rancher server installation using Helm, and configure the audit log settings. The instructions for doing so can be found in the reference section below.

Reference

3.2.1 - Change the local administrator password from the default value

Profile Applicability

  • Level 1

Description

The local administrator password should be changed from the default.

Rationale

The default administrator password is common across all Rancher installations and should be changed immediately upon startup.

Audit

Attempt to login into the UI with the following credentials:

  • Username: admin
  • Password: admin

The login attempt must not succeed.

Remediation

Change the password from admin to a password that meets the recommended password standards for your organization.

3.2.2 - Configure an Identity Provider for Authentication

Profile Applicability

  • Level 1

Description

When running Rancher in a production environment, configure an identity provider for authentication.

Rationale

Rancher supports several authentication backends that are common in enterprises. It is recommended to tie Rancher into an external authentication system to simplify user and group access in the Rancher cluster. Doing so assures that access control follows the organization’s change management process for user accounts.

Audit

  • In the Rancher UI, select Global
  • Select Security
  • Select Authentication
  • Ensure the authentication provider for your environment is active and configured correctly

Remediation

Configure the appropriate authentication provider for your Rancher installation according to the documentation found at the link in the reference section below.

Reference

3.3 - Rancher Management Control Plane RBAC

Profile Applicability

  • Level 1

Description

Restrict administrator access to only those responsible for managing and operating the Rancher server.

Rationale

The admin privilege level gives the user the highest level of access to the Rancher server and all attached clusters. This privilege should only be granted to a few people who are responsible for the availability and support of Rancher and the clusters that it manages.

Audit

The following script uses the Rancher API to show users with administrator privileges:

  1. #!/bin/bash
  2. for i in $(curl -sk -u 'token-<id>:<secret>' https://<RANCHER_URL>/v3/users|jq -r .data[].links.globalRoleBindings); do
  3. curl -sk -u 'token-<id>:<secret>' $i| jq '.data[] | "\(.userId) \(.globalRoleId)"'
  4. done

The admin role should only be assigned to users that require administrative privileges. Any role that is not admin or user should be audited in the RBAC section of the UI to ensure that the privileges adhere to policies for global access.

The Rancher server permits customization of the default global permissions. We recommend that auditors also review the policies of any custom global roles.

Remediation

Remove the role from any user that does not require administrative privileges.

3.4.1 - Ensure only approved node drivers are active

Profile Applicability

  • Level 1

Description

Ensure that node drivers that are not needed or approved are not active in the Rancher console.

Rationale

Node drivers are used to provision compute nodes in various cloud providers and local IaaS infrastructure. For convenience, popular cloud providers are enabled by default. If the organization does not intend to use these or does not allow users to provision resources in certain providers, the drivers should be disabled. This will prevent users from using Rancher resources to provision the nodes.

Audit

  • In the Rancher UI select Global
  • Review the list of node drivers that are in an Active state.

Remediation


Appendix A - Complete RKE cluster.yml Example