Skip to content

Commit 3671048

Browse files
authored
Merge pull request #54 from jdolitsky/docker-socket
scanner-audit: add ability to mount docker socket when needed
2 parents 0200772 + 5b756d5 commit 3671048

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

scanner-audit/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ export API_SECRET="your-secret"
100100
go run . --env cloud-scanner
101101
```
102102

103+
#### Special Environment Variable: REQUIRES_DOCKER
104+
105+
The `REQUIRES_DOCKER` environment variable is a special case. When this variable is defined in your audit-env.yaml file (with any value), scanner-audit will automatically mount the Docker socket into the container:
106+
107+
```yaml
108+
environment:
109+
environment:
110+
REQUIRES_DOCKER: "true" # Any value will trigger Docker socket mounting
111+
# ... other variables ...
112+
```
113+
114+
This enables scanners that need to interact with Docker (e.g., to pull images or inspect containers) to function properly. The Docker socket will be mounted as `/var/run/docker.sock:/var/run/docker.sock`.
115+
116+
**Note:** Unlike other environment variables, `REQUIRES_DOCKER`:
117+
- Is not validated for empty or placeholder values
118+
- Is not passed to the container as an environment variable
119+
- Only serves to trigger the Docker socket mount
120+
103121
### Custom Scanner Installation
104122

105123
You can use Melange pipelines in your audit-env.yaml to install custom tools:

scanner-audit/main.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ var testCaseDocLinks = generateTestCaseDocLinks()
129129
const (
130130
dockerStdoutStream = 1
131131
dockerStderrStream = 2
132+
requiresDockerVar = "REQUIRES_DOCKER"
132133
)
133134

134135
func main() {
@@ -251,7 +252,7 @@ func runAudit(envName string, forceBuild, noRemove, noCache bool, filterDistro,
251252
}
252253

253254
// Start a single long-running container
254-
containerID, err := startLongRunningContainer(dockerClient, wrapperImage, wrapperContent, noCache)
255+
containerID, err := startLongRunningContainer(dockerClient, wrapperImage, wrapperContent, noCache, requiredEnvVars)
255256
if err != nil {
256257
return fmt.Errorf("failed to start container: %w", err)
257258
}
@@ -389,6 +390,11 @@ func validateEnvironmentVars(requiredEnvVars []string) error {
389390
var invalid []string
390391

391392
for _, envVar := range requiredEnvVars {
393+
// Skip REQUIRES_DOCKER - it's optional and not treated as a masked variable
394+
if envVar == requiresDockerVar {
395+
continue
396+
}
397+
392398
value := os.Getenv(envVar)
393399
if value == "" {
394400
missing = append(missing, envVar)
@@ -583,7 +589,7 @@ func getCacheDir() string {
583589
return filepath.Join(getBaseCacheDir(), "cache-mount")
584590
}
585591

586-
func startLongRunningContainer(dockerClient *client.Client, wrapperImage string, wrapperContent []byte, noCache bool) (string, error) {
592+
func startLongRunningContainer(dockerClient *client.Client, wrapperImage string, wrapperContent []byte, noCache bool, requiredEnvVars []string) (string, error) {
587593
ctx := context.Background()
588594

589595
// Create container configuration - keep it alive with a sleep command
@@ -639,6 +645,15 @@ func startLongRunningContainer(dockerClient *client.Client, wrapperImage string,
639645
fmt.Printf("Mounting cache directory: %s -> /root/.cache\n", cacheDir)
640646
}
641647

648+
// Check if REQUIRES_DOCKER is in requiredEnvVars and has a value
649+
hasRequiresDocker := slices.Contains(requiredEnvVars, requiresDockerVar)
650+
651+
// Add Docker socket mount if REQUIRES_DOCKER is set
652+
if hasRequiresDocker {
653+
binds = append(binds, "/var/run/docker.sock:/var/run/docker.sock")
654+
fmt.Printf("Mounting Docker socket: /var/run/docker.sock -> /var/run/docker.sock\n")
655+
}
656+
642657
hostConfig := &container.HostConfig{
643658
Binds: binds,
644659
}
@@ -648,6 +663,9 @@ func startLongRunningContainer(dockerClient *client.Client, wrapperImage string,
648663
if !noCache {
649664
dockerCmd += fmt.Sprintf(" -v %s:/root/.cache", getCacheDir())
650665
}
666+
if hasRequiresDocker {
667+
dockerCmd += " -v /var/run/docker.sock:/var/run/docker.sock"
668+
}
651669
dockerCmd += fmt.Sprintf(" %s -c 'trap : TERM INT; sleep 9999999999d & wait'", wrapperImage)
652670
fmt.Printf("Running \"%s\"\n", dockerCmd)
653671

@@ -750,6 +768,11 @@ func prepareEnvVarsAndCommand(requiredEnvVars []string, containerID, testImage s
750768
var maskedEnvVars []string
751769

752770
for _, envVar := range requiredEnvVars {
771+
// Skip REQUIRES_DOCKER - it's not passed to the container
772+
if envVar == requiresDockerVar {
773+
continue
774+
}
775+
753776
value := os.Getenv(envVar)
754777
envVars = append(envVars, fmt.Sprintf("%s=%s", envVar, value))
755778
maskedEnvVars = append(maskedEnvVars, fmt.Sprintf("%s=\"$%s\"", envVar, envVar)) // Show as env var reference

0 commit comments

Comments
 (0)