Skip to content
This repository has been archived by the owner on Oct 22, 2023. It is now read-only.

Commit

Permalink
Fix issue where mounting special Docker Desktop sockets with run as c…
Browse files Browse the repository at this point in the history
…urrent user mode enabled fails.

Fixes #1435.
  • Loading branch information
charleskorn committed Jan 26, 2023
1 parent 9a66b58 commit 722767c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import batect.os.SystemInfo
import kotlinx.coroutines.runBlocking
import java.nio.file.FileSystem
import java.nio.file.Files
import java.nio.file.Path

class RunAsCurrentUserConfigurationProvider(
private val systemInfo: SystemInfo,
Expand Down Expand Up @@ -183,7 +184,7 @@ class RunAsCurrentUserConfigurationProvider(
mounts.filterIsInstance<HostMount>().forEach { mount ->
val path = fileSystem.getPath(mount.localPath.toString())

if (!Files.exists(path)) {
if (!Files.exists(path) && !path.isSpecialDockerDesktopPath) {
Files.createDirectories(path)
}
}
Expand Down Expand Up @@ -216,6 +217,11 @@ class RunAsCurrentUserConfigurationProvider(
?.key
}

private val Path.isSpecialDockerDesktopPath: Boolean
get() {
return this.startsWith("/run/host-services") || this.startsWith("/run/guest-services")
}

private fun List<String>.relativePathTo(other: List<String>) = other.subList(this.lastIndex + 1, other.lastIndex + 1)

// This method assumes that paths can't contain forward slashes except as path separators, which appears to be true for Linux.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,33 @@ object RunAsCurrentUserConfigurationProviderSpec : Spek({
assertThat(Files.exists(fileSystem.getPath(localPath, "volume-2")), equalTo(true))
}
}

// See https://github.com/batect/batect/issues/1435#issuecomment-1400286103 for explanation.
given("special Docker Desktop sockets are being mounted") {
val guestService = "/run/guest-services/some-guest-service.sock"
val hostService = "/run/host-services/some-host-service.sock"
val notSpecial = "/run/host-services-not/some-other.sock"

val volumeMounts = setOf(
HostMount(guestService.toPath(), "/container/guest-service.sock", null),
HostMount(hostService.toPath(), "/container/host-service.sock", null),
HostMount(notSpecial.toPath(), "/container/not-special", null),
)

beforeEachTest { provider.createMissingMountDirectories(volumeMounts, container) }

it("does not create a directory for the Docker Desktop guest service") {
assertThat(Files.exists(fileSystem.getPath(guestService)), equalTo(false))
}

it("does not create a directory for the Docker Desktop host service") {
assertThat(Files.exists(fileSystem.getPath(hostService)), equalTo(false))
}

it("creates a directory for the path that is not a known Docker Desktop path") {
assertThat(Files.exists(fileSystem.getPath(notSpecial)), equalTo(true))
}
}
}
}
}
Expand Down

0 comments on commit 722767c

Please sign in to comment.