From dea9b95474338142b4759da53810c7295c7c3af3 Mon Sep 17 00:00:00 2001 From: Marius Kimmina <38843153+mariuskimmina@users.noreply.github.com> Date: Thu, 7 Dec 2023 15:33:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20for=20domain=20scanning=20?= =?UTF-8?q?integration=20(#2756)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- providers/network/provider/provider.go | 62 +++++++++++++++++--------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/providers/network/provider/provider.go b/providers/network/provider/provider.go index 4eb9ca302c..d988a48e3a 100644 --- a/providers/network/provider/provider.go +++ b/providers/network/provider/provider.go @@ -38,9 +38,34 @@ func Init() *Service { func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) { target := req.Args[0] + host, port, scheme, path, err := parseTarget(target) + if err != nil { + return nil, err + } + + insecure := false + if found, ok := req.Flags["insecure"]; ok { + insecure, _ = found.RawData().Value.(bool) + } + + asset := inventory.Asset{ + Connections: []*inventory.Config{{ + Type: "host", + Port: int32(port), + Host: host, + Path: path, + Runtime: scheme, + Insecure: insecure, + }}, + } + + return &plugin.ParseCLIRes{Asset: &asset}, nil +} + +func parseTarget(target string) (string, int, string, string, error) { // Note on noSchema handling: // A user may type in a target like: `google.com`. Technically, this is not - // avalid scheme. We need to make it into a valid url scheme for parsing + // a valid scheme. We need to make it into a valid url scheme for parsing // and further processing, but we also want to be mindful of what users intend. // // If we set this to e.g. an HTTP scheme with port 80, then we break @@ -60,7 +85,7 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) url, err := url.Parse(target) if err != nil { - return nil, err + return "", 0, "", "", err } host, port := domain.SplitHostPort(url.Host) @@ -73,23 +98,10 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) scheme = "" } - insecure := false - if found, ok := req.Flags["insecure"]; ok { - insecure, _ = found.RawData().Value.(bool) - } + path := url.Path - asset := inventory.Asset{ - Connections: []*inventory.Config{{ - Type: "host", - Port: int32(port), - Host: host, - Path: url.Path, - Runtime: scheme, - Insecure: insecure, - }}, - } + return host, port, scheme, path, nil - return &plugin.ParseCLIRes{Asset: &asset}, nil } // Shutdown is automatically called when the shell closes. @@ -152,7 +164,15 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba } if conn.Conf.Options != nil && conn.Conf.Options["host"] != "" { - conn.Conf.Host = conn.Conf.Options["host"] + target := conn.Conf.Options["host"] + host, port, scheme, path, err := parseTarget(target) + if err != nil { + return nil, err + } + conn.Conf.Host = host + conn.Conf.Path = path + conn.Conf.Port = int32(port) + conn.Conf.Runtime = scheme } if err != nil { @@ -183,7 +203,8 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba } func (s *Service) detect(asset *inventory.Asset, conn *connection.HostConnection) error { - asset.Name = conn.Conf.Host + hostWithScheme := conn.Conf.Runtime + conn.Conf.Host + asset.Name = hostWithScheme asset.Platform = &inventory.Platform{ Name: "host", Family: []string{"network"}, @@ -192,7 +213,8 @@ func (s *Service) detect(asset *inventory.Asset, conn *connection.HostConnection } asset.Fqdn = conn.FQDN() - asset.PlatformIds = []string{"//platformid.api.mondoo.app/runtime/network/host/" + conn.Conf.Host} + hostWithTrimedScheme := strings.Replace(hostWithScheme, "://", "", -1) + asset.PlatformIds = []string{"//platformid.api.mondoo.app/runtime/network/host/" + hostWithTrimedScheme} return nil }