Skip to content

Commit f6e2aa8

Browse files
test(gitleaks): split sealed-secrets tests; use realistic-looking sealed-secret blob
1 parent bfaaa5b commit f6e2aa8

File tree

1 file changed

+54
-10
lines changed

1 file changed

+54
-10
lines changed

tests/test_gitleaks_precommit.py

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,38 @@ def test_gitleaks_stable_patterns_fail(tmp_path: Path, fname: str, content: str)
3232

3333

3434
# --- Sealed-secrets: YAML/YML allowlisted; non-YAML should be flagged ---
35-
def _fake_sealed_secret_blob(n: int = 800) -> str:
36-
body = ("Qw9+/" * ((n // 4) + 1))[:n]
37-
return "Ag" + body + "=="
35+
def _fake_sealed_secret_blob(n: int = 800, seed: str = "sealed-secrets-test") -> str:
36+
"""
37+
Generate a deterministic, base64-looking ciphertext that resembles a SealedSecret.
38+
- Always starts with 'Ag'
39+
- Uses a realistic base64 alphabet mix (via sha256-derived bytes)
40+
- Adds '=' padding only if required by base64 length
41+
- Deterministic for stable tests (change `seed` to vary appearance)
42+
"""
43+
import base64
44+
import hashlib
45+
46+
# Build a deterministic byte stream from the seed, not random
47+
chunk = hashlib.sha256(seed.encode("utf-8")).digest() # 32 bytes
48+
raw = (chunk * ((n // len(chunk)) + 4))[: n + 64] # extra slack, then trim
49+
50+
# Base64-encode -> realistic distribution of A–Z a–z 0–9 + /
51+
b64 = base64.b64encode(raw).decode("ascii")
3852

53+
# Compose with 'Ag' prefix and keep length near n
54+
body = b64.replace("=", "") # remove padding from the body
55+
s = "Ag" + body[:n] # ensure 'Ag' at start
3956

40-
def test_gitleaks_yaml_allowlist_for_sealed_secrets(tmp_path: Path):
57+
# Fix padding so total length is a multiple of 4 (valid base64-looking)
58+
rem = len(s) % 4
59+
if rem:
60+
s += "=" * (4 - rem)
61+
return s
62+
63+
64+
def test_gitleaks_yaml_allowlist_for_sealed_secrets_yaml(tmp_path: Path):
4165
"""
42-
Keep .gitleaks.toml as-is (realistic behavior).
43-
- In .yaml/.yml: blob under spec.encryptedData -> allowlisted -> hook PASS
44-
- In non-YAML: same blob in code -> not allowlisted -> hook FAIL
66+
Case 1: .yaml (allowlisted => PASS)
4567
"""
4668
blob = _fake_sealed_secret_blob()
4769

@@ -56,7 +78,6 @@ def test_gitleaks_yaml_allowlist_for_sealed_secrets(tmp_path: Path):
5678
token: "{blob}"
5779
"""
5880

59-
# Case 1: .yaml (allowlisted => PASS)
6081
proj_yaml = tmp_path / "proj_yaml"
6182
proj_yaml.mkdir()
6283
copy_project(proj_yaml)
@@ -65,7 +86,24 @@ def test_gitleaks_yaml_allowlist_for_sealed_secrets(tmp_path: Path):
6586
run_yaml("git add -A")
6687
run_yaml("./venv/bin/tox -e pre-commit")
6788

68-
# Case 2: .yml (allowlisted => PASS)
89+
90+
def test_gitleaks_yaml_allowlist_for_sealed_secrets_yml(tmp_path: Path):
91+
"""
92+
Case 2: .yml (allowlisted => PASS)
93+
"""
94+
blob = _fake_sealed_secret_blob()
95+
96+
sealed_yaml = f"""\
97+
apiVersion: bitnami.com/v1alpha1
98+
kind: SealedSecret
99+
metadata:
100+
name: demo
101+
namespace: default
102+
spec:
103+
encryptedData:
104+
token: "{blob}"
105+
"""
106+
69107
proj_yml = tmp_path / "proj_yml"
70108
proj_yml.mkdir()
71109
copy_project(proj_yml)
@@ -74,7 +112,13 @@ def test_gitleaks_yaml_allowlist_for_sealed_secrets(tmp_path: Path):
74112
run_yml("git add -A")
75113
run_yml("./venv/bin/tox -e pre-commit")
76114

77-
# Case 3: non-YAML (should be flagged => FAIL)
115+
116+
def test_leaky_code_fails_gitleaks(tmp_path: Path):
117+
"""
118+
Case 3: non-YAML (should be flagged => FAIL)
119+
"""
120+
blob = _fake_sealed_secret_blob()
121+
78122
proj_code = tmp_path / "proj_code"
79123
proj_code.mkdir()
80124
copy_project(proj_code)

0 commit comments

Comments
 (0)