Skip to content

Commit 305ceb2

Browse files
committed
Add a ruletype that checks for the presence of a file header
Signed-off-by: Radoslav Dimitrov <[email protected]>
1 parent c09f5f3 commit 305ceb2

File tree

8 files changed

+133
-0
lines changed

8 files changed

+133
-0
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
tests:
2+
- name: "Specific file has a header"
3+
def:
4+
filter: LICENSE
5+
header: |
6+
# SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
7+
# SPDX-License-Identifier: Apache-2.0
8+
params: {}
9+
expect: "pass"
10+
git:
11+
repo_base: test_1
12+
- name: "Specific file doesn't have a header"
13+
def:
14+
filter: LICENSE
15+
header: |
16+
# SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
17+
# SPDX-License-Identifier: Apache-2.0
18+
params: {}
19+
expect: "fail"
20+
git:
21+
repo_base: test_2
22+
- name: "All go files have a header"
23+
def:
24+
filter: LICENSE
25+
header: |
26+
# SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
27+
# SPDX-License-Identifier: Apache-2.0
28+
params: {}
29+
expect: "pass"
30+
filter: "^.*\\.go$"
31+
git:
32+
repo_base: test_1
33+
- name: "Not all go files have a header"
34+
def:
35+
filter: LICENSE
36+
header: |
37+
# SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
38+
# SPDX-License-Identifier: Apache-2.0
39+
params: {}
40+
expect: "fail"
41+
filter: "^.*\\.go$"
42+
git:
43+
repo_base: test_2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
Test file for license header
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
package test_1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
package test_1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Another header
2+
3+
Test file for license header
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
package test_1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package test_1

rule-types/common/file_header.yaml

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
header:
32+
type: string
33+
description: |
34+
The header to check for in the file.
35+
36+
This is the expected content that should be present in the beginning of the file.
37+
required:
38+
- filter
39+
- header
40+
ingest:
41+
type: git
42+
git:
43+
eval:
44+
type: rego
45+
rego:
46+
type: constraints
47+
def: |
48+
package minder
49+
50+
import future.keywords.in
51+
import future.keywords.if
52+
53+
violations[{"msg": msg}] if {
54+
# Walk all files in the repo
55+
files_in_repo := file.walk(".")
56+
57+
some current_file in files_in_repo
58+
59+
# Filter files based on the regex in filter
60+
regex.match(input.profile.filter, current_file)
61+
62+
# Read the file
63+
file_content := file.read(current_file)
64+
65+
# Check if the file contains the expected header
66+
not startswith(file_content, input.profile.header)
67+
68+
msg := sprintf("File does not contain the expected header: %s", [current_file])
69+
}
70+
# Defines the configuration for alerting on the rule
71+
alert:
72+
type: security_advisory
73+
security_advisory: {}

0 commit comments

Comments
 (0)