Aller au contenu principal

Aanalyse de code d'infra avec Regula

· 5 minutes de lecture
Stéphane ROBERT
Consultant DevOps

Comme quoi la vielle permet de découvrir des pépites et je nomme aujourd'hui. regula est un outil écrit en GO, développé par Fugue, permettant d'évaluer la conformité en termes de sécurité d'un code d'Infra As Code.

Donc contrairement à d'autres outils comme popeye ou polaris, c'est un outil d'analyse statique de code. Il prend en charge les langages d'Infra As Code suivant :

  • les templates CloudFormation (JSON/YAML)
  • Terraform (HCL/JSON)
  • les manifests Kubernetes (YAML
  • les modèles JSON Azure Resource Manager (ARM)

regula est livré sous la forme d'un simple binaire faisant appel à une bibliothèque de règles écrites en Rego, le langage de politique utilisé par le projet Open Policy Agent (OPA).

Autre point important, ces règles peuvent être complétées facilement pour intégrer vos propres règles. Ainsi vous pouvez vérifier que vos propres bonnes pratiques sont appliqués.

Installation de Regula

Fugue livre régulièrement les binaires de son outil et voyons comment l'installer via un simple curl :

wget https://github.com/fugue/regula/releases/download/v2.4.0/regula_2.4.0_Linux_x86_64.tar.gz -O - | sudo tar -xz -C /usr/local/bin

J'ai écris un plugin asdf pour installer plusieurs versions de regula:

asdf plugin add regula https://github.com/stephrobert/asdf-regula.git
# Show all installable versions
asdf list-all regula
# Install latest version
asdf install regula latest

# Set a version globally (on your ~/.tool-versions file)
asdf global regula latest

# Now regula commands are available
regula --help

Utilisation de Regula

Comme il s'agit d'un CLI elle utilise des options qui sont les suivantes :

regula help

Regula

Usage:
  regula [command]

Available Commands:
  completion        generate the autocompletion script for the specified shell
  help              Help about any command
  init              Create a new Regula configuration file in the current working directory.
  repl              Start an interactive session for testing rules with Regula
  run               Evaluate rules against infrastructure as code with Regula.
  show              Show debug information.
  test              Run OPA test with Regula.
  version           Print version information.
  write-test-inputs Persist dynamically-generated test inputs for use with other Rego interpreters

Flags:
  -h, --help      help for regula
  -v, --verbose   verbose output

Use "regula [command] --help" for more information about a command.

regula help run

Evaluate rules against infrastructure-as-code contained in one or more paths.

....

Output formats:
    text    A human friendly format (default)
    json    A JSON report containing rule results and a summary
    table   An ASCII table of rule results
    junit   The JUnit XML format
    tap     The Test Anything Protocol format
    compact An alternate, more compact human friendly format
    none    Do not print any output on stdout

Input types:
    auto        Automatically determine input types (default)
    tf-plan     Terraform plan JSON
    cfn         CloudFormation template in YAML or JSON format
    tf          Terraform directory or file
    k8s         Kubernetes manifest in YAML format
    arm         Azure Resource Manager (ARM) JSON templates (feature in preview)

Severities:
    unknown         Lowest setting. Used for rules without a severity specified (default)
    informational
    low
    medium
    high
    critical
    off             Never exit with a non-zero exit code.

Usage:
  regula run [input...] [flags]

Flags:
  -c, --config string           Path to .regula.yaml file. By default regula will look in the current working directory and its parents.
  -e, --environment-id string   Environment ID in Fugue
  -x, --exclude strings         Rule IDs or names to exclude. Can be specified multiple times.
  -f, --format string           Set the output format (default "text")
  -h, --help                    help for run
  -i, --include strings         Specify additional rego files or directories to include
  -t, --input-type strings      Search for or assume the input type for the given paths. Can be specified multiple times. (default [auto])
  -n, --no-built-ins            Disable built-in rules
      --no-config               Do not look for or load a regula config file.
      --no-ignore               Disable use of .gitignore
  -o, --only strings            Rule IDs or names to run. All other rules will be excluded. Can be specified multiple times.
  -s, --severity string         Set the minimum severity that will result in a non-zero exit code. (default "unknown")
      --sync                    Fetch rules and configuration from Fugue
      --upload                  Upload rule results to Fugue

Global Flags:
  -v, --verbose   verbose output

En parcourant ces options on peut voir qu'il est très bien pensé pour être utilisé dans une chaine CI/CD. Rien que la prise en charge en entrée comme en sortie de plusieurs formats permet de l'intégrer à d'autres outils. Si vous utilisez Helm ou Kustomize il faudra générer les manifests avant de lancer regula.

On retrouve une option -s qui permet de désactiver (pas bien) voir d'augmenter le niveau de criticité pour sortir en erreur.

Lançons-le sur ma stack de side-car qui a été écrite vite faite et voyons ce qu'il retourne :

regula run .
FG_R00484: Service account 'automountServiceAccountToken' should be set to 'false' [Medium]
           https://docs.fugue.co/FG_R00484.html

  [1]: Deployment.default.app
       in app-deployment.yml:1:1

  [2]: Deployment.default.app
       in manifests.yml:1:1

FG_R00490: Pods should not run containers as the root user [Medium]
           https://docs.fugue.co/FG_R00490.html

  [1]: Deployment.default.app
       in app-deployment.yml:1:1

  [2]: Deployment.default.app
       in manifests.yml:1:1

FG_R00491: Pods should not run containers with the NET_RAW capability [Medium]
           https://docs.fugue.co/FG_R00491.html

  [1]: Deployment.default.app
       in app-deployment.yml:1:1

  [2]: Deployment.default.app
       in manifests.yml:1:1

FG_R00493: Pods should not run containers with default capabilities assigned [Medium]
           https://docs.fugue.co/FG_R00493.html

  [1]: Deployment.default.app
       in app-deployment.yml:1:1

  [2]: Deployment.default.app
       in manifests.yml:1:1

FG_R00495: Pod seccomp profile should be set to 'docker/default' [Medium]
           https://docs.fugue.co/FG_R00495.html

  [1]: Deployment.default.app
       in app-deployment.yml:1:1

  [2]: Deployment.default.app
       in manifests.yml:1:1

FG_R00496: Pods and containers should apply a security context [Medium]
           https://docs.fugue.co/FG_R00496.html

  [1]: Deployment.default.app
       in app-deployment.yml:1:1

  [2]: Deployment.default.app
       in manifests.yml:1:1

Tout est documenté. J'ai suivi quelques liens et c'est vraiment bien expliqué. Y a plus qu'à l'intégrer dans un sonarqube pour avoir de beau dashboard ! J'adore cet outil est vraiment pensé pour une chaine CI/CD Gitops. Un "must have" !