Skip to content

Commit

Permalink
cmd/create: Don't use gh.c/coreos/go-systemd
Browse files Browse the repository at this point in the history
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

#444
  • Loading branch information
debarshiray committed May 14, 2020
1 parent 35752e9 commit dcaf06a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
58 changes: 51 additions & 7 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
1 change: 0 additions & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit dcaf06a

Please sign in to comment.