Skip to content

Instantly share code, notes, and snippets.

@schosterbarak
Created March 1, 2022 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schosterbarak/3beeb93b247291a494d0771c40b73f70 to your computer and use it in GitHub Desktop.
Save schosterbarak/3beeb93b247291a494d0771c40b73f70 to your computer and use it in GitHub Desktop.
import re
from checkov.common.models.enums import CheckResult
from checkov.terraform.checks.module.base_module_check import BaseModuleCheck
MODULE_GIT_VERSION_PATTERN = re.compile(r"git::https?:\/\/[^\/]+\/.+.git\?ref=(\b[0-9a-f]{5,40}\b)")
class ModuleSourceHashCheck(BaseModuleCheck):
def __init__(self):
name = "Ensure module is immutable using commit hash"
id = "CKV_TF_MODULE_2"
supported_resources = ['module']
categories = []
super().__init__(name=name, id=id, categories=categories, supported_resources=supported_resources)
def scan_module_conf(self, conf):
"""
Some test for module source
:param conf: module call
:return: <CheckResult>
"""
source = conf.get('source', [])
if not source:
# source is using latest or tagged version
return CheckResult.FAILED
else:
if MODULE_GIT_VERSION_PATTERN.match(source[0]):
# immutable source is being used
return CheckResult.PASSED
# non immutable source is used
return CheckResult.FAILED
scanner = ModuleSourceHashCheck()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment