image/svg+xml Checkov home
  • Docs
    • Quick start
    • Overview
    • Integrations
  • Download
  • Docs
    • Quick start
    • Overview
    • Integrations

Checkov Documentation

  • 1.Welcome
    • What is Checkov?
    • Terms and Concepts
    • Quick Start
    • Feature Descriptions
    • Migration
  • 2.Basics
    • Installing Checkov
    • CLI Command Reference
    • Suppressing and Skipping Policies
    • Hard and soft fail
    • Scanning Credentials and Secrets
    • Reviewing Scan Results
    • Visualizing Checkov Output
    • Handling Variables
  • 3.Custom Policies
    • Custom Policies Overview
    • Python Custom Policies
    • YAML Custom Policies
    • Custom YAML Policies Examples
    • Sharing Custom Policies
  • 4.Integrations
    • Jenkins
    • Bitbucket Cloud Pipelines
    • GitHub Actions
    • GitLab CI
    • Kubernetes
    • Pre-Commit Hooks
    • Docker
  • 5.Policy Index
    • all resource scans
    • ansible resource scans
    • argo_workflows resource scans
    • arm resource scans
    • azure_pipelines resource scans
    • bicep resource scans
    • bitbucket_configuration resource scans
    • bitbucket_pipelines resource scans
    • circleci_pipelines resource scans
    • cloudformation resource scans
    • dockerfile resource scans
    • github_actions resource scans
    • github_configuration resource scans
    • gitlab_ci resource scans
    • gitlab_configuration resource scans
    • kubernetes resource scans
    • openapi resource scans
    • secrets resource scans
    • serverless resource scans
    • terraform resource scans
  • 6.Contribution
    • Checkov Runner Contribution Guide
    • Implementing CI Metadata extractor
      • How to implement a new Run metadata extractor?
    • Implementing ImageReferencer
    • Contribution Overview
    • Contribute Python-Based Policies
    • Contribute YAML-based Policies
    • Contribute New Terraform Provider
    • Contribute New Argo Workflows configuration policy
    • Contribute New Azure Pipelines configuration policy
    • Contribute New Bitbucket configuration policy
    • Contribute New GitHub configuration policy
    • Contribute New Gitlab configuration policy
  • 7.Scan Examples
    • Terraform Plan Scanning
    • Terraform Scanning
    • Helm
    • Kustomize
    • AWS SAM configuration scanning
    • Ansible configuration scanning
    • Argo Workflows configuration scanning
    • Azure ARM templates configuration scanning
    • Azure Pipelines configuration scanning
    • Azure Bicep configuration scanning
    • Bitbucket configuration scanning
    • AWS CDK configuration scanning
    • Cloudformation configuration scanning
    • Dockerfile configuration scanning
    • GitHub configuration scanning
    • Gitlab configuration scanning
    • Kubernetes configuration scanning
    • OpenAPI configuration scanning
    • SCA scanning
    • Serverless framework configuration scanning
  • 8.Outputs
    • CSV
    • CycloneDX BOM
    • GitLab SAST
    • JUnit XML
    • SARIF
  • Docs
  • 6.contribution
  • Implementing CI Metadata extractor
Edit on GitHub

Implementing CI Metadata extractor

CI/CD jobs have environment variables that can enrich the execution context. Attributes like:

  1. Author of the run
  2. Commit sha
  3. Pull request ID
  4. Link to the host running the CI

Those attributes can be added by reading environment variables published on the public docs of CI/CD vendors. Examples: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html https://docs.github.com/en/actions/learn-github-actions/environment-variables

How to implement a new Run metadata extractor?

You’ll need to implement a new class derived from RunMetaDataExtractor and commit it into the directory checkov/common/bridgecrew/run_metadata/extractors. Example:

import os

from checkov.common.bridgecrew.run_metadata.abstract_run_metadata_extractor import RunMetaDataExtractor


class GithubActionsRunMetadataExtractor(RunMetaDataExtractor):
    def is_current_ci(self):
        if os.getenv("GITHUB_ACTIONS", ""):
            return True
        return False

    def __init__(self):
        server_url = os.getenv('GITHUB_SERVER_URL', '')
        from_branch = os.getenv('GIT_BRANCH', "master")
        to_branch = os.getenv('GITHUB_BASE_REF', "")
        pr_id = os.getenv("$GITHUB_REF", "//").split("/")
        repository = os.getenv('GITHUB_REPOSITORY', "")
        pr_url = f"{server_url}/{repository}/pull/{pr_id}"
        commit_hash = os.getenv("GITHUB_SHA", "")
        commit_url = f"{server_url}/{repository}/commit/${commit_hash}"
        author_name = os.getenv("GITHUB_ACTOR", "")
        author_url = f"{server_url}/{author_name}"
        run_id = os.getenv("GITHUB_RUN_NUMBER", "")
        run_url = f"{server_url}/{repository}/actions/runs/{run_id}"
        repository_url = f"{server_url}/{repository}"

        super().__init__(from_branch=from_branch,
                         to_branch=to_branch,
                         pr_id=pr_id,
                         pr_url=pr_url,
                         commit_hash=commit_hash,
                         commit_url=commit_url,
                         author_name=author_name,
                         author_url=author_url,
                         run_id=run_id,
                         run_url=run_url,
                         repository_url=repository_url)


extractor = GithubActionsRunMetadataExtractor()

Powered By

  • Slack Community
  • Prisma Cloud
  • Terms of use
  • GitHub
  • Docs
  • Privacy policy