-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruleParser.py
More file actions
executable file
·55 lines (45 loc) · 1.8 KB
/
Copy pathruleParser.py
File metadata and controls
executable file
·55 lines (45 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import yaml
import requests
# Semgrep token with API access
token = os.environ.get("SEMGREP_APP_TOKEN")
# Test options
output_dir = './tests/rules3'
test_limit = 20
def represent_multiline_string(dumper, data):
if '\n' in data:
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
return dumper.represent_scalar('tag:yaml.org,2002:str', data)
yaml.add_representer(str, represent_multiline_string)
def fetch_yaml_from_endpoint(url, headers):
response = requests.get(url, headers=headers)
if response.status_code == 200:
return yaml.safe_load(response.text)
else:
raise Exception(f"Failed to fetch data: {response.status_code}")
def save_rule_files(rules, output_dir, limit=None):
for i, rule in enumerate(rules):
if limit is not None and i >= limit:
break
rule_id = rule.get('id', f'rule_{i+1}')
rule_filename = '.'.join(rule_id.split('.')[:-1]) + '.yaml'
rule_path = os.path.join(output_dir, rule_filename)
with open(rule_path, 'w') as file:
yaml.dump({'rules': [rule]}, file, default_flow_style=False, sort_keys=False)
print(f"Saved {rule_filename}")
def process_and_save_rules(rules_data, output_dir, limit=None):
os.makedirs(output_dir, exist_ok=True)
save_rule_files(rules_data['rules'], output_dir, limit=limit)
# URL to the ruleset YAML data. Pulls down everything you have access to
url = 'https://semgrep.dev/c/r/all'
# Authorization header
headers = {
'Authorization': f'Bearer {token}'
}
# Fetching rules and process them
try:
rules_data = fetch_yaml_from_endpoint(url, headers)
process_and_save_rules(rules_data, output_dir, limit=test_limit)
print("Processing completed successfully.")
except Exception as e:
print(f"An error occurred: {e}")