aws-cloudformation / cfn-lint

CloudFormation Linter
MIT No Attribution
2.43k stars 586 forks source link

Warning not ignored with pre-commit hook #1196

Open jia2 opened 4 years ago

jia2 commented 4 years ago

cfn-lint version: 0.25 Python: 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 pre-commit: 1.17.0 Description of issue.

I have below in my .pre-commit-config.yaml in my git repository

repos:
  - repo: https://github.com/awslabs/cfn-python-lint
    rev: v0.25.0 # The version of cfn-lint to use
    hooks:
      - id: cfn-python-lint
        name: AWS CloudFormation Linter
        files: cfn_templates/.*\.(yml|yaml)$

When I run pre-commit, I got a warning:

AWS CloudFormation Linter................................................Failed
hookid: cfn-python-lint

W1020 Fn::Sub isn't needed because there are no variables at Resources/KafkaLaunchConfig/Metadata/AWS::CloudFormation::Init/configureKafka/files//opt/kafka/bin/kafka-start.sh/content/Fn::Sub
cfn_templates/kafka_kafkacluster.yaml:244:29

I have already put this section in my cloudformation template:

Metadata:
    cfn-lint:
        config:
            regions:
                - eu-central-1
            ignore_checks:
                - W1020

But I still get the warning. When I run cfn-lint standaone to validate this template, then the warning will not be displayed.

How can I get the validation work with pre-commit? Is there a way to specify ignore checks in .pre-commit-config.yaml?

kddejong commented 4 years ago

Interesting. I'll take a look. Thanks for the detail.

kddejong commented 4 years ago

I'm having troubles replicating this. This template will appropriately skip error W1020 for me. How many other yaml/yml/json files do you have in that repo?

Our entry point is the same as the command line. https://github.com/aws-cloudformation/cfn-python-lint/blob/master/.pre-commit-hooks.yaml#L3

---
Metadata:
  cfn-lint:
    config:
      ignore_checks:
        - W1020
Resources:
  Vpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Sub "10.0.0.0/24"
jia2 commented 4 years ago

There are 7 yaml files and no json file in my repo.

kddejong commented 4 years ago

Can you run cfn-lint with debug. I'm looking for ~15 lines of that debug output. It should tell me what your loaded config is and what the effective config is.

jia2 commented 4 years ago

As I said, if I invoke cfn-lint directly, not via pre-commit, validation ist fine. The warning W1020 is only shown, when I execute pre-commit.

Here is validation without debug option, and this "warning" is also new for me. I don't know why it comes now. I don't have any custom rules.

cfn-lint.exe cfn_templates/kafka_kafkacluster.yaml c:\users\dingjunjia\appdata\local\programs\python\python37\lib\site-packages\cfnlint__init__.py:40: FutureWarning: CloudFormationLintRule is refactored and deprecated. Please use cfnlint.rules.CloudFormationLintRule warnings.warn(message, FutureWarning)

Here with debug option:

$ cfn-lint.exe --debug cfn_templates/kafka_kafkacluster.yaml 2019-11-21 07:06:14,956 - cfnlint - DEBUG - Looking for CFLINTRC before attempting to load 2019-11-21 07:06:14,956 - cfnlint - DEBUG - Validating User CFNLINTRC 2019-11-21 07:06:14,957 - cfnlint - DEBUG - Validating CFNLINTRC config with given JSONSchema 2019-11-21 07:06:14,957 - cfnlint - DEBUG - Schema used: {'$id': 'https://github.com/aws-cloudformation/cfn-python-lint/blob/master/src/cfnlint/data/CfnLintCli/config/schema.json', '$schema': 'http://json-schema.org/draft-07/schema#', 'description': 'CFNLINTRC configuration schema', 'title': 'CFNLINTRC JSON Schema', 'type': 'object', 'additionalProperties': False, 'properties': {'append_rules': {'description': 'Location of directories to append rules from', 'items': {'type': 'string'}, 'type': 'array'}, 'ignore_checks': {'description': 'List of checks to ignore', 'items': {'type': 'string'}, 'type': 'array'}, 'include_checks': {'description': 'List of checks to include', 'items': {'type': 'string'}, 'type': 'array'}, 'override_spec': {'description': 'Path to spec file to override with', 'type': 'string'}, 'regions': {'description': 'Regions to test against', 'items': {'type': 'string'}, 'type': 'array'}, 'configure_rules': {'description': 'Configure rules', 'patternProperties': {'^.$': {'type': 'object', 'patternProperties': {'^.$': {'anyOf': [{'type': 'string'}, {'type': 'integer'}, {'type': 'boolean'}]}}}}, 'additionalProperties': False, 'type': 'object'}, 'templates': {'description': 'Templates to lint', 'items': {'type': 'string'}, 'type': 'array'}, 'ignore_templates': {'description': 'Templates to ignore', 'items': {'type': 'string'}, 'type': 'array'}}} 2019-11-21 07:06:14,958 - cfnlint - DEBUG - Config used: {} 2019-11-21 07:06:14,960 - cfnlint - DEBUG - CFNLINTRC looks valid! 2019-11-21 07:06:14,960 - cfnlint - DEBUG - Validating Project CFNLINTRC 2019-11-21 07:06:14,960 - cfnlint - DEBUG - Validating CFNLINTRC config with given JSONSchema 2019-11-21 07:06:14,961 - cfnlint - DEBUG - Schema used: {'$id': 'https://github.com/aws-cloudformation/cfn-python-lint/blob/master/src/cfnlint/data/CfnLintCli/config/schema.json', '$schema': 'http://json-schema.org/draft-07/schema#', 'description': 'CFNLINTRC configuration schema', 'title': 'CFNLINTRC JSON Schema', 'type': 'object', 'additionalProperties': False, 'properties': {'append_rules': {'description': 'Location of directories to append rules from', 'items': {'type': 'string'}, 'type': 'array'}, 'ignore_checks': {'description': 'List of checks to ignore', 'items': {'type': 'string'}, 'type': 'array'}, 'include_checks': {'description': 'List of checks to include', 'items': {'type': 'string'}, 'type': 'array'}, 'override_spec': {'description': 'Path to spec file to override with', 'type': 'string'}, 'regions': {'description': 'Regions to test against', 'items': {'type': 'string'}, 'type': 'array'}, 'configure_rules': {'description': 'Configure rules', 'patternProperties': {'^.$': {'type': 'object', 'patternProperties': {'^.$': {'anyOf': [{'type': 'string'}, {'type': 'integer'}, {'type': 'boolean'}]}}}}, 'additionalProperties': False, 'type': 'object'}, 'templates': {'description': 'Templates to lint', 'items': {'type': 'string'}, 'type': 'array'}, 'ignore_templates': {'description': 'Templates to ignore', 'items': {'type': 'string'}, 'type': 'array'}}} 2019-11-21 07:06:14,962 - cfnlint - DEBUG - Config used: {} 2019-11-21 07:06:14,964 - cfnlint - DEBUG - CFNLINTRC looks valid! 2019-11-21 07:06:14,965 - cfnlint - DEBUG - User configuration loaded as 2019-11-21 07:06:14,966 - cfnlint - DEBUG - {} 2019-11-21 07:06:14,966 - cfnlint - DEBUG - Project configuration loaded as 2019-11-21 07:06:14,967 - cfnlint - DEBUG - {} 2019-11-21 07:06:14,967 - cfnlint - DEBUG - Merging configurations... 2019-11-21 07:06:14,968 - cfnlint - DEBUG - Begin linting of file: cfn_templates/kafka_kafkacluster.yaml c:\users\dingjunjia\appdata\local\programs\python\python37\lib\site-packages\cfnlint__init__.py:40: FutureWarning: CloudFormationLintRule is refactored and deprecated. Please use cfnlint.rules.CloudFormationLintRule warnings.warn(message, FutureWarning) 2019-11-21 07:06:15,155 - cfnlint - DEBUG - Search for key Ref as far down as the template goes 2019-11-21 07:06:15,156 - cfnlint - DEBUG - Search for key Fn::Sub as far down as the template goes 2019-11-21 07:06:15,156 - cfnlint - DEBUG - Search for key Fn::FindInMap as far down as the template goes 2019-11-21 07:06:15,157 - cfnlint - DEBUG - Search for key Fn::Equals as far down as the template goes 2019-11-21 07:06:15,158 - cfnlint - DEBUG - Transform templates if needed 2019-11-21 07:06:15,158 - cfnlint - INFO - Run scan of template cfn_templates/kafka_kafkacluster.yaml

skshahidur commented 1 year ago

Facing the same error still. Was it resolved?

kddejong commented 1 year ago

@skshahidur I'm working on some updates to the pre-commit stuff today. I have not been able to replicate this issue to determine a fix.

Can you let me know what rev you have for your hook configuration? Are you using a .cfnlintrc file? If so what is its configuration?

Also are you on win32?

mmaeng commented 1 year ago

I am also having trouble replicating this issue on win32 . Multiple versions of cfn-lint attempted from v0.25.0 in the original report to quite a few versions afterwards, including v0.69.1

Removing the Metadata from the provided kafka_cluster.yaml causes the run to fail but including that part it passes precommit with cfn-lint.

What is your pre-commit-config.yaml and cfn template like @skshahidur ?