Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Enhance go port recognition #181

Merged
merged 6 commits into from
Apr 19, 2023
Merged
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
40 changes: 40 additions & 0 deletions go/pkg/apis/enricher/framework/go/go_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package enricher

import (
"context"
"os"
"regexp"
"strings"

Expand All @@ -28,6 +30,44 @@ func hasFramework(modules []*modfile.Require, tag string) bool {
return false
}

func DoGoPortsDetection(component *model.Component, ctx *context.Context) {
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
if err != nil {
return
}

matchRegexRules := model.PortMatchRules{
MatchIndexRegexes: []model.PortMatchRule{
{
Regex: regexp.MustCompile(`.ListenAndServe\([^,)]*`),
ToReplace: ".ListenAndServe(",
},
{
Regex: regexp.MustCompile(`.Start\([^,)]*`),
thepetk marked this conversation as resolved.
Show resolved Hide resolved
ToReplace: ".Start(",
},
},
MatchRegexes: []model.PortMatchSubRule{
{
Regex: regexp.MustCompile(`Addr:\s+"([^",]+)`),
SubRegex: regexp.MustCompile(`:*(\d+)$`),
},
},
}

for _, file := range files {
bytes, err := os.ReadFile(file)

Check failure

Code scanning / gosec

Potential file inclusion via variable

Potential file inclusion via variable
if err != nil {
continue
}
ports := GetPortFromFileGo(matchRegexRules, string(bytes))
if len(ports) > 0 {
component.Ports = ports
return
}
}
}

func GetPortFromFileGo(rules model.PortMatchRules, text string) []int {
ports := []int{}
for _, matchIndexRegex := range rules.MatchIndexRegexes {
Expand Down
3 changes: 3 additions & 0 deletions go/pkg/apis/enricher/go_enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (j GoEnricher) DoEnrichComponent(component *model.Component, settings model
}
}
}
if len(component.Ports) == 0 {
framework.DoGoPortsDetection(component, ctx)
}
}
}
if len(ports) > 0 {
Expand Down
4 changes: 4 additions & 0 deletions go/test/apis/component_recognizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ func TestPortDetectionGoGin(t *testing.T) {
testPortDetectionInProject(t, "projectGin", []int{8789})
}

func TestPortDetectionGo(t *testing.T) {
testPortDetectionInProject(t, "projectGo", []int{8080})
}

func TestPortDetectionGoFiber(t *testing.T) {
testPortDetectionInProject(t, "projectGoFiber", []int{3000})
}
Expand Down
3 changes: 3 additions & 0 deletions resources/projectPortTesting/projectGo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/burrsutter/go-net-http-hello

go 1.19
22 changes: 22 additions & 0 deletions resources/projectPortTesting/projectGo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"net/http"
"os"
)

func main() {
http.HandleFunc("/", HelloHandler)
fmt.Println("Listening on localhost:8080")
http.ListenAndServe(":8080", nil)

Check failure

Code scanning / gosec

Use of net/http serve function that has no support for setting timeouts

Use of net/http serve function that has no support for setting timeouts

Check warning

Code scanning / gosec

Errors unhandled.

Errors unhandled.
}

func HelloHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
hostname, err := os.Hostname()
if err != nil {
fmt.Println("unable to get hostname")
}
fmt.Fprintf(w, "Go Hello on %s\n", hostname)
}