-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(secrets): Add Terraform multiline secrets handling (#3907)
* add multiline secret support in Terraform * support jsonencode and heredoc and ignore data blocks * fix PR comments * create a dedicated single line parser class * add buildx step to build the image on self-hosted runners
- Loading branch information
Showing
22 changed files
with
597 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
|
||
|
||
class BaseMultiLineParser(ABC): | ||
|
||
def get_lines_from_same_object( | ||
self, | ||
search_range: range, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Any, TYPE_CHECKING | ||
|
||
|
||
if TYPE_CHECKING: | ||
from detect_secrets.core.potential_secret import PotentialSecret | ||
from detect_secrets.plugins.high_entropy_strings import Base64HighEntropyString, HexHighEntropyString | ||
from detect_secrets.util.code_snippet import CodeSnippet | ||
|
||
|
||
class BaseSingleLineParser(ABC): | ||
def detect_secret( | ||
self, | ||
scanners: tuple[Base64HighEntropyString, HexHighEntropyString], | ||
filename: str, | ||
raw_context: CodeSnippet | None, | ||
line: str, | ||
line_number: int = 0, | ||
**kwargs: Any, | ||
) -> set[PotentialSecret]: | ||
for entropy_scanner in scanners: | ||
matches = entropy_scanner.analyze_line(filename, line, line_number, **kwargs) | ||
if matches: | ||
if raw_context and self.ignore_secret(raw_context=raw_context): | ||
return set() | ||
|
||
return matches | ||
return set() | ||
|
||
@abstractmethod | ||
def ignore_secret(self, raw_context: CodeSnippet) -> bool: | ||
"""Check for false-positive secrets by leveraging the context""" | ||
|
||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
from typing import TYPE_CHECKING | ||
|
||
from checkov.secrets.parsers.multiline_parser import BaseMultiLineParser | ||
|
||
if TYPE_CHECKING: | ||
from detect_secrets.util.code_snippet import CodeSnippet | ||
|
||
START_OBJ = re.compile(r"^\s*\w*\s*=?\s*{\s*$") | ||
END_OBJ = re.compile(r"^\s*}\s*$") | ||
COMMENT_PREFIX = re.compile(r"^[\s]*(#|//)") | ||
|
||
|
||
class TerraformMultiLineParser(BaseMultiLineParser): | ||
def consecutive_lines_in_same_object( | ||
self, | ||
raw_context: CodeSnippet | None, | ||
other_line_idx: int, | ||
) -> bool: | ||
return bool(raw_context and 0 <= other_line_idx < len(raw_context.lines)) | ||
|
||
@staticmethod | ||
def is_object_start(line: str) -> bool: | ||
return bool(re.match(START_OBJ, line)) | ||
|
||
@staticmethod | ||
def is_object_end(line: str) -> bool: | ||
return bool(re.match(END_OBJ, line)) | ||
|
||
@staticmethod | ||
def is_line_comment(line: str) -> bool: | ||
return bool(re.match(COMMENT_PREFIX, line)) | ||
|
||
|
||
terraform_multiline_parser = TerraformMultiLineParser() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from checkov.secrets.parsers.single_line_parser import BaseSingleLineParser | ||
|
||
if TYPE_CHECKING: | ||
from detect_secrets.util.code_snippet import CodeSnippet | ||
|
||
|
||
class TerraformSingleLineParser(BaseSingleLineParser): | ||
def ignore_secret(self, raw_context: CodeSnippet) -> bool: | ||
return self.ignore_terraform_data_block(raw_context=raw_context) | ||
|
||
def ignore_terraform_data_block(self, raw_context: CodeSnippet) -> bool: | ||
"""Check for a possible data block usage""" | ||
|
||
# search backwards to find a possible 'data' block | ||
for line_index in range(raw_context.target_index - 1, -1, -1): | ||
if raw_context.lines[line_index].lstrip().startswith('data "'): | ||
# a data block is typically used to get remote information, | ||
# therefore can retrieve a secret, but has not a hardcoded secret | ||
return True | ||
|
||
return False | ||
|
||
|
||
terraform_single_line_parser = TerraformSingleLineParser() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
resource "aws_cloudformation_stack" "lambda" { | ||
name = "lambda" | ||
|
||
parameters = { | ||
VPCCidr = "10.0.0.0/16" | ||
} | ||
|
||
template_body = <<STACK | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Description: VPC function. | ||
Resources: | ||
Function: | ||
Type: AWS::Lambda::Function | ||
Properties: | ||
Handler: index.handler | ||
Role: arn:aws:iam::123456789012:role/lambda-role | ||
Code: | ||
S3Bucket: my-bucket | ||
S3Key: function.zip | ||
Runtime: nodejs12.x | ||
Timeout: 5 | ||
TracingConfig: | ||
Mode: Active | ||
VpcConfig: | ||
SecurityGroupIds: | ||
- sg-085912345678492fb | ||
SubnetIds: | ||
- subnet-071f712345678e7c8 | ||
- subnet-07fd123456788a036 | ||
Tags: | ||
- Key: "SOME_NAME" | ||
Value: "some_value" | ||
# name1 & value1 are not valid arguments | ||
- Value: "Zo5Zhexnf9TUggdn+zBKGEkmUUvuKzVN+/fKPaMBA4zVyef4irH5H5YfwoC4IqAX0DNoMD12yIF67nIdIMg13atW4WM33eNMfXlE" | ||
Key: "TEST_PASSWORD_1" | ||
Key1: "TEST_PASSWORD_2" | ||
Value1: "1Vab3xejyUlh89P6tUJNXgO4t07DzmomF4tPBwTbwt+sjXHg3G0MPMRpH/I2ho4gS5H3AKJkvJZj87V7/Qnp/rHdbMVYK1F0BX35" | ||
- Key: "TEST_PASSWORD_3" | ||
# comment 1 | ||
# comment 2 | ||
# comment 3 | ||
Value: "PtpfIZR+zZGPUWUYvLojqylVeEg63CBYN0FpGJ4yuH+9YxZZe8Uq7drEoTSfL64kElPEnVJk+H7SZr+wBoxN5qDWsbDmmUS2H76h" | ||
- Value: "emDJTiv6H/hP6I8Tmr5+kUdpBIQDrXMwFO7AkmbwROf3rM6uNToJlIJW7H5ApfPmSGU0oWBwflV6Cd9pPu5nEvgxt4YMHZ0SQ85z" | ||
# comment 1 | ||
Key: "TEST_PASSWORD_4" | ||
- Key: "TEST_PASSWORD_LONG_1" | ||
Value: "m9+1ONt6FdpnByhlaKDwZ/jjA5gaPzrKY9q5G8cr6kjn092ogigwEOGGryjDqq/NkX1DnKGGG7iduJUJ48+Rv0tgpdVAxwLQuiszRnssmi2ck/Zf1iDFlNQtiE8rvXE6OTCsb6mrpyItLOVnEwsRSpggyRa3KLSuiguiZsK5KyXQ6BsiAclpLvz6QFBQoQkZNxownQrqgLwVwkK1gW0/EEm0m1ylz20ZeLgYO6tRSvKDW0lrgAI7g60F7/eJGv1UqQlxK58T+7u1UX/K11Q69e9jJE+LkQ932eY37U70oVbBVchHwSFKUoffernEaG9XP1tyEpIptPqVpcS2BMpktoR1p1yyWuxC5GsPc2RlPQzEbs3n5lPPnC/uEVu7/cJENSw5+9DzigiHYPz1Cq/p5HedIl5ysn2U2VFgHWekGBYin6ytfmF2Sx+hYqeRd6RcxyU434CXspWQqc330sp9q7vwPQHNecBrvG2Iy7mqVSvaJDnkZ8AN" | ||
- Key: "TEST_PASSWORD_no_password" | ||
Value: "RandomP@ssw0rd" | ||
STACK | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
data "google_secret_manager_secret_version" "secret" { | ||
secret = "somesecretid" | ||
} |
Oops, something went wrong.