Skip to content

Commit 53ce3b0

Browse files
Add tutorial for end-to-end scanning to DejaCode #1280 (#1295)
* Add e2e doc skeleton #1280 This is a basic sekelton for a new end-to-end tutorial bringing together - deplock - scancode.io - dejacode Reference: #1280 Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com> * Add tutorial for end-to-end scanning to DejaCode Signed-off-by: Keshav Priyadarshi <git@keshav.space> * Fix archive url Signed-off-by: Keshav Priyadarshi <git@keshav.space> * Fix typo Signed-off-by: Keshav Priyadarshi <git@keshav.space> --------- Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com> Signed-off-by: Keshav Priyadarshi <git@keshav.space> Co-authored-by: Keshav Priyadarshi <git@keshav.space>
1 parent a41f90a commit 53ce3b0

4 files changed

+266
-0
lines changed
Loading
Loading

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ you’ll find information on:
3333
tutorial_license_policies
3434
tutorial_vulnerablecode_integration
3535
tutorial_web_ui_symbol_and_string_collection
36+
tutorial_cli_end_to_end_scanning_to_dejacode
3637

3738
.. toctree::
3839
:maxdepth: 2
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
.. _tutorial_cli_end_to_end_scanning_to_dejacode:
2+
3+
Analyze Codebase End to End with DepLock and DejaCode (Command Line)
4+
=====================================================================
5+
6+
The focus of this tutorial is to guide you through scanning a codebase end to end, starting with the
7+
dependency resolution, through the scanning proper, and finally the upload of the scan in DejaCode,
8+
using DepLock and ScanCode.io.
9+
10+
This is designed to run a faster, simple **inspect_packages** ScanCode.io pipeline.
11+
12+
13+
.. note::
14+
This tutorial assumes you have Docker installed locally on your machine and that you have
15+
access to a DejaCode installation with an API key. See the DejaCode installation guide
16+
for installation instructions: https://dejacode.readthedocs.io/en/latest/installation.html
17+
18+
19+
Requirements
20+
------------
21+
22+
Before you follow the instructions in this tutorial, you need to:
23+
24+
- Have **Shell access** on the machine where docker is installed.
25+
- Have the API URL and API key for your DejaCode instance.
26+
27+
28+
Docker Engine installation
29+
~~~~~~~~~~~~~~~~~~~~~~~~~~
30+
31+
The ScanCode app container requires the Docker engine to run scans.
32+
If you haven't already, install Docker on your machine by referring
33+
to the `official Docker documentation <https://docs.docker.com/get-docker/>`_
34+
and following the installation instructions.
35+
36+
37+
38+
Outline of the processing steps
39+
--------------------------------
40+
41+
The process for this tutorial is to:
42+
43+
1. Fetch the codebase to scan.
44+
2. Ensure that it contains a proper scancode-config.yml file with references to a DejaCode
45+
product and version for this codebase.
46+
3. Download and run the latest DepLock for each ecosystem of this codebase.
47+
4. Run ScanCode.io and collect results.
48+
5. Upload the scans results to DejaCode for the product of this codebase.
49+
6. Trigger detailed scan in DejaCode for all packages of the product.
50+
51+
52+
.. note::
53+
Below instructions have been tested on Linux for now.
54+
55+
56+
Fetch codebase to scan
57+
----------------------
58+
59+
**Local Project Codebase**: Ensure you have a local checkout of your project's codebase.
60+
61+
We are using this repo for our tutorial: https://github.com/nexB/scancode-workbench
62+
63+
We download the following: https://github.com/nexB/scancode-workbench/archive/refs/tags/v4.0.2.tar.gz
64+
65+
Extract the checkout and delete the archive:
66+
67+
.. code-block:: shell
68+
69+
tar -xzf scancode-workbench-4.0.2.tar.gz && rm scancode-workbench-4.0.2.tar.gz
70+
71+
72+
Create scancode-config.yml config file
73+
--------------------------------------
74+
75+
Add the scancode-config.yml file for ScanCode.io.
76+
77+
Here we provide the product name and version and also configure it to
78+
ignore test files and development dependencies.
79+
80+
.. code-block:: yaml
81+
:caption: scancode-config.yml
82+
:emphasize-lines: 4, 6-7
83+
84+
product_name: scancode-workbench
85+
product_version: '4.0.2'
86+
ignored_patterns:
87+
- 'tests/*'
88+
ignored_dependency_scopes:
89+
- package_type: npm
90+
scope: devDependencies
91+
92+
.. note::
93+
See :ref:`project_configuration_settings` for all the available
94+
scancode-config settings and details about each setting.
95+
96+
97+
Download and run DepLock
98+
------------------------
99+
100+
Now we will use DepLock to make sure that all the lockfiles are in place before we run our scan.
101+
102+
Download the latest DepLock binary depending on your OS and architecture:
103+
104+
.. code-block:: shell
105+
106+
# Replace <OS> with your operating system (linux, darwin, windows)
107+
# Replace <ARCH> with your architecture (amd64, arm64)
108+
curl -LO https://github.com/nexB/dependency-inspector/releases/latest/download/deplock-<OS>-<ARCH>
109+
110+
111+
- Here, we are on amd64 linux and will use:
112+
113+
.. code-block:: shell
114+
115+
curl -LO https://github.com/nexB/dependency-inspector/releases/latest/download/deplock-linux-amd64
116+
117+
- Make the binary executable:
118+
119+
.. code-block:: shell
120+
121+
chmod +x deplock-linux-amd64
122+
123+
- Run DepLock on scancode-workbench-4.0.2:
124+
125+
.. code-block:: shell
126+
127+
./deplock-linux-amd64 npm scancode-workbench-4.0.2
128+
129+
.. note::
130+
Since the workbench is entirely an npm project, we are running npm only.
131+
If your project uses multiple package managers, run DepLock for all the
132+
package manager ecosystems. See the supported ecosystems here:
133+
https://github.com/nexB/dependency-inspector?tab=readme-ov-file#supported-ecosystems.
134+
135+
136+
.. tip::
137+
If your project is composed of multiple packages, let's suppose it's a monorepo
138+
for multiple npm packages. Then, run DepLock for each package and provide the
139+
root of each package as the path.
140+
141+
In the case of a NuGet project containing multiple packages, run ``./deplock-linux-amd64 nuget``
142+
at the root of the entire project, and there is no need to run it separately for each package.
143+
144+
145+
Run ScanCode Package Detection
146+
-------------------------------
147+
148+
Execute the following command to run the ScanCode scanner
149+
with **inspect_packages** pipeline on codebase in our current directory.
150+
151+
.. code-block:: shell
152+
153+
docker run --rm \
154+
-v "$(pwd)":/code \
155+
ghcr.io/nexb/scancode.io:latest \
156+
sh -c "run inspect_packages /code" \
157+
> results.json
158+
159+
160+
Once completed, you will find the `results.json`
161+
**results file in your current directory**.
162+
163+
164+
Upload Scan Results in DejaCode
165+
--------------------------------
166+
167+
To upload the scan results stored in the ``results.json`` file to DejaCode,
168+
we need to ensure that we have a product set up for this project in DejaCode.
169+
170+
- **Create Product on DejaCode**
171+
172+
.. note::
173+
You can skip this step if you already have a product in DejaCode, just get
174+
the ``UUID`` of the product and proceed to the next step.
175+
176+
Run this command to create the scancode-workbench v4.0.2 product in DejaCode:
177+
178+
Replace `<DEJACODE-URL>` with your DejaCode URL and `<YOUR-DEJACODE-API-KEY>` with your DejaCode API key.
179+
180+
.. code-block:: shell
181+
182+
curl -X POST "https://<DEJACODE-URL>/api/v2/products/" \
183+
-H "Authorization: Token <YOUR-DEJACODE-API-KEY>" \
184+
-H "Content-Type: application/json" \
185+
-d '{
186+
"name": "scancode-workbench",
187+
"version": "4.0.2",
188+
"vcs_url": "https://github.com/nexB/scancode-workbench.git",
189+
"homepage_url": "https://github.com/nexB/scancode-workbench"
190+
}'
191+
192+
.. tip::
193+
Apart from name, version, and vcs_url, you can provide a host of parameters.
194+
See `https://<DEJACODE-URL>/api/v2/docs/#products-create` for more details.
195+
196+
We get the below response, and we need to grab the ``UUID`` from the highlighted line in the JSON response.
197+
198+
.. code-block:: json
199+
:emphasize-lines: 5
200+
201+
{
202+
"display_name":"scancode-workbench 4.0.2",
203+
"api_url":"https://<DEJACODE-URL>/api/v2/products/<UUID>/",
204+
"absolute_url":"https://<DEJACODE-URL>/products/your-org/scancode-workbench/4.0.2/",
205+
"uuid":"<UUID>",
206+
"name":"scancode-workbench",
207+
"version":"4.0.2",
208+
"owner":null,
209+
"configuration_status":"New",
210+
"license_expression":"",
211+
"licenses":[],
212+
"components":[],
213+
"packages":[],
214+
"keywords":[],
215+
"release_date":null,
216+
"description":"",
217+
"copyright":"",
218+
"contact":"",
219+
"homepage_url":"https://github.com/nexB/scancode-workbench",
220+
"vcs_url":"https://github.com/nexB/scancode-workbench.git",
221+
"code_view_url":"",
222+
"bug_tracking_url":"",
223+
"primary_language":"",
224+
"admin_notes":"",
225+
"notice_text":"",
226+
"created_date":"<Redacted>",
227+
"last_modified_date":"<Redacted>",
228+
"dataspace":"your-org"
229+
}
230+
231+
232+
- **Upload Product on DejaCode**
233+
234+
Finally, we can use the ``UUID`` from the above step to upload the scan results to DejaCode.
235+
236+
Replace `<DEJACODE-URL>` with your DejaCode URL, `<YOUR-DEJACODE-API-KEY>` with your DejaCode
237+
API key, and `<UUID>` with the UUID for the product we created in the above step.
238+
239+
.. code-block:: shell
240+
241+
curl -X POST "https://<DEJACODE-URL>/api/v2/products/<UUID>/import_from_scan/" \
242+
-H "Authorization: Token <YOUR-DEJACODE-API-KEY>" \
243+
-F "upload_file=@results.json" \
244+
-F "create_codebase_resources=true" \
245+
-F "stop_on_error=true"
246+
247+
Upon successful upload, we get this response:
248+
249+
.. code-block:: json
250+
251+
{"status":"Imported from Scan: 4 Packages, 4 Product Packages, 5 Codebase Resources"}
252+
253+
254+
Trigger detailed Scan in DejaCode for all packages of the product
255+
-----------------------------------------------------------------
256+
257+
To trigger a detailed scan of all packages in the uploaded product we will use
258+
DejaCode UI, go to `https://<DEJACODE-URL>/products/your-org/scancode-workbench/4.0.2/`,
259+
click on the `Scan` dropdown, and choose `Scan all Packages`.
260+
261+
.. image:: images/tutorial-cli-end-to-end-scanning-to-dejacode-scan-all-packages.png
262+
263+
In the confirmation dialog, click `Submit Scan Request`.
264+
265+
.. image:: images/tutorial-cli-end-to-end-scanning-to-dejacode-submit-scan-request.png

0 commit comments

Comments
 (0)