Skip to content

Commit

Permalink
Merge pull request #22112 from Luap99/remote-buildah-isolation
Browse files Browse the repository at this point in the history
fix remote build isolation when server runs as root
  • Loading branch information
openshift-merge-bot[bot] authored Mar 21, 2024
2 parents fcdff47 + 493179b commit aaa6dc3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
11 changes: 8 additions & 3 deletions cmd/podman/common/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,14 @@ func buildFlagsWrapperToOptions(c *cobra.Command, contextDir string, flags *Buil
compression = buildahDefine.Uncompressed
}

isolation, err := parse.IsolationOption(flags.Isolation)
if err != nil {
return nil, err
isolation := buildahDefine.IsolationDefault
// Only parse the isolation when it is actually needed as we do not want to send a wrong default
// to the server in the remote case (root vs rootless).
if flags.Isolation != "" {
isolation, err = parse.IsolationOption(flags.Isolation)
if err != nil {
return nil, err
}
}

usernsOption, idmappingOptions, err := parse.IDMappingOptions(c, isolation)
Expand Down
15 changes: 12 additions & 3 deletions pkg/api/handlers/compat/images_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,19 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
return
}

// make sure to force rootless as rootless otherwise buildah runs code which is intended to be run only as root.
if isolation == buildah.IsolationOCI && rootless.IsRootless() {
isolation = buildah.IsolationOCIRootless
// Make sure to force rootless as rootless otherwise buildah runs code which is intended to be run only as root.
// Same the other way around: https://github.com/containers/podman/issues/22109
switch isolation {
case buildah.IsolationOCI:
if rootless.IsRootless() {
isolation = buildah.IsolationOCIRootless
}
case buildah.IsolationOCIRootless:
if !rootless.IsRootless() {
isolation = buildah.IsolationOCI
}
}

registry = ""
format = query.OutputFormat
} else {
Expand Down
10 changes: 10 additions & 0 deletions pkg/machine/e2e/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ var _ = Describe("run basic podman commands", func() {
Expect(runAlp).To(Exit(0))
Expect(runAlp.outputToString()).To(ContainSubstring("Alpine Linux"))

contextDir := GinkgoT().TempDir()
cfile := filepath.Join(contextDir, "Containerfile")
err = os.WriteFile(cfile, []byte("FROM quay.io/libpod/alpine_nginx\nRUN ip addr\n"), 0o644)
Expect(err).ToNot(HaveOccurred())

build, err := mb.setCmd(bm.withPodmanCommand([]string{"build", contextDir})).run()
Expect(err).ToNot(HaveOccurred())
Expect(build).To(Exit(0))
Expect(build.outputToString()).To(ContainSubstring("COMMIT"))

rmCon, err := mb.setCmd(bm.withPodmanCommand([]string{"rm", "-a"})).run()
Expect(err).ToNot(HaveOccurred())
Expect(rmCon).To(Exit(0))
Expand Down

0 comments on commit aaa6dc3

Please sign in to comment.