Skip to content

Commit 94fbcc1

Browse files
committed
v0.2.0
- Add Data Extensions support
1 parent b6b401a commit 94fbcc1

File tree

7 files changed

+89
-8
lines changed

7 files changed

+89
-8
lines changed

Pipfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ black = "*"
88
pyright = "*"
99

1010
[requires]
11-
python_version = "3.9"
11+
python_version = "3.10"
1212

1313
[scripts]
14+
main = "python3 codeqlsummurize/__main__.py"
1415
lint = "black ."
1516
format = "black ."
1617
tests = "python -m unittest discover -v -s ./tests -p test_*.py"

codeqlsummarize/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
parser.add_argument(
2525
"-f",
2626
"--format",
27-
default="bundle",
28-
help="Export format (`json`, `customizations`, `mad`, `bundle`)",
27+
default="extensions",
28+
help="Export format (`json`, `customizations`, `extensions`, `bundle`)",
2929
)
3030
parser.add_argument("-i", "--input", help="Input / Project File")
3131
parser.add_argument(

codeqlsummarize/__version__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
__title__ = "CodeQL Summarize"
33
__name__ = "codeqlsummarize"
4-
__version__ = "0.1.0"
4+
__version__ = "0.2.0"
55

66
__description__ = "GitHub CodeQL Summaries Toolkit"
77
__summary__ = """\
@@ -11,7 +11,7 @@
1111
__url__ = "https://github.com/advanced-security/gh-codeql-summarize"
1212

1313
__license__ = "MIT License"
14-
__copyright__ = "Copyright (c) 2022, GitHub"
14+
__copyright__ = "Copyright (c) 2023, GitHub"
1515

1616
__author__ = "GitHub Field Team"
1717
__email__ = ""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from codeqlsummarize.exporters.exptjson import exportToJson
22
from codeqlsummarize.exporters.customizations import exportBundle, exportCustomizations
3+
from codeqlsummarize.exporters.extensions import exportDataExtensions
34

45
EXPORTERS = {
56
"json": exportToJson,
7+
"extensions": exportDataExtensions,
68
"customizations": exportCustomizations,
79
"bundle": exportBundle,
810
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
import yaml
3+
import logging
4+
5+
from codeqlsummarize.models import CodeQLDatabase, GitHub
6+
7+
logger = logging.getLogger("codeqlsummarize.exporters.extensions")
8+
9+
CODEQL_EXTENSION = """\
10+
- addsTo:
11+
pack: codeql/{language}-queries
12+
extensible: {extensible}
13+
data:
14+
{rows}
15+
"""
16+
17+
EXTENSIBLE = {
18+
"SinkModel": "sinkModel",
19+
"SourceModel": "sourceModel",
20+
"SummaryModel": "summaryModel",
21+
}
22+
23+
24+
def exportDataExtensions(database: CodeQLDatabase, output: str, github: GitHub, **kargs):
25+
logger.info("Running export to Data Extensions")
26+
27+
if database.language == "javascript":
28+
logger.warning("Skipping JavaScript for now")
29+
return
30+
31+
# Get the CodeQL pack for the language
32+
codeqlPack = findCodeQLPack(output, database.language)
33+
os.makedirs(os.path.join(codeqlPack, "generated"), exist_ok=True)
34+
35+
if github.owner:
36+
os.makedirs(os.path.join(codeqlPack, "generated", github.owner), exist_ok=True)
37+
extensions_file = os.path.join(codeqlPack, "generated", github.owner, f"{database.name}.yml")
38+
else:
39+
extensions_file = os.path.join(codeqlPack, "generated", f"{database.name}.yml")
40+
41+
data = "extensions:\n"
42+
for sname, summary in database.summaries.items():
43+
if len(summary.rows) == 0:
44+
continue
45+
46+
summary_rows = ""
47+
for mad in sorted(summary.rows):
48+
m = mad.split(";")
49+
summary_rows += " - "
50+
summary_rows += f'["{m[0]}", "{m[1]}", {m[2]}, "{m[3]}", "{m[4]}", "{m[5]}", "{m[6]}", "{m[7]}", "{m[8]}"]\n'
51+
52+
data += CODEQL_EXTENSION.format(
53+
rows=summary_rows,
54+
language=database.language,
55+
extensible=EXTENSIBLE.get(sname, "sinkModel")
56+
)
57+
58+
logger.info(f"Writing Data Extensions to: {extensions_file}")
59+
with open(extensions_file, "w") as handle:
60+
handle.write(data)
61+
62+
63+
def findCodeQLPack(location: str, language: str) -> str:
64+
"""Find the CodeQL pack for the given language in the output directory"""
65+
66+
if os.path.isfile(location):
67+
raise Exception(f"Directory {location} does not exist")
68+
69+
for root, dirs, files in os.walk(location):
70+
for file in files:
71+
if file == "qlpack.yml":
72+
with open(os.path.join(root, file), "r") as f:
73+
qlpack = yaml.safe_load(f)
74+
75+
if f"codeql/{language}-queries" in qlpack.get("extensionTargets", []):
76+
return root
77+
78+
raise Exception(f"Could not find CodeQL pack for {language} in {location}")

codeqlsummarize/generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
QUERIES = {
2424
"SinkModel": "CaptureSinkModels.ql",
2525
"SourceModel": "CaptureSourceModels.ql",
26-
"SummaryModel": "CaptureSummaryModels.ql",
26+
# "SummaryModel": "CaptureSummaryModels.ql",
2727
}
2828

2929

@@ -47,7 +47,7 @@ def getModelGeneratorQuery(self, name) -> Optional[str]:
4747
query_file = QUERIES.get(name)
4848

4949
if query_file:
50-
return f"{self.pack_name}:utils/model-generator/{query_file}"
50+
return f"{self.pack_name}:utils/modelgenerator/{query_file}"
5151

5252
# Find in this repo
5353
return None

codeqlsummarize/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Summaries:
2121

2222
@dataclass
2323
class GitHub:
24-
owner: str = "Security"
24+
owner: str = "security"
2525
repo: str = "codeql"
2626

2727
endpoint: ClassVar[str] = "https://api.github.com"

0 commit comments

Comments
 (0)