From c2f621b37399681b7f6ab02429548f8c0e0e8a46 Mon Sep 17 00:00:00 2001 From: Preslav Gerchev Date: Tue, 3 Oct 2023 18:55:03 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20docker=20connection=20to?= =?UTF-8?q?=20support=20specifying=20either=20an=20image=20or=20a=20contai?= =?UTF-8?q?ner.=20Add=20support=20for=20shell=20docker=20container=20.=20(#2060)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- providers/os/connection/docker_container.go | 28 +++++++++++++++++++++ providers/os/provider/provider.go | 26 ++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/providers/os/connection/docker_container.go b/providers/os/connection/docker_container.go index 0da495edab..6d76b564c3 100644 --- a/providers/os/connection/docker_container.go +++ b/providers/os/connection/docker_container.go @@ -372,3 +372,31 @@ func NewDockerContainerImageConnection(id uint32, conf *inventory.Config, asset tarConn.Metadata.Labels = ii.Labels return tarConn, nil } + +// based on the target, try and find out what kind of connection we are dealing with, this can be either a +// 1. a container, referenced by name or id +// 2. a locally present image, referenced by tag or digest +// 3. a remote image, referenced by tag or digest +func FetchConnectionType(target string) (string, error) { + ded, err := docker_discovery.NewDockerEngineDiscovery() + if err != nil { + return "", err + } + + if ded != nil { + _, err = ded.ContainerInfo(target) + if err == nil { + return "docker-container", nil + } + _, err = ded.ImageInfo(target) + if err == nil { + return "docker-image", nil + } + } + _, err = name.ParseReference(target, name.WeakValidation) + if err == nil { + return "docker-image", nil + } + + return "", errors.New("could not find container or image " + target) +} diff --git a/providers/os/provider/provider.go b/providers/os/provider/provider.go index 51a157f04d..aa8f7a7273 100644 --- a/providers/os/provider/provider.go +++ b/providers/os/provider/provider.go @@ -88,7 +88,31 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) port = 5985 case "vagrant": conf.Type = "vagrant" - case "container", "docker": + case "docker": + if len(req.Args) > 1 { + switch req.Args[0] { + case "image": + conf.Type = "docker-image" + conf.Host = req.Args[1] + case "registry": + conf.Type = "docker-registry" + conf.Host = req.Args[1] + case "tar": + conf.Type = "docker-snapshot" + conf.Path = req.Args[1] + case "container": + conf.Type = "docker-container" + conf.Host = req.Args[1] + } + } else { + connType, err := connection.FetchConnectionType(req.Args[0]) + if err != nil { + return nil, err + } + conf.Type = connType + containerID = req.Args[0] + } + case "container": if len(req.Args) > 1 { switch req.Args[0] { case "image":