Skip to content

Commit

Permalink
🐛 Fix docker connection to support specifying either an image or a co…
Browse files Browse the repository at this point in the history
…ntainer. Add support for shell docker container <id/name>. (#2060)
  • Loading branch information
preslavgerchev authored Oct 3, 2023
1 parent 1a1ba23 commit c2f621b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
28 changes: 28 additions & 0 deletions providers/os/connection/docker_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
26 changes: 25 additions & 1 deletion providers/os/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit c2f621b

Please sign in to comment.