Skip to content

Support configuring ownership (almost) everywhere - #4932

Merged
vito merged 25 commits into
dagger:mainfrom
vito:mount-uid-gid
Apr 21, 2023
Merged

vito merged 25 commits into
dagger:mainfrom
vito:mount-uid-gid

Conversation

@vito

@vito vito commented Apr 11, 2023 •

Copy link
Copy Markdown
Contributor

fixes #4739

Adds owner: String to the following APIs:

type Container {
  withMountedDirectory(..., owner: String): Container!
  withMountedFile(..., owner: String): Container!
  withDirectory(..., owner: String): Container!
  withFile(..., owner: String): Container!
  withNewFile(..., owner: String): Container!
  withMountedCache(..., owner: String): Container!
  withMountedSecret(..., owner: String): Container!
  withUnixSocket(..., owner: String): Container!
}

The String type is a bit nebulous and might warrant its own scalar (edit: nevermind - #4932 (comment)). The following formats are supported:

user:group # set uid/gid by name
1234:4321  # set uid/gid
user       # same as user:user
1234       # same as 1234:1234

This was a little tricky to implement because only some parts of Buildkit support resolving names to IDs. Luckily we can use the gateway API to just fetch /etc/passwd and /etc/group ourselves, which this PR does so that everything supports names.

Note: we only change ownership if explicitly told to in the API; it will not automatically chown to the container's "current user." See rationale in #4739 comments and in a comment for the test showing we don't inherit.

@vito
vito requested review from helderco, shykes and sipsma April 11, 2023 22:45
@vito
vito force-pushed the mount-uid-gid branch 4 times, most recently from 65a12f6 to a7bc4ed Compare April 13, 2023 23:55
@marcosnils

Copy link
Copy Markdown
Contributor

The String type is a bit nebulous and might warrant its own scalar. The following formats are supported:

+1

@vito

vito commented Apr 14, 2023

Copy link
Copy Markdown
Contributor Author

@marcosnils FWIW, I implemented that but ended up backing out. In the end it didn't seem to actually help more than it hindered. We went through a very similar thing before where we went from container { from(address: ContainerAddress!) } to a regular String: #3257

@vito
vito marked this pull request as ready for review April 17, 2023 16:48
@vito

vito commented Apr 17, 2023

Copy link
Copy Markdown
Contributor Author

This is finally ready to go.

There were two decisions I made but then backed out on:

  • As mentioned before, using a special FilesystemOwner scalar rather than String didn't seem worth it, for the same reasons we switched from ContainerAddress (slight bump in API clarity but a bit clunky to use).
  • Inheriting the container's configured user as a default owner for newly mounted files/directories led me to finding and fixing a few unrelated issues, but I ultimately decided against this feature and left a big comment explaining why in the tests. (The bugfixes remain, as separate commits in this PR.)

@aluzzardi

Copy link
Copy Markdown
Contributor

Overall looks good to me.

Nitpicky comment about the API (not leaning towards a way or another, just raising the question): Some attributes such as workdir are "contextual" (e.g. WithWorkdir) whereas owner is per operation (e.g. withFile(..., owner: String): Container!).

Would it make sense to have a WithOwner (similar to WithWorkdir, WithUser). Or should user be an attribute of WithExec instead of being a separate WithUser? Or should just leave it the way it is? (there's "natural" reasons why things are the way they are, namely inheritance from OCI)

@vito

vito commented Apr 18, 2023

Copy link
Copy Markdown
Contributor Author

@aluzzardi That's a good question. I think we need a mix of both, but not everything needs to be contextual.

I think in general it's better to have per-call settings because having to deal with "temporary context" is pretty clunky. For example, I was motivated to add #4919 because I had a use case where I wanted to completely ignore the entrypoint for a single WithExec, and dealing with the contextual entrypoint is really awkward (prev := Entrypoint(ctx) => WithEntrypoint(nil) => WithExec => WithEntrypoint(prev)).

That being said, I think some things like WithWorkdir and WithEnvVariable still make sense even ignoring the OCI motivations, because they have parallels to shell context (cd dir and export FOO=bar). Contextual config makes it easier to string together a bunch of commands that share the same context, and I think comparing to shells is probably our best bet for what should be contextual vs. only per-call.

It would also be nice to have anything that's "contextual" also be possible to set per-invocation, in the same way that you can do env FOO=bar cmd or go test -C /src ./... (admitting that second example isn't really the shell, but if the shell had it, -C wouldn't need to exist).

For this API, the closest analogue to a shell would be running cp (WithFile, WithDirectory) or mount (WithMountedX). The catch is that it's Dagger running these operations for you, which would be like running these commands as root, which preserves ownership in both cases, so I think it makes sense to not have contextual ownership by default.

As for WithOwner, I can't think of a way this would map to the shell context, so by default I think being able to set it per-invocation is better. Theoretically I could see someone wanting to set up a bunch of paths with different owners, which would be easy with per-invocation but awkward/leaky with contextual.

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it also make sense to include WithMountTmpfs here so that users can set the owner of the root of the mount (even though obviously there will be nothing to chown under it)

Comment thread core/container.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this always be 0700? Or would it make sense to look at the permissions on the directory in the source and set it to be the same here? Or maybe that happens implicitly in the Copy below?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixing this now + adding tests for keeping permissions

Comment thread core/container.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing it's not possible to use a source path mount for / so we have to use a copy? Just double checking

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this doesn't seem to work.

I tried changing the test like this, which should approximate that approach:

diff --git a/core/integration/container_test.go b/core/integration/container_test.go
index b1e98866..b2e2f5e7 100644
--- a/core/integration/container_test.go
+++ b/core/integration/container_test.go
@@ -345,8 +345,8 @@ func TestContainerWithRootFSSubdir(t *testing.T) {
                WithExec([]string{"go", "build", "-o", "/out/hello", "/src/main.go"})
 
        out, err := c.Container().
-               WithRootfs(ctr.Directory("/out")).
+               WithMountedDirectory("/", ctr.Directory("/out")).
                WithExec([]string{"/hello"}).
                Stdout(ctx)
        require.NoError(t, err)
        require.Equal(t, "Hello, world!\n", out)

But it doesn't work. It works if I change /hello to /out/hello so it seems like Buildkit just ignores the source path argument when you remount /. 🤔

Comment thread core/schema/container.graphqls Outdated

@sipsma sipsma Apr 20, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably add to the doc string here something that explains this will change the ownership of whole filesystem tree of source if it is set and the ownership of the mount point. Otherwise it would be easy to assume this changes all the files that have been cached, which IIUC is not the case

@vito

vito commented Apr 20, 2023

Copy link
Copy Markdown
Contributor Author

@sipsma AFAICT there isn't a way to set ownership for tmpfs mounts in Buildkit. 🤔 It only supports setting a size, and I can't do the llb.Copy /chown trickery that I can do with cache mounts because it only supports 'scratch':

=== RUN   TestContainerWithMountedTempOwner/names
    container_test.go:3459:
            Error Trace:    /home/vito/src/dagger/core/integration/container_test.go:3459
            Error:          Received unexpected error:
                            input:1: container.from.withExec.withExec.withExec.withUser.withWorkdir.withMountedTemp.withUser.withExec marshal: tmpfs mounts must use scratch
                            Please visit https://dagger.io/help#go for troubleshooting guidance.
            Test:           TestContainerWithMountedTempOwner/names

I figured we can just skip it for now and submit a PR upstream if/when someone requests it.

@vito vito changed the title Support configuring ownership everywhere Support configuring ownership (almost) everywhere Apr 20, 2023
vito added 4 commits April 20, 2023 17:32
TODO: the other types of mounts

Signed-off-by: Alex Suraci <[email protected]>
* WithFile
* WithDirectory
* WithNewFile

Signed-off-by: Alex Suraci <[email protected]>
vito added 20 commits April 20, 2023 17:32
Signed-off-by: Alex Suraci <[email protected]>
I'm not sure these are worth exposing; let's wait for a use case to come
up.

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
nevermind. too much trouble for too little benefit.

This reverts commit 908576434819e457e6d7ad51ab1a18078eb13732.

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
Previously WithRootFS would ignore the given directory's relative path,
which was necessary for this to work. Now we'll explicitly repoint the
directory to /. Updated naming along the way.

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
I think this was originally done so it can be inherited at runtime, but
that's no longer necessary, and it required "re-writing" container
mounts at WithExec time which seemed strange.

also remove Container.WithUser now that we don't need to do anything
special there anymore

Signed-off-by: Alex Suraci <[email protected]>
@vito

vito commented Apr 20, 2023

Copy link
Copy Markdown
Contributor Author

@sipsma Permissions issue fixed; ready for another look! 🙏

@helderco helderco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +3468 to +3469
withOwner := addContent(ctr, example.name, example.owner)
output, err := withOwner.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could now be: 😁

output, err := ctr.
    With(addContent(example.name, example.owner)).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gave this ago but it just resulted in more code since each addContent then needs to return a function. In this case every call site does something different so I don't think it benefits as much from With vs. common middleware. Oh well!

Comment thread core/integration/container_test.go Outdated
Comment on lines +3449 to +3450
// NB: inheriting the user/group from the container was implemented, but we
// decided to back out for two reasons:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like "three" reasons now. 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eye, fixing :P

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 48fb31d into dagger:main Apr 21, 2023
@vito
vito deleted the mount-uid-gid branch April 21, 2023 15:21
Comment thread core/container.go
if srcDef == nil {
// e.g. empty cache mount
srcSt = llb.Scratch().File(
llb.Mkdir("/chown", 0o700, ownership.Opt()),

@sipsma sipsma Apr 21, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vito Sorry for the late review, head was buried in debugging other stuff, but I think if you specify a mount point in an exec at a path that doesn't exist yet, buildkit defaults to 0755. So using 0700 here might technically be a deviation.

I really doubt this will ever matter, so I don't care if we leave it as is, just bringing it up in case you think it matters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops! Fix: #5006

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐞 WithMounted{File,Directory,Cache,Secret,Temp} don't allow setting arbitrary uid/gid

5 participants