Skip to content

Commit d08500b

Browse files
chore: add new workflow to upload specified OCM version and improve error handling in token rotation (#58)
On-behalf-of: Gerald Morrison (SAP) <gerald.morrison@sap.com> Signed-off-by: Gerald Morrison (SAP) <gerald.morrison@sap.com> <!-- markdownlint-disable MD041 --> #### What this PR does / why we need it add manual workflow for BDBA scan of specified OCM version --------- Signed-off-by: Gerald Morrison (SAP) <gerald.morrison@sap.com> Co-authored-by: Gerald Morrison (SAP) <gerald.morrison@sap.com>
1 parent 5aa9cd0 commit d08500b

File tree

2 files changed

+122
-6
lines changed

2 files changed

+122
-6
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Workflow to upload released versions, RC or final, Black Duck Binary Analysis (BDBA) for scanning.
2+
# Uses CTF from GitHub release assets.
3+
# This workflow is triggered manually and allows to specify the OCM version to scan.
4+
# Can be used in case the BDBA upload did not work in the release workflow.
5+
6+
name: BDBA Scan for dedicated OCM version
7+
8+
on:
9+
workflow_dispatch:
10+
inputs:
11+
OCM_VERSION:
12+
description: 'The OCM version to scan (e.g., 0.22.0)'
13+
required: true
14+
type: string
15+
16+
permissions:
17+
actions: read
18+
contents: read
19+
20+
jobs:
21+
upload-and-scan-ctfs:
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
# Checkout code from correct repository as executed in .github repo
26+
- name: Checkout code
27+
uses: actions/checkout@v4
28+
with:
29+
repository: open-component-model/ocm
30+
ref: main
31+
32+
# Download CTF from GH release assets
33+
- name: Download CTF
34+
run: |
35+
if [ -z "${{ github.event.inputs.OCM_VERSION }}" ]; then
36+
echo "Error: OCM_VERSION parameter is required"
37+
exit 1
38+
fi
39+
40+
CTF_URL="https://github.com/open-component-model/ocm/releases/download/v${{ github.event.inputs.OCM_VERSION }}/ocm-${{ github.event.inputs.OCM_VERSION }}-ctf.tgz"
41+
echo "Downloading CTF from: $CTF_URL"
42+
43+
mkdir -p "${{ github.workspace }}/gen"
44+
curl -L -o "${{ github.workspace }}/gen/ctf-aggregated" "$CTF_URL"
45+
46+
# Since OCM cli is required to download CVs from CTF, extract binary from CTF
47+
- name: Extract OCM Binary from CTF
48+
id: extract-ocm
49+
run: |
50+
ocm_binary="$(bash ./hack/get_bare_resource_from_ctf.sh \
51+
"ocm.software/ocmcli" \
52+
"" \
53+
"ocmcli" \
54+
"amd64" \
55+
"linux" \
56+
"application/octet-stream" \
57+
${{ github.workspace }}/gen/ctf-aggregated)"
58+
59+
new_loc="${{ github.workspace }}/bin/ocm"
60+
mkdir -p "$(dirname "$new_loc")"
61+
ln -s "$ocm_binary" "$new_loc"
62+
chmod +x "$new_loc"
63+
echo "OCM binary linked to \"$new_loc\""
64+
echo "binary=\"$new_loc\"" >> "$GITHUB_OUTPUT"
65+
66+
# Download CVs from CTF as TAR, loop over all TARs and upload them to BDBA
67+
- name: Upload CVs from CTF from GH assets to Blackduck
68+
id: blackduck-upload-ctf
69+
run: |
70+
set -e # Exit immediately if any command fails with non-zero status
71+
echo "Download CVs from CTF (creates CommonTransportFormat-ctf root folder)"
72+
echo "Upload single CVs to BDBA"
73+
echo "Large files may take a while to upload. Please be patient."
74+
echo
75+
cd ${{ github.workspace }}/gen/
76+
${{ steps.extract-ocm.outputs.binary }} download cv --type tar ${{ github.workspace }}/gen/ctf-aggregated
77+
# Find all CV tar files within CommonTransportFormat-ctf
78+
find "CommonTransportFormat-${{ github.workspace }}/gen/ctf-aggregated" -type f -print0 | while IFS= read -r -d '' file; do
79+
# Extract the relative path and construct the upload name
80+
relative_path="${file#CommonTransportFormat-${{ github.workspace }}/gen/ctf-aggregated/}"
81+
upload_name="${relative_path%/*}"
82+
upload_name="${upload_name//\//-}"
83+
84+
# Extract the version from the filename
85+
version=$(basename "$file")
86+
version="${version%.tar}"
87+
88+
# Construct the API URL
89+
api_url="${{ secrets.BDBA_URL }}/api/upload/${upload_name}"
90+
91+
# Upload the file using curl
92+
echo "Uploading $upload_name to BDBA"
93+
curl_output=$(curl -sS -X PUT -H "Authorization: Bearer ${{ secrets.BDBA_API_TOKEN }}" -H "Group: ${{ secrets.BDBA_GROUP_ID }}" -H "Version: $version" -H "Delete-Binary: true" --data-binary "@$file" "$api_url")
94+
95+
# Check if upload was successful and print results
96+
if [[ $(echo "$curl_output" | jq '.meta.code') == "200" ]]; then
97+
echo "--- Upload successful ---"
98+
echo " filename: $(echo "$curl_output" | jq '.results.filename')"
99+
echo " last_updated: $(echo "$curl_output" | jq '.results.last_updated')"
100+
else
101+
echo "Upload failed with"
102+
echo "$curl_output"
103+
exit 1
104+
fi
105+
done

.github/workflows/rotate-bdba-token.yml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,29 @@ jobs:
2727
run: |
2828
# Generate new token from the Black Duck Binary Analysis API
2929
# Using the validity period of 3024000 seconds (35 days)
30-
RESPONSE=$(curl -s -X PUT \
31-
-H "Content-Type: application/json" \
30+
if ! RESPONSE=$(curl -sf -X POST \
3231
-H "Authorization: Bearer ${{ secrets.BDBA_API_TOKEN }}" \
3332
-d '{"validity": 3024000}' \
34-
"https://bdba.tools.sap/api/key/")
33+
"https://bdba.tools.sap/api/key/"); then
34+
echo "::error::Failed to connect to BDBA API"
35+
exit 1
36+
fi
3537
36-
# Extract token from response
38+
# Extract token and error message
3739
TOKEN=$(echo "$RESPONSE" | jq -r '.key.value')
40+
ERROR=$(echo "$RESPONSE" | jq -r '.meta.error')
41+
CODE=$(echo "$RESPONSE" | jq -r '.meta.code')
42+
REASON=$(echo "$RESPONSE" | jq -r '.meta.reason')
3843
3944
# Verify token was generated successfully
45+
if [ -n "$ERROR" ] && [ "$ERROR" != "null" ]; then
46+
echo "::error::BDBA API Error ($CODE): $ERROR - $REASON"
47+
exit 1
48+
fi
49+
4050
if [ -z "$TOKEN" ] || [ "$TOKEN" = "null" ]; then
41-
echo "Failed to generate new token. API response: $RESPONSE"
51+
echo "::error::Failed to extract token from API response"
52+
echo "::debug::Full API response: $RESPONSE"
4253
exit 1
4354
fi
4455
@@ -59,4 +70,4 @@ jobs:
5970
--visibility all \
6071
--body "${{ steps.generate-bdba-token.outputs.bdba_token }}"
6172
62-
echo "BDBA API token successfully rotated at $(date)"
73+
echo "BDBA API token successfully rotated at $(date). Valid for 35 days."

0 commit comments

Comments
 (0)