Skip to content

Commit

Permalink
🐛 fix for domain scanning integration (#2756)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuskimmina authored Dec 7, 2023
1 parent 9dbc378 commit dea9b95
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions providers/network/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"},
Expand All @@ -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
}
Expand Down

0 comments on commit dea9b95

Please sign in to comment.