Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⭐️ Make following redirects configurable for http.get resource #5011

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions providers/network/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package config
import (
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v11/providers/network/connection"
"go.mondoo.com/cnquery/v11/providers/network/provider"
)

Expand Down Expand Up @@ -42,6 +43,12 @@ var Config = plugin.Provider{
Default: "",
Desc: "Disable TLS/SSL verification",
},
{
Long: connection.OPTION_FOLLOW_REDIRECTS,
Type: plugin.FlagType_Bool,
Default: "",
Desc: "Follow HTTP redirects",
},
},
},
},
Expand Down
38 changes: 29 additions & 9 deletions providers/network/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ import (
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
)

const (
OPTION_FOLLOW_REDIRECTS = "follow-redirects"
)

type HostConnection struct {
plugin.Connection
Conf *inventory.Config
asset *inventory.Asset
httpClient *http.Client
Conf *inventory.Config
FollowRedirects bool
asset *inventory.Asset
transport *http.Transport
}

func NewHostConnection(id uint32, asset *inventory.Asset, conf *inventory.Config) *HostConnection {
Expand All @@ -40,11 +45,17 @@ func NewHostConnection(id uint32, asset *inventory.Asset, conf *inventory.Config
}
}

var followRedirects bool
if followRedirectsStr, ok := conf.Options[OPTION_FOLLOW_REDIRECTS]; ok {
followRedirects = followRedirectsStr == "true"
}

return &HostConnection{
Connection: plugin.NewConnection(id, asset),
Conf: conf,
asset: asset,
httpClient: &http.Client{Transport: transport},
Connection: plugin.NewConnection(id, asset),
Conf: conf,
asset: asset,
transport: transport,
FollowRedirects: followRedirects,
}
}

Expand All @@ -63,6 +74,15 @@ func (p *HostConnection) FQDN() string {
return p.Conf.Host
}

func (p *HostConnection) Client() *http.Client {
return p.httpClient
func (p *HostConnection) Client(followRedirects bool) *http.Client {
c := &http.Client{
Transport: p.transport,
}

if !followRedirects {
c.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
return c
}
7 changes: 7 additions & 0 deletions providers/network/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"net/url"
"strconv"
"strings"

"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
Expand Down Expand Up @@ -44,6 +45,11 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error)
insecure, _ = found.RawData().Value.(bool)
}

options := map[string]string{}
if found, ok := req.Flags[connection.OPTION_FOLLOW_REDIRECTS]; ok {
options[connection.OPTION_FOLLOW_REDIRECTS] = strconv.FormatBool(found.RawData().Value.(bool))
}

asset := inventory.Asset{
Connections: []*inventory.Config{{
Type: "host",
Expand All @@ -52,6 +58,7 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error)
Path: path,
Runtime: scheme,
Insecure: insecure,
Options: options,
}},
}

Expand Down
9 changes: 7 additions & 2 deletions providers/network/resources/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@ func initHttpGet(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[str
return nil, nil, err
}
args["url"] = llx.ResourceData(url, "url")
args["followRedirects"] = llx.BoolData(conn.FollowRedirects)
}

if _, ok := args["followRedirects"]; !ok {
args["followRedirects"] = llx.BoolData(false)
}

return args, nil, nil
}

func (x *mqlHttpGet) id() (string, error) {
return x.Url.Data.__id, nil
return strings.Join([]string{x.Url.Data.__id, strconv.FormatBool(x.FollowRedirects.Data)}, ";"), nil
}

func (x *mqlHttpGet) do() error {
Expand All @@ -97,7 +102,7 @@ func (x *mqlHttpGet) do() error {
}

conn := x.MqlRuntime.Connection.(*connection.HostConnection)
resp, err := conn.Client().Get(x.Url.Data.String.Data)
resp, err := conn.Client(x.FollowRedirects.Data).Get(x.Url.Data.String.Data)
x.resp.State = plugin.StateIsSet
x.resp.Data = resp
x.resp.Error = err
Expand Down
4 changes: 3 additions & 1 deletion providers/network/resources/network.lr
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ http {}

// HTTP GET requests
http.get @defaults("url statusCode") {
init(rawUrl string)
init(rawUrl string, followRedirects bool)
// URL for this request
url url
// Follow redirects
followRedirects bool
// Header returned from this request
header() http.header
// Status returned from this request
Expand Down
12 changes: 12 additions & 0 deletions providers/network/resources/network.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions providers/network/resources/network.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ resources:
http.get:
fields:
body: {}
followRedirects: {}
header: {}
statusCode: {}
url: {}
Expand Down
3 changes: 2 additions & 1 deletion providers/network/resources/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func (x *mqlUrl) string() (string, error) {
}

host := x.Host.Data
if x.Port.Data != 0 {
isStandardPort := x.Port.Data == 80 && x.Scheme.Data == "http" || x.Port.Data == 443 && x.Scheme.Data == "https"
if x.Port.Data != 0 && !isStandardPort {
host += ":" + strconv.Itoa(int(x.Port.Data))
}

Expand Down
Loading