Skip to content

Commit 1d0d69e

Browse files
authored
Merge pull request #9 from microsoft/dev/jacobmsft/bug-fixes-8-13
bugfixes and document changes
2 parents 8331f58 + 27b824e commit 1d0d69e

File tree

7 files changed

+69
-35
lines changed

7 files changed

+69
-35
lines changed

README.md

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
## CodeQL Container
22

33
> **Note:** CodeQL container is currently in **public preview**. Please report any bugs to https://github.com/microsoft/codeql-container/issues.
4-
> Current version of CodeQL only works for interpreted languages. We will add compiled languages support on future versions.
4+
> Current version of CodeQL only works for interpreted languages. We will add compiled languages support in future versions.
55
6-
CodeQL Container is a project aimed at making it easier to start using CodeQL (https://github.com/github/codeql). This project
7-
contains a Docker file which builds a container, with the latest version of codeql-cli and codeql queries precompiled.
8-
It also contains scripts to keep the toolchain in the container updated. You can use this container to:
6+
The CodeQL Container is a project aimed at making it easier to start using CodeQL (more about codeQL at https://github.com/github/codeql). This project
7+
contains a Docker file which builds a container with the latest version of codeql-cli, and the latest codeql queries precompiled.
8+
It also contains automation to keep the toolchain in the container updated. You can use this container to:
99

1010
* Start using codeql-cli and run queries on your projects without installing it on your local machine.
11-
* Use is as an environment to develop codeql queries and test them.
12-
* Test how the queries perform in windows and linux environments.
11+
* Use it as an environment to develop codeql queries and test them.
12+
* Test how the queries perform in windows and linux environments (and more...)
1313

1414
We shall continue to add more features and would be happy to accept contributions from the community.
1515

16+
### TL;DR
17+
18+
Analyze a python project django located in the folder ```/tmp/django``` by running the security and quality QL pack on it:
19+
20+
```
21+
/scripts/unix/analyze_security.sh /tmp/django/src /tmp/django/results python
22+
```
23+
24+
The results will be stored in /tmp/django/results/issues.sarif.
25+
26+
Analyze the Javascript project express located in /tmp/express/src by running the extended security QL pack on it:
27+
```
28+
scripts/unix/run_qlpack.sh /tmp/express/src /tmp/express/results javascript security-extended
29+
```
30+
31+
The results will be stored in /tmp/express/results/issues.sarif
32+
33+
To find a list of installed QL packs in the container:
34+
35+
```
36+
docker run --rm --name codeql-container -e CODEQL_CLI_ARGS="resolve qlpacks" mcr.microsoft.com/cstsectools/codeql-container
37+
```
38+
1639
### Basic Usage
1740

1841
#### Downloading a pre-built container
@@ -23,45 +46,48 @@ You can pull the image by running the command:
2346
```
2447
$ docker pull mcr.microsoft.com/cstsectools/codeql-container
2548
```
49+
The codeQL container executes one codeQL command per invocation. We kept this design decision because it makes it easy for the user to run any codeQL command, and not be bound by the automation scripts inside the container.
2650

27-
If you want to analyze a particular source directory with codeql, run the container as:
51+
So, if you want to analyze a particular source directory with the container, you start by running the container as:
2852

2953
```
3054
$ docker run --rm --name codeql-container -v /dir/to/analyze:/opt/src -v /dir/for/results:/opt/results -e CODEQL_CLI_ARGS=<query run...> mcr.microsoft.com/cstsectools/codeql-container
3155
```
3256

3357
where `/dir/to/analyze` contains the source files that have to be analyzed, and `/dir/for/results` is where the result output
34-
needs to be stored, and you can specify QL_PACKS environment variable for specific QL packs to be run on the provided code.
58+
needs to be stored, and you can specify CODEQL_CLI_ARGS environment variable for specific QL packs to be run on the provided code, among other things. The CODEQL_CLI_ARGS will be passed over to codeQL command line as it is.
59+
3560
For more information on CodeQL and QL packs, please visit https://www.github.com/github/codeql.
3661

37-
`CODEQL_CLI_ARGS` are the arguments that will be directly passed on to the codeql-cli. Some examples of `CODEQL_CLI_ARGS` are:
62+
`CODEQL_CLI_ARGS` are the arguments that will be directly passed on to the codeql-cli. For example:
3863

3964
```
40-
CODEQL_CLI_ARGS="database create /opt/src/source_db"
65+
CODEQL_CLI_ARGS="database create /opt/results/source_db -s /opt/src"
4166
```
4267

43-
**Note:** If you map your source volume to some other mountpoint other than /opt/src, you will have to make the corresponding changes
44-
in the `CODEQL_CLI_ARGS`.
68+
This argument will create a codeQL db of your project (that you have mapped to the /opt/src folder above) in the /opt/results folder, which maps to /dir/for/results above on your local computer.
69+
70+
> **Note:** If you map your source volume to some other mountpoint other than /opt/src, you will have to make the corresponding changes
71+
> in the `CODEQL_CLI_ARGS`.
4572
46-
There are some additional docker environment variables that you can specify to control the execution of the container:
73+
There are some additional docker environment flags that you can set/unset to control the execution of the container:
4774

4875
* `CHECK_LATEST_CODEQL_CLI` - If there is a newer version of codeql-cli, download and install it
4976
* `CHECK_LATEST_QUERIES` - if there is are updates to the codeql queries repo, download and use it
5077
* `PRECOMPILE_QUERIES` - If we downloaded new queries, precompile all new query packs (query execution will be faster)
5178

52-
**WARNING:** Precompiling query packs might take a few hours, depending on speed of your machine and the CPU/memory limits (if any)
53-
you have placed on the container.
79+
> **WARNING:** Precompiling query packs might take a few hours, depending on speed of your machine and the CPU/memory limits (if any)
80+
> you have placed on the container.
5481
55-
Since CodeQL first creates a database of the code representation, and then analyzes the db for issues, we need a few commands to
56-
analyze a source code repo.
82+
Since CodeQL first creates a database of the code representation, and then analyzes the said database for issues, we need to invoke the container more than once to analyze a source code repo. (Since the container only executes one codeQL command per invocation.)
5783

5884
For example, if you want to analyze a python project source code placed in `/dir/to/analyze` (or `C:\dir\to\analyze` for example, in Windows),
5985
to analyze and get a SARIF result file, you will have to run:
6086

6187
```
62-
$ docker run --rm --name codeql-container -v /dir/to/analyze:/opt/src -v /dir/for/results:/opt/results -e CODEQL_CLI_ARGS="database create --language=python /opt/src/source_db /opt/output/source_db" mcr.microsoft.com/cstsectools/codeql-container
63-
$ docker run --rm --name codeql-container -v /dir/to/analyze:/opt/src -v /dir/for/results:/opt/results -e CODEQL_CLI_ARGS=" database upgrade /opt/src/source_db" mcr.microsoft.com/cstsectools/codeql-container
64-
$ docker run --rm --name codeql-container -v /dir/to/analyze:/opt/src -v /dir/for/results:/opt/results -e CODEQL_CLI_ARGS="database analyze --format=sarifv2 --output=/opt/results/issues.sarif /opt/src/source_db" mcr.microsoft.com/cstsectools/codeql-container
88+
$ docker run --rm --name codeql-container -v /dir/to/analyze:/opt/src -v /dir/for/results:/opt/results -e CODEQL_CLI_ARGS="database create --language=python /opt/results/source_db -s /opt/src" mcr.microsoft.com/cstsectools/codeql-container
89+
$ docker run --rm --name codeql-container -v /dir/to/analyze:/opt/src -v /dir/for/results:/opt/results -e CODEQL_CLI_ARGS=" database upgrade /opt/results/source_db" mcr.microsoft.com/cstsectools/codeql-container
90+
$ docker run --rm --name codeql-container -v /dir/to/analyze:/opt/src -v /dir/for/results:/opt/results -e CODEQL_CLI_ARGS="database analyze --format=sarifv2 --output=/opt/results/issues.sarif /opt/results/source_db" mcr.microsoft.com/cstsectools/codeql-container
6591
```
6692

6793
For more information on CodeQL and QL packs, please visit https://www.github.com/github/codeql.
@@ -101,8 +127,15 @@ for JavaScript:
101127

102128
##### run_qlpack.sh
103129
If you know which QL suite you would like to run on the code to be analyzed, use scripts/unix/run_qlpack.sh (or scripts/windows/run_qlpack.bat for windows).
130+
131+
```
132+
scripts/unix/run_qlpack.sh /path/to/analyze /path/to/results language qlpack
133+
```
134+
135+
For example, on windows:
136+
104137
```
105-
scripts/unix/analyze_security.sh /path/to/analyze /path/to/results language qlpack
138+
scripts\windows\run_ql_suite.bat e:\temp\express\src e:\temp\express\results javascript code-scanning
106139
```
107140

108141
# Contributing

container/libs/codeql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ def execute_codeql_command(self, args):
9292
if ret_string is CalledProcessError:
9393
logger.error("Could not run codeql command")
9494
exit(ERROR_EXECUTING_CODEQL)
95-
return ret_string
95+
return bytearray(ret_string).decode('utf-8')

container/startup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def main():
4545
else:
4646
codeql = CodeQL(CODEQL_HOME)
4747
run_result = codeql.execute_codeql_command(CODEQL_CLI_ARGS)
48-
48+
print(run_result)
49+
4950
if WAIT_AFTER_EXEC:
5051
logger.info("Wait forever specified, waiting...")
5152
while True:

scripts/unix/analyze_security.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ else
4848
exit 1
4949
fi
5050

51-
docker run --rm --name codeql-container -v "${inputfile}:/opt/src" -v "${outputfile}:/opt/results" -e CODEQL_CLI_ARGS=database\ upgrade\ /opt/src/source_db mcr.microsoft.com/cstsectools/codeql-container
51+
docker run --rm --name codeql-container -v "${inputfile}:/opt/src" -v "${outputfile}:/opt/results" -e CODEQL_CLI_ARGS=database\ upgrade\ /opt/results/source_db mcr.microsoft.com/cstsectools/codeql-container
5252
if [ $? -eq 0 ]
5353
then
5454
print_green "\nUpgraded the database\n"
@@ -58,7 +58,7 @@ else
5858
fi
5959

6060
print_yellow "\nRunning the Quality and Security rules on the project"
61-
docker run --rm --name codeql-container -v ${inputfile}:/opt/src -v ${outputfile}:/opt/results -e CODEQL_CLI_ARGS=database\ analyze\ /opt/src/source_db\ --format=sarifv2\ --output=/opt/results/issues.sarif\ ${language}-security-and-quality.qls mcr.microsoft.com/cstsectools/codeql-container
61+
docker run --rm --name codeql-container -v ${inputfile}:/opt/src -v ${outputfile}:/opt/results -e CODEQL_CLI_ARGS=database\ analyze\ /opt/results/source_db\ --format=sarifv2\ --output=/opt/results/issues.sarif\ ${language}-security-and-quality.qls mcr.microsoft.com/cstsectools/codeql-container
6262
if [ $? -eq 0 ]
6363
then
6464
print_green "\nQuery execution successful"

scripts/unix/run_ql_suite.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ else
5050
exit 1
5151
fi
5252

53-
docker run --rm --name codeql-container -v "${inputfile}:/opt/src" -v "${outputfile}:/opt/results" -e CODEQL_CLI_ARGS=database\ upgrade\ /opt/src/source_db mcr.microsoft.com/cstsectools/codeql-container
53+
docker run --rm --name codeql-container -v "${inputfile}:/opt/src" -v "${outputfile}:/opt/results" -e CODEQL_CLI_ARGS=database\ upgrade\ /opt/results/source_db mcr.microsoft.com/cstsectools/codeql-container
5454
if [ $? -eq 0 ]
5555
then
5656
print_green "\nUpgraded the database\n"
@@ -59,8 +59,8 @@ else
5959
exit 2
6060
fi
6161

62-
print_yellow "\nRunning the Quality and Security rules on the project"
63-
docker run --rm --name codeql-container -v ${inputfile}:/opt/src -v ${outputfile}:/opt/results -e CODEQL_CLI_ARGS=database\ analyze\ /opt/src/source_db\ --format=sarifv2\ --output=/opt/results/issues.sarif\ ${language}-${qlpack}.qls mcr.microsoft.com/cstsectools/codeql-container
62+
print_yellow "\nRunning the ${qlpack} ql pack rules on the project"
63+
docker run --rm --name codeql-container -v ${inputfile}:/opt/src -v ${outputfile}:/opt/results -e CODEQL_CLI_ARGS=database\ analyze\ /opt/results/source_db\ --format=sarifv2\ --output=/opt/results/issues.sarif\ ${language}-${qlpack}.qls mcr.microsoft.com/cstsectools/codeql-container
6464
if [ $? -eq 0 ]
6565
then
6666
print_green "\nQuery execution successful"

scripts/windows/analyze_security.bat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ docker pull mcr.microsoft.com/cstsectools/codeql-container
2424
call :print_green "Pulled the container"
2525

2626
call :print_yellow "Creating the codeQL database. This might take some time depending on the size of the project..."
27-
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database create --language=%language%% /opt/src/source_db -s /opt/src" mcr.microsoft.com/cstsectools/codeql-container
27+
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database create --language=%language%% /opt/results/source_db -s /opt/src" mcr.microsoft.com/cstsectools/codeql-container
2828

2929
if %errorlevel% GTR 0 (
3030
call :print_red "Failed creating the database"
3131
exit /b %errorlevel%
3232
)
3333

34-
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database upgrade /opt/src/source_db" mcr.microsoft.com/cstsectools/codeql-container
34+
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database upgrade /opt/results/source_db" mcr.microsoft.com/cstsectools/codeql-container
3535
if %errorlevel% GTR 0 (
3636
call :print_red "Failed upgrading the database"
3737
exit /b %errorlevel%
3838
)
3939

4040
call :print_yellow "Running the Quality and Security rules on the project"
41-
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database analyze /opt/src/source_db --format=sarifv2 --output=/opt/results/issues.sarif %language%-security-and-quality.qls" mcr.microsoft.com/cstsectools/codeql-container
41+
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database analyze /opt/results/source_db --format=sarifv2 --output=/opt/results/issues.sarif %language%-security-and-quality.qls" mcr.microsoft.com/cstsectools/codeql-container
4242
if %errorlevel% GTR 0 (
4343
call :print_red "Failed to run the query on the database"
4444
exit /b %errorlevel%

scripts/windows/run_ql_suite.bat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ docker pull mcr.microsoft.com/cstsectools/codeql-container
2525
call :print_green "Pulled the container"
2626

2727
call :print_yellow "Creating the codeQL database. This might take some time depending on the size of the project..."
28-
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database create --language=%language%% /opt/src/source_db -s /opt/src" mcr.microsoft.com/cstsectools/codeql-container
28+
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database create --language=%language%% /opt/results/source_db -s /opt/src" mcr.microsoft.com/cstsectools/codeql-container
2929

3030
if %errorlevel% GTR 0 (
3131
call :print_red "Failed creating the database"
3232
exit /b %errorlevel%
3333
)
3434

35-
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database upgrade /opt/src/source_db" mcr.microsoft.com/cstsectools/codeql-container
35+
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database upgrade /opt/results/source_db" mcr.microsoft.com/cstsectools/codeql-container
3636
if %errorlevel% GTR 0 (
3737
call :print_red "Failed upgrading the database"
3838
exit /b %errorlevel%
3939
)
4040

41-
call :print_yellow "Running the Quality and Security rules on the project"
42-
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database analyze /opt/src/source_db --format=sarifv2 --output=/opt/results/issues.sarif %language%-%qlpack%.qls" mcr.microsoft.com/cstsectools/codeql-container
41+
call :print_yellow "Running the %qlpack% ql pack rules on the project"
42+
start /W /B docker run --rm --name codeql-container -v "%inputfile%:/opt/src" -v "%outputfile%:/opt/results" -e CODEQL_CLI_ARGS="database analyze /opt/results/source_db --format=sarifv2 --output=/opt/results/issues.sarif %language%-%qlpack%.qls" mcr.microsoft.com/cstsectools/codeql-container
4343
if %errorlevel% GTR 0 (
4444
call :print_red "Failed to run the query on the database"
4545
exit /b %errorlevel%

0 commit comments

Comments
 (0)