Maintaining Open Source Singularity Since 2017
Improve Security in your CI/CD Workflows
By Adam Hughes
Use case
Build a SIF image and ensure security by documenting and analyzing packages and dependencies, for this, the following requirements are:- GitHub public repository.
- A free Singularity Container Services account.
$ tree
├── cmd
│ └── main.go <-- Command line program, imports hello package.
├── go.mod <-- Standard module definition **no deps here**
├── hello.go <-- Source code of the program
├── hello_test.go <-- Testing source code of the program
├── helloworld.def <-- SIF image definition file
├── main <-- Compiled program
└── README.md <-- This file
package main
import (
"fmt"
"hello"
)
func main() {
message := hello.SayHello("World")
fmt.Println(message)
}
package hello
func SayHello(a string) string {
return "Hello " + a + "!"
}
package hello
import (
"strings"
"testing"
)
func TestLength(t *testing.T) {
msg := SayHello("World")
length := len(msg)
if length != 12 {
t.Errorf("SayHello(\"World\") length is %d; want 12", length)
}
}
func TestContainsUTF(t *testing.T) {
msg := SayHello("嗨")
if !strings.Contains(msg, "嗨") {
t.Error("SayHello(\"嗨\") doesn't support UTF8")
}
}
Bootstrap: library
From: josue-sylabs/devtools/golang:1.19.2
Stage: build
%files
. /src
%post
cd /src
go test ./...
go build -v -o /usr/bin/main ./cmd/main.go
Bootstrap: library
From: alpine
Stage: final
%files from build
/usr/bin/main
%runscript
/usr/bin/main
If you are new, let’s explain:
- Bootstrap is telling the image builder where to get the base image, options are, library, dockerhub and others. At this time we are going to download the alpine image from the Singularity library.
- From is the name of our image. In this example, the latest alpine image is used. Whether this is not good for production, the scope of this post is a demonstration.
- %files. This section is going to copy our helloworld program which is named: main.
- %post. This is the section where you perform the majority of the operations like download files, install software, compile source code, etc.
- %runscript. This tells the singularity engine to run our program when called via the
runsubcommand.
At a very low level the SIF is made up of a global header, metadata, and the actual data. Our interest is in the metadata part, which in the SIF standard are called descriptors.
The descriptors are an important part of the image because it tells how the image was created using a definition file, it stores data partitions, labels, signing data, cryptographic data, and our data type of interest: SBOM data.
Fortunately, Grype supports SIF images out of the box, so you don’t have to worry about anything else, scan it by prepend “singularity:” to your image name, like so:
$ grype singularity:ubuntu-sbom.sif
✔ Vulnerability DB [no update available]
✔ Parsed image
✔ Cataloged packages [102 packages]
✔ Scanned image [27 vulnerabilities]
NAME INSTALLED FIXED-IN TYPE VULNERABILITY SEVERITY
coreutils 8.32-4.1ubuntu1 deb CVE-2016-2781 Low
e2fsprogs 1.46.5-2ubuntu1 1.46.5-2ubunt deb CVE-2022-1304 Medium
...
name: Build Image
on:
push:
branches: [ "master" ]
Next section is variable definition, in this part, the definition file is set, as well as the name of the SIF file to be created. As an option, we can also keep a copy of the image file on the build server.
env:
DEF_FILE: "helloworld.def"
OUTPUT_SIF: "helloworld.sif"
LIBRARY_PATH: "library://josue-sylabs/temp/helloworld:latest"
SCS_BUILDER_VERSION: “0.7.5"
SYLABS_AUTH_TOKEN: ${{ secrets.SYLABS_AUTH_TOKEN }}
GOLANG_IMAGE: "library://josue-sylabs/devtools/golang:1.19.2"
These are the steps the runner executes when a push occurs.
- Setup the standard “checkout” step
- Install the SCS builder from GitHub, which at the creation of this document is 0.7.5.
- Compile the program, test and build the actual SIF file remotely and create an image, this is important to understand, this is where you need a Singularity Container Services account already mentioned before. So, create an access token and call it
SYLABS_AUTH_TOKEN, the value is given by the Singularity Container Services when you create the account there. - Now it is time to scan the source code for known package vulnerabilities, Grype has a wide support of programming languages including Go, Rust, Javascript, Python, and others.
- Perform the actual scan of the SIF image.
- Save the generated SIF file in GitHub artifact list.
jobs:
build:
runs-on: ubuntu-latest
steps:
#1
- uses: actions/checkout@v3
#2
- name: Install scs-build
run: |
mkdir {tmp,tmp-sif-path}
pushd tmp
wget https://github.com/sylabs/scs-build-client/releases/download/v${SCS_BUILDER_VERSION}/scs-build-client_${SCS_BUILDER_VERSION}_linux_amd64.tar.gz
tar -zxf scs-build-client_${SCS_BUILDER_VERSION}_linux_amd64.tar.gz
sudo cp scs-build /usr/bin/
popd
#3
- name: Compile program and build image
run: scs-build build $DEF_FILE ${{ github.workspace }}/tmp-sif-path/$OUTPUT_SIF
#4
- name: Scan source code
uses: anchore/scan-action@v3
with:
path: "${{ github.workspace }}"
#5
- name: Scan final image
uses: anchore/scan-action@v3
with:
image: "singularity:${{ github.workspace }}/tmp-sif-path/{{ env.OUTPUT_SIF }}"
#6
- name: Save artifacts
uses: actions/upload-artifact@v2
with:
name: ${{ env.OUTPUT_SIF }}
path: ${{ github.workspace }}/tmp-sif-path
severity-cutoff to low, high or critical: - name: Scan SBOM
uses: anchore/scan-action@v3
with:
image: "singularity:${{ github.workspace }}/tmp-sif-path/{{ env.OUTPUT_SIF }}"
severity-cutoff: critical
You could inspect the output of the report, for example in SARIF, like so:
- name: Inspect the report
run: cat ${{ steps.scan.outputs.sarif }}
Summary
Scanning for vulnerabilities with the help of Singularity’s SBOM and Grype in a CI/CD workflow is a good start. Using many open source tools provides value, saves time and effort, but every day a new bug comes and it is not always easy to see how critical they are. Source code: https://github.com/josueneo/helloworld.Join Our Mailing List
Recent Posts
Related Posts
Laying the Groundwork for 2025: Sylabs’ Vision for Secure Multi-Cloud Solutions
With a legacy rooted in innovation through the work we’ve done with the Singularity platform, Sylabs has consistently worked to provide secure container technology to empower researchers, enterprises, and innovators worldwide. These efforts have paid dividends as we...
Creating New Levels of Security with Containers and Confidential Computing
Data breaches are becoming increasingly common. It's more important than ever to find robust solutions that protect sensitive information. Recent high-profile data breaches underscore the necessity for fresh approaches to data protection. Breaches like these are...
OCI-SIF Container Images: Unraveling Their Features and Benefits
Among the container industry, OCI (Open Container Initiative) and SIF (Singularity Image Format) stand out as two prominent formats, each offering unique features and benefits. Understanding their distinct characteristics is crucial for HPC developers seeking optimal...