Để 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.
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]

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


Để đả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

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

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