Skip to content

Commit f08fb41

Browse files
committed
Added the ability to add standard strings to .gitleaks.toml.
Creates the file if it doesn't already exist.
1 parent dbe778b commit f08fb41

5 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Taught plugin-template how to interact with .gitleaks.toml files.

plugin-template

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,12 @@ def write_template_section(
416416
destination,
417417
template_vars,
418418
)
419+
elif destination.startswith(".gitleaks.toml."):
420+
utils.merge_gitleaks(
421+
template,
422+
plugin_root_dir,
423+
destination,
424+
)
419425
else:
420426
utils.template_to_file(
421427
template,

templates/bootstrap/pyproject.toml.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,6 @@ ignore = [
118118
".dependabot/config.yml",
119119
".ci/**",
120120
".github/**",
121+
".gitleaks.toml",
121122
]
122123
{% endif %}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[allowlist]
2+
description = "Allow specific test-keys."
3+
paths = [
4+
]
5+
regexes = [
6+
'''AKIAIT2Z5TDYPX3ARJBA''',
7+
'''qR\+vjWPU50fCqQuUWbj9Fain/j2pV\+ZtBCiDiieS''',
8+
'''fqRvjWaPU5o0fCqQuUWbj9Fainj2pVZtBCiDiieS''',
9+
'''Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw''',
10+
]

utils.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,58 @@ def template_to_file(template, plugin_root_path, relative_path, template_vars):
188188
destination_path.chmod(mode)
189189

190190

191+
def merge_gitleaks(template, plugin_root_path, relative_path, template_vars={}):
192+
"""
193+
Take values from .gitleaks.allowlist.j2 and insert them into the allowlist of
194+
an existing .gitleaks.toml file, or create said file otherwise.
195+
"""
196+
basename, merge_key = relative_path.split(".toml.", maxsplit=1)
197+
# "allowlist" is all we recognize for gitleaks
198+
assert "allowlist" == merge_key
199+
# We aren't using template-vars *currently* - but may want to at some point.
200+
data = tomlkit.loads(template.render(**template_vars))
201+
path = Path(plugin_root_path / f"{basename}.toml")
202+
if path.exists():
203+
old_toml = tomlkit.load(path.open())
204+
if merge_key not in old_toml:
205+
old_toml["allowlist"] = data["allowlist"]
206+
else:
207+
old_toml["allowlist"]["description"] = data["allowlist"]["description"]
208+
merge_sets("paths", data, old_toml)
209+
merge_sets("regexes", data, old_toml)
210+
211+
else:
212+
old_toml = data
213+
# Update MANIFEST.in to ignore the file we're about to create
214+
# (if we are only updating .gitleaks, we 'assume' it's already being ignored)
215+
manifest = Path(plugin_root_path / "MANIFEST.in")
216+
if manifest.exists():
217+
# MANIFEST.in is small enough to look at the whole thing at once
218+
manifest_contents = manifest.read_text()
219+
if ".gitleaks.toml" not in manifest_contents:
220+
manifest.write_text(manifest_contents + "\nexclude .gitleaks.toml")
221+
output = tomlkit.dumps(old_toml)
222+
if output[-1] != "\n":
223+
output = output + "\n"
224+
path.write_text(output)
225+
226+
227+
def merge_sets(key, data, old_toml):
228+
"""
229+
For a given key, merge any existing values and incoming new ones.
230+
Use set() to get rid of duplicates, use sorted() to enforce ordering.
231+
If incoming values are already a subset of the existing ones - nothing to do here.
232+
"""
233+
if key in old_toml["allowlist"]:
234+
old_values = set(old_toml["allowlist"][key])
235+
new_values = set(data["allowlist"][key])
236+
if new_values.issubset(old_values): # Everything we want to add is already there
237+
return
238+
old_toml["allowlist"][key] = sorted(old_values.union(new_values))
239+
else:
240+
old_toml["allowlist"][key] = sorted(set(data["allowlist"][key]))
241+
242+
191243
def merge_toml(template, plugin_root_path, relative_path, template_vars):
192244
"""
193245
Template a file of the form 'basename.toml.merge_key' and combine its content beneath

0 commit comments

Comments
 (0)