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

[ArgoCD/KSOPS] Encrypting Resource on kustomize and Argocd.

Posted on July 3, 2022April 11, 2023 By nim No Comments on [ArgoCD/KSOPS] Encrypting Resource on kustomize and Argocd.

Để Gitops mọi hệ thống thì chúng ta cần template mọi hệ thống trở thành manifest, helm chart, kustomize rồi đẩy lên github hay gitlab.

Từ đây chúng ta phát sinh ra 1 vần đề là có nên đẩy luôn secret hay credentials lên git hay không?
Mình đã có 1 bài hướng dẫn tương tác kustomize và argocd với vault.

Giờ chúng ta có một cách khác encrypt secret rồi lên argocd setup như thế nào để argocd tự decrypt được.

Contents

Toggle
  • 1) Overview GPG
  • 2) Setup argocd integrate GPG
  • 3) Encrypt Data in Your Application and Integrate with Argocd.

1) Overview GPG

Đầu tiên bạn cần làm quen mới 1 khái niệm là  GPG/OpenPGP
Bạn sẽ tham khảo bạn viết bên dưới này:
https://cystack.net/vi/blog/huong-dan-su-dung-gpg-de-ma-hoa-va-ky-thong-diep

Sau khi đã nghiềm ngẫm về GPG chúng ta sẽ làm gì đây?

Mục tiêu là muốn encrypt the secret manifests khi deploy bằng arogcd.
Bạn sẽ chú ý phần này giúp mình.
https://blog.devgenius.io/argocd-with-kustomize-and-ksops-2d43472e9d3b
https://github.com/viaduct-ai/kustomize-sops#argo-cd-integration-

Đầu tiên chúng ta cần gen một GPG key first:

export GPG_NAME="k0s.nimtechnology.cluster"
export GPG_COMMENT="argocd secrets"

gpg --batch --full-generate-key <<EOF
%no-protection
Key-Type: 1
Key-Length: 4096
Subkey-Type: 1
Subkey-Length: 4096
Expire-Date: 0
Name-Comment: ${GPG_COMMENT}
Name-Real: ${GPG_NAME}
EOF


>>>>
output
>>>>
gpg: key D8D1E3F39710B570 marked as ultimately trusted
gpg: revocation certificate stored as '/root/.gnupg/openpgp-revocs.d/E580ABECFC7260E628574541D8D1E3F39710B570.rev'

The code above will create an RSA key with 4096 bits. For full configuration of creating GPG keys, check this link

Lấy lại và lưu key name

gpg --list-secret-keys "${GPG_NAME}"

>>>>
output
>>>>
gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   2  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 2u
sec   rsa4096 2022-07-02 [SCEA]
      E580ABECFC7260E628574541D8D1E3F39710B570
uid           [ultimate] k0s.nimtechnology.cluster (argocd secrets)
ssb   rsa4096 2022-07-02 [SEA]
Bạn cần lưu lại string trong ô vuông.
export GPG_ID=02FFE7F59336E4499E5A66772C9152921C9F2B1C

giờ bạn thực hiện export private key và create 1 secret từ private key trên kubernetes

gpg --export-secret-keys --armor "${GPG_ID}" |
kubectl create secret generic sops-gpg \
--namespace=argocd \
--from-file=sops.asc=/dev/stdin

>>>>
output
>>>>
secret/sops-gpg created
OK bạn đã tạo secret thành công

Để đảm bảo về tính bảo mật bạn có thể xóa, chúng ta đã đẩy lên k8s. Nhiệm vụ chính của private key là decrypt.

gpg --delete-secret-keys "${GPG_ID}"

>>>>
output
>>>>
gpg (GnuPG) 2.2.19; Copyright (C) 2019 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


sec  rsa4096/D8D1E3F39710B570 2022-07-02 k0s.nimtechnology.cluster (argocd secrets)

Delete this key from the keyring? (y/N) y
This is a secret key! - really delete? (y/N) y

export public key
Public key này dùng để mã hóa (encrypt) data hoặc file manifest.

gpg --export --armor "${GPG_ID}" > .sops.pub.asc
gpg --import .sops.pub.asc

>>>>
output
>>>>
gpg: key D8D1E3F39710B570: "k0s.nimtechnology.cluster (argocd secrets)" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1

Chúng ta sẽ hình thành mindset như sau:
– Trước khi push secret lên git chúng ta sẽ encrypt data bằng public key. Bạn có thể push file public key lên github để mọi người có thể import public key sau đó là encrypt.
– Bạn push private key và public key lên argocd(k8s ) để decrypt data.

Bạn copy content bên trong file .sops.pub.asc và add vào GnuPG keys

Giờ bạn thực hiện tạo nhanh 1 manifest secret trên k8s để tý chúng ta sử dụng.

kubectl -n myapp create secret generic app-secret \
--from-literal=token=4BD1CE23-E3C0-4BBF-A75E-ABA103EE95C9 \
--dry-run=client \
-o yaml > secret.yaml

Encrypt(mã hóa) the secret.yaml with sops

Chúng ta cần cài sops
https://docs.technotim.live/posts/install-mozilla-sops/

#https://docs.technotim.live/posts/install-mozilla-sops/
SOPS_LATEST_VERSION=$(curl -s "https://api.github.com/repos/mozilla/sops/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')
curl -Lo sops.deb "https://github.com/mozilla/sops/releases/latest/download/sops_${SOPS_LATEST_VERSION}_amd64.deb"
sudo apt --fix-broken install ./sops.deb
rm -rf sops.deb
sops -version

giờ bạn tạo 1 file config để sosp hoạt động

cat <<EOF > ./.sops.yaml
creation_rules:
  - path_regex: .*.yaml
    encrypted_regex: ^(data|stringData)$
    pgp: ${GPG_ID}
EOF

Bạn có thể dùng unencrypted_regex

unencrypted_regex: '^(apiVersion.*|metadata.*|kind.*|type.*|annotations.*)$'


Bược tiếp theo là bạn kiểm tra file secret.
sau đó thực hiện mã hóa secret dưới local.

sops --encrypt --in-place secret.yaml

>>>>
output
>>>>
INFO found a configuration for 'secret.yaml' in '.sops.yaml'
INFO: generating and storing data encryption key
Bạn thấy là secret đã được mã hóa nhìn phức tạp vl.

nếu bạn cần decrypt lại thì sử dụng command sau:

sops --decrypt --in-place secret.yaml

2) Setup argocd integrate GPG

Giờ đến phần cài đặt argocd để sử dụng được với sosp hay gpg
file agocd-cm-plugin.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-cm
  labels:
    app.kubernetes.io/name: argocd-cm
    app.kubernetes.io/part-of: argocd
data:
  # For KSOPs versions < v2.5.0, use the old kustomize flag style
  # kustomize.buildOptions: "--enable_alpha_plugins"
  kustomize.buildOptions: "--enable-alpha-plugins"

Thêm 1file để config argo-cd-repo là: argo-cd-repo-server-ksops-patch.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: argocd-repo-server
spec:
  template:
    spec:
      initContainers:
        - name: install-ksops
          image: viaductoss/ksops:v3.0.2
          command: ["/bin/sh", "-c"]
          args:
            - echo "Installing KSOPS...";
              export PKG_NAME=ksops;
              mv ${PKG_NAME} /custom-tools/;
              mv $GOPATH/bin/kustomize /custom-tools/;
              echo "Done.";
          volumeMounts:
            - mountPath: /custom-tools
              name: custom-tools
        - name: import-gpg-key
          image: argoproj/argocd:v2.4.3
          command: ["gpg", "--import","/sops-gpg/sops.asc"]
          env:
            - name: GNUPGHOME
              value: /gnupg-home/.gnupg
          volumeMounts:
            - mountPath: /sops-gpg
              name: sops-gpg
            - mountPath: /gnupg-home
              name: gnupg-home
      containers:
      - name: argocd-repo-server
        env:
          - name: XDG_CONFIG_HOME
            value: /.config
          - name: GNUPGHOME
            value: /home/argocd/.gnupg
        volumeMounts:
        - mountPath: /home/argocd/.gnupg
          name: gnupg-home
          subPath: .gnupg
        - mountPath: /usr/local/bin/kustomize
          name: custom-tools
          subPath: kustomize
        - mountPath: /.config/kustomize/plugin/viaduct.ai/v1/ksops/ksops
          name: custom-tools
          subPath: ksops
      volumes:
      - name: custom-tools
        emptyDir: {}
      - name: gnupg-home
        emptyDir: {}
      - name: sops-gpg
        secret:
          secretName: sops-gpg

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - https://raw.githubusercontent.com/argoproj/argo-cd/v2.4.3/manifests/install.yaml

patchesStrategicMerge:
  # - argocd-vault-plugin.yaml
  - agocd-cm-plugin.yaml
  - argo-cd-repo-server-ksops-patch.yaml

3) Encrypt Data in Your Application and Integrate with Argocd.

Config với application của bạn.

Giờ mình lập lại bước tạo secret ở trên

kubectl -n myapp create secret generic app-secret \
--from-literal=token=4BD1CE23-E3C0-4BBF-A75E-ABA103EE95C9 \
--dry-run=client \
-o yaml > secret.yaml

Tiếp đến là encrypt manifest secret

sops --encrypt --in-place secret.yaml

>>>>
output
>>>>
INFO found a configuration for 'secret.yaml' in '.sops.yaml'
INFO: generating and storing data encryption key
Đây là cấu trúc thư mục của mình
và mình để file secret đã được encrypted ở vị trí như ảnh.

kops-secret.yaml

apiVersion: viaduct.ai/v1
kind: ksops
metadata:
  name: ksops-secret
files:
  - secret/secret_enc.yaml

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

generators:
 - ./kops-secret.yaml

generatorOptions:
  labels:
    deploy.type: kustomize
  disableNameSuffixHash: true

Create application trên argocd thì bạn cứ create như bình thường không cần chọn plugin gì cả!

Bạn có thể coi thêm video của anh tàu nhé:
https://www.bilibili.com/video/BV1TL4y1V7oB/

Để tìm hiểu về Manage your secrets in Git with SOPS

ArgoCD, Kubernetes & Container

Post navigation

Previous Post: [Argocd/Vault] Integrate Vault inside Argocd by the plugin
Next Post: [Kubernetes] Security in Kubernetes Deployments

More Related Articles

[Istio/multi cluster] Install multi-cluster Istio with mode “Primary-Remote” for Kubernetes on Google Cloud Platform. Isito-EnvoyFilter
[SSL] Kích hoạt chứng chỉ Let’s Encrypt Wildcard SSL bằng docker Kubernetes & Container
[ArgoCD Image Updater] How does Argocd trigger images on Dockerhub and deploy workload on k8s automatically? ArgoCD
[Argocd – Azure] Login to ArgoCD using Microsoft or Azure Accounts. ArgoCD
[Gitops] Evolving DevOps to GitOps ArgoCD
[istio] Sử dụng istio-operator và istioctl để cài đặt Istio 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

  • [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.