Sealed Secrets
Encrypted secret management in Kubernetes: a Bitnami open-source tool for safe secret storage in GitOps environments
Encrypted secret management in Kubernetes: a Bitnami open-source tool for safe secret storage in GitOps environments
Sealed Secrets is a Kubernetes secret encryption tool developed by Bitnami. It solves the biggest challenge in GitOps environments: "how to safely store secrets in Git." A regular Kubernetes Secret is only base64-encoded, so it is effectively plaintext, but when you use Sealed Secrets, it is encrypted with a public key and can be decrypted only by the Sealed Secrets Controller inside the cluster.
It works on the basis of asymmetric encryption (public key/private key). The Controller installed in the cluster holds the private key, and developers encrypt a Secret with the public key through the kubeseal CLI tool to create a SealedSecret resource. This SealedSecret can be stored safely in Git, and when it is deployed to the cluster, the Controller automatically decrypts it and creates the actual Secret.
The biggest advantage of Sealed Secrets is its seamless integration with existing Kubernetes workflows. When used together with GitOps tools such as ArgoCD and Flux, the entire infrastructure can be managed as code. In addition, scopes can be specified per namespace or per cluster, which makes it flexible for managing multiple environments. An encrypted SealedSecret can be restricted so that it is decrypted only in a specific cluster and a specific namespace.
The point to watch out for in operation is private key management. If the Controller's private key is leaked, all SealedSecrets can be decrypted, so a key rotation policy and a backup strategy are essential. Also, because a SealedSecret is encrypted with the public key at the time of its creation, existing SealedSecrets must be re-encrypted after a key rotation. In large-scale environments, combining it with HashiCorp Vault or External Secrets Operator is also worth considering.
# Sealed Secrets Controller ์ค์น (Helm ์ฌ์ฉ)
helm repo add sealed-secrets https://bitnami-labs.github.io/sealed-secrets
helm repo update
helm install sealed-secrets sealed-secrets/sealed-secrets \
--namespace kube-system \
--set fullnameOverride=sealed-secrets-controller
# kubeseal CLI ์ค์น (macOS)
brew install kubeseal
# kubeseal CLI ์ค์น (Linux)
wget https://github.com/bitnami-labs/sealed-secrets/releases/download/v0.24.5/kubeseal-0.24.5-linux-amd64.tar.gz
tar -xvzf kubeseal-0.24.5-linux-amd64.tar.gz
sudo install -m 755 kubeseal /usr/local/bin/kubeseal
# ๊ณต๊ฐํค ๊ฐ์ ธ์ค๊ธฐ (์คํ๋ผ์ธ ์ฌ์ฉ ์)
kubeseal --fetch-cert \
--controller-name=sealed-secrets-controller \
--controller-namespace=kube-system \
> sealed-secrets-pub.pem
# 1. ๋จผ์ ์ผ๋ฐ Secret YAML ์์ฑ
cat > my-secret.yaml << EOF
apiVersion: v1
kind: Secret
metadata:
name: database-credentials
namespace: production
type: Opaque
data:
username: YWRtaW4= # base64 encoded: admin
password: c3VwZXJzZWNyZXQ= # base64 encoded: supersecret
EOF
# 2. kubeseal๋ก ์ํธํ (ํด๋ฌ์คํฐ์ ์ฐ๊ฒฐ๋ ์ํ)
kubeseal --format yaml < my-secret.yaml > sealed-secret.yaml
# 3. ๋๋ ๊ณต๊ฐํค ํ์ผ ์ง์ ์ง์ (์คํ๋ผ์ธ ํ๊ฒฝ)
kubeseal --format yaml \
--cert sealed-secrets-pub.pem \
< my-secret.yaml > sealed-secret.yaml
# 4. ์์ฑ๋ SealedSecret ํ์ธ
cat sealed-secret.yaml
# sealed-secret.yaml - Git์ ์์ ํ๊ฒ ์ ์ฅ ๊ฐ๋ฅ
apiVersion: bitnami.com/v1alpha1
kind: SealedSecret
metadata:
name: database-credentials
namespace: production
spec:
encryptedData:
username: AgBy3i4OJSWK+PiTySYZZA9rO43cGDEq...
password: AgBvLh7fH9/WTBu+ZqMC2nvDpAJJSzAR...
template:
metadata:
name: database-credentials
namespace: production
type: Opaque
---
# ๋ค๋ฅธ ์ค์ฝํ ์ต์
๋ค
# strict (๊ธฐ๋ณธ๊ฐ): namespace์ name ๋ ๋ค ์ผ์นํด์ผ ๋ณตํธํ
# namespace-wide: ๊ฐ์ namespace ๋ด ์ด๋ค ์ด๋ฆ์ผ๋ก๋ ๋ณตํธํ ๊ฐ๋ฅ
# cluster-wide: ํด๋ฌ์คํฐ ์ ์ฒด์์ ๋ณตํธํ ๊ฐ๋ฅ
# namespace-wide ์ค์ฝํ๋ก ์ํธํ
# kubeseal --scope namespace-wide --format yaml < my-secret.yaml
# cluster-wide ์ค์ฝํ๋ก ์ํธํ
# kubeseal --scope cluster-wide --format yaml < my-secret.yaml
# .github/workflows/seal-secrets.yml
name: Seal Secrets
on:
workflow_dispatch:
inputs:
environment:
description: 'Target environment'
required: true
type: choice
options:
- development
- staging
- production
jobs:
seal-secret:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install kubeseal
run: |
wget https://github.com/bitnami-labs/sealed-secrets/releases/download/v0.24.5/kubeseal-0.24.5-linux-amd64.tar.gz
tar -xvzf kubeseal-0.24.5-linux-amd64.tar.gz
sudo install -m 755 kubeseal /usr/local/bin/kubeseal
- name: Seal secrets for environment
run: |
CERT_FILE="certs/${{ inputs.environment }}-sealed-secrets.pem"
# ํ๊ฒฝ๋ณ ์ํฌ๋ฆฟ ์ํธํ
for secret_file in secrets/${{ inputs.environment }}/*.yaml; do
output_file="manifests/${{ inputs.environment }}/$(basename $secret_file .yaml)-sealed.yaml"
kubeseal --format yaml \
--cert "$CERT_FILE" \
< "$secret_file" > "$output_file"
done
- name: Commit sealed secrets
run: |
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git add manifests/
git commit -m "Seal secrets for ${{ inputs.environment }}"
git push
import subprocess
import yaml
import json
from pathlib import Path
from datetime import datetime, timedelta
from kubernetes import client, config
class SealedSecretsManager:
def __init__(self, kubeconfig_path: str = None):
if kubeconfig_path:
config.load_kube_config(config_file=kubeconfig_path)
else:
config.load_incluster_config()
self.custom_api = client.CustomObjectsApi()
self.core_api = client.CoreV1Api()
def list_sealed_secrets(self, namespace: str = None):
"""ํด๋ฌ์คํฐ์ ๋ชจ๋ SealedSecret ๋ชฉ๋ก ์กฐํ"""
if namespace:
secrets = self.custom_api.list_namespaced_custom_object(
group="bitnami.com",
version="v1alpha1",
namespace=namespace,
plural="sealedsecrets"
)
else:
secrets = self.custom_api.list_cluster_custom_object(
group="bitnami.com",
version="v1alpha1",
plural="sealedsecrets"
)
return secrets.get('items', [])
def verify_sealed_secret(self, sealed_secret_path: str) -> bool:
"""SealedSecret์ด ํ์ฌ Controller ํค๋ก ๋ณตํธํ ๊ฐ๋ฅํ์ง ๊ฒ์ฆ"""
try:
result = subprocess.run(
['kubeseal', '--validate', '--format', 'yaml'],
input=Path(sealed_secret_path).read_bytes(),
capture_output=True
)
return result.returncode == 0
except Exception as e:
print(f"Validation failed: {e}")
return False
def get_controller_key_age(self, namespace: str = "kube-system"):
"""Controller์ ํ์ฌ ํค ์์ฑ ์์ ํ์ธ"""
secrets = self.core_api.list_namespaced_secret(
namespace=namespace,
label_selector="sealedsecrets.bitnami.com/sealed-secrets-key=active"
)
if secrets.items:
key_secret = secrets.items[0]
creation_time = key_secret.metadata.creation_timestamp
age = datetime.now(creation_time.tzinfo) - creation_time
return {
"name": key_secret.metadata.name,
"created": creation_time.isoformat(),
"age_days": age.days
}
return None
def backup_controller_keys(self, output_dir: str):
"""Controller ํค ๋ฐฑ์
(์ฌํด๋ณต๊ตฌ์ฉ)"""
Path(output_dir).mkdir(parents=True, exist_ok=True)
# ๋ชจ๋ ํค ์ํฌ๋ฆฟ ๋ฐฑ์
secrets = self.core_api.list_namespaced_secret(
namespace="kube-system",
label_selector="sealedsecrets.bitnami.com/sealed-secrets-key"
)
for secret in secrets.items:
backup_file = Path(output_dir) / f"{secret.metadata.name}.yaml"
secret_dict = client.ApiClient().sanitize_for_serialization(secret)
with open(backup_file, 'w') as f:
yaml.dump(secret_dict, f)
print(f"Backed up: {backup_file}")
def rotate_and_reseal(self, manifest_dir: str, new_cert_path: str):
"""ํค ๋กํ
์ด์
ํ ๋ชจ๋ SealedSecret ์ฌ์ํธํ"""
sealed_files = list(Path(manifest_dir).rglob("*-sealed.yaml"))
for sealed_file in sealed_files:
# ์๋ณธ Secret ํ์ผ ์ฐพ๊ธฐ
original_file = str(sealed_file).replace("-sealed.yaml", ".yaml")
if not Path(original_file).exists():
print(f"Warning: Original secret not found for {sealed_file}")
continue
# ์ ์ธ์ฆ์๋ก ์ฌ์ํธํ
result = subprocess.run(
['kubeseal', '--format', 'yaml', '--cert', new_cert_path],
input=Path(original_file).read_bytes(),
capture_output=True
)
if result.returncode == 0:
sealed_file.write_bytes(result.stdout)
print(f"Re-sealed: {sealed_file}")
else:
print(f"Failed to re-seal: {sealed_file}")
# ์ฌ์ฉ ์์
if __name__ == "__main__":
manager = SealedSecretsManager()
# ๋ชจ๋ SealedSecret ์กฐํ
secrets = manager.list_sealed_secrets()
for secret in secrets:
name = secret['metadata']['name']
ns = secret['metadata']['namespace']
print(f"SealedSecret: {ns}/{name}")
# ํค ๋์ด ํ์ธ (30์ผ ์ด์์ด๋ฉด ๋กํ
์ด์
๊ถ์ฅ)
key_info = manager.get_controller_key_age()
if key_info and key_info['age_days'] > 30:
print(f"Warning: Key is {key_info['age_days']} days old. Consider rotation.")
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free