Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/specgen/generate/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ func MakeContainer(ctx context.Context, rt *libpod.Runtime, s *specgen.SpecGener
}
}

// if userns is auto, set up annotation, and default IDMappings if none are set
if s.UserNS.IsAuto() {
if s.Annotations == nil {
s.Annotations = make(map[string]string)
}
s.Annotations[define.UserNsAnnotation] = string(s.UserNS.NSMode)

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 thinking if we can guarantee that s.Annotations is not nil in all cases in this function. Quick search shows there are places in the code checking if Annotations is nil, so it probably can happen.

Maybe you could add

if s.Annotations == nil {
    s.Annotations = make(map[string]string)
}

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.

Ok, pushed. Now I'm also unconditionally setting up the annotation, so it also works when a client requests creation with explicit IDMappings but no annotation.


if s.IDMappings == nil {
mappings, err := util.ParseIDMapping(namespaces.UsernsMode(s.UserNS.NSMode), nil, nil, "", "")
if err != nil {
return nil, nil, nil, err
}
s.IDMappings = mappings
}
}

if err := s.Validate(); err != nil {
return nil, nil, nil, fmt.Errorf("invalid config provided: %w", err)
}
Expand Down
25 changes: 25 additions & 0 deletions test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,31 @@ for runtime in "${oci_runtimes[@]}"; do
t DELETE containers/$cid 204
done

# 27988: make sure creation with "userns": {"nsmode": "auto"} works even without
# explicit idmappings. check for the same behaviour the cmd line shows: when the
# container is created, UsernsMode is ""; after the container is started,
# UsernsMode becomes "private"

t POST libpod/containers/create \
Image=$IMAGE \
UserNS='{"NSMode":"auto"}' \
201
cid=$(jq -r '.Id' <<<"$output")

t GET libpod/containers/$cid/json \
200 \
.HostConfig.UsernsMode=''

t POST libpod/containers/$cid/start 204

t GET libpod/containers/$cid/json \
200 \
.HostConfig.UsernsMode=private

t DELETE libpod/containers/$cid 200 .[0].Id=$cid


# clean up
podman rmi -f $IMAGE

# Test health status in /containers/json (GH #27786)
Expand Down