Closed scottford-io closed 1 year ago
@chris-rock was able to properly diagnose this, and it is not a bug. Per @chris-rock
.where
is not a truthy condition as you use infilters: asset.platform == "terraform-hcl" && terraform.resources.where( nameLabel == "aws_account_alternate_contact")
I updated the filter to filters: asset.platform == "terraform-hcl" && terraform.resources.any( nameLabel == "aws_account_alternate_contact")
and the issue was resolved.
Describe the bug
I have defined an asset filter in a policy for scanning
terraform-hcl
that uses&&
requiring that both queries in the filter match in order for the check to run against a specific Terraform resource. The filter does not respect the&&
and will incorrectly run against Terraform code that only matches one side of the expression.The check that is incorrectly executing contains the following asset filter:
In order for the check to run it should match both sides of the expression:
asset.platform == "terraform-hcl"
nameLabel == "aws_account_alternate_contact"
What I see is that if I scan a folder that does not contain any
aws_account_alternate_contact
resources, the check runs. cnspec shell validates that the folder I am scanning does not contain anyaws_account_alternate_contact
resources:NOTE THIS DOES NOT HAPPEN WHEN SCANNING TERRAFORM PLAN FILES
To Reproduce
create
terraform/autoscaling/main.tf
Create security policy
uid: aws-security-hub-security-account-information-provided-terraform-plan filters: asset.platform == "terraform-plan" && terraform.plan.resourceChanges.any( type == "aws_account_alternate_contact" ) mql: terraform.plan.resourceChanges.where( type == "aws_account_alternate_contact" ).one( change.after['alternate_contact_type'] == "SECURITY" ) docs: remediation:
id: terraform desc: | The
aws_account_alternate_contact
manages the specified alternate contact attached to an AWS Account.uid: aws-security-hub-security-account-part-of-organizations title: '[Account.2] AWS accounts should be part of an AWS Organizations organization' impact: 80 docs: desc: | Related requirements: NIST.800-53.r5 CA-9(1), NIST.800-53.r5 CM-2
variants:
uid: aws-security-hub-security-account-part-of-organizations-api filters: asset.platform == "aws" mql: aws.account.organization.masterAccountId != null docs: remediation:
uid: aws-security-hub-acm-certificate-expiration-check title: '[ACM.1] Imported and ACM-issued certificates should be renewed after a specified time period' impact: 50 props:
uid: acmDaysToExpiration mql: return 30 docs: desc: | Related requirements: NIST.800-53.r5 SC-28(3), NIST.800-53.r5 SC-7(16)
Category: Protect > Data protection > Encryption of data in transit
Impact: Medium
Resource type:
AWS::ACM::Certificate
This checks whether ACM certificates in your account are marked for expiration within 30 days. It checks both imported certificates and certificates provided by AWS Certificate Manager.
ACM can automatically renew certificates that use DNS validation. For certificates that use email validation, you must respond to a domain validation email. ACM does not automatically renew certificates that you import. You must renew imported certificates manually.
For more information about managed renewal for ACM certificates, see Managed renewal for ACM certificates in the AWS Certificate Manager User Guide. variants:
uid: aws-security-hub-acm-certificate-expiration-check-api filters: asset.platform == "aws" mql: aws.acm.certificates.where( status != /PENDING_VALIDATION/ ).all (notAfter - notBefore <= props.acmDaysToExpiration * time.day) docs: remediation:
id: console desc: | ACM provides managed renewal for your SSL/TLS certificates issued by Amazon. This means that ACM either renews your certificates automatically (if you use DNS validation), or it sends you email notices when the certificate expiration approaches. These services are provided for both public and private ACM certificates.
uid: aws-security-hub-acm-certificate-rsa-check title: '[ACM.2] RSA certificates managed by ACM should use a key length of at least 2,048 bits' impact: 80 docs: desc: | Category: Identify > Inventory > Inventory services
variants:
uid: aws-security-hub-acm-certificate-rsa-check-api filters: asset.platform == "aws" mql: true docs: remediation:
id: console desc: | The minimum key length for RSA certificates issued by ACM is already 2,048 bits. For instructions on issuing new RSA certificates with ACM, see Issuing and managing certificates in the AWS Certificate Manager User Guide.
uid: aws-security-hub-api-gw-execution-logging-enabled title: '[APIGateway.1] API Gateway REST and WebSocket API execution logging should be enabled' impact: 50 docs: desc: | Related requirements: NIST.800-53.r5 AC-4(26), NIST.800-53.r5 AU-10, NIST.800-53.r5 AU-12, NIST.800-53.r5 AU-2, NIST.800-53.r5 AU-3, NIST.800-53.r5 AU-6(3), NIST.800-53.r5 AU-6(4), NIST.800-53.r5 CA-7, NIST.800-53.r5 SC-7(9), NIST.800-53.r5 SI-7(8)
variants:
uid: aws-security-hub-api-gw-execution-logging-enabled-api filters: asset.platform == "aws" mql: aws.apigateway.restApis.all( stages.all( methodSettings.values.all( ["LoggingLevel"] == "ERROR" || ["LoggingLevel"] == "INFO" ))) docs: remediation:
uid: aws-security-hub-api-gw-ssl-enabled title: '[APIGateway.2] API Gateway REST API stages should be configured to use SSL certificates for backend authentication' impact: 50 docs: desc: | This checks whether Amazon API Gateway REST API stages have SSL certificates configured. Backend systems use these certificates to authenticate that incoming requests are from API Gateway.
variants:
uid: aws-security-hub-api-gw-ssl-enabled-api filters: asset.platform == "aws" mql: true docs: remediation:
uid: aws-security-hub-api-gw-xray-enabled title: '[APIGateway.3] API Gateway REST API stages should have AWS X-Ray tracing enabled' impact: 10 docs: desc: | Related requirements: NIST.800-53.r5 CA-7
variants:
uid: aws-security-hub-api-gw-xray-enabled-api filters: asset.platform == "aws" mql: true docs: remediation:
uid: aws-security-hub-api-gw-associated-with-waf title: '[APIGateway.4] API Gateway should be associated with a WAF Web ACL' impact: 50 docs: desc: | Related requirements: NIST.800-53.r5 AC-4(21)
variants:
uid: aws-security-hub-api-gw-associated-with-waf-api filters: asset.platform == "aws" mql: true docs: remediation:
uid: aws-security-hub-api-gw-cache-encrypted title: '[APIGateway.5] API Gateway REST API cache data should be encrypted at rest' impact: 50 docs: desc: | Related requirements: NIST.800-53.r5 CA-9(1), NIST.800-53.r5 CM-3(6), NIST.800-53.r5 SC-13, NIST.800-53.r5 SC-28, NIST.800-53.r5 SC-28(1), NIST.800-53.r5 SC-7(10), NIST.800-53.r5 SI-7(6)
variants:
uid: aws-security-hub-api-gw-cache-encrypted-api filters: asset.platform == "aws" mql: true docs: remediation:
uid: aws-security-hub-api-gwv2-authorization-type-configured title: '[APIGateway.8] API Gateway routes should specify an authorization type' impact: 50 docs: desc: | Related requirements: NIST.800-53.r5 AC-3, NIST.800-53.r5 CM-2, NIST.800-53.r5 CM-2(2)
variants:
uid: aws-security-hub-api-gwv2-authorization-type-configured-api filters: asset.platform == "aws" mql: true docs: remediation:
uid: aws-security-hub-api-gwv2-access-logs-enabled title: '[APIGateway.9] Access logging should be configured for API Gateway V2 Stages' impact: 50 docs: desc: | Related requirements: NIST.800-53.r5 AC-4(26), NIST.800-53.r5 AU-10, NIST.800-53.r5 AU-12, NIST.800-53.r5 AU-2, NIST.800-53.r5 AU-3, NIST.800-53.r5 AU-6(3), NIST.800-53.r5 AU-6(4), NIST.800-53.r5 CA-7, NIST.800-53.r5 SC-7(9), NIST.800-53.r5 SI-7(8)
variants:
uid: aws-security-hub-api-gwv2-access-logs-enabled-api filters: asset.platform == "aws" mql: true docs: remediation:
uid: aws-security-hub-appsync-logging-enabled title: '[AppSync.2] AWS AppSync should have request-level and field-level logging turned on' impact: 50 docs: desc: | Category: Identify > Logging
variants:
uid: aws-security-hub-appsync-logging-enabled-api filters: asset.platform == "aws" mql: true docs: remediation:
uid: aws-security-hub-athena-workgroup-encrypted-at-rest title: '[Athena.1] Athena workgroups should be encrypted at rest' impact: 50 docs: desc: | Related requirements: NIST.800-53.r5 CA-9(1), NIST.800-53.r5 CM-3(6), NIST.800-53.r5 SC-13, NIST.800-53.r5 SC-28, NIST.800-53.r5 SC-28(1), NIST.800-53.r5 SC-7(10), NIST.800-53.r5 SI-7(6)
variants:
uid: aws-security-hub-athena-workgroup-encrypted-at-rest-api filters: asset.platform == "aws" mql: true docs: remediation:
uid: aws-security-hub-autoscaling-group-elb-healthcheck-required title: '[AutoScaling.1] Auto Scaling groups associated with a Classic Load Balancer should use load balancer health checks' impact: 10 docs: desc: | Related requirements: PCI DSS v3.2.1/2.2, NIST.800-53.r5 CA-7, NIST.800-53.r5 CP-2(2), NIST.800-53.r5 SI-2
variants:
uid: aws-security-hub-autoscaling-group-elb-healthcheck-required-api filters: asset.platform == "aws" mql: aws.autoscaling.groups.where(loadBalancerNames.length > 0).all( healthCheckType == "ELB" ) docs: remediation:
uid: aws-security-hub-autoscaling-multiple-az title: '[AutoScaling.2] Amazon EC2 Auto Scaling group should cover multiple Availability Zones' impact: 50 docs: desc: | Related requirements: NIST.800-53.r5 CP-10, NIST.800-53.r5 CP-2(2), NIST.800-53.r5 CP-6(2), NIST.800-53.r5 SC-36, NIST.800-53.r5 SC-5(2), NIST.800-53.r5 SI-13(5)
variants:
uid: aws-security-hub-autoscaling-multiple-az-api filters: asset.platform == "aws" mql: |
TO DO
true docs: remediation:
uid: aws-security-hub-autoscaling-launchconfig-requires-imdsv2 title: '[AutoScaling.3] Auto Scaling group launch configurations should configure EC2 instances to require Instance Metadata Service Version 2 (IMDSv2)' impact: 80 docs: desc: | Related requirements: NIST.800-53.r5 AC-3, NIST.800-53.r5 AC-3(15), NIST.800-53.r5 AC-3(7), NIST.800-53.r5 AC-6, NIST.800-53.r5 CA-9(1), NIST.800-53.r5 CM-2
variants:
uid: aws-security-hub-autoscaling-launchconfig-requires-imdsv2-api filters: asset.platform == "aws" mql: |
TO DO
true docs: remediation:
uid: aws-security-hub-autoscaling-launchconfig-requires-imdsv2-terraform-hcl filters: asset.platform == "terraform-hcl" && terraform.resources.any( nameLabel == "aws_launch_configuration" ) mql: terraform.resources.where( nameLabel == "aws_launch_configuration" ).all( blocks.where( type == "metadata_options" ).all( arguments['http_endpoint'] == 'disabled' || arguments['http_tokens'] == 'required' )) docs: remediation:
id: terraform desc: |
Require the use of IMDSv2 when requesting instance metadata
uid: aws-security-hub-autoscaling-launchconfig-requires-imdsv2-terraform-plan filters: asset.platform == "terraform-plan" && terraform.plan.resourceChanges.any( type == "aws_launch_configuration" ) mql: terraform.plan.resourceChanges.where( type == "aws_launch_configuration" ).all( change.after['metadata_options'][0]['http_tokens'] == "required" || change.after['metadata_options'][0]['http_endpoint'] == "disabled" ) docs: remediation:
id: terraform desc: |
Require the use of IMDSv2 when requesting instance metadata
uid: aws-security-hub-autoscaling-launch-config-hop-limit title: '[AutoScaling.4] Auto Scaling group launch configuration should not have a metadata response hop limit greater than 1' impact: 80 docs: desc: | Related requirements: NIST.800-53.r5 CA-9(1), NIST.800-53.r5 CM-2, NIST.800-53.r5 CM-2(2)
variants:
uid: aws-security-hub-autoscaling-launch-config-hop-limit-api filters: asset.platform == "aws" mql: |
TO DO
true docs: remediation:
uid: aws-security-hub-autoscaling-launch-config-hop-limit-terraform-hcl filters: asset.platform == "terraform-hcl" && terraform.resources.any( nameLabel == "aws_launch_configuration" ) mql: terraform.resources.where( nameLabel == "aws_launch_configuration" ).all( blocks.where( type == "metadata_options" ).one( arguments['http_put_response_hop_limit'] <= 1 )) docs: remediation:
id: terraform desc: |
Set the desired HTTP PUT response hop limit for instance metadata requests.
uid: aws-security-hub-autoscaling-launch-config-hop-limit-terraform-plan filters: asset.platform == "terraform-plan" && terraform.plan.resourceChanges.any( type == "aws_launch_configuration" ) mql: terraform.plan.resourceChanges.where( type == "aws_launch_configuration" ).all( change.after['metadata_options'][0]['http_put_response_hop_limit'] <= 1 ) docs: remediation:
id: terraform desc: |
Set the desired HTTP PUT response hop limit for instance metadata requests.
uid: aws-security-hub-autoscaling-launch-config-public-ip-disabled title: '[Autoscaling.5] Amazon EC2 instances launched using Auto Scaling group launch configurations should not have Public IP addresses' impact: 80 docs: desc: | Related requirements: NIST.800-53.r5 AC-21, NIST.800-53.r5 AC-3, NIST.800-53.r5 AC-3(7), NIST.800-53.r5 AC-4, NIST.800-53.r5 AC-4(21), NIST.800-53.r5 AC-6, NIST.800-53.r5 SC-7, NIST.800-53.r5 SC-7(11), NIST.800-53.r5 SC-7(16), NIST.800-53.r5 SC-7(20), NIST.800-53.r5 SC-7(21), NIST.800-53.r5 SC-7(3), NIST.800-53.r5 SC-7(4), NIST.800-53.r5 SC-7(9)
variants:
uid: aws-security-hub-autoscaling-launch-config-public-ip-disabled-api filters: asset.platform == "aws" mql: |
TO DO
true docs: remediation:
id: console desc: | An Auto Scaling group is associated with one launch configuration at a time. You cannot modify a launch configuration after you create it. To change the launch configuration for an Auto Scaling group, use an existing launch configuration as the basis for a new launch configuration. Then, update the Auto Scaling group to use the new launch configuration. For step-by-step instructions, see Change the launch configuration for an Auto Scaling group in the Amazon EC2 Auto Scaling User Guide. When creating the new launch configuration, under Additional configuration, for Advanced details, IP address type, choose Do not assign a public IP address to any instances.
uid: aws-security-hub-autoscaling-launch-config-public-ip-disabled-terraform-hcl filters: asset.platform == "terraform-hcl" && terraform.resources.any( nameLabel == "aws_launch_configuration" ) mql: terraform.resources.where( nameLabel == "aws_launch_configuration" ).all( arguments['associate_public_ip_address'] != true ) docs: remediation:
id: terraform desc: |
Set associate_public_ip_address to
false
in the aws_launch_configuration resourceuid: aws-security-hub-autoscaling-launch-config-public-ip-disabled-terraform-plan filters: asset.platform == "terraform-plan" && terraform.plan.resourceChanges.any( type == "aws_launch_configuration" ) mql: terraform.plan.resourceChanges.where( type == "aws_launch_configuration" ).all( change.after['associate_public_ip_address'] != true ) docs: remediation:
id: terraform desc: |
Set associate_public_ip_address to
false
in the aws_launch_configuration resourceuid: aws-security-hub-autoscaling-multiple-instance-types title: '[AutoScaling.6] Auto Scaling groups should use multiple instance types in multiple Availability Zones' impact: 50 docs: desc: | Related requirements: NIST.800-53.r5 CA-9(1), NIST.800-53.r5 CM-2, NIST.800-53.r5 CM-2(2)
variants:
uid: aws-security-hub-autoscaling-multiple-instance-types-api filters: asset.platform == "aws" mql: |
TO DO
true docs: remediation:
uid: aws-security-hub-autoscaling-multiple-instance-types-terraform-hcl filters: asset.platform == "terraform-hcl" && terraform.resources.any( nameLabel == "aws_launch_configuration" ) mql: |
TO DO
true docs: remediation:
id: terraform desc: |
Set the desired HTTP PUT response hop limit for instance metadata requests.
uid: aws-security-hub-autoscaling-multiple-instance-types-terraform-plan filters: asset.platform == "terraform-plan" && terraform.plan.resourceChanges.any( type == "aws_launch_configuration" ) mql: |
TO DO
true docs: remediation:
id: terraform desc: |
Set the desired HTTP PUT response hop limit for instance metadata requests.
uid: aws-security-hub-autoscaling-launch-template title: '[AutoScaling.9] Amazon EC2 Auto Scaling groups should use Amazon EC2 launch templates' impact: 50 docs: desc: | Related requirements: NIST.800-53.r5 CA-9(1), NIST.800-53.r5 CM-2, NIST.800-53.r5 CM-2(2)
variants:
uid: aws-security-hub-autoscaling-launch-template-api filters: asset.platform == "aws" mql: |
TO DO
true docs: remediation:
uid: aws-security-hub-autoscaling-launch-template-terraform-hcl filters: asset.platform == "terraform-hcl" && terraform.resources.any( nameLabel == "aws_autoscaling_group" ) mql: terraform.resources.where( nameLabel == "aws_autoscaling_group" ).all( arguments['launch_configuration'] ) docs: remediation:
id: terraform desc: |
Set the desired HTTP PUT response hop limit for instance metadata requests.
uid: aws-security-hub-autoscaling-launch-template-terraform-plan filters: asset.platform == "terraform-plan" && terraform.plan.resourceChanges.any( type == "aws_autoscaling_group" ) mql: terraform.plan.resourceChanges.where( type == "aws_autoscaling_group" ).all( change.after['launch_configuration'] ) docs: remediation:
id: terraform desc: |
Set the desired HTTP PUT response hop limit for instance metadata requests.
Expected behavior A clear and concise description of what you expected to happen.
Screenshots or CLI Output If applicable, add screenshots or the CLI output to help explain your problem.
Desktop (please complete the following information):
Additional context Add any other context about the problem here.