You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
6.3 KiB

  1. # Cluster Hardening
  2. If you want to improve the security on your cluster and make it compliant with the [CIS Benchmarks](https://learn.cisecurity.org/benchmarks), here you can find a configuration to harden your **kubernetes** installation.
  3. To apply the hardening configuration, create a file (eg. `hardening.yaml`) and paste the content of the following code snippet into that.
  4. ## Minimum Requirements
  5. The **kubernetes** version should be at least `v1.23.6` to have all the most recent security features (eg. the new `PodSecurity` admission plugin, etc).
  6. **N.B.** Some of these configurations have just been added to **kubespray**, so ensure that you have the latest version to make it works properly. Also, ensure that other configurations doesn't override these.
  7. `hardening.yaml`:
  8. ```yaml
  9. # Hardening
  10. ---
  11. ## kube-apiserver
  12. authorization_modes: ['Node', 'RBAC']
  13. # AppArmor-based OS
  14. # kube_apiserver_feature_gates: ['AppArmor=true']
  15. kube_apiserver_request_timeout: 120s
  16. kube_apiserver_service_account_lookup: true
  17. # enable kubernetes audit
  18. kubernetes_audit: true
  19. audit_log_path: "/var/log/kube-apiserver-log.json"
  20. audit_log_maxage: 30
  21. audit_log_maxbackups: 10
  22. audit_log_maxsize: 100
  23. tls_min_version: VersionTLS12
  24. tls_cipher_suites:
  25. - TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  26. - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  27. - TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305
  28. # enable encryption at rest
  29. kube_encrypt_secret_data: true
  30. kube_encryption_resources: [secrets]
  31. kube_encryption_algorithm: "secretbox"
  32. kube_apiserver_enable_admission_plugins:
  33. - EventRateLimit
  34. - AlwaysPullImages
  35. - ServiceAccount
  36. - NamespaceLifecycle
  37. - NodeRestriction
  38. - LimitRanger
  39. - ResourceQuota
  40. - MutatingAdmissionWebhook
  41. - ValidatingAdmissionWebhook
  42. - PodNodeSelector
  43. - PodSecurity
  44. kube_apiserver_admission_control_config_file: true
  45. # Creates config file for PodNodeSelector
  46. # kube_apiserver_admission_plugins_needs_configuration: [PodNodeSelector]
  47. # Define the default node selector, by default all the workloads will be scheduled on nodes
  48. # with label network=srv1
  49. # kube_apiserver_admission_plugins_podnodeselector_default_node_selector: "network=srv1"
  50. # EventRateLimit plugin configuration
  51. kube_apiserver_admission_event_rate_limits:
  52. limit_1:
  53. type: Namespace
  54. qps: 50
  55. burst: 100
  56. cache_size: 2000
  57. limit_2:
  58. type: User
  59. qps: 50
  60. burst: 100
  61. kube_profiling: false
  62. # Remove anonymous access to cluster
  63. remove_anonymous_access: true
  64. ## kube-controller-manager
  65. kube_controller_manager_bind_address: 127.0.0.1
  66. kube_controller_terminated_pod_gc_threshold: 50
  67. # AppArmor-based OS
  68. # kube_controller_feature_gates: ["RotateKubeletServerCertificate=true", "AppArmor=true"]
  69. kube_controller_feature_gates: ["RotateKubeletServerCertificate=true"]
  70. ## kube-scheduler
  71. kube_scheduler_bind_address: 127.0.0.1
  72. # AppArmor-based OS
  73. # kube_scheduler_feature_gates: ["AppArmor=true"]
  74. ## etcd
  75. etcd_deployment_type: kubeadm
  76. ## kubelet
  77. kubelet_authorization_mode_webhook: true
  78. kubelet_authentication_token_webhook: true
  79. kube_read_only_port: 0
  80. kubelet_rotate_server_certificates: true
  81. kubelet_protect_kernel_defaults: true
  82. kubelet_event_record_qps: 1
  83. kubelet_rotate_certificates: true
  84. kubelet_streaming_connection_idle_timeout: "5m"
  85. kubelet_make_iptables_util_chains: true
  86. kubelet_feature_gates: ["RotateKubeletServerCertificate=true"]
  87. kubelet_seccomp_default: true
  88. kubelet_systemd_hardening: true
  89. # In case you have multiple interfaces in your
  90. # control plane nodes and you want to specify the right
  91. # IP addresses, kubelet_secure_addresses allows you
  92. # to specify the IP from which the kubelet
  93. # will receive the packets.
  94. kubelet_secure_addresses: "localhost link-local {{ kube_pods_subnet }} 192.168.10.110 192.168.10.111 192.168.10.112"
  95. # additional configurations
  96. kube_owner: root
  97. kube_cert_group: root
  98. # create a default Pod Security Configuration and deny running of insecure pods
  99. # kube_system namespace is exempted by default
  100. kube_pod_security_use_default: true
  101. kube_pod_security_default_enforce: restricted
  102. ```
  103. Let's take a deep look to the resultant **kubernetes** configuration:
  104. * The `anonymous-auth` (on `kube-apiserver`) is set to `true` by default. This is fine, because it is considered safe if you enable `RBAC` for the `authorization-mode`.
  105. * The `enable-admission-plugins` includes `PodSecurity` (for more details, please take a look here: <https://kubernetes.io/docs/concepts/security/pod-security-admission/>). Then, we set the `EventRateLimit` plugin, providing additional configuration files (that are automatically created under the hood and mounted inside the `kube-apiserver` container) to make it work.
  106. * The `encryption-provider-config` provide encryption at rest. This means that the `kube-apiserver` encrypt data that is going to be stored before they reach `etcd`. So the data is completely unreadable from `etcd` (in case an attacker is able to exploit this).
  107. * The `rotateCertificates` in `KubeletConfiguration` is set to `true` along with `serverTLSBootstrap`. This could be used in alternative to `tlsCertFile` and `tlsPrivateKeyFile` parameters. Additionally it automatically generates certificates by itself. By default the CSRs are approved automatically via [kubelet-csr-approver](https://github.com/postfinance/kubelet-csr-approver). You can customize approval configuration by modifying Helm values via `kubelet_csr_approver_values`.
  108. See <https://kubernetes.io/docs/reference/access-authn-authz/kubelet-tls-bootstrapping/> for more information on the subject.
  109. * If you are installing **kubernetes** in an AppArmor-based OS (eg. Debian/Ubuntu) you can enable the `AppArmor` feature gate uncommenting the lines with the comment `# AppArmor-based OS` on top.
  110. * The `kubelet_systemd_hardening`, both with `kubelet_secure_addresses` setup a minimal firewall on the system. To better understand how these variables work, here's an explanatory image:
  111. ![kubelet hardening](img/kubelet-hardening.png)
  112. Once you have the file properly filled, you can run the **Ansible** command to start the installation:
  113. ```bash
  114. ansible-playbook -v cluster.yml \
  115. -i inventory.ini \
  116. -b --become-user=root \
  117. --private-key ~/.ssh/id_ecdsa \
  118. -e "@vars.yaml" \
  119. -e "@hardening.yaml"
  120. ```
  121. **N.B.** The `vars.yaml` contains our general cluster information (SANs, load balancer, dns, etc..) and `hardening.yaml` is the file described above.