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
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • 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

[Udemy] Download khóa học Udemy giá rẻ chỉ với 50k/khóa Kubernetes & Container
[Oauth2-Proxy] Oauth2-Proxy encounters issue with Cognito Kubernetes & Container
[Helm Chart] Learning Helm on Kubernetes in 60 minutes!! Helm Chart
[kiali] Config authentication on Kiali. Isito-EnvoyFilter
[K8s] Hướng dẫn sử dụng kubectl trên mấy local kết nối đến k8s master Kubernetes
[Longhorn] Store label with longhorn in order to create many storage classes and have many storage styles “SSD, HDD, fast, slow” Kubernetes & Container

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

  • [Azure] The subscription is not registered to use namespace ‘Microsoft.ContainerService’ May 8, 2025
  • [Azure] Insufficient regional vcpu quota left May 8, 2025
  • [WordPress] How to add a Dynamic watermark on WordPress. May 6, 2025
  • [vnet/Azure] VNet provisioning via Terraform. April 28, 2025
  • [tracetcp] How to perform a tracert command using a specific port. April 3, 2025

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • 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
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.