Skip to content

Commit fae2c73

Browse files
Feat/trivy (#109)
* Integrated trivy in code base and in Dockerfile * changed to dockerfile in action.yml * changed entrypoint.sh for aws auth * feat: changed action.yml image to version 2.0.0 release * feat: changed to release 1.0.0 in action.yml
1 parent 2926b32 commit fae2c73

File tree

5 files changed

+73
-95
lines changed

5 files changed

+73
-95
lines changed

Dockerfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ RUN apk add --no-cache \
77
unzip \
88
docker-cli \
99
aws-cli \
10-
bash
10+
bash \
11+
docker
1112

1213
# Install Terraform
1314
RUN curl -fsSL https://releases.hashicorp.com/terraform/1.5.7/terraform_1.5.7_linux_amd64.zip -o terraform.zip && \
1415
unzip terraform.zip && \
1516
mv terraform /usr/local/bin/ && \
1617
rm terraform.zip
1718

19+
# Install Trivy (Vulnerability Scanner)
20+
RUN curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
21+
1822
# Set working directory
1923
WORKDIR /go/src/app
2024

cmd/sdkr/provisionHub.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ Set DOCKER_USERNAME and DOCKER_PASSWORD environment variables for Docker Hub aut
115115
}
116116
pterm.Success.Println("Build completed successfully.")
117117

118-
pterm.Info.Println("Starting scan...")
119-
scanErr := docker.Scout(fullImageName, configs.SarifFile)
118+
pterm.Info.Println("Starting scan with Trivy...")
119+
scanErr := docker.Trivy(fullImageName)
120120
if scanErr != nil {
121121
pterm.Error.Println("Scan failed:", scanErr)
122122
} else {
@@ -158,7 +158,7 @@ Set DOCKER_USERNAME and DOCKER_PASSWORD environment variables for Docker Hub aut
158158
# Provide "myuser/myimage:latest" as an argument
159159
smurf sdkr provision-hub myuser/myimage:latest --context . --file Dockerfile --no-cache \
160160
--build-arg key1=value1 --build-arg key2=value2 --target my-target --platform linux/amd64 \
161-
--output myscan.sarif --yes --delete
161+
--yes --delete
162162
163163
# If you omit the argument, it will read from config and rely on "image_name" from there
164164
smurf sdkr provision-hub --yes --delete
@@ -208,14 +208,6 @@ func init() {
208208
"",
209209
"Build context directory (default: current directory)",
210210
)
211-
212-
provisionHubCmd.Flags().StringVarP(
213-
&configs.SarifFile,
214-
"output", "o",
215-
"",
216-
"Output file for SARIF report",
217-
)
218-
219211
provisionHubCmd.Flags().BoolVarP(
220212
&configs.ConfirmAfterPush,
221213
"yes", "y",
@@ -230,4 +222,4 @@ func init() {
230222
)
231223

232224
sdkrCmd.AddCommand(provisionHubCmd)
233-
}
225+
}

cmd/sdkr/scan.go

+40-45
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,53 @@
11
package sdkr
22

33
import (
4-
"errors"
5-
"fmt"
6-
7-
"github.com/clouddrove/smurf/configs"
8-
"github.com/clouddrove/smurf/internal/docker"
9-
"github.com/pterm/pterm"
10-
"github.com/spf13/cobra"
4+
"errors"
5+
"fmt"
6+
"github.com/clouddrove/smurf/configs"
7+
"github.com/clouddrove/smurf/internal/docker"
8+
"github.com/pterm/pterm"
9+
"github.com/spf13/cobra"
1110
)
1211

1312
// scanCmd provides functionality to scan a Docker image for known security issues.
1413
// It supports both direct command-line arguments and configuration file values for the image name,
1514
// and optionally allows saving the scan report to a specified SARIF file.
1615
var scanCmd = &cobra.Command{
17-
Use: "scan [IMAGE_NAME[:TAG]]",
18-
Short: "Scan a Docker image for known vulnerabilities.",
19-
Args: cobra.MaximumNArgs(1),
20-
RunE: func(cmd *cobra.Command, args []string) error {
21-
var imageRef string
22-
23-
if len(args) == 1 {
24-
imageRef = args[0]
25-
} else {
26-
data, err := configs.LoadConfig(configs.FileName)
27-
if err != nil {
28-
return fmt.Errorf("failed to load config: %w", err)
29-
}
30-
if data.Sdkr.ImageName == "" {
31-
return errors.New("image name (with optional tag) must be provided either as an argument or in the config")
32-
}
33-
imageRef = data.Sdkr.ImageName
34-
}
35-
36-
pterm.Info.Printf("Scanning Docker image %q...\n", imageRef)
37-
err := docker.Scout(imageRef, configs.SarifFile)
38-
if err != nil {
39-
pterm.Error.Println("Scan failed:", err)
40-
return err
41-
}
42-
pterm.Success.Println("Scan completed successfully.")
43-
return nil
44-
},
45-
Example: `
46-
smurf sdkr scan my-image:latest
47-
smurf sdkr scan
48-
# In the second example, it will read IMAGE_NAME from the config file
49-
50-
smurf sdkr scan my-image:latest --output scan.json
51-
# Saves the scan report to 'scan.json' in SARIF format
16+
Use: "scan [IMAGE_NAME[:TAG]]",
17+
Short: "Scan a Docker image for known vulnerabilities.",
18+
Args: cobra.MaximumNArgs(1),
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
var imageRef string
21+
if len(args) == 1 {
22+
imageRef = args[0]
23+
} else {
24+
data, err := configs.LoadConfig(configs.FileName)
25+
if err != nil {
26+
return fmt.Errorf("failed to load config: %w", err)
27+
}
28+
if data.Sdkr.ImageName == "" {
29+
return errors.New("image name (with optional tag) must be provided either as an argument or in the config")
30+
}
31+
imageRef = data.Sdkr.ImageName
32+
}
33+
34+
pterm.Info.Printf("Scanning Docker image %q...\n", imageRef)
35+
err := docker.Trivy(imageRef)
36+
if err != nil {
37+
pterm.Error.Println("Scan failed:", err)
38+
return err
39+
}
40+
41+
pterm.Success.Println("Scan completed successfully.")
42+
return nil
43+
},
44+
Example: `
45+
smurf sdkr scan my-image:latest
46+
smurf sdkr scan
47+
# In the second example, it will read IMAGE_NAME from the config file
5248
`,
5349
}
5450

5551
func init() {
56-
scanCmd.Flags().StringVarP(&configs.SarifFile, "output", "o", "", "Output file for SARIF report")
57-
sdkrCmd.AddCommand(scanCmd)
58-
}
52+
sdkrCmd.AddCommand(scanCmd)
53+
}

entrypoint.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ if [[ -n "$DOCKER_USERNAME" && -n "$DOCKER_PASSWORD" ]]; then
5050
echo "✅ Successfully logged into Docker Hub."
5151
fi
5252

53-
# Perform AWS and EKS login
54-
aws_eks_login
53+
# Perform AWS and EKS login only if AWS_AUTH=true
54+
if [[ "$AWS_AUTH" == "true" ]]; then
55+
echo "🔹 AWS authentication is enabled. Performing AWS login..."
56+
aws_eks_login
57+
else
58+
echo "⚠️ AWS authentication is disabled. Skipping AWS login."
59+
fi
5560

5661
# Initialize command with base command
5762
SMURF_CMD="/usr/local/bin/smurf"

internal/docker/scan.go

+17-35
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,44 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"os"
87
"os/exec"
9-
108
"github.com/fatih/color"
119
"github.com/pterm/pterm"
1210
)
1311

14-
// Scout runs 'docker scout cves' to scan a Docker image for vulnerabilities
15-
// and optionally saves the results to a SARIF file.
16-
// It displays the output of the scan and prints a success message upon completion.
17-
func Scout(dockerTag, sarifFile string) error {
12+
// Trivy runs 'trivy image' to scan a Docker image for vulnerabilities
13+
// and displays the results. It's a simplified version that accepts just the image name and tag.
14+
func Trivy(dockerImage string) error {
1815
ctx := context.Background()
19-
20-
args := []string{"scout", "cves", dockerTag}
21-
22-
if sarifFile != "" {
23-
args = append(args, "--output", sarifFile)
24-
}
25-
26-
cmd := exec.CommandContext(ctx, "docker", args...)
27-
16+
args := []string{"image", dockerImage, "--format", "table"}
17+
18+
cmd := exec.CommandContext(ctx, "trivy", args...)
2819
var stdoutBuf, stderrBuf bytes.Buffer
2920
cmd.Stdout = &stdoutBuf
3021
cmd.Stderr = &stderrBuf
31-
32-
spinner, _ := pterm.DefaultSpinner.Start("Running 'docker scout cves'")
22+
23+
spinner, _ := pterm.DefaultSpinner.Start("Running 'trivy image' scan")
3324
defer spinner.Stop()
34-
25+
3526
err := cmd.Run()
36-
3727
spinner.Stop()
38-
28+
3929
outStr := stdoutBuf.String()
4030
errStr := stderrBuf.String()
41-
31+
4232
if err != nil {
43-
pterm.Error.Println("Error running 'docker scout cves':", err)
33+
pterm.Error.Println("Error running 'trivy image':", err)
4434
if errStr != "" {
4535
pterm.Error.Println(errStr)
4636
}
47-
return fmt.Errorf("failed to run 'docker scout cves': %w", err)
37+
return fmt.Errorf("failed to run 'trivy image': %w", err)
4838
}
49-
39+
5040
if outStr != "" {
51-
pterm.Info.Println("Docker Scout CVEs output:")
41+
pterm.Info.Println("Trivy scan results:")
5242
fmt.Println(color.YellowString(outStr))
5343
}
54-
55-
if sarifFile != "" {
56-
if _, err := os.Stat(sarifFile); err == nil {
57-
pterm.Success.Println("SARIF report saved to:", sarifFile)
58-
} else {
59-
pterm.Warning.Println("Expected SARIF report not found at:", sarifFile)
60-
}
61-
}
62-
44+
6345
pterm.Success.Println("Scan completed successfully.")
6446
return nil
65-
}
47+
}

0 commit comments

Comments
 (0)