Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
    • Helm Chart
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log & Monitor
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[OPA Gatekeeper] Sử dụng openpolicyagent để ngăn chặn việc apply yaml tuỳ tiện và sai lên kubernetes!

Posted on September 26, 2021September 27, 2021 By nim No Comments on [OPA Gatekeeper] Sử dụng openpolicyagent để ngăn chặn việc apply yaml tuỳ tiện và sai lên kubernetes!

Ý tưởng của việc sử dụng OPA là. Chặn việc apply các yaml mà sai quy tắc.
ví dụ như là: apply service mysql port 3306 mà bạn quên để type là https thay vị là tcp

Video cho anh em tham khảo.

Links Deploy OPA
https://raw.githubusercontent.com/open-policy-agent/gatekeeper/master/deploy/gatekeeper.yaml

# Deploy OPA Gatekeeper
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper/master/deploy/gatekeeper.yaml

Để thực hiện áp các policy thì bạn đọc link sau:

https://www.magalix.com/blog/how-to-integrate-opa-into-your-kubernetes-cluster-using-kube-mgmt

https://www.magalix.com/blog/integrating-open-policy-agent-opa-with-kubernetes-a-deep-dive-tutorial

Còn từ đây xuống dưới là phần đọc hiểu của mình
Đầu tiên là chúng ta cần định nghĩ Constraint Template

# src: https://github.com/open-policy-agent/gatekeeper/blob/master/demo/basic/templates/k8srequiredlabels_template.yaml
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
  name: k8srequiredlabels
spec:
  crd:
    spec:
      names:
        kind: K8sRequiredLabels
      validation:
        # Schema for the `parameters` field
        openAPIV3Schema:
          properties:
            labels:
              type: array
              items: string
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package k8srequiredlabels
        violation[{"msg": msg, "details": {"missing_labels": missing}}] {
          provided := {label | input.review.object.metadata.labels[label]}
          required := {label | label := input.parameters.labels[_]}
          missing := required - provided
          count(missing) > 0
          msg := sprintf("you must provide labels: %v", [missing])
        }

violation[{"msg": msg, "details": {"missing_labels": missing}}]
– nó dùng để bắt đầu policy, cơ bản thì gồm 2 phần là:
+ message
+ details: khi người dụng vi phạm(violated) thì nó hiển thị thông điệp j đó

provided := {label | input.review.object.metadata.labels[label]}
– input.review.object. : In Rego, this is called Comprehension, nó sử dụng để giữ list of labels for the resource. Chúng ta thấy labels có dạng dictionary of label=value
– Nếu kết hợp với manifest K8sRequiredLabels thì khi bạn apply manifest với Kind là Namespace. Nó sử dụng input.review.object. để lấy thông tin trong metadata->labels và lưu các labels “[gatekeeper: OPA]”

required := {label | label := input.parameters.labels[_]}
– required labels được cung cấp theo kiểu the form of an array thay vì là dictionary như của provided
– Ở đây chúng ta cũng có a list of the labels được lấy từ K8sRequiredLabels và chức năng ở đây là yêu cầu resource phải thoải điều kiện của list required này. ở ví dụ trên thì yêu cấu namespace phải label gatekeeper(iterate through all the items in the labels array – được quy định ở properties “items: string” của ConstraintTemplate)

missing := required - provided
– Chúng ta đang có 2 array: provided chữa thông tin lấy được từ manifest và required chứa các yêu cầu
– missing tạo ra 1 array chứa kết quả được tìm thấy ở required nhưng ko tìm thấy ở provided -> kiểu như là tìm những yêu cấu mà namespace của bạn chưa đặt được ấy.

count(missing) > 0
– nếu mà mảng missing được đến các phần tử bên trong rồi cho kết quả lớn hơn không -> namespace đã vi phạm (violated)

msg := sprintf("you must provide labels: %v", [missing])
– Nếu mà namespace vi phạm, chúng ta đã định nghĩ 1 message và sẽ được hiển thị cho client thấy ở dòng đầu tiên “violation[{"msg": msg, "details": {"missing_labels": missing}}]”

File yaml được lưu ở đây
https://github.com/HoussemDellai/k8s-opa-gatekeeper-demo

Bạn có thể sem video và apply để kiểm tra.
Điều lưu ý bạn cần apply ConstraintTemplate trước xong mới đến K8sRequiredLabels
Language Rego cũng khá là khó hiểu, họ bảo có tool https://play.openpolicyagent.org/
nhưng thật sự mình chưa áp dụng đc tool này huhu

Mình lướt thấy có một thanh niên tương tự:
https://neonmirrors.net/post/2021-02/kubernetes-policy-comparison-opa-gatekeeper-vs-kyverno/

Kubernetes & Container

Post navigation

Previous Post: [Cert manager] Tìm hiểu cách sử cert-manager để create ssl cho website sử dụng ingress nginx.
Next Post: [Case dị – istio] Những case khó đỡ khiến chúng ra suy nghĩ là istio có chạy được hay ko?

More Related Articles

[Kustomize] How to combine Kustomize and Helm-chart! Helm Chart
[Istio/multi cluster] Install multi-cluster Istio with mode “Primary-Remote” for Kubernetes on Google Cloud Platform. Isito-EnvoyFilter
[Kubernetes] Check pods cpu/memory usage without Third Party ? Kubernetes & Container
[wordpress] Install WordPress so easily. Kubernetes & Container
[Vault/Consul] Install vault and consul through Helm in K8s Kubernetes & Container
[Istio-Multi Cluster] Tutorial is very detailed to install Primary-Remote on different networks Isito-EnvoyFilter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Prometheus/Grafana] Install Prometheus and Grafana on ubuntu. March 27, 2023
  • [Kong Gateway] WebSocket connection failed March 26, 2023
  • [Nextcloud] Can’t download files to have a size bigger than 2Gi on NextCloud – RaspBerry March 24, 2023
  • [Datadog] Using DataDog to monitor all services on kubernetes March 19, 2023
  • [Metrics Server] Failed to make webhook authorizer request: the server could not find the requested resource March 17, 2023

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • Golang
    • Jquery & JavaScript
    • Laravel
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
    • Longhorn – Storage
    • MetalLB
    • Vault
    • VictoriaMetrics
  • Log & Monitor
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2023 NimTechnology.