Skip to content

Commit 1fce4e5

Browse files
authored
Merge pull request #19 from scality/gh_refacto
♻️ refactor all ghaction
2 parents 2ce3741 + 0d86bed commit 1fce4e5

39 files changed

+3011
-991
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
88
bash-completion \
99
curl \
1010
git \
11+
libmagic1 \
1112
libsqlite3-dev \
1213
python3 \
1314
python3-pip \

.devcontainer/devcontainer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
//Git history
3131
"donjayamanne.githistory",
3232
//Git blame
33-
"solomonkinard.git-blame"
33+
"solomonkinard.git-blame",
34+
//Mermaid support
35+
"bierner.markdown-mermaid"
3436
],
3537
"settings": {
3638
"terminal.integrated.profiles.linux": {

.devcontainer/requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ requests==2.32.3
22
GitPython==3.1.44
33
pyunpack==0.3
44
patool==4.0.0
5-
pre-commit==4.1.0
5+
pre-commit==4.1.0
6+
click==8.1.8
7+
python-magic==0.4.27
8+
pydantic==2.11.2

.github/scripts/update_scanners.py

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,80 @@
11
#!/usr/bin/env python3
2+
""""
3+
Update scanner versions in the install.py file.
4+
This script fetches the latest release versions of the specified scanners
5+
from their GitHub repositories and updates the version strings in the
6+
install.py file.
7+
"""
28

39
import re
410
import requests
511

612
# Define the scanners and their GitHub repositories
7-
scanners = {
13+
SCANNERS_REPOSITORIES = {
814
"syft": "anchore/syft",
915
"grype": "anchore/grype",
10-
"trivy": "aquasecurity/trivy"
1116
}
1217

1318
def get_latest_release(repo):
19+
"""Fetch the latest release version from a GitHub repository."""
1420
url = f"https://api.github.com/repos/{repo}/releases/latest"
15-
response = requests.get(url)
21+
response = requests.get(url, timeout=10)
1622
response.raise_for_status()
1723
return response.json()["tag_name"].lstrip("v")
1824

1925
def update_versions(file_path):
20-
with open(file_path, "r") as file:
26+
"""Update the scanner versions in the specified file."""
27+
content = None
28+
with open(file_path, "r", encoding="utf8") as file:
2129
content = file.read()
2230

23-
for scanner, repo in scanners.items():
31+
# Find the scanners dictionary block
32+
scanners_pattern = r"SCANNERS_VERSION = \{(.*?)\}"
33+
scanners_match = re.search(scanners_pattern, content, re.DOTALL)
34+
35+
if not scanners_match:
36+
raise ValueError("Could not find scanners dictionary in the file")
37+
38+
scanners_block = scanners_match.group(0)
39+
updated_block = scanners_block
40+
updates_made = False
41+
42+
for scanner, repo in SCANNERS_REPOSITORIES.items():
43+
# Extract current version from the file
44+
current_version_pattern = rf'"{scanner}": "([0-9]+\.[0-9]+\.[0-9]+)"'
45+
current_version_match = re.search(current_version_pattern, scanners_block)
46+
current_version = current_version_match.group(1) if current_version_match else "unknown"
47+
48+
# Get latest version from GitHub
2449
latest_version = get_latest_release(repo)
25-
content = re.sub(
26-
f'("{scanner}": ")([^"]+)',
27-
lambda match: f'{match.group(1)}{latest_version}',
28-
content
29-
)
3050

31-
with open(file_path, "w") as file:
51+
# Compare versions
52+
if current_version == latest_version:
53+
print(f"✓ {scanner} is already at latest version {latest_version}")
54+
continue
55+
56+
# Version is different, update needed
57+
print(f"⬆ Updating {scanner} from {current_version} to {latest_version}")
58+
updates_made = True
59+
60+
# Build pattern and replacement
61+
scanner_pattern = rf'("{scanner}": ")([0-9]+\.[0-9]+\.[0-9]+)'
62+
63+
# Use lambda for replacement
64+
updated_block = re.sub(
65+
scanner_pattern,
66+
lambda match, version=latest_version: match.group(1) + version, updated_block)
67+
68+
# If no updates were made, print and return
69+
if not updates_made:
70+
print("✓ All scanners are already at their latest versions. No updates needed.")
71+
return
72+
73+
# Write to file if changes were made
74+
content = content.replace(scanners_block, updated_block)
75+
with open(file_path, "w", encoding="utf8") as file:
3276
file.write(content)
77+
print("✓ Updates applied to", file_path)
3378

3479
if __name__ == "__main__":
35-
update_versions("src/lib/install.py")
80+
update_versions("src/modules/install.py")

.github/workflows/nightly.yaml

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,6 @@ permissions:
1111
contents: read
1212

1313
jobs:
14-
update-scanners:
15-
runs-on: ubuntu-24.04
16-
steps:
17-
18-
- name: Create github token
19-
uses: actions/create-github-app-token@v2
20-
id: app-token
21-
with:
22-
app-id: ${{ vars.ACTIONS_APP_ID }}
23-
private-key: ${{ secrets.ACTIONS_APP_PRIVATE_KEY }}
24-
owner: ${{ github.repository_owner }}
25-
26-
- name: Checkout repository
27-
uses: actions/checkout@v4
28-
with:
29-
token: ${{ steps.app-token.outputs.token }}
30-
31-
- name: Set up Python
32-
uses: actions/setup-python@v5
33-
with:
34-
python-version: 3.12
35-
36-
- name: Install dependencies
37-
run: pip install requests
38-
39-
- name: Update scanner versions
40-
run: python .github/scripts/update_scanners.py
41-
42-
- name: Create pull request
43-
uses: actions/github-script@v7
44-
with:
45-
script: |
46-
const pr = await github.rest.pulls.create({
47-
owner: context.repo.owner,
48-
repo: context.repo.repo,
49-
head: "feature/deps-update",
50-
base: "main",
51-
title: ":arrow_up: Update scanner versions"
52-
})
53-
5414
vuln-scan:
5515
permissions:
5616
contents: read # for actions/checkout to fetch code
@@ -65,23 +25,17 @@ jobs:
6525
fetch-tags: true
6626

6727
- name: Create SBOM
68-
uses: anchore/sbom-action@v0
69-
with:
70-
path: ./
71-
format: cyclonedx-json
72-
output-file: "${{ github.event.repository.name }}-sbom.cdx.json"
73-
74-
- name: Scan SBOM
75-
uses: anchore/scan-action@v6
76-
id: scan
28+
uses: ./
7729
with:
78-
sbom: "${{ github.event.repository.name }}-sbom.cdx.json"
79-
output-format: sarif
80-
fail-build: false
81-
add-cpes-if-none: true
82-
by-cve: true
30+
target: ./
31+
target_type: file
32+
output_format: cyclonedx-json
33+
output_file: "/tmp/sbom/sbom.cdx.json"
34+
vuln: true
35+
vuln_output_format: sarif
36+
vuln_output_file: "/tmp/sbom/sbom.sarif"
8337

8438
- name: Upload SARIF report
8539
uses: github/codeql-action/upload-sarif@v3
8640
with:
87-
sarif_file: ${{ steps.scan.outputs.sarif }}
41+
sarif_file: "/tmp/sbom/sbom.sarif"

.github/workflows/tests.yaml

Lines changed: 105 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,143 @@
1-
name: "action-test"
1+
name: "SBOM Action Tests"
22
on:
33
push:
44
pull_request:
55

66
jobs:
7-
test-as-action:
7+
test-repository:
88
runs-on: ubuntu-24.04
99
steps:
10-
- name: Checkout
10+
- name: Checkout repository
1111
uses: actions/checkout@v4
12+
13+
- name: Test file target type (repository)
14+
id: repo-test
15+
uses: ./
1216
with:
13-
path: ./
14-
fetch-depth: 0
15-
fetch-tags: true
17+
target: ${{ github.workspace }}
18+
target_type: file
19+
output_dir: "/tmp/sbom"
20+
name: "repo"
21+
output_file: "repository_sbom.json"
22+
vuln: true
23+
vuln_output_format: "sarif"
24+
vuln_output_file: "repository_vuln.sarif"
1625

17-
- name: Download artifact
26+
- name: Validate Repository SBOM
1827
shell: bash
19-
run: curl -o /tmp/Core-15.0.iso https://distro.ibiblio.org/tinycorelinux/15.x/x86/release/Core-15.0.iso
28+
run: |
29+
echo "## File Target (Repository)" >> $GITHUB_STEP_SUMMARY
30+
if jq -e '.components[] | select(.name == "lodash" or .name == "yaml")' /tmp/sbom/repository_sbom.json > /dev/null 2>&1; then
31+
echo "✅ Repository SBOM contains expected packages" >> $GITHUB_STEP_SUMMARY
32+
echo "RESULT=PASS" >> $GITHUB_ENV
33+
else
34+
echo "❌ Repository SBOM missing expected packages" >> $GITHUB_STEP_SUMMARY
35+
echo "RESULT=FAIL" >> $GITHUB_ENV
36+
exit 1
37+
fi
2038
21-
- name: Scan repo
22-
uses: ./
23-
with:
24-
target: ./
25-
output-dir: "/tmp/test/sbom"
26-
vuln-report: True
39+
test-directory:
40+
runs-on: ubuntu-24.04
41+
steps:
42+
- name: Checkout repository
43+
uses: actions/checkout@v4
2744

28-
- name: Scan directory
45+
- name: Test directory target type
46+
id: dir-test
2947
uses: ./
3048
with:
3149
target: /usr/local/bin
32-
output-dir: "/tmp/test/sbom"
33-
syft-version: "1.10.0"
50+
target_type: file
3451
name: "usrlocalbin"
52+
output_format: "spdx-json"
53+
syft_version: "1.21.0"
54+
55+
- name: Validate Directory SBOM
56+
shell: bash
57+
run: |
58+
echo "## Directory Target" >> $GITHUB_STEP_SUMMARY
59+
if jq -e '(.spdxVersion != null) and (.name | contains("usrlocalbin"))' /tmp/sbom/usrlocalbin_sbom.json > /dev/null 2>&1; then
60+
echo "✅ Directory SBOM contains correct metadata" >> $GITHUB_STEP_SUMMARY
61+
echo "RESULT=PASS" >> $GITHUB_ENV
62+
else
63+
echo "❌ Directory SBOM has incorrect metadata" >> $GITHUB_STEP_SUMMARY
64+
echo "RESULT=FAIL" >> $GITHUB_ENV
65+
exit 1
66+
fi
67+
68+
- name: List files
69+
shell: bash
70+
run: |
71+
find /tmp/sbom -type f -name "*.json" | sort
72+
73+
test-iso:
74+
runs-on: ubuntu-24.04
75+
steps:
76+
- name: Checkout repository
77+
uses: actions/checkout@v4
78+
79+
- name: Download test iso
80+
shell: bash
81+
run: |
82+
# Download ISO for testing
83+
curl -o /tmp/Core-15.0.iso https://distro.ibiblio.org/tinycorelinux/15.x/x86/release/Core-15.0.iso
3584
36-
- name: Scan iso
85+
- name: Test ISO target type
86+
id: iso-test
3787
uses: ./
3888
with:
3989
target: /tmp/Core-15.0.iso
40-
output-dir: "/tmp/test/sbom"
90+
target_type: iso
4191
version: "15.0"
92+
output_dir: "/tmp/sbom"
4293
name: "tinycorelinux"
43-
vuln-report: False
4494

45-
- name: Ensure generated sbom file for repo contains the expected content
95+
- name: Validate ISO SBOM
4696
shell: bash
4797
run: |
48-
if jq -e '.components[] | select(.name == "lodash")' /tmp/test/sbom/repo_sbom_*.json > /dev/null; then
49-
echo "lodash is present in the JSON file."
50-
exit 0
98+
echo "## ISO Target" >> $GITHUB_STEP_SUMMARY
99+
if jq -e '.metadata.component.name == "tinycorelinux"' /tmp/sbom/tinycorelinux_15.0_sbom.json > /dev/null 2>&1; then
100+
echo "✅ ISO SBOM contains correct metadata" >> $GITHUB_STEP_SUMMARY
101+
echo "RESULT=PASS" >> $GITHUB_ENV
51102
else
52-
echo "lodash is NOT present in the JSON file."
103+
echo "❌ ISO SBOM missing expected components" >> $GITHUB_STEP_SUMMARY
104+
echo "RESULT=FAIL" >> $GITHUB_ENV
53105
exit 1
54106
fi
55107
56-
- name: Ensure generated sbom file for iso contains the expected content
108+
test-image:
109+
runs-on: ubuntu-24.04
110+
steps:
111+
- name: Checkout repository
112+
uses: actions/checkout@v4
113+
114+
- name: Download test image
57115
shell: bash
58116
run: |
59-
if jq -e '.components[] | select(.version == "6.6.8-tinycore")' /tmp/test/sbom/iso_tinycorelinux_15.0.json > /dev/null; then
60-
echo "tinycore is present in the JSON file."
61-
exit 0
62-
else
63-
echo "tinycore is NOT present in the JSON file."
64-
exit 1
65-
fi
117+
docker pull alpine:latest
118+
docker save alpine:latest > /tmp/alpine.tar
66119
67-
- name: Ensure generated sbom file for dir contains the expected content
68-
shell: bash
120+
- name: Test image target type (tarball)
121+
id: image-test
122+
uses: ./
123+
with:
124+
target: /tmp/alpine.tar
125+
target_type: image
126+
output_dir: "/tmp/sbom"
127+
128+
- name: Validate container image SBOM
69129
run: |
70-
if jq -e '.components[] | select(.purl == "pkg:golang/github.com/anchore/syft@v1.10.0")' /tmp/test/sbom/dir_bin_undefined.json > /dev/null; then
71-
echo "syft is present in the JSON file."
72-
exit 0
130+
if jq -e '.metadata.component.type == "container"' /tmp/sbom/alpine_latest_sbom.json > /dev/null 2>&1; then
131+
echo "✅ Container image SBOM has correct type"
73132
else
74-
echo "syft is NOT present in the JSON file."
133+
echo "❌ Container image SBOM missing expected type"
75134
exit 1
76135
fi
77136
78-
- name: Print the content of generated sbom file
79-
shell: bash
80-
run: |
81-
for sbom in /tmp/test/sbom/*.json; do
82-
echo "Content of $sbom"
83-
cat $sbom
84-
done
85-
for sbom in /tmp/test/sbom/reports/*.html; do
86-
echo "Content of vulnerability result for SBOM: $sbom"
87-
cat $sbom
88-
done
137+
test-summary:
138+
needs: [test-repository, test-directory, test-iso, test-image]
139+
if: always()
140+
runs-on: ubuntu-24.04
141+
steps:
142+
- name: Tests summary
143+
run: echo "All tests completed"

0 commit comments

Comments
 (0)