Skip to content

Commit 0401b2a

Browse files
committed
clean: Improve build process for OpenSSF malicious packages index
1 parent 438d8ef commit 0401b2a

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

.circleci/config.yml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ references:
1212
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b . v0.66.0
1313
mkdir cache
1414
./trivy --cache-dir ./cache image --download-db-only
15-
# Download OpenSSF malicious packages database
16-
mkdir -p openssf-cache
17-
echo "Downloading OpenSSF malicious packages database..."
18-
curl -sfL https://api.github.com/repos/ossf/malicious-packages/tarball/main | tar -xz --strip-components=1 -C openssf-cache
19-
# Copy OSV files to the expected cache location
20-
mkdir -p openssf-cache/osv
21-
find openssf-cache -name "*.json" -path "*/osv/*" -exec cp {} openssf-cache/osv/ \;
15+
16+
build_openssf_malicious_package_index: &build_openssf_malicious_package_index
17+
persist_to_workspace: true
18+
cmd: |
19+
mkdir openssf-malicious-packages
20+
curl -sfL https://api.github.com/repos/ossf/malicious-packages/tarball/main | tar -xz --strip-components=1 -C openssf-malicious-packages
21+
python3 scripts/build_openssf_index.py
2222
2323
build_and_publish_docker: &build_and_publish_docker
2424
persist_to_workspace: true
@@ -43,11 +43,17 @@ workflows:
4343
name: install_trivy_and_download_dbs
4444
requires:
4545
- generate_and_test
46+
- codacy/shell:
47+
<<: *build_openssf_malicious_package_index
48+
name: build_openssf_malicious_package_index
49+
requires:
50+
- generate_and_test
4651
- codacy/shell:
4752
<<: *build_and_publish_docker
4853
name: publish_docker_local
4954
requires:
5055
- install_trivy_and_download_dbs
56+
- build_openssf_malicious_package_index
5157
- codacy_plugins_test/run:
5258
name: plugins_test
5359
run_multiple_tests: true
@@ -91,11 +97,17 @@ workflows:
9197
name: install_trivy_and_download_dbs
9298
requires:
9399
- generate_and_test
100+
- codacy/shell:
101+
<<: *build_openssf_malicious_package_index
102+
name: build_openssf_malicious_package_index
103+
requires:
104+
- generate_and_test
94105
- codacy/shell:
95106
<<: *build_and_publish_docker
96107
name: publish_docker_local
97108
requires:
98109
- install_trivy_and_download_dbs
110+
- build_openssf_malicious_package_index
99111
- codacy/publish_docker:
100112
name: publish_dockerhub
101113
context: CodacyDocker

.gitignore

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ project
1111
target
1212
bin
1313
cache
14+
openssf-malicious-packages
1415
*.gen.go
1516
.codacyrc
1617
trivy
1718

1819

19-
#Ignore vscode AI rules
20+
# Ignore vscode AI rules
2021
.github/copilot-instructions.md
2122

22-
23-
#Ignore cursor AI rules
23+
# Ignore cursor AI rules
2424
.cursor/rules/codacy.mdc
2525

26-
#Ignore codacy stuff
26+
# Ignore codacy stuff
2727
.codacy/cli.sh
2828
.codacy/codacy.yaml
2929

30-
#Ignore patterns.json
30+
# Ignore patterns.json
3131
docs/patterns.json

Dockerfile

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.25-alpine as builder
1+
FROM golang:1.25-alpine AS builder
22

33
ARG TRIVY_VERSION=dev
44
ENV TRIVY_VERSION=$TRIVY_VERSION
@@ -24,21 +24,13 @@ COPY docs docs
2424
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg/mod \
2525
go run ./cmd/docgen
2626

27-
# Generate the OpenSSF index during build
28-
COPY scripts/ scripts/
29-
COPY openssf-cache/osv/ openssf-cache/osv/
30-
RUN apk add --no-cache python3 && \
31-
python3 scripts/build_openssf_index.py
32-
3327
FROM busybox
3428

3529
RUN adduser -u 2004 -D docker
3630

3731
COPY --from=builder --chown=docker:docker /src/bin /dist/bin
3832
COPY --from=builder --chown=docker:docker /src/docs /docs
39-
COPY --from=builder --chown=docker:docker /src/openssf-index.json.gz /dist/cache/openssf-index.json.gz
4033
COPY --chown=docker:docker cache/ /dist/cache/codacy-trivy
41-
42-
USER docker
34+
COPY --chown=docker:docker openssf-malicious-packages/openssf-malicious-packages-index.json.gz /dist/cache/codacy-trivy/openssf-malicious-packages-index.json.gz
4335

4436
CMD [ "/dist/bin/codacy-trivy" ]

scripts/build_openssf_index.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,18 @@
3636
import os, json, gzip
3737
from concurrent.futures import ThreadPoolExecutor, as_completed
3838

39-
BASE = os.environ.get('OPENSSF_OSV_DIR', 'openssf-cache/osv')
40-
OUT = os.environ.get('OPENSSF_INDEX_OUT', 'openssf-index.json.gz')
39+
# We are ignoring withdrawn packages.
40+
# See https://github.com/ossf/malicious-packages/tree/main/osv/withdrawn
41+
BASE = os.environ.get('OPENSSF_OSV_MALICIOUS_DIR', 'openssf-malicious-packages/osv/malicious')
42+
OUT = os.environ.get('OPENSSF_INDEX_OUT', 'openssf-malicious-packages/openssf-malicious-packages-index.json.gz')
4143

4244
def read_json_file(path):
4345
with open(path, 'r', encoding='utf-8') as fh:
4446
return json.load(fh)
4547

4648

4749
def extract_package_info(pkg):
48-
"""Extract and validate package information."""
50+
"""Extract package information."""
4951
eco = (pkg.get('ecosystem') or '').lower()
5052
name = (pkg.get('name') or '').lower()
5153
return eco, name
@@ -85,6 +87,7 @@ def process_file(path):
8587
return []
8688

8789

90+
# Get all malicious package files to work on them in parallel.
8891
files = []
8992
for root, _, fns in os.walk(BASE):
9093
for fn in fns:
@@ -94,7 +97,7 @@ def process_file(path):
9497
index = {}
9598
workers = min(32, os.cpu_count() or 8)
9699
with ThreadPoolExecutor(max_workers=workers) as ex:
97-
futs = [ex.submit(process_file, p) for p in files]
100+
futs = [ex.submit(process_file, f) for f in files]
98101
for fut in as_completed(futs):
99102
for eco, name, entry in fut.result():
100103
eco_map = index.setdefault(eco, {})

0 commit comments

Comments
 (0)