Skip to content

Commit c566e17

Browse files
Merge pull request #8 from mrcaron/main
Dockerization
2 parents 0fb5033 + 4eabc1d commit c566e17

File tree

7 files changed

+206
-3
lines changed

7 files changed

+206
-3
lines changed

.github/workflows/docker-image.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Build and push Docker image
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
workflow_dispatch:
7+
8+
jobs:
9+
build-and-push:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v4
14+
15+
- name: Set up QEMU
16+
uses: docker/setup-qemu-action@v2
17+
18+
- name: Set up Docker Buildx
19+
uses: docker/setup-buildx-action@v2
20+
21+
- name: Log in to Docker Hub
22+
uses: docker/login-action@v2
23+
with:
24+
registry: docker.io
25+
username: ${{ secrets.DOCKERHUB_USERNAME }}
26+
password: ${{ secrets.DOCKERHUB_TOKEN }}
27+
28+
- name: Build and push image
29+
uses: docker/build-push-action@v4
30+
with:
31+
context: .
32+
file: ./Dockerfile
33+
push: true
34+
tags: |
35+
${{ secrets.DOCKERHUB_USERNAME }}/yasplitter-scanner:latest
36+
${{ secrets.DOCKERHUB_USERNAME }}/yasplitter-scanner:${{ github.sha }}
37+
platforms: linux/amd64

Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM alpine:latest
2+
3+
# Small scanner image that runs the repo's scanlargefolder.sh script.
4+
# It installs bash, curl and CA certs so the script can call external services.
5+
6+
# Black Duck server URL (set at runtime)
7+
ENV BD_URL=""
8+
ENV JAVA_HOME="/usr/lib/jvm/java-11-openjdk"
9+
# Default DETECT_SERIAL_MODE to 1 (true-ish). Users can override at runtime.
10+
ENV DETECT_SERIAL_MODE="true"
11+
12+
WORKDIR /app
13+
14+
# install runtime deps
15+
RUN apk add --no-cache grep pcre bash curl ca-certificates findutils openjdk11-jre && update-ca-certificates && \
16+
apk upgrade
17+
18+
# Copy only the scanner script(s) we need. Keep permissions.
19+
20+
# Copy scanner script and an entrypoint that will load secrets if mounted
21+
COPY src/*.sh /app/
22+
RUN chmod +x /app/*.sh
23+
24+
# Use the secure entrypoint which exports secrets (if provided) and then
25+
# execs the scanner script while forwarding any CLI args.
26+
ENTRYPOINT ["/bin/bash", "/app/docker-entrypoint.sh"]
27+
WORKDIR /app
28+
29+
# By using ENTRYPOINT, `docker run <image> <args...>` will pass <args...>
30+
# to the script.

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,31 @@ bash scanlargefolder.sh <PATH_TO_SOURCE_FOLDER> <PROJECT_NAME> <VERSION_NAME> si
4141
4242
```
4343

44+
## Docker Guide:
4445

46+
Build the container:
47+
48+
docker build -t yasplitter-scanner:latest .
49+
50+
Create a secret for your API token.
51+
52+
echo -n "your_token_here" | docker secret create bd_api_token -
53+
54+
Create a service that uses a mounted secret, mounting the large folder you wish to scan into the container.
55+
56+
docker service create --name scanner \
57+
--secret source=bd_api_token,target=BD_API_TOKEN \
58+
-e BD_URL='https://your-blackduck.example' \
59+
-v /tmp/large_folder_to_scan:/large_folder_to_scan \
60+
yasplitter-scanner:latest /large_folder_to_scan PROJECT VERSION SUFFIX
61+
62+
Run a container with the secret:
63+
64+
printf '%s' 'your_token_here' > /tmp/bd_token
65+
66+
# run container and mount the file as /run/secrets/BD_API_TOKEN
67+
docker run --rm \
68+
-v /tmp/large_folder_to_scan:/large_folder_to_scan \
69+
-v /tmp/bd_token:/run/secrets/BD_API_TOKEN:ro \
70+
-e BD_URL='https://your-blackduck.example' \
71+
yasplitter-scanner:latest /tmp/myproject PROJECT VERSION SUFFIX

src/docker-entrypoint.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
# Entry point that loads BD_API_TOKEN from Docker secrets if present.
3+
# If /run/secrets/BD_API_TOKEN exists, prefer it over the BD_API_TOKEN env var.
4+
# After loading the secret, exec the scanner script with all passed arguments.
5+
6+
set -euo pipefail
7+
8+
# If a secret file exists, read its contents into BD_API_TOKEN
9+
if [ -f "/run/secrets/BD_API_TOKEN" ]; then
10+
export BD_API_TOKEN=$(cat /run/secrets/BD_API_TOKEN)
11+
echo "Loaded BD_API_TOKEN from /run/secrets/BD_API_TOKEN" >&2
12+
fi
13+
14+
# Allow passing a file path to a mounted token as well (convention)
15+
if [ -n "${BD_API_TOKEN_FILE:-}" ] && [ -f "${BD_API_TOKEN_FILE}" ]; then
16+
export BD_API_TOKEN=$(cat "${BD_API_TOKEN_FILE}")
17+
echo "Loaded BD_API_TOKEN from path in BD_API_TOKEN_FILE" >&2
18+
fi
19+
20+
# Basic validation: warn if BD_URL or BD_API_TOKEN are empty
21+
if [ -z "${BD_URL:-}" ]; then
22+
echo "Warning: BD_URL is empty. Set BD_URL via -e or Docker secrets." >&2
23+
fi
24+
if [ -z "${BD_API_TOKEN:-}" ]; then
25+
echo "Warning: BD_API_TOKEN is empty. Provide it via Docker secret or -e BD_API_TOKEN." >&2
26+
fi
27+
28+
# Exec the scanner script, forwarding args and replacing the shell process.
29+
exec /bin/bash /app/scanlargefolder.sh "$@"

src/scan-binary-readiness.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
#
3+
4+
PROJECTPATH=$1
5+
PROJECT=$2
6+
VERSION=$3
7+
SUFFIX=$4
8+
9+
DETECT_URL_PATH=https://detect.blackduck.com/detect${DETECT_MAJOR_VERSION:-10}.sh
10+
11+
if [ "$PROJECTPATH" = "" ]
12+
then
13+
echo "specify folder to scan"
14+
exit 1
15+
fi
16+
17+
if [ "$PROJECT" = "" ]
18+
then
19+
PROJECT=${PROJECTPATH}
20+
fi
21+
22+
if [ "$VERSION" = "" ]
23+
then
24+
VERSION=LATEST
25+
fi
26+
27+
# SET BD_URL and BD_API_TOKEN variables to point to your instance of Black Duck
28+
#
29+
get_bearer_token() {
30+
local response
31+
response=$(curl -s -X POST -H "Authorization: token $BD_API_TOKEN" "$BD_URL/api/tokens/authenticate")
32+
echo "$response" | grep -oP '"bearerToken"\s*:\s*"\K[^"]+'
33+
}
34+
35+
get_scan_readiness() {
36+
local api_url="${BD_URL}/api/codelocations?q=name:${PROJECT}_${VERSION}_${SUFFIX}_code%20binary"
37+
echo "API URL: $api_url"
38+
local response
39+
local bearer_token=$(get_bearer_token)
40+
response=$(curl -s -H "Authorization: Bearer $bearer_token" "$api_url")
41+
echo "Response: $response"
42+
43+
# Extract count of IN_PROGRESS status items
44+
local count
45+
count=$(echo "$response" | grep -o '"status":[^]]*' | grep -c 'IN_PROGRESS')
46+
echo "Count of IN_PROGRESS scans: $count"
47+
48+
# Return 0 if no scans are in progress (ready), 1 otherwise (not ready)
49+
if [ "$count" -eq 0 ]; then
50+
echo "No scans in progress. Scan is ready."
51+
return 0
52+
else
53+
echo "Scans are still in progress."
54+
return 1
55+
fi
56+
}
57+
58+
# bash <(curl -s -L $DETECT_URL_PATH) \
59+
# --blackduck.url=$BD_URL \
60+
# --blackduck.api.token=$BD_API_TOKEN \
61+
# --blackduck.trust.cert=true \
62+
# --detect.binary.scan.file.path=${PROJECTPATH} \
63+
# --detect.tools=BINARY_SCAN \
64+
# --detect.project.name=${PROJECT} \
65+
# --detect.project.version.name=${VERSION} \
66+
# --detect.code.location.name=${PROJECT}_${VERSION}_${SUFFIX}_code \
67+
68+
if [ "$DETECT_SERIAL_MODE" = "true" ] || [ "$DETECT_SERIAL_MODE" = "TRUE" ]; then
69+
# Wait for scan readiness
70+
echo "Waiting for scan readiness..."
71+
while ! get_scan_readiness; do
72+
echo "Scan is still in progress. Waiting 30 seconds before rechecking..."
73+
sleep 30
74+
done
75+
echo "Scan completed."
76+
fi

src/scan-binary.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ get_bearer_token() {
3434

3535
get_scan_readiness() {
3636
local api_url="${BD_URL}/api/codelocations?q=name:${PROJECT}_${VERSION}_${SUFFIX}_code%20binary"
37+
38+
echo "Checking scans in progress with api: $api_url"
39+
3740
local response
3841
local bearer_token=$(get_bearer_token)
3942
response=$(curl -s -H "Authorization: Bearer $bearer_token" "$api_url")
4043

44+
echo " - API Response: $response"
45+
4146
# Extract count of IN_PROGRESS status items
4247
local count
4348
count=$(echo "$response" | grep -o '"status":[^]]*' | grep -c 'IN_PROGRESS')

src/scanlargefolder.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ fi
117117

118118

119119
# Check if we have BD_URL
120-
if [ -n "$DETECT_SERIAL_MODE" ]
121-
then
122-
echo Serial mode is enabled
120+
if [ "${DETECT_SERIAL_MODE:-}" = "1" ] || [ "${DETECT_SERIAL_MODE,,}" = "true" ]; then
121+
echo "Serial mode is enabled"
123122
fi
124123

125124
#

0 commit comments

Comments
 (0)