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.

Jul 24, 2025 - 16:41
Updated: 
102.3k

Cloud & Container Security in Cybersecurity & Ethical Hacking · Explainer

DevOps & Platform Engineering

GitOps and Secure Infrastructure as Code: A Practical Guide

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?

The CNCF-hosted OpenGitOps project defines four principles:

  1. Declarative: the desired state is described, not scripted step by step.
  2. Versioned and immutable: the desired state is stored with full history.
  3. Pulled automatically: agents fetch the desired state from the source.
  4. Continuously reconciled: agents correct any difference between desired and actual state.

How Does GitOps Improve Security?

Push-based CI/CDPull-based GitOps
CI system holds cluster or cloud admin credentialsAgent inside the cluster pulls; CI needs no production access
Manual hotfixes drift from codeDrift detected and reverted or alerted
Audit trail spread across toolsEvery change is a reviewed Git commit
Rollback by re-running jobsRollback 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 plan output 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?

RiskMitigation
Compromised Git account pushes malicious configMFA, branch protection, required reviews, signed commits
GitOps agent over-privilegedScope projects and RBAC per namespace
Exposed Argo CD dashboardSSO, network restriction, no default admin
Malicious Helm chart or imagePin 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

An operating model where Git holds the desired state and an agent continuously pulls and reconciles the live system to match.

IaC describes infrastructure in code; GitOps adds pull-based automation and continuous reconciliation from Git.

The CI system no longer needs production credentials, because the agent inside the environment pulls changes.

Argo CD and Flux are the most common, both CNCF projects.

As sealed (encrypted) secrets or references used by an external secrets operator, never as plain text.

State files can contain passwords and keys, so store them in an encrypted, access-controlled backend.

Comparing live infrastructure with the declared state and alerting or correcting when they differ.

A compromised Git account or weak branch protection, since Git controls what is deployed.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.