From dcaf06a950326ac0b7d688dfde89e4b4b67a72b8 Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Thu, 14 May 2020 21:13:50 +0200 Subject: [PATCH] cmd/create: Don't use gh.c/coreos/go-systemd Fedora's golang-github-coreos-systemd package is still at version 19, while the support for Go modules is only available from version 22. Since all that's necessary is to retrieve the 'Listen' property from the sssd-kcm.socket unit, it's quite easy to do so directly using Go's D-Bus bindings. The systemdNeedsEscape and systemdPathBusEscape functions are copied from github.com/coreos/go-systemd's needsEscape and PathBusEscape functions respectively. The github.com/coreos/go-systemd code is licensed under the ASL 2.0, the same as Toolbox. It's copyright notice lists 'CoreOS, Inc.', but since CoreOS has been acquired by Red Hat [1], it should be covered by Toolbox's existing copyright notices that list 'Red Hat, Inc'. [1] https://www.redhat.com/en/about/press-releases/red-hat-acquire-coreos-expanding-its-kubernetes-and-containers-leadership https://github.com/containers/toolbox/pull/444 --- src/cmd/create.go | 58 +++++++++++++++++++++++++++++++++++++++++------ src/go.mod | 1 - src/go.sum | 2 -- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/cmd/create.go b/src/cmd/create.go index fcd6a1300..80e4f8c36 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -28,11 +28,17 @@ import ( "github.com/containers/toolbox/pkg/podman" "github.com/containers/toolbox/pkg/shell" "github.com/containers/toolbox/pkg/utils" - systemd "github.com/coreos/go-systemd/v22/dbus" + "github.com/godbus/dbus/v5" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) +const ( + alpha = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ` + num = `0123456789` + alphanum = alpha + num +) + var ( createFlags struct { container string @@ -496,24 +502,34 @@ func getFullyQualifiedImageName(image string) (string, error) { func getKCMSocket() (string, error) { logrus.Debug("Resolving path to the KCM socket") - connection, err := systemd.NewSystemConnection() + connection, err := dbus.SystemBus() if err != nil { return "", errors.New("failed to connect to the D-Bus system instance") } - defer connection.Close() + kcmUnitNameEscaped := systemdPathBusEscape("sssd-kcm.socket") + kcmUnitPath := dbus.ObjectPath("/org/freedesktop/systemd1/unit/" + kcmUnitNameEscaped) + kcmUnit := connection.Object("org.freedesktop.systemd1", kcmUnitPath) + call := kcmUnit.Call("org.freedesktop.DBus.Properties.GetAll", 0, "") - properties, err := connection.GetAllProperties("sssd-kcm.socket") + var result map[string]dbus.Variant + err = call.Store(&result) if err != nil { return "", errors.New("failed to get the properties of sssd-kcm.socket") } - value := properties["Listen"] - if value == nil { + listenVariant, listenFound := result["Listen"] + if !listenFound { return "", errors.New("failed to find the Listen property of sssd-kcm.socket") } - sockets := value.([][]interface{}) + listenVariantSignature := listenVariant.Signature().String() + if listenVariantSignature != "aav" { + return "", errors.New("unknown reply from org.freedesktop.DBus.Properties.GetAll") + } + + listenValue := listenVariant.Value() + sockets := listenValue.([][]interface{}) for _, socket := range sockets { if socket[0] == "Stream" { path := socket[1].(string) @@ -632,3 +648,31 @@ func pullImage(image, release string) (bool, error) { return true, nil } + +// systemdNeedsEscape checks whether a byte in a potential dbus ObjectPath needs to be escaped +func systemdNeedsEscape(i int, b byte) bool { + // Escape everything that is not a-z-A-Z-0-9 + // Also escape 0-9 if it's the first character + return strings.IndexByte(alphanum, b) == -1 || + (i == 0 && strings.IndexByte(num, b) != -1) +} + +// systemdPathBusEscape sanitizes a constituent string of a dbus ObjectPath using the +// rules that systemd uses for serializing special characters. +func systemdPathBusEscape(path string) string { + // Special case the empty string + if len(path) == 0 { + return "_" + } + n := []byte{} + for i := 0; i < len(path); i++ { + c := path[i] + if systemdNeedsEscape(i, c) { + e := fmt.Sprintf("_%x", c) + n = append(n, []byte(e)...) + } else { + n = append(n, c) + } + } + return string(n) +} diff --git a/src/go.mod b/src/go.mod index eb87cdbfe..07891e13a 100644 --- a/src/go.mod +++ b/src/go.mod @@ -6,7 +6,6 @@ require ( github.com/HarryMichal/go-version v1.0.0 github.com/acobaugh/osrelease v0.0.0-20181218015638-a93a0a55a249 github.com/briandowns/spinner v1.10.0 - github.com/coreos/go-systemd/v22 v22.0.0 github.com/godbus/dbus/v5 v5.0.3 github.com/sirupsen/logrus v1.4.2 github.com/spf13/cobra v0.0.5 diff --git a/src/go.sum b/src/go.sum index e144f35f0..5ee0d2179 100644 --- a/src/go.sum +++ b/src/go.sum @@ -9,8 +9,6 @@ github.com/briandowns/spinner v1.10.0/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/coreos/go-systemd/v22 v22.0.0 h1:XJIw/+VlJ+87J+doOxznsAWIdmWuViOVhkQamW5YV28= -github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=