Skip to content

Modern Docker Management, Designed for Everyone

fix: dont create trivy cache in server/client mode (#3087)

Kyle Mendell217ddc18fd42
diff --git a/backend/internal/services/vulnerability_service.go b/backend/internal/services/vulnerability_service.go
index 37f9107ed..dabc74bbf 100644
--- a/backend/internal/services/vulnerability_service.go
+++ b/backend/internal/services/vulnerability_service.go
@@ -1403,9 +1403,13 @@ func (s *VulnerabilityService) setupTrivyBatchContainer(ctx context.Context) (st
return "", nil, fmt.Errorf("trivy scanner not available: %w", err)
}
- cacheVolume, err := s.ensureTrivyCacheVolumeInternal(ctx)
- if err != nil {
- return "", nil, fmt.Errorf("failed to ensure trivy cache volume: %w", err)
+ serverURL, _ := s.getTrivyServerConfig()
+ var cacheVolume string
+ if serverURL == "" {
+ cacheVolume, err = s.ensureTrivyCacheVolumeInternal(ctx)
+ if err != nil {
+ return "", nil, fmt.Errorf("failed to ensure trivy cache volume: %w", err)
+ }
}
containerID, err := s.createBatchTrivyContainer(ctx, trivyImage, cacheVolume)
@@ -2232,11 +2236,13 @@ func buildTrivyBatchHostConfigInternal(
privileged bool,
) *containertypes.HostConfig {
mounts := append([]mounttypes.Mount{}, runtimeOptions.Mounts...)
- mounts = append(mounts, mounttypes.Mount{
- Type: mounttypes.TypeVolume,
- Source: cacheVolume,
- Target: trivyCacheMountTarget,
- })
+ if strings.TrimSpace(cacheVolume) != "" {
+ mounts = append(mounts, mounttypes.Mount{
+ Type: mounttypes.TypeVolume,
+ Source: cacheVolume,
+ Target: trivyCacheMountTarget,
+ })
+ }
hostConfig := &containertypes.HostConfig{
NetworkMode: containertypes.NetworkMode(selectDefaultTrivyNetworkModeInternal(runtimeOptions.NetworkMode)),
@@ -2258,11 +2264,13 @@ func buildTrivyHostConfig(
privileged bool,
) *containertypes.HostConfig {
mounts := append([]mounttypes.Mount{}, runtimeOptions.Mounts...)
- mounts = append(mounts, mounttypes.Mount{
- Type: mounttypes.TypeVolume,
- Source: cacheVolume,
- Target: trivyCacheMountTarget,
- })
+ if strings.TrimSpace(cacheVolume) != "" {
+ mounts = append(mounts, mounttypes.Mount{
+ Type: mounttypes.TypeVolume,
+ Source: cacheVolume,
+ Target: trivyCacheMountTarget,
+ })
+ }
hostConfig := &containertypes.HostConfig{
NetworkMode: containertypes.NetworkMode(selectDefaultTrivyNetworkModeInternal(runtimeOptions.NetworkMode)),
@@ -3286,15 +3294,22 @@ func (s *VulnerabilityService) runTrivyScan(ctx context.Context, trivyImage stri
return nil, fmt.Errorf("failed to connect to Docker: %w", err)
}
- cacheVolume, err := s.ensureTrivyCacheVolumeInternal(ctx)
- if err != nil {
- return nil, err
+ serverURL, serverToken := s.getTrivyServerConfig()
+ var cacheVolume string
+ if serverURL == "" {
+ cacheVolume, err = s.ensureTrivyCacheVolumeInternal(ctx)
+ if err != nil {
+ return nil, err
+ }
}
// Get Trivy config files if they exist
configContent, ignoreContent := s.getTrivyConfigFiles()
- cacheDir := s.getTrivySingleScanCacheDirForSlotInternal(slotID)
+ var cacheDir string
+ if serverURL == "" {
+ cacheDir = s.getTrivySingleScanCacheDirForSlotInternal(slotID)
+ }
if cacheDir != "" {
slog.DebugContext(ctx,
"using slot-based trivy cache dir for scan",
@@ -3319,7 +3334,7 @@ func (s *VulnerabilityService) runTrivyScan(ctx context.Context, trivyImage stri
if len(registryConfigJSON) > 0 {
env = append(env, "DOCKER_CONFIG="+trivyRegistryConfigDir)
}
- if _, serverToken := s.getTrivyServerConfig(); serverToken != "" {
+ if serverToken != "" {
env = append(env, vuln.ServerTokenEnv(serverToken)...)
}
diff --git a/backend/internal/services/vulnerability_service_test.go b/backend/internal/services/vulnerability_service_test.go
index bc387c76a..396487db0 100644
--- a/backend/internal/services/vulnerability_service_test.go
+++ b/backend/internal/services/vulnerability_service_test.go
@@ -391,6 +391,43 @@ func TestBuildTrivyHostConfig_AppliesRuntimeSecurity(t *testing.T) {
require.True(t, hostConfig.Privileged)
}
+func TestBuildTrivyHostConfig_SkipsCacheMountWhenVolumeEmpty(t *testing.T) {
+ hostConfig := buildTrivyHostConfig(
+ "",
+ nil,
+ containertypes.Resources{},
+ "",
+ false,
+ trivyRuntimeOptions{NetworkMode: "bridge"},
+ nil,
+ false,
+ )
+
+ require.Empty(t, hostConfig.Mounts)
+
+ socketMount := mounttypes.Mount{
+ Type: mounttypes.TypeBind,
+ Source: "/run/user/1000/podman/podman.sock",
+ Target: "/run/user/1000/podman/podman.sock",
+ }
+ hostConfig = buildTrivyHostConfig(
+ "",
+ nil,
+ containertypes.Resources{},
+ "",
+ false,
+ trivyRuntimeOptions{
+ NetworkMode: "bridge",
+ Mounts: []mounttypes.Mount{socketMount},
+ },
+ nil,
+ false,
+ )
+
+ require.Len(t, hostConfig.Mounts, 1)
+ require.Equal(t, socketMount.Source, hostConfig.Mounts[0].Source)
+}
+
func TestBuildTrivyBatchHostConfig_AppliesRuntimeSecurity(t *testing.T) {
hostConfig := buildTrivyBatchHostConfigInternal(
"cache-volume",
@@ -406,6 +443,17 @@ func TestBuildTrivyBatchHostConfig_AppliesRuntimeSecurity(t *testing.T) {
require.Equal(t, "cache-volume", hostConfig.Mounts[0].Source)
}
+func TestBuildTrivyBatchHostConfig_SkipsCacheMountWhenVolumeEmpty(t *testing.T) {
+ hostConfig := buildTrivyBatchHostConfigInternal(
+ "",
+ trivyRuntimeOptions{NetworkMode: "arcane-external"},
+ nil,
+ false,
+ )
+
+ require.Empty(t, hostConfig.Mounts)
+}
+
func TestBuildTrivyHostConfig_IncludesInheritedSocketMount(t *testing.T) {
hostConfig := buildTrivyHostConfig(
"cache-volume",