From 4daaf358a0e62fd2a31227bc20fb661e179f6f85 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 6 Apr 2022 11:55:40 +0200 Subject: [PATCH] API: use no_hosts from containers.conf The API endpoints should properly honour the `no_hosts=true` setting in containers.conf. Fixes #13719 Signed-off-by: Paul Holzinger --- cmd/podman/common/create_opts.go | 1 + pkg/api/handlers/libpod/containers_create.go | 13 +++++- test/apiv2/20-containers.at | 43 ++++++++++++++++++++ test/apiv2/containers.no_host.conf | 2 + 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 test/apiv2/containers.no_host.conf diff --git a/cmd/podman/common/create_opts.go b/cmd/podman/common/create_opts.go index ad6b3870a806..39146f9188a0 100644 --- a/cmd/podman/common/create_opts.go +++ b/cmd/podman/common/create_opts.go @@ -181,6 +181,7 @@ func ContainerCreateToContainerCLIOpts(cc handlers.CreateContainerConfig, rtc *c Network: nsmode, PublishPorts: specPorts, NetworkOptions: netOpts, + NoHosts: rtc.Containers.NoHosts, } // network names diff --git a/pkg/api/handlers/libpod/containers_create.go b/pkg/api/handlers/libpod/containers_create.go index 61f437fafc9b..4f9dc008d203 100644 --- a/pkg/api/handlers/libpod/containers_create.go +++ b/pkg/api/handlers/libpod/containers_create.go @@ -18,7 +18,18 @@ import ( // the new container ID on success along with any warnings. func CreateContainer(w http.ResponseWriter, r *http.Request) { runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) - var sg specgen.SpecGenerator + conf, err := runtime.GetConfigNoCopy() + if err != nil { + utils.InternalServerError(w, err) + return + } + + // we have to set the default before we decode to make sure the correct default is set when the field is unset + sg := specgen.SpecGenerator{ + ContainerNetworkConfig: specgen.ContainerNetworkConfig{ + UseImageHosts: conf.Containers.NoHosts, + }, + } if err := json.NewDecoder(r.Body).Decode(&sg); err != nil { utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "Decode()")) diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index 94de2cf247a3..c837aae5919d 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -447,3 +447,46 @@ t GET images/$iid/json 200 \ t DELETE containers/$cid 204 t DELETE images/docker.io/library/newrepo:v3?force=false 200 + +# test create without default no_hosts +t POST containers/create \ + Image=$IMAGE \ + 201 \ + .Id~[0-9a-f]\\{64\\} +cid=$(jq -r '.Id' <<<"$output") + +t POST libpod/containers/$cid/init 204 + +t GET libpod/containers/$cid/json 200 + +cpid_file=$(jq -r '.ConmonPidFile' <<<"$output") +userdata_path=$(dirname $cpid_file) + +t GET libpod/containers/$cid/json 200 \ + .HostsPath=$userdata_path/hosts + +t DELETE containers/$cid 204 + +# test create with default no_hosts=true +stop_service + +CONTAINERS_CONF=$(pwd)/test/apiv2/containers.no_hosts.conf start_service + +# check docker and libpod endpoint +for endpoint in containers/create libpod/containers/create; do + t POST $endpoint \ + Image=$IMAGE \ + 201 \ + .Id~[0-9a-f]\\{64\\} + cid=$(jq -r '.Id' <<<"$output") + + t POST libpod/containers/$cid/init 204 + + t GET libpod/containers/$cid/json 200 \ + .HostsPath="" + + t DELETE containers/$cid 204 +done + +stop_service +start_service diff --git a/test/apiv2/containers.no_host.conf b/test/apiv2/containers.no_host.conf new file mode 100644 index 000000000000..b4c78bedbc0a --- /dev/null +++ b/test/apiv2/containers.no_host.conf @@ -0,0 +1,2 @@ +[containers] +no_hosts=true