GitOps and Secure Infrastructure as Code: A Practical Guide
Infrastructure as code (IaC) describes servers, networks and cloud resources in version-controlled files, applied by tools such as Terraform. GitOps goes a step further: a Git repository holds the desired state of the whole system, and an agent inside the environment, such as Argo CD or Flux, continuously pulls that state and reconciles the live system to match.
Topic: Cloud & Container Security in Cybersecurity & Ethical Hacking · Explainer
Also covers: DevOps & Platform Engineering
Quick answer: Infrastructure as code (IaC) describes servers, networks and cloud resources in version-controlled files, applied by tools such as Terraform. GitOps goes a step further: a Git repository holds the desired state of the whole system, and an agent inside the environment, such as Argo CD or Flux, continuously pulls that state and reconciles the live system to match. This improves security because every change is reviewed and recorded in Git, drift is detected automatically, and CI pipelines no longer need powerful credentials to production. The new risk is that Git itself becomes the control point, so repository access, branch protection and secret handling must be strict.
Key takeaways
- OpenGitOps defines four principles: declarative, versioned and immutable, pulled automatically, continuously reconciled.
- Pull-based deployment removes production credentials from CI systems.
- Policy as code catches insecure infrastructure before it is created.
- Never store plain secrets in Git; use sealed or external secrets.
- Protect Terraform state; it often contains sensitive values.
Table of Contents
- What Are the GitOps Principles?
- How Does GitOps Improve Security?
- What Does a Secure Repository Layout Look Like?
- How Do You Set Up Argo CD Safely?
- How Do You Secure Terraform?
- How Do You Handle Secrets in GitOps?
- What New Risks Does GitOps Create?
- References
What Are the GitOps Principles?
The CNCF-hosted OpenGitOps project defines four principles:
- Declarative: the desired state is described, not scripted step by step.
- Versioned and immutable: the desired state is stored with full history.
- Pulled automatically: agents fetch the desired state from the source.
- Continuously reconciled: agents correct any difference between desired and actual state.
How Does GitOps Improve Security?
| Push-based CI/CD | Pull-based GitOps |
|---|---|
| CI system holds cluster or cloud admin credentials | Agent inside the cluster pulls; CI needs no production access |
| Manual hotfixes drift from code | Drift detected and reverted or alerted |
| Audit trail spread across tools | Every change is a reviewed Git commit |
| Rollback by re-running jobs | Rollback by reverting a commit |
What Does a Secure Repository Layout Look Like?
platform-config/
apps/
orders/
base/ # Kubernetes manifests or Helm values
overlays/dev/
overlays/prod/ # changes here need two approvals (CODEOWNERS)
infra/
modules/ # reviewed Terraform modules
envs/prod/ # prod infrastructure, protected branch
policies/ # OPA/Conftest and Kyverno policies
.github/CODEOWNERS
Use branch protection, required reviews, signed commits and CODEOWNERS so production changes need approval from the right people.
How Do You Set Up Argo CD Safely?
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: orders-prod
namespace: argocd
spec:
project: prod # AppProject restricts allowed repos and namespaces
source:
repoURL: https://github.com/example/platform-config.git
targetRevision: main
path: apps/orders/overlays/prod
destination:
server: https://kubernetes.default.svc
namespace: orders
syncPolicy:
automated: { prune: true, selfHeal: true }
Restrict each AppProject to specific source repositories and destination namespaces, put Argo CD behind SSO, disable the default admin account, and keep it off the public internet. See the Argo CD documentation; Flux offers equivalent controls.
How Do You Secure Terraform?
- Protect state: store it in an encrypted, access-controlled remote backend with locking. State can contain secrets.
- Scan before apply: run Checkov or Conftest policies in pull requests.
- Review plans: show the
terraform planoutput in the pull request. - Least privilege: the identity that applies Terraform should only manage the resources in its scope.
- Detect drift: run scheduled plans and alert on unexpected changes.
# In CI for every pull request
terraform init -input=false
terraform plan -out=tfplan -input=false
terraform show -json tfplan > tfplan.json
checkov -f tfplan.json --framework terraform_plan --hard-fail-on HIGH
How Do You Handle Secrets in GitOps?
- Sealed Secrets: encrypt secrets with a cluster key so only the cluster can decrypt them; the encrypted form can live in Git.
- External Secrets Operator: Git holds only a reference; the operator fetches the value from AWS Secrets Manager, Azure Key Vault, Vault or similar.
- Scan repositories for accidental plain secrets and rotate anything exposed.
What New Risks Does GitOps Create?
| Risk | Mitigation |
|---|---|
| Compromised Git account pushes malicious config | MFA, branch protection, required reviews, signed commits |
| GitOps agent over-privileged | Scope projects and RBAC per namespace |
| Exposed Argo CD dashboard | SSO, network restriction, no default admin |
| Malicious Helm chart or image | Pin versions and digests, verify signatures with admission policies |
NIST SP 800-204C covers DevSecOps for cloud-native applications. Related: DevOps security practices and Terraform interview questions. WebAsha offers Kubernetes and Terraform training.
References
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0