From 28b62d00bc6df0c5921770d2ab3cef416911cdad Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Sun, 17 Sep 2023 22:14:40 -0700 Subject: [PATCH] =?UTF-8?q?Revert=20"=E2=9C=A8=20Add=20container=20discove?= =?UTF-8?q?ry=20to=20v9=20os=20provider=20(#1701)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 0e7b2f9bf85cf2484770a179e7de3d7b0a8a1aca. --- providers/os/config/config.go | 4 +- providers/os/connection/docker_container.go | 6 +-- providers/os/provider/provider.go | 41 +++---------------- .../discovery/docker_engine/container.go | 21 +++++++++- .../discovery/docker_engine/images.go | 25 ++++++++++- .../discovery/docker_engine/resolver.go | 4 -- 6 files changed, 53 insertions(+), 48 deletions(-) diff --git a/providers/os/config/config.go b/providers/os/config/config.go index 916f98906b..23e6046637 100644 --- a/providers/os/config/config.go +++ b/providers/os/config/config.go @@ -33,7 +33,7 @@ var Config = plugin.Provider{ MinArgs: 0, MaxArgs: 0, Discovery: []string{ - "container", + "containers", "container-images", }, Flags: []plugin.Flag{ @@ -170,7 +170,7 @@ var Config = plugin.Provider{ MinArgs: 1, MaxArgs: 1, Discovery: []string{ - "container", + "containers", "container-images", }, Flags: []plugin.Flag{ diff --git a/providers/os/connection/docker_container.go b/providers/os/connection/docker_container.go index 557ddb71a5..f9d5a6736f 100644 --- a/providers/os/connection/docker_container.go +++ b/providers/os/connection/docker_container.go @@ -58,9 +58,9 @@ func NewDockerContainerConnection(id uint32, conf *inventory.Config, asset *inve } // check if we are having a container - data, err := dockerClient.ContainerInspect(context.Background(), conf.Host) + data, err := dockerClient.ContainerInspect(context.Background(), asset.Name) if err != nil { - return nil, errors.New("cannot find container " + conf.Host) + return nil, errors.New("cannot find container " + asset.Name) } if !data.State.Running { @@ -70,7 +70,7 @@ func NewDockerContainerConnection(id uint32, conf *inventory.Config, asset *inve conn := &DockerContainerConnection{ asset: asset, Client: dockerClient, - container: conf.Host, + container: asset.Name, kind: "container", runtime: "docker", } diff --git a/providers/os/provider/provider.go b/providers/os/provider/provider.go index 4b5b158292..f51ff9f9af 100644 --- a/providers/os/provider/provider.go +++ b/providers/os/provider/provider.go @@ -20,7 +20,6 @@ import ( "go.mondoo.com/cnquery/providers/os/connection/shared" "go.mondoo.com/cnquery/providers/os/resources" "go.mondoo.com/cnquery/providers/os/resources/discovery/container_registry" - "go.mondoo.com/cnquery/providers/os/resources/discovery/docker_engine" ) const ( @@ -50,14 +49,8 @@ func Init() *Service { } func parseDiscover(flags map[string]*llx.Primitive) *inventory.Discovery { - discovery := &inventory.Discovery{Targets: []string{"auto"}} - if flag, ok := flags["discover"]; ok && len(flag.Array) > 0 { - discovery.Targets = []string{} - for i := range flag.Array { - discovery.Targets = append(discovery.Targets, string(flag.Array[i].Value)) - } - } - return discovery + // TODO: parse me... + return &inventory.Discovery{Targets: []string{"auto"}} } func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) { @@ -206,16 +199,8 @@ func (s *Service) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallba } var inv *inventory.Inventory - connType := conn.Asset().Connections[0].Type - switch connType { - case "docker-registry": - tarConn := conn.(*connection.TarConnection) - inv, err = s.discoverRegistry(tarConn) - if err != nil { - return nil, err - } - case "local", "docker-container": - inv, err = s.discoverLocalContainers(conn.Asset().Connections[0]) + if conn.Asset().Connections[0].Type == "docker-registry" { + inv, err = s.discover(conn.(*connection.TarConnection)) if err != nil { return nil, err } @@ -451,7 +436,7 @@ func (s *Service) StoreData(req *plugin.StoreReq) (*plugin.StoreRes, error) { return &plugin.StoreRes{}, nil } -func (s *Service) discoverRegistry(conn *connection.TarConnection) (*inventory.Inventory, error) { +func (s *Service) discover(conn *connection.TarConnection) (*inventory.Inventory, error) { conf := conn.Asset().Connections[0] if conf == nil { return nil, nil @@ -468,19 +453,3 @@ func (s *Service) discoverRegistry(conn *connection.TarConnection) (*inventory.I return inventory, nil } - -func (s *Service) discoverLocalContainers(conf *inventory.Config) (*inventory.Inventory, error) { - if conf == nil { - return nil, nil - } - - resolvedAssets, err := docker_engine.DiscoverDockerEngineAssets(conf) - if err != nil { - return nil, err - } - - inventory := &inventory.Inventory{} - inventory.AddAssets(resolvedAssets...) - - return inventory, nil -} diff --git a/providers/os/resources/discovery/docker_engine/container.go b/providers/os/resources/discovery/docker_engine/container.go index 9c266b0127..edfc2c5658 100644 --- a/providers/os/resources/discovery/docker_engine/container.go +++ b/providers/os/resources/discovery/docker_engine/container.go @@ -132,16 +132,35 @@ func (e *dockerEngineDiscovery) ListContainer() ([]*inventory.Asset, error) { container := make([]*inventory.Asset, len(dContainers)) for i, dContainer := range dContainers { + name := strings.Join(DockerDisplayNames(dContainer.Names), ",") asset := &inventory.Asset{ + Name: name, + PlatformIds: []string{containerid.MondooContainerID(dContainer.ID)}, + Platform: &inventory.Platform{ + Kind: "container", + Runtime: "docker-container", + }, Connections: []*inventory.Config{ { Backend: "docker-engine", - Type: "docker-container", Host: dContainer.ID, }, }, + State: mapContainerState(dContainer.State), + Labels: make(map[string]string), + } + + for key := range dContainer.Labels { + asset.Labels[key] = dContainer.Labels[key] } + // fetch docker specific metadata + labels := map[string]string{} + labels["mondoo.com/image-id"] = dContainer.ImageID + labels["docker.io/image-name"] = dContainer.Image + labels["docker.io/names"] = name + asset.Labels = labels + container[i] = asset } return container, nil diff --git a/providers/os/resources/discovery/docker_engine/images.go b/providers/os/resources/discovery/docker_engine/images.go index 42b55e5a11..5a0eec8a8e 100644 --- a/providers/os/resources/discovery/docker_engine/images.go +++ b/providers/os/resources/discovery/docker_engine/images.go @@ -9,6 +9,7 @@ import ( "github.com/docker/docker/api/types" "go.mondoo.com/cnquery/providers-sdk/v1/inventory" + "go.mondoo.com/cnquery/providers/os/id/containerid" ) // be aware that images are prefixed with sha256:, while containers are not @@ -52,14 +53,34 @@ func (e *dockerEngineDiscovery) ListImages() ([]*inventory.Asset, error) { } asset := &inventory.Asset{ + Name: strings.Join(dImg.RepoTags, ","), + PlatformIds: []string{containerid.MondooContainerImageID(digest)}, + Platform: &inventory.Platform{ + Kind: "container-image", + Runtime: "docker-image", + }, Connections: []*inventory.Config{ { - Type: "docker-image", - Host: dImg.ID, + Backend: "docker-image", + Host: dImg.ID, }, }, + State: inventory.State_STATE_ONLINE, + } + + // update labels + labels := map[string]string{} + for key := range dImg.Labels { + labels[key] = dImg.Labels[key] } + labels["mondoo.com/image-id"] = dImg.ID + // project/repo:5e664d0e,gcr.io/project/repo:5e664d0e + labels["docker.io/tags"] = strings.Join(dImg.RepoTags, ",") + // gcr.io/project/repo@sha256:5248...2bee + labels["docker.io/digests"] = strings.Join(dImg.RepoDigests, ",") + asset.Labels = labels + imgs[i] = asset } diff --git a/providers/os/resources/discovery/docker_engine/resolver.go b/providers/os/resources/discovery/docker_engine/resolver.go index 54f5e150b8..e91629db68 100644 --- a/providers/os/resources/discovery/docker_engine/resolver.go +++ b/providers/os/resources/discovery/docker_engine/resolver.go @@ -209,10 +209,6 @@ func DiscoverDockerEngineAssets(conf *inventory.Config) ([]*inventory.Asset, err // the system is using docker or podman locally assetList := []*inventory.Asset{} - if conf.Discover == nil { - return assetList, nil - } - // discover running container: container if stringx.Contains(conf.Discover.Targets, "all") || stringx.Contains(conf.Discover.Targets, DiscoveryContainerRunning) { ded, err := NewDockerEngineDiscovery()