Skip to content

Commit db96b9c

Browse files
authored
[EDI] Publishing and using CodeQL packs (#60066)
1 parent 249a816 commit db96b9c

File tree

7 files changed

+202
-200
lines changed

7 files changed

+202
-200
lines changed

content/code-security/how-tos/scan-code-for-vulnerabilities/scan-from-the-command-line/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ topics:
1212
- CodeQL
1313
children:
1414
- /setting-up-the-codeql-cli
15-
- /writing-and-sharing-custom-queries-for-the-codeql-cli
15+
- /write-custom-queries
16+
- /publish-and-use-packs
1617
- /testing-custom-queries
1718
- /testing-query-help-files
1819
- /download-databases
1920
- /check-out-source-code
2021
- /specifying-command-options-in-a-codeql-configuration-file
2122
- /creating-database-bundle-for-troubleshooting
22-
2323
redirect_from:
2424
- /code-security/codeql-cli/using-the-codeql-cli
2525
- /code-security/codeql-cli/getting-started-with-the-codeql-cli
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
title: Publishing and using CodeQL packs
3+
shortTitle: Publish and use packs
4+
intro: Share or download a {% data variables.product.prodname_codeql %} pack, then analyze your {% data variables.product.prodname_codeql %} database.
5+
product: '{% data reusables.gated-features.codeql %}'
6+
versions:
7+
fpt: '*'
8+
ghes: '*'
9+
ghec: '*'
10+
topics:
11+
- Code Security
12+
- Code scanning
13+
- CodeQL
14+
redirect_from:
15+
- /code-security/codeql-cli/publishing-and-using-codeql-packs
16+
- /code-security/codeql-cli/using-the-codeql-cli/publishing-and-using-codeql-packs
17+
- /code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/publishing-and-using-codeql-packs
18+
- /code-security/tutorials/customize-code-scanning/publishing-and-using-codeql-packs
19+
contentType: how-tos
20+
---
21+
22+
{% ifversion ghec or ghes %}
23+
24+
## Working with {% data variables.product.prodname_codeql %} packs on {% data variables.enterprise.gh_enterprise %}
25+
26+
By default, the {% data variables.product.prodname_codeql_cli %} expects to download {% data variables.product.prodname_codeql %} packs from and publish packs to the {% data variables.product.prodname_container_registry %} on {% data variables.product.prodname_dotcom_the_website %}. However, you can also work with {% data variables.product.prodname_codeql %} packs in a {% data variables.product.prodname_container_registry %} on {% data variables.enterprise.gh_enterprise %} by creating a `qlconfig.yml` file to tell the CLI which {% data variables.product.prodname_container_registry %} to use for each pack.
27+
28+
Create a `~/.codeql/qlconfig.yml` file on Linux/MacOS or `%HOMEPATH%\.codeql\qlconfig.yml` on Windows using your preferred text editor, and add entries to specify which registry to use for one or more package name patterns.
29+
For example, the following `qlconfig.yml` file associates all packs with the {% data variables.product.prodname_container_registry %} at `{% data variables.enterprise.gh_enterprise_domain %}`, except packs matching `codeql/\*` or the `other-org/*` organization, which are associated with the {% data variables.product.prodname_container_registry %} on {% data variables.product.prodname_dotcom_the_website %}:
30+
31+
```yaml
32+
registries:
33+
- packages:
34+
- 'codeql/*'
35+
- 'other-org/*'
36+
# {% data variables.product.prodname_container_registry %} on {% data variables.product.prodname_dotcom_the_website %}
37+
url: https://ghcr.io/v2/
38+
- packages: '*'
39+
# {% data variables.product.prodname_container_registry %} hosted at `{% data variables.enterprise.gh_enterprise_domain %}`
40+
url: {% data variables.enterprise.gh_enterprise_container_registry %}
41+
```
42+
43+
The {% data variables.product.prodname_codeql_cli %} will determine which registry to use for a given package name by finding the first item in the `registries` list with a `packages` property that matches that package name.
44+
This means that you’ll generally want to define the most specific package name patterns first. The `packages` property may be a single package name, a glob pattern, or a YAML list of package names and glob patterns.
45+
46+
The `registries` list can also be placed inside a `codeql-workspace.yml` file. Doing so will allow you to define the registries to be used within a specific workspace, so that it can be shared amongst other {% data variables.product.prodname_codeql %} users of the workspace. The `registries` list in `codeql-workspace.yml` will be merged with and take precedence over the list in the global `qlconfig.yml`. For more information about `codeql-workspace.yml`, see [AUTOTITLE](/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/about-codeql-workspaces#about-codeql-workspaces).
47+
48+
You can now use `codeql pack publish`, `codeql pack download`, and `codeql database analyze` to manage packs on {% data variables.enterprise.gh_enterprise %}.
49+
50+
{% endif %}
51+
52+
## Authenticating to {% data variables.product.github %} {% data variables.product.prodname_container_registries %}
53+
54+
You can publish packs and download private packs by authenticating to the appropriate {% data variables.product.github %} {% data variables.product.prodname_container_registry %}.
55+
56+
{% ifversion ghec or ghes %}
57+
58+
### Authenticating to {% data variables.product.prodname_container_registries %} on {% data variables.product.prodname_dotcom_the_website %}
59+
60+
{% endif %}
61+
62+
You can authenticate to the {% data variables.product.prodname_container_registry %} in two ways:
63+
64+
1. Pass the `--github-auth-stdin` option to the {% data variables.product.prodname_codeql_cli %}, then supply a {% data variables.product.prodname_github_apps %} token or {% data variables.product.pat_generic %} via standard input.
65+
1. Set the `GITHUB_TOKEN` environment variable to a {% data variables.product.prodname_github_apps %} token or {% data variables.product.pat_generic %}.
66+
67+
{% ifversion ghec or ghes %}
68+
69+
### Authenticating to {% data variables.product.prodname_container_registries %} on {% data variables.enterprise.gh_enterprise %}
70+
71+
Similarly, you can authenticate to a {% data variables.product.prodname_container_registry %} on {% data variables.enterprise.gh_enterprise %}, or authenticate to multiple registries simultaneously (for example, to download or run private packs from multiple registries) in two ways:
72+
73+
1. Pass the `--registries-auth-stdin` option to the {% data variables.product.prodname_codeql_cli %}, then supply a registry authentication string via standard input.
74+
1. Set the `CODEQL_REGISTRIES_AUTH` environment variable to a registry authentication string.
75+
76+
A registry authentication string is a comma-separated list of `<registry-url>=<token>` pairs, where `registry-url` is a {% data variables.product.prodname_container_registry %} URL, such as `{% data variables.enterprise.gh_enterprise_container_registry %}`, and `token` is a {% data variables.product.prodname_github_apps %} token or {% data variables.product.pat_generic %} for that {% data variables.product.prodname_container_registry %}.
77+
This ensures that each token is only passed to the {% data variables.product.prodname_container_registry %} you specify.
78+
79+
For example, the following registry authentication string specifies that the {% data variables.product.prodname_codeql_cli %} should authenticate as follows:
80+
81+
* Use the token `<token1>` to authenticate to {% data variables.product.prodname_container_registry %} on {% data variables.product.prodname_dotcom_the_website %}.
82+
* Use the token `<token2>` to authenticate to the {% data variables.product.prodname_container_registry %} for the enterprise at `{% data variables.enterprise.gh_enterprise_container_registry %}`.
83+
84+
```shell
85+
https://ghcr.io/v2/=<token1>,{% data variables.enterprise.gh_enterprise_container_registry %}=<token2>
86+
```
87+
88+
{% endif %}
89+
90+
## Publishing your {% data variables.product.prodname_codeql %} pack
91+
92+
To share your {% data variables.product.prodname_codeql %} pack with other people, you can publish it to the {% data variables.product.prodname_container_registry %}.
93+
94+
### Configuring the `qlpack.yml` file before publishing
95+
96+
{% data reusables.code-scanning.codeql-cli-version-ghes %}
97+
98+
You can check and modify the configuration details of your {% data variables.product.prodname_codeql %} pack prior to publishing. Open the `qlpack.yml` file in your preferred text editor.
99+
100+
```yaml
101+
library: # set to true if the pack is a library. Set to false or omit for a query pack
102+
name: <scope>/<pack>
103+
version: <x.x.x>
104+
description: <Description to publish with the package>
105+
defaultSuite: # optional, one or more queries in the pack to run by default
106+
- query: <relative-path>/query-file>.ql
107+
defaultSuiteFile: default-queries.qls # optional, a pointer to a query-suite in this pack
108+
license: # optional, the license under which the pack is published
109+
dependencies: # map from CodeQL pack name to version range
110+
```
111+
112+
* `name:` must follow the `<scope>/<pack>` format, where `<scope>` is the {% data variables.product.prodname_dotcom %} organization that you will publish to and `<pack>` is the name for the pack.
113+
114+
* A maximum of one of `defaultSuite` or `defaultSuiteFile` is allowed. These are two different ways to define a default query suite to be run, the first by specifying queries directly in the qlpack.yml file and the second by specifying a query suite in the pack.
115+
116+
### Running `codeql pack publish`
117+
118+
When you are ready to publish a pack to the {% data variables.product.prodname_dotcom %} {% data variables.product.prodname_container_registry %}, you can run the following command in the root of the pack directory:
119+
120+
```shell
121+
codeql pack publish
122+
```
123+
124+
The published package will be displayed in the packages section of {% data variables.product.prodname_dotcom %} organization specified by the scope in the `qlpack.yml` file.
125+
126+
> [!NOTE]
127+
> If you're publishing model packs to the {% data variables.product.prodname_dotcom %} {% data variables.product.prodname_container_registry %} in order to extend coverage to all repositories in an organization as part of a default setup configuration, then you need to ensure that repositories running code scanning can access those model packs. For more information, see [AUTOTITLE](/code-security/code-scanning/managing-your-code-scanning-configuration/editing-your-configuration-of-default-setup) and [AUTOTITLE](/packages/learn-github-packages/configuring-a-packages-access-control-and-visibility).
128+
129+
## Downloading an existing {% data variables.product.prodname_codeql %} pack
130+
131+
To run a pack that someone else has created, you must first download it by running the following command:
132+
133+
```shell
134+
codeql pack download <scope>/<pack>@x.x.x
135+
```
136+
137+
* `<scope>`: the name of the {% data variables.product.prodname_dotcom %} organization that you will download from.
138+
* `<pack>`: the name for the pack that you want to download.
139+
* `@x.x.x`: an optional version number. If omitted, the latest version will be downloaded.
140+
141+
This command accepts arguments for multiple packs.
142+
143+
If you write scripts that specify a particular version number of a
144+
query pack to download, keep in mind that when you update your version of
145+
{% data variables.product.prodname_codeql %} to a newer one, you may
146+
also need to switch to a newer version of the query pack. Newer
147+
versions of {% data variables.product.prodname_codeql %} _may_ provide
148+
degraded performance when used with query packs that have been pinned
149+
to a very old version. For more information, see [About {% data variables.product.prodname_codeql %}
150+
pack compatibility](#about-codeql-pack-compatibility).
151+
152+
## Using a {% data variables.product.prodname_codeql %} pack to analyze a {% data variables.product.prodname_codeql %} database
153+
154+
To analyze a {% data variables.product.prodname_codeql %} database with a {% data variables.product.prodname_codeql %} pack, run the following command:
155+
156+
```shell
157+
codeql database analyze <database> <scope>/<pack>@x.x.x:<path>
158+
```
159+
160+
* `<database>`: the {% data variables.product.prodname_codeql %} database to be analyzed.
161+
* `<scope>`: the name of the {% data variables.product.prodname_dotcom %} organization that the pack is published to.
162+
* `<pack>`: the name for the pack that you are using.
163+
* `@x.x.x`: an optional version number. If omitted, the latest version will be used.
164+
* `:<path>`: an optional path to a query, directory, or query suite. If omitted, the pack’s default query suite will be used.
165+
166+
The `analyze` command will run the default suite of any specified {% data variables.product.prodname_codeql %} packs. You can specify multiple {% data variables.product.prodname_codeql %} packs to be used for analyzing a {% data variables.product.prodname_codeql %} database. For example:
167+
168+
```shell
169+
codeql <database> analyze <scope>/<pack> <scope>/<other-pack>
170+
```
171+
172+
> [!NOTE]
173+
> The `codeql pack download` command stores the pack it downloads in an internal location that is not intended for local modification. Unexpected (and hard to troubleshoot) behavior may result if the pack is modified after downloading. For more information about customizing packs, see [AUTOTITLE](/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/creating-and-working-with-codeql-packs).

content/code-security/how-tos/scan-code-for-vulnerabilities/scan-from-the-command-line/writing-and-sharing-custom-queries-for-the-codeql-cli.md renamed to content/code-security/how-tos/scan-code-for-vulnerabilities/scan-from-the-command-line/write-custom-queries.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: Writing and sharing custom queries for the CodeQL CLI
3-
shortTitle: Write and share queries
2+
title: Writing custom queries for the CodeQL CLI
3+
shortTitle: Write custom queries
44
intro: You can write your own {% data variables.product.prodname_codeql %} queries to find specific vulnerabilities and errors.
55
product: '{% data reusables.gated-features.codeql %}'
66
allowTitleToDifferFromFilename: true
@@ -16,6 +16,7 @@ redirect_from:
1616
- /code-security/codeql-cli/using-custom-queries-with-the-codeql-cli
1717
- /code-security/codeql-cli/using-the-codeql-cli/using-custom-queries-with-the-codeql-cli
1818
- /code-security/how-tos/scan-code-for-vulnerabilities/scan-from-the-command-line/using-custom-queries-with-the-codeql-cli
19+
- /code-security/how-tos/scan-code-for-vulnerabilities/scan-from-the-command-line/writing-and-sharing-custom-queries-for-the-codeql-cli
1920
contentType: how-tos
2021
---
2122

@@ -35,21 +36,6 @@ When running queries with the `database analyze` command, you must include the f
3536

3637
For more information about these metadata properties, see [Metadata for {% data variables.product.prodname_codeql %} queries](https://codeql.github.com/docs/writing-codeql-queries/metadata-for-codeql-queries/#metadata-for-codeql-queries) and the [Query metadata style guide](https://github.com/github/codeql/blob/main/docs/query-metadata-style-guide.md).
3738

38-
## Packaging custom queries
39-
40-
When you write queries to share with others, save them in a custom {% data variables.product.prodname_codeql %} pack.
41-
42-
To package your custom queries:
43-
44-
1. Create a {% data variables.product.prodname_codeql %} pack with a `qlpack.yml` file in the root directory.
45-
1. Save your custom queries (`.ql` files) in the pack root or its subdirectories.
46-
1. Configure the `qlpack.yml` file to specify:
47-
* How to compile the queries
48-
* Dependencies on other {% data variables.product.prodname_codeql %} packs and libraries
49-
* Query suite definitions
50-
For more information about `qlpack.yml` properties, see [AUTOTITLE](/code-security/codeql-cli/getting-started-with-the-codeql-cli/customizing-analysis-with-codeql-packs#codeqlpack-yml-properties).
51-
1. Publish your pack to {% data variables.product.prodname_registry %} - the {% data variables.product.prodname_dotcom %} {% data variables.product.prodname_container_registry %}. For more information, see [AUTOTITLE](/code-security/codeql-cli/getting-started-with-the-codeql-cli/customizing-analysis-with-codeql-packs).
52-
5339
## Including query help for custom {% data variables.product.prodname_codeql %} queries in SARIF files
5440

5541
For information about query help and documentation formats, see [AUTOTITLE](/code-security/concepts/code-scanning/codeql/custom-codeql-queries#query-documentation).
@@ -69,6 +55,6 @@ To include query help in SARIF files when running code scanning analyses:
6955
> The `--sarif-add-query-help` option is available from {% data variables.product.prodname_codeql_cli %} v2.7.1 onwards.
7056
1. Upload the SARIF file to {% data variables.product.github %}.
7157

72-
## Further reading
58+
## Next steps
7359

74-
* [{% data variables.product.prodname_codeql %} queries](https://codeql.github.com/docs/writing-codeql-queries/codeql-queries/#codeql-queries)
60+
To share and use your custom queries, see [AUTOTITLE](/code-security/how-tos/scan-code-for-vulnerabilities/scan-from-the-command-line/publish-and-use-packs).

0 commit comments

Comments
 (0)