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

handle specific check for case agent-target address is url #118

Closed
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
35 changes: 20 additions & 15 deletions utils/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ var (
log *logger.Log
)

func init() {
log = logger.Logger(component, logger.Info, "", logger.Json)
}

type HttpOptions struct {
Target *cacao.AgentTarget
Command *cacao.Command
Expand All @@ -44,7 +48,6 @@ const (
)

func (httpRequest *HttpRequest) Request(httpOptions HttpOptions) ([]byte, error) {
log = logger.Logger(component, logger.Info, "", logger.Json)
request, err := httpOptions.setupRequest()
if err != nil {
return []byte{}, err
Expand Down Expand Up @@ -171,9 +174,17 @@ func (httpOptions *HttpOptions) ExtractUrl() (string, error) {
}
}

// If for an http-api command the agent-target address is a URL, it must be handled differently
if len(target.Address["url"]) > 0 {
if target.Address["url"][0] != "" {
return parsePathBasedUrl(target.Address["url"][0])
urlObject, err := parsePathBasedUrl(target.Address["url"][0])
if err != nil {
return "", err
}
if (urlObject.Path != "" && urlObject.Path != "/") && urlObject.Path != path {
log.Warn("agent-target url has path that does not match http-api command path")
}
return urlObject.String(), nil
}
}
return buildSchemeAndHostname(path, target)
Expand All @@ -183,7 +194,7 @@ func buildSchemeAndHostname(path string, target *cacao.AgentTarget) (string, err
var hostname string

scheme := setDefaultScheme(target)
hostname, err := extractHostname(scheme, target)
hostname, err := extractHostname(target)
if err != nil {
return "", err
}
Expand All @@ -210,7 +221,7 @@ func setDefaultScheme(target *cacao.AgentTarget) string {
return scheme
}

func extractHostname(scheme string, target *cacao.AgentTarget) (string, error) {
func extractHostname(target *cacao.AgentTarget) (string, error) {
var address string

if len(target.Address["dname"]) > 0 {
Expand All @@ -226,28 +237,22 @@ func extractHostname(scheme string, target *cacao.AgentTarget) (string, error) {
return "", errors.New("failed regex rule for domain name")
}
address = target.Address["ipv4"][0]
} else if len(target.Address["url"]) > 0 {
match, _ := regexp.MatchString(ipv4Regex, target.Address["url"][0])
if !match {
return "", errors.New("failed regex rule for domain name")
}
address = target.Address["url"][0]

} else {
return "", errors.New("unsupported target address type")
}
return address, nil
}

func parsePathBasedUrl(httpUrl string) (string, error) {
func parsePathBasedUrl(httpUrl string) (*url.URL, error) {
parsedUrl, err := url.ParseRequestURI(httpUrl)
if err != nil {
return "", err
return parsedUrl, err
}

if parsedUrl.Hostname() == "" {
return "", errors.New("no domain name")
return parsedUrl, errors.New("no domain name")
}
return parsedUrl.String(), nil
return parsedUrl, nil
}

func validatePort(port string) error {
Expand Down
Loading