Skip to content

[v5.5] Fix: inheritlabels=true if query param absent #26003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
156 changes: 78 additions & 78 deletions pkg/api/handlers/compat/images_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,83 +95,83 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
}

query := struct {
AddHosts string `schema:"extrahosts"`
AdditionalCapabilities string `schema:"addcaps"`
AdditionalBuildContexts string `schema:"additionalbuildcontexts"`
AllPlatforms bool `schema:"allplatforms"`
Annotations string `schema:"annotations"`
AppArmor string `schema:"apparmor"`
BuildArgs string `schema:"buildargs"`
CacheFrom string `schema:"cachefrom"`
CacheTo string `schema:"cacheto"`
CacheTTL string `schema:"cachettl"`
CgroupParent string `schema:"cgroupparent"`
CompatVolumes bool `schema:"compatvolumes"`
Compression uint64 `schema:"compression"`
ConfigureNetwork string `schema:"networkmode"`
CPPFlags string `schema:"cppflags"`
CpuPeriod uint64 `schema:"cpuperiod"`
CpuQuota int64 `schema:"cpuquota"`
CpuSetCpus string `schema:"cpusetcpus"`
CpuSetMems string `schema:"cpusetmems"`
CpuShares uint64 `schema:"cpushares"`
DNSOptions string `schema:"dnsoptions"`
DNSSearch string `schema:"dnssearch"`
DNSServers string `schema:"dnsservers"`
Devices string `schema:"devices"`
Dockerfile string `schema:"dockerfile"`
DropCapabilities string `schema:"dropcaps"`
Envs []string `schema:"setenv"`
Excludes string `schema:"excludes"`
ForceRm bool `schema:"forcerm"`
From string `schema:"from"`
GroupAdd []string `schema:"groupadd"`
HTTPProxy bool `schema:"httpproxy"`
IDMappingOptions string `schema:"idmappingoptions"`
IdentityLabel bool `schema:"identitylabel"`
Ignore bool `schema:"ignore"`
InheritLabels bool `schema:"inheritlabels"`
Isolation string `schema:"isolation"`
Jobs int `schema:"jobs"`
LabelOpts string `schema:"labelopts"`
Labels string `schema:"labels"`
LayerLabels []string `schema:"layerLabel"`
Layers bool `schema:"layers"`
LogRusage bool `schema:"rusage"`
Manifest string `schema:"manifest"`
MemSwap int64 `schema:"memswap"`
Memory int64 `schema:"memory"`
NamespaceOptions string `schema:"nsoptions"`
NoCache bool `schema:"nocache"`
NoHosts bool `schema:"nohosts"`
OmitHistory bool `schema:"omithistory"`
OSFeatures []string `schema:"osfeature"`
OSVersion string `schema:"osversion"`
OutputFormat string `schema:"outputformat"`
Platform []string `schema:"platform"`
Pull bool `schema:"pull"`
PullPolicy string `schema:"pullpolicy"`
Quiet bool `schema:"q"`
Registry string `schema:"registry"`
Rm bool `schema:"rm"`
RusageLogFile string `schema:"rusagelogfile"`
Remote string `schema:"remote"`
Retry int `schema:"retry"`
RetryDelay string `schema:"retry-delay"`
Seccomp string `schema:"seccomp"`
Secrets string `schema:"secrets"`
SecurityOpt string `schema:"securityopt"`
ShmSize int `schema:"shmsize"`
SkipUnusedStages bool `schema:"skipunusedstages"`
Squash bool `schema:"squash"`
TLSVerify bool `schema:"tlsVerify"`
Tags []string `schema:"t"`
Target string `schema:"target"`
Timestamp int64 `schema:"timestamp"`
Ulimits string `schema:"ulimits"`
UnsetEnvs []string `schema:"unsetenv"`
UnsetLabels []string `schema:"unsetlabel"`
Volumes []string `schema:"volume"`
AddHosts string `schema:"extrahosts"`
AdditionalCapabilities string `schema:"addcaps"`
AdditionalBuildContexts string `schema:"additionalbuildcontexts"`
AllPlatforms bool `schema:"allplatforms"`
Annotations string `schema:"annotations"`
AppArmor string `schema:"apparmor"`
BuildArgs string `schema:"buildargs"`
CacheFrom string `schema:"cachefrom"`
CacheTo string `schema:"cacheto"`
CacheTTL string `schema:"cachettl"`
CgroupParent string `schema:"cgroupparent"`
CompatVolumes bool `schema:"compatvolumes"`
Compression uint64 `schema:"compression"`
ConfigureNetwork string `schema:"networkmode"`
CPPFlags string `schema:"cppflags"`
CpuPeriod uint64 `schema:"cpuperiod"`
CpuQuota int64 `schema:"cpuquota"`
CpuSetCpus string `schema:"cpusetcpus"`
CpuSetMems string `schema:"cpusetmems"`
CpuShares uint64 `schema:"cpushares"`
DNSOptions string `schema:"dnsoptions"`
DNSSearch string `schema:"dnssearch"`
DNSServers string `schema:"dnsservers"`
Devices string `schema:"devices"`
Dockerfile string `schema:"dockerfile"`
DropCapabilities string `schema:"dropcaps"`
Envs []string `schema:"setenv"`
Excludes string `schema:"excludes"`
ForceRm bool `schema:"forcerm"`
From string `schema:"from"`
GroupAdd []string `schema:"groupadd"`
HTTPProxy bool `schema:"httpproxy"`
IDMappingOptions string `schema:"idmappingoptions"`
IdentityLabel bool `schema:"identitylabel"`
Ignore bool `schema:"ignore"`
InheritLabels types.OptionalBool `schema:"inheritlabels"`
Isolation string `schema:"isolation"`
Jobs int `schema:"jobs"`
LabelOpts string `schema:"labelopts"`
Labels string `schema:"labels"`
LayerLabels []string `schema:"layerLabel"`
Layers bool `schema:"layers"`
LogRusage bool `schema:"rusage"`
Manifest string `schema:"manifest"`
MemSwap int64 `schema:"memswap"`
Memory int64 `schema:"memory"`
NamespaceOptions string `schema:"nsoptions"`
NoCache bool `schema:"nocache"`
NoHosts bool `schema:"nohosts"`
OmitHistory bool `schema:"omithistory"`
OSFeatures []string `schema:"osfeature"`
OSVersion string `schema:"osversion"`
OutputFormat string `schema:"outputformat"`
Platform []string `schema:"platform"`
Pull bool `schema:"pull"`
PullPolicy string `schema:"pullpolicy"`
Quiet bool `schema:"q"`
Registry string `schema:"registry"`
Rm bool `schema:"rm"`
RusageLogFile string `schema:"rusagelogfile"`
Remote string `schema:"remote"`
Retry int `schema:"retry"`
RetryDelay string `schema:"retry-delay"`
Seccomp string `schema:"seccomp"`
Secrets string `schema:"secrets"`
SecurityOpt string `schema:"securityopt"`
ShmSize int `schema:"shmsize"`
SkipUnusedStages bool `schema:"skipunusedstages"`
Squash bool `schema:"squash"`
TLSVerify bool `schema:"tlsVerify"`
Tags []string `schema:"t"`
Target string `schema:"target"`
Timestamp int64 `schema:"timestamp"`
Ulimits string `schema:"ulimits"`
UnsetEnvs []string `schema:"unsetenv"`
UnsetLabels []string `schema:"unsetlabel"`
Volumes []string `schema:"volume"`
}{
Dockerfile: "Dockerfile",
IdentityLabel: true,
Expand Down Expand Up @@ -745,7 +745,7 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
IDMappingOptions: &idMappingOptions,
IgnoreUnrecognizedInstructions: query.Ignore,
IgnoreFile: ignoreFile,
InheritLabels: types.NewOptionalBool(query.InheritLabels),
InheritLabels: query.InheritLabels,
Isolation: isolation,
Jobs: &jobs,
Labels: labels,
Expand Down
26 changes: 26 additions & 0 deletions pkg/api/handlers/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package handlers
import (
"encoding/json"
"reflect"
"strconv"
"strings"
"syscall"
"time"

"github.com/containers/image/v5/types"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/util"
"github.com/gorilla/schema"
Expand All @@ -28,6 +30,9 @@ func NewAPIDecoder() *schema.Decoder {

var Signal syscall.Signal
d.RegisterConverter(Signal, convertSignal)

d.RegisterConverter(types.OptionalBoolUndefined, convertOptionalBool)

return d
}

Expand All @@ -39,6 +44,16 @@ func NewCompatAPIDecoder() *schema.Decoder {
s = strings.ToLower(strings.TrimSpace(s))
return reflect.ValueOf(s != "" && s != "0" && s != "no" && s != "false" && s != "none")
})
dec.RegisterConverter(types.OptionalBoolUndefined, func(s string) reflect.Value {
if len(s) == 0 {
return reflect.ValueOf(types.OptionalBoolUndefined)
}
s = strings.ToLower(strings.TrimSpace(s))
if s != "0" && s != "no" && s != "false" && s != "none" {
return reflect.ValueOf(types.OptionalBoolTrue)
}
return reflect.ValueOf(types.OptionalBoolFalse)
})

return dec
}
Expand Down Expand Up @@ -143,3 +158,14 @@ func convertSignal(query string) reflect.Value {
}
return reflect.ValueOf(signal)
}

func convertOptionalBool(s string) reflect.Value {
if len(s) == 0 {
return reflect.ValueOf(types.OptionalBoolUndefined)
}
val, _ := strconv.ParseBool(s)
if val {
return reflect.ValueOf(types.OptionalBoolTrue)
}
return reflect.ValueOf(types.OptionalBoolFalse)
}
26 changes: 26 additions & 0 deletions test/apiv2/90-build.at
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- sh -*-
#
# Tests for build-related endpoints
#

# test if default compat build contains labels from base image
TMPD=$(mktemp -d podman-apiv2-test.build.XXXXXXXX)
function cleanBuildTest() {
podman rmi -a -f
rm -rf "${TMPD}" &> /dev/null
}
CONTAINERFILE_TAR="${TMPD}/containerfile.tar"
cat > $TMPD/containerfile << EOF
FROM $IMAGE
RUN echo hello
EOF
tar --format=posix -C $TMPD -cvf ${CONTAINERFILE_TAR} containerfile &> /dev/null

t POST "/build?dockerfile=containerfile&t=labeltest" $CONTAINERFILE_TAR 200 \
'.aux|select(has("ID")).ID~^sha256:[0-9a-f]\{64\}$'

t GET images/labeltest/json 200 \
.Config.Labels.created_by="test/system/build-testimage"
cleanBuildTest

# vim: filetype=sh