|
| 1 | +--- |
| 2 | +version: v1 |
| 3 | +release_phase: alpha |
| 4 | +type: rule-type |
| 5 | +name: file_header |
| 6 | +display_name: Checks for the presence of a header in a file |
| 7 | +short_failure_message: File does not contain the expected header |
| 8 | +severity: |
| 9 | + value: low |
| 10 | +context: {} |
| 11 | +description: | |
| 12 | + Checks for the presence of a header in a file. |
| 13 | +guidance: | |
| 14 | + Check if the file contains the expected header. |
| 15 | + |
| 16 | + This rule is useful for enforcing the presence of a header in a file, such as license headers, code of conduct, |
| 17 | + or other important information that should be present in the beginning of the file. |
| 18 | +def: |
| 19 | + in_entity: repository |
| 20 | + rule_schema: |
| 21 | + type: object |
| 22 | + properties: |
| 23 | + filter: |
| 24 | + type: string |
| 25 | + description: | |
| 26 | + The filter is a regular expression that is used to filter the files that should be checked for the header. |
| 27 | + |
| 28 | + For example, if you want to check all files with the extension `.yml`, you can use the following regex `^.*\.yml$`. |
| 29 | + |
| 30 | + If you want to check a specific file, you can use the file name as the filter. For example, `main.go`. |
| 31 | + |
| 32 | + The default value is `^.*$`, which matches all files. |
| 33 | + default: "^.*$" |
| 34 | + header: |
| 35 | + type: string |
| 36 | + description: | |
| 37 | + The header to check for in the file. |
| 38 | + |
| 39 | + This is the expected content that should be present in the beginning of the file. |
| 40 | + required: |
| 41 | + - header |
| 42 | + ingest: |
| 43 | + type: git |
| 44 | + git: |
| 45 | + eval: |
| 46 | + type: rego |
| 47 | + rego: |
| 48 | + type: constraints |
| 49 | + def: | |
| 50 | + package minder |
| 51 | +
|
| 52 | + import future.keywords.in |
| 53 | + import future.keywords.if |
| 54 | +
|
| 55 | + violations[{"msg": msg}] if { |
| 56 | + # Walk all files in the repo |
| 57 | + files_in_repo := file.walk(".") |
| 58 | + |
| 59 | + some current_file in files_in_repo |
| 60 | + |
| 61 | + # Filter files based on the regex in filter |
| 62 | + regex.match(input.profile.filter, current_file) |
| 63 | + |
| 64 | + # Read the file |
| 65 | + file_content := file.read(current_file) |
| 66 | + |
| 67 | + # Check if the file contains the expected header |
| 68 | + not startswith(file_content, input.profile.header) |
| 69 | + |
| 70 | + msg := sprintf("File does not contain the expected header: %s", [current_file]) |
| 71 | + } |
| 72 | + # Defines the configuration for alerting on the rule |
| 73 | + alert: |
| 74 | + type: security_advisory |
| 75 | + security_advisory: {} |
0 commit comments