Skip to content

Commit cf8e142

Browse files
authored
Merge pull request #13 from advanced-security/trap-caching
Massive improvement to workflow
2 parents 4d7e3de + 71cd779 commit cf8e142

File tree

5 files changed

+265
-68
lines changed

5 files changed

+265
-68
lines changed

README.md

Lines changed: 118 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
# gh-codeql-scan
1+
<!-- markdownlint-disable -->
2+
<div align="center">
23

3-
GitHub CLI CodeQL Scan Extension to help abstract CodeQL away from users.
4+
<h1>gh-codeql-scan</h1>
5+
6+
[![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white)][github]
7+
[![GitHub Issues](https://img.shields.io/github/issues/advanced-security/gh-codeql-scan?style=for-the-badge)][github-issues]
8+
[![GitHub Stars](https://img.shields.io/github/stars/advanced-security/gh-codeql-scan?style=for-the-badge)][github]
9+
[![License](https://img.shields.io/github/license/advanced-security/gh-codeql-scan?style=for-the-badge)][license]
10+
11+
</div>
12+
<!-- markdownlint-restore -->
13+
14+
[GitHub CLI CodeQL Scan Extension][github] to help abstract [CodeQL][codeql] away from users.
15+
16+
<details>
17+
<summary>Motivation</summary>
18+
19+
This project was created to make the lives of users that use CodeQL simpiler.
20+
CodeQL outside of GitHub Actions can be complicated but this projects aim is to make it as simple as possible.
21+
22+
</details>
423

524
## Requirements
625

726
- [GitHub CLI](https://cli.github.com/)
27+
- [CodeQL GH Extension][gh-codeql] (optional)
828

929
## Install and Setup
1030

@@ -17,9 +37,59 @@ gh extensions install advanced-security/gh-codeql-scan
1737
gh codeql-scan --help
1838
```
1939

40+
<details>
41+
<summary>CLI Help</summary>
42+
43+
<pre>
44+
GitHub CodeQL Scan tool
45+
46+
gh codeql-scan {MODE} {ARGS}
47+
48+
# Modes
49+
50+
gh codeql-scan # default: "scan"
51+
gh codeql-scan init # initialise the scan
52+
gh codeql-scan analyze # run the analysis
53+
gh codeql-scan upload # upload present SARIF files
54+
gh codeql-scan scan # full end-to-end scan
55+
56+
# Arguments
57+
58+
> All arguments can be set with enviroment variables
59+
60+
-h|--help # Print help
61+
--debug # Enable debugging
62+
63+
-r=*|--repo=* # GitHub Respository (OWNER/NAME)
64+
-i=*|--instance=* # GitHub Instance (github.com or Enterprise Server)
65+
66+
-l=*|--language=* # Set language to scan
67+
--auto-detect # Auto-detect languages
68+
69+
-s=*|--suite=* # Query Suite to use
70+
-d=*|--databases=* # Location of the databases to store
71+
-b=*|--binary=* # Path to the CodeQL Binary
72+
-w=*|--workspace=* # Workspace for the souce code
73+
74+
-c=*|--command=* # Set the build comment (compiled languages)
75+
-m=*|--mode=* # Build mode (autobuild | none)
76+
--buildless # Enable buildless / build mode none
77+
78+
--view-in-vscode # Auto-open the results in VSCode
79+
80+
--disable-tracing # Disable Build Tracing
81+
--disable-trap-caching # Disable Trap file caching
82+
--disable-upload # Disable Uploading SARIF to GitHub
83+
--disable-banner # Disable printing banner
84+
</pre>
85+
86+
</details>
87+
88+
### Alias / Stub
89+
2090
A couple of tips and tricks:
2191

22-
```
92+
```bash
2393
# Create an alias to make things even easier
2494
alias codeql-scan="gh codeql-scan"
2595
```
@@ -33,46 +103,79 @@ The main use of the script is to automatically run CodeQL in a number of modes.
33103
gh codeql-scan
34104
```
35105

106+
#### Initialise with language
107+
108+
Automatically detect languages or manually set the language to create an initial CodeQL database.
109+
36110
```bash
37-
# `init` mode: Create only the Codeql database
38111
gh codeql-scan init --auto-detect
39112
# or manually set language
40113
gh codeql-scan init -l=java
41114
```
42115

116+
#### Scan without build
117+
118+
This will scan your code in build mode `none`.
119+
120+
```bash
121+
gh codeql-scan -m="none"
122+
# or simply
123+
gh codeql-scan --buildless
124+
```
125+
126+
#### Scan with Build Command
127+
128+
Pass in the build command for a compiled language and it will be run along with CodeQL.
129+
43130
```bash
44-
# Compiled languages - pass in build command
45131
gh codeql-scan -c "mvn build ..."
46132
```
47133

134+
#### Indirect build tracing
135+
136+
For Compiled languages, complicated build process using indirect build tracing
137+
48138
```bash
49-
# Compiled languages - complicated build process using indirect build tracing
50139
gh codeql-scan init
51140
echo "password=$password" > settings.xml
52141
mvn build --random-custom=flags
53142
gh codeql-scan analyze
54143
```
55144

145+
#### Running analysis
146+
147+
Run query-suites on an existing database (auto-detects databases)
148+
56149
```bash
57-
# `analyze` mode: Run query-suites on an existing database (auto-detects databases)
58150
gh codeql-scan analyze
59151
```
60152

153+
#### Uploading results to GitHub
154+
155+
The `upload` mode will upload all SARIF files for you to a repository
156+
61157
```bash
62-
# `upload` mode: Upload all SARIF files
63158
gh codeql-scan upload
64159
```
65160

66-
## License
67-
68-
This project is licensed under the terms of the MIT open source license. Please refer to [MIT](./LICENSE.md) for the full terms.
69-
70161
## Maintainers
71162

72163
- @GeekMasher
73164

74-
## Support
165+
## Support / Maintainance
166+
167+
Support is via [GitHub Issues][github-issues]
168+
169+
## License
170+
171+
This project is licensed under the terms of the MIT open source license.
172+
Please refer to [MIT][license] for the full terms.
173+
174+
<!-- Resources -->
75175

76-
Support is via [GitHub Issues](https://github.com/advanced-security/gh-codeql-scan/issues)
176+
[license]: ./LICENSE
177+
[github]: https://github.com/advanced-security/gh-codeql-scan
178+
[github-issues]: https://github.com/advanced-security/gh-codeql-scan/issues
179+
[codeql]: https://codeql.github.com/
180+
[gh-codeql]: https://github.com/github/gh-codeql
77181

78-
## Acknowledgement

bin/codeql-analyze

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ for i in "$@"; do
1919
CODEQL_ADD_SNIPPETS=""
2020
shift
2121
;;
22-
--disable-uploading)
23-
GITHUB_UPLOAD=0
24-
shift
25-
;;
2622
-s=*|--suite=*)
2723
export CODEQL_SUITE="${i#*=}"
2824
shift
@@ -36,7 +32,7 @@ for i in "$@"; do
3632
esac
3733
done
3834

39-
if [ -z $CODEQL_DATABASE_PATHS ]; then
35+
if [[ -z $CODEQL_DATABASE_PATHS ]]; then
4036
error "No CodeQL Databases found"
4137
exit 1
4238
fi
@@ -55,14 +51,14 @@ for CODEQL_DATABASE in $CODEQL_DATABASE_PATHS ; do
5551

5652
# For tracing
5753
debug "Finalizing CodeQL Database"
58-
$CODEQL_BINARY database finalize $CODEQL_DATABASE 2>/dev/null | true
54+
$CODEQL_BINARY database finalize --finalize-dataset $CODEQL_DATABASE 2>/dev/null | true
5955

6056
# The --sarif-category must be set in case of multiple databases
6157
$CODEQL_BINARY database analyze \
6258
--format="sarif-latest" \
6359
--sarif-category="codeql-scan:${CODEQL_LANGUAGE}" \
6460
--output=$CODEQL_SARIF \
65-
-j=0 \
61+
-j=0 -M="$(memory)" \
6662
$CODEQL_ADD_HELP $CODEQL_ADD_SNIPPETS \
6763
${CODEQL_DATABASE} ${CODEQL_SUITE}
6864

bin/codeql-init

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,34 @@
22
set -e
33
source $EXTENSION_LOCATION/bin/codeql-utils
44

5-
for i in "$@"; do
6-
case $i in
5+
while [[ "$#" -gt 0 ]]; do
6+
case $1 in
77
--auto-detect)
88
GITHUB_AUTO_DETECT="1"
9-
shift
109
;;
1110
--disable-tracing)
1211
export CODEQL_TRACING=0
13-
shift
1412
;;
1513
-l=*|--language=*)
16-
export CODEQL_LANGUAGES="${i#*=}"
17-
shift
14+
export CODEQL_LANGUAGES="${1#*=}"
1815
;;
19-
-c="*"|--command="*")
20-
export CODEQL_BUILD_COMMAND="${i#*=}"
16+
-c=*|--command=*)
17+
export CODEQL_BUILD_COMMAND="${1#*=}"
2118
export CODEQL_TRACING="1" # enable tracing
22-
shift
19+
;;
20+
-m=*|--mode=*)
21+
export CODEQL_BUILD_MODE="${1#*=}"
22+
;;
23+
--disable-trap-caching)
24+
export CODEQL_TRAP_CACHING=0
2325
;;
2426
*)
2527
;;
2628
esac
29+
shift
2730
done
2831

29-
32+
3033
if [ ${GITHUB_UPLOAD} == 0 ] && [ -z ${GITHUB_REPOSITORY+x} ]; then
3134
# If the repo isn't set
3235
export GITHUB_REPOSITORY="$(basename $PWD)"
@@ -62,6 +65,9 @@ if [ -z $CODEQL_LANGUAGES ] ; then
6265
exit 1
6366
fi
6467

68+
MEMORY=$(memory)
69+
info "Memory :: $MEMORY (MB)"
70+
6571
# comma seperated list (`java,javascript`)
6672
for CODEQL_LANGUAGE in ${CODEQL_LANGUAGES//,/ } ; do
6773
debug "CodeQL Language :: $CODEQL_LANGUAGE"
@@ -79,9 +85,14 @@ for CODEQL_LANGUAGE in ${CODEQL_LANGUAGES//,/ } ; do
7985
debug "CodeQL Binary :: $CODEQL_BINARY"
8086
debug "Tracing :: $CODEQL_TRACING"
8187

82-
CODEQL_CREATE="$CODEQL_BINARY database create -j=0 -M=16000 --language=$CODEQL_LANGUAGE"
83-
84-
if [ "$CODEQL_TRACING" = "1" ] && [[ "$CODEQL_LANGUAGE" =~ ^(cpp|csharp|java|go)$ ]]; then
88+
CODEQL_CREATE="$CODEQL_BINARY"
89+
90+
if [ "$CODEQL_BUILD_MODE" = "none" ] && [[ "$CODEQL_LANGUAGES" =~ ^(csharp|java)$ ]]; then
91+
info "CodeQL build mode is set to none"
92+
93+
CODEQL_CREATE="$CODEQL_CREATE database init --language=$CODEQL_LANGUAGE --build-mode=none"
94+
95+
elif [ "$CODEQL_TRACING" = "1" ] && [[ "$CODEQL_LANGUAGE" =~ ^(cpp|csharp|java|go)$ ]]; then
8596
debug "Swap to a init command and add --begin-tracing"
8697

8798
# check if the OS is Mac
@@ -91,13 +102,40 @@ for CODEQL_LANGUAGE in ${CODEQL_LANGUAGES//,/ } ; do
91102
fi
92103

93104
# https://codeql.github.com/docs/codeql-cli/creating-codeql-databases/#using-indirect-build-tracing
94-
CODEQL_CREATE="$CODEQL_BINARY database init --begin-tracing --language=$CODEQL_LANGUAGE --source-root=$WORKSPACE $CODEQL_DATABASE"
105+
CODEQL_CREATE="$CODEQL_CREATE database init --begin-tracing --language=$CODEQL_LANGUAGE"
106+
107+
elif [[ ! -z $CODEQL_BUILD_COMMAND ]]; then
108+
debug "Enabling CodeQL manual build"
109+
CODEQL_CREATE="$CODEQL_CREATE --command \"${CODEQL_BUILD_COMMAND}\""
95110
else
96111
debug "Append only Database"
97112
debug "CodeQL auto-build is enabled"
98-
CODEQL_CREATE="$CODEQL_CREATE $CODEQL_DATABASE"
113+
CODEQL_CREATE="$CODEQL_CREATE database create -j=0 -M=$MEMORY --language=$CODEQL_LANGUAGE"
114+
fi
115+
116+
if [[ ! -z "${WORKSPACE}" ]]; then
117+
debug "Setting source-root to workspace"
118+
CODEQL_CREATE="$CODEQL_CREATE --source-root=$WORKSPACE"
99119
fi
100120

121+
# TRAP File caching (C/C++ only for now)
122+
if [ "$CODEQL_TRAP_CACHING" = "1" ] && [[ "$CODEQL_LANGUAGE" = "cpp" ]]; then
123+
info "CodeQL TRAP Caching is enabled"
124+
info "CodeQL TRAP Caching Dir :: $CODEQL_TRAP_CACHING_DIR"
125+
126+
CODEQL_TRAP_CACHING_PROJECT="$CODEQL_TRAP_CACHING_DIR/${CODEQL_LANGUAGE}-${CODEQL_GITHUB_REPOSITORY}"
127+
# Make sure the directory exists
128+
mkdir -p $CODEQL_TRAP_CACHING_PROJECT
129+
130+
# Set the environment variable
131+
export CODEQL_EXTRACTOR_CPP_TRAP_CACHING=true
132+
133+
CODEQL_CREATE="$CODEQL_CREATE -O=cpp.trap.cache.dir=$CODEQL_TRAP_CACHING_PROJECT -O=cpp.trap.cache.bound=1024 -O=cpp.trap.cache.write=true"
134+
fi
135+
136+
# Add the database to the command
137+
CODEQL_CREATE="$CODEQL_CREATE $CODEQL_DATABASE"
138+
101139
info "Runnning CodeQL database init/create command..."
102140
debug "CodeQL Create Command :: $CODEQL_CREATE"
103141

@@ -111,7 +149,15 @@ for CODEQL_LANGUAGE in ${CODEQL_LANGUAGES//,/ } ; do
111149
debug "CodeQL Tracer Languages :: $CODEQL_TRACER_LANGUAGES"
112150
fi
113151

114-
if [ ! -z ${CODEQL_BUILD_COMMAND+x} ] && [[ "$CODEQL_LANGUAGE" =~ ^(cpp|csharp|java|go)$ ]] ; then
152+
if [ "$CODEQL_BUILD_MODE" = "none" ] && [[ "$CODEQL_LANGUAGE" =~ ^(csharp|java)$ ]]; then
153+
info "Running buildless mode..."
154+
155+
$CODEQL_BINARY database trace-command --use-build-mode $CODEQL_DATABASE
156+
157+
debug "Finalizing CodeQL Database"
158+
$CODEQL_BINARY database finalize $CODEQL_DATABASE | true
159+
160+
elif [ ! -z ${CODEQL_BUILD_COMMAND+x} ] && [[ "$CODEQL_LANGUAGE" =~ ^(cpp|csharp|java|go)$ ]] ; then
115161
info "Running build command :: '$CODEQL_BUILD_COMMAND'"
116162

117163
eval ${CODEQL_BUILD_COMMAND}

0 commit comments

Comments
 (0)