Skip to content

Commit e486794

Browse files
thepetkrm3l
andauthored
Specify files for port detection for every detector (#28)
* Update dotnet detector with universal port detection Signed-off-by: thepetk <[email protected]> * Align go detectors to universal port detection Signed-off-by: thepetk <[email protected]> * TMP update detector Signed-off-by: thepetk <[email protected]> * Update various java frameworks Signed-off-by: thepetk <[email protected]> * Finalize quarkus port detection Signed-off-by: thepetk <[email protected]> * Update spring port detection Signed-off-by: thepetk <[email protected]> * Update Vertx port detection Signed-off-by: thepetk <[email protected]> * Use consistent naming for enricher models Signed-off-by: thepetk <[email protected]> * Update wildfly port detection Signed-off-by: thepetk <[email protected]> * Update angular port detection Signed-off-by: thepetk <[email protected]> * Add filtering to js files for express enricher Signed-off-by: thepetk <[email protected]> * Update next port detection Signed-off-by: thepetk <[email protected]> * Update nuxt port detection Signed-off-by: thepetk <[email protected]> * Update react port detection Signed-off-by: thepetk <[email protected]> * Update svelte js port detection Signed-off-by: thepetk <[email protected]> * Update vue js port detection Signed-off-by: thepetk <[email protected]> * Sort models Signed-off-by: thepetk <[email protected]> * Update laravel port detection Signed-off-by: thepetk <[email protected]> * Refactor django enricher Signed-off-by: thepetk <[email protected]> * Loop for every application file found during port detection process Signed-off-by: thepetk <[email protected]> * Refactor flask enricher Signed-off-by: thepetk <[email protected]> * Fix vertx port detection Signed-off-by: thepetk <[email protected]> * Remove unused model Signed-off-by: thepetk <[email protected]> * Add comments to all func in detector.go Signed-off-by: thepetk <[email protected]> * Improve detector_test coverage Signed-off-by: thepetk <[email protected]> * Add comments to model.go Signed-off-by: thepetk <[email protected]> * Update documentation for port detection Signed-off-by: thepetk <[email protected]> * Update docs/public/port_detection.md Signed-off-by: thepetk <[email protected]> Co-authored-by: Armel Soro <[email protected]> Signed-off-by: thepetk <[email protected]> * Update docs/public/port_detection.md Signed-off-by: thepetk <[email protected]> Co-authored-by: Armel Soro <[email protected]> Signed-off-by: thepetk <[email protected]> * Update docs/public/port_detection.md Signed-off-by: thepetk <[email protected]> Co-authored-by: Armel Soro <[email protected]> Signed-off-by: thepetk <[email protected]> * Return nil on not implemented GetApplicationFileInfos Signed-off-by: thepetk <[email protected]> --------- Signed-off-by: thepetk <[email protected]> Co-authored-by: Armel Soro <[email protected]>
1 parent 060a15e commit e486794

31 files changed

+1344
-451
lines changed

docs/public/port_detection.md

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ services:
7474
### Port detection with frameworks
7575
### Java Frameworks
7676

77+
For Java frameworks not having a specific application file, Alizer will only try to detect ports defined inside `.java` files and not inside the entire component directory.
78+
7779
#### Micronaut
7880

7981
Alizer checks if the environment variable MICRONAUT_SERVER_SSL_ENABLED is set to true. If so, both MICRONAUT_SERVER_SSL_PORT and MICRONAUT_SERVER_PORT are checked (if false, only the MICRONAUT_SERVER_PORT is used for verification). Alizer will first look for if the env vars are set in the system and if it doesn't find them it will also look for a dockerfile. If they are not set or they do not contain valid port values, Alizer searches for the `application.[yml|yaml]` file in `src/main/resources` folder and verify if one or more ports are set.
@@ -179,6 +181,8 @@ Alizer searches inside the `pom.xml` file to find any configuration inside the p
179181

180182
### Javascript Frameworks
181183

184+
For JavaScript frameworks not having a specific application file, Alizer will only try to detect ports defined inside `.js` files and not inside the entire component directory.
185+
182186
### Angular
183187

184188
Alizer uses three ways to detect ports configuration in an Angular project
@@ -317,6 +321,8 @@ Example
317321

318322
### GoLang Frameworks
319323

324+
For Golang frameworks not having a specific application file, Alizer will only try to detect ports defined inside `.go` files and not inside the entire component directory.
325+
320326
#### Beego
321327

322328
Alizer parses the `conf/app.conf` file looking for the `httpport` variable

pkg/apis/enricher/framework/dotnet/dotnet_detector.go

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func (d DotNetDetector) GetSupportedFrameworks() []string {
3131
return []string{""}
3232
}
3333

34+
func (d DotNetDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
35+
// not implemented yet
36+
return []model.ApplicationFileInfo{}
37+
}
38+
3439
// DoFrameworkDetection uses configFilePath to check for the name of the framework
3540
func (d DotNetDetector) DoFrameworkDetection(language *model.Language, configFilePath string) {
3641
framework := getFrameworks(configFilePath)
@@ -52,6 +57,7 @@ func (d DotNetDetector) DoFrameworkDetection(language *model.Language, configFil
5257
}
5358

5459
func (d DotNetDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
60+
// not implemented yet
5561
}
5662

5763
func getFrameworks(configFilePath string) string {

pkg/apis/enricher/framework/go/beego_detector.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ func (b BeegoDetector) GetSupportedFrameworks() []string {
2626
return []string{"Beego"}
2727
}
2828

29+
func (b BeegoDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
30+
return []model.ApplicationFileInfo{
31+
{
32+
Context: ctx,
33+
Root: componentPath,
34+
Dir: "conf",
35+
File: "app.conf",
36+
},
37+
}
38+
}
39+
2940
// DoFrameworkDetection uses a tag to check for the framework name
3041
func (b BeegoDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
3142
if hasFramework(goMod.Require, "github.com/beego/beego") {
@@ -40,15 +51,16 @@ type ApplicationPropertiesFile struct {
4051

4152
// DoPortsDetection searches for the port in conf/app.conf
4253
func (b BeegoDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
43-
bytes, err := utils.ReadAnyApplicationFile(component.Path, []model.ApplicationFileInfo{
44-
{
45-
Dir: "conf",
46-
File: "app.conf",
47-
},
48-
}, ctx)
54+
fileContents, err := utils.GetApplicationFileContents(b.GetApplicationFileInfos(component.Path, ctx))
4955
if err != nil {
5056
return
5157
}
52-
re := regexp.MustCompile(`httpport\s*=\s*(\d+)`)
53-
component.Ports = utils.FindAllPortsSubmatch(re, string(bytes), 1)
58+
for _, fileContent := range fileContents {
59+
re := regexp.MustCompile(`httpport\s*=\s*(\d+)`)
60+
ports := utils.FindAllPortsSubmatch(re, fileContent, 1)
61+
if len(ports) > 0 {
62+
component.Ports = ports
63+
return
64+
}
65+
}
5466
}

pkg/apis/enricher/framework/go/echo_detector.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ func (e EchoDetector) GetSupportedFrameworks() []string {
2626
return []string{"Echo"}
2727
}
2828

29+
func (e EchoDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
30+
files, err := utils.GetCachedFilePathsFromRoot(componentPath, ctx)
31+
if err != nil {
32+
return []model.ApplicationFileInfo{}
33+
}
34+
return utils.GenerateApplicationFileFromFilters(files, componentPath, ".go", ctx)
35+
}
36+
2937
// DoFrameworkDetection uses a tag to check for the framework name
3038
func (e EchoDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
3139
if hasFramework(goMod.Require, "github.com/labstack/echo") {
@@ -34,11 +42,10 @@ func (e EchoDetector) DoFrameworkDetection(language *model.Language, goMod *modf
3442
}
3543

3644
func (e EchoDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
37-
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
45+
fileContents, err := utils.GetApplicationFileContents(e.GetApplicationFileInfos(component.Path, ctx))
3846
if err != nil {
3947
return
4048
}
41-
4249
matchRegexRules := model.PortMatchRules{
4350
MatchIndexRegexes: []model.PortMatchRule{
4451
{
@@ -58,8 +65,11 @@ func (e EchoDetector) DoPortsDetection(component *model.Component, ctx *context.
5865
},
5966
}
6067

61-
ports := GetPortFromFilesGo(matchRegexRules, files)
62-
if len(ports) > 0 {
63-
component.Ports = ports
68+
for _, fileContent := range fileContents {
69+
ports := GetPortFromFileGo(matchRegexRules, fileContent)
70+
if len(ports) > 0 {
71+
component.Ports = ports
72+
return
73+
}
6474
}
6575
}

pkg/apis/enricher/framework/go/fasthttp_detector.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ func (f FastHttpDetector) GetSupportedFrameworks() []string {
2626
return []string{"FastHttp"}
2727
}
2828

29+
func (f FastHttpDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
30+
files, err := utils.GetCachedFilePathsFromRoot(componentPath, ctx)
31+
if err != nil {
32+
return []model.ApplicationFileInfo{}
33+
}
34+
return utils.GenerateApplicationFileFromFilters(files, componentPath, ".go", ctx)
35+
}
36+
2937
// DoFrameworkDetection uses a tag to check for the framework name
3038
func (f FastHttpDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
3139
if hasFramework(goMod.Require, "github.com/valyala/fasthttp") {
@@ -34,7 +42,7 @@ func (f FastHttpDetector) DoFrameworkDetection(language *model.Language, goMod *
3442
}
3543

3644
func (f FastHttpDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
37-
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
45+
fileContents, err := utils.GetApplicationFileContents(f.GetApplicationFileInfos(component.Path, ctx))
3846
if err != nil {
3947
return
4048
}
@@ -47,8 +55,11 @@ func (f FastHttpDetector) DoPortsDetection(component *model.Component, ctx *cont
4755
},
4856
},
4957
}
50-
ports := GetPortFromFilesGo(matchRegexRules, files)
51-
if len(ports) > 0 {
52-
component.Ports = ports
58+
for _, fileContent := range fileContents {
59+
ports := GetPortFromFileGo(matchRegexRules, fileContent)
60+
if len(ports) > 0 {
61+
component.Ports = ports
62+
return
63+
}
5364
}
5465
}

pkg/apis/enricher/framework/go/gin_detector.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ func (g GinDetector) GetSupportedFrameworks() []string {
2626
return []string{"Gin"}
2727
}
2828

29+
func (g GinDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
30+
files, err := utils.GetCachedFilePathsFromRoot(componentPath, ctx)
31+
if err != nil {
32+
return []model.ApplicationFileInfo{}
33+
}
34+
return utils.GenerateApplicationFileFromFilters(files, componentPath, ".go", ctx)
35+
}
36+
2937
// DoFrameworkDetection uses a tag to check for the framework name
3038
func (g GinDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
3139
if hasFramework(goMod.Require, "github.com/gin-gonic/gin") {
@@ -34,7 +42,7 @@ func (g GinDetector) DoFrameworkDetection(language *model.Language, goMod *modfi
3442
}
3543

3644
func (g GinDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
37-
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
45+
fileContents, err := utils.GetApplicationFileContents(g.GetApplicationFileInfos(component.Path, ctx))
3846
if err != nil {
3947
return
4048
}
@@ -48,8 +56,11 @@ func (g GinDetector) DoPortsDetection(component *model.Component, ctx *context.C
4856
},
4957
}
5058

51-
ports := GetPortFromFilesGo(matchRegexRules, files)
52-
if len(ports) > 0 {
53-
component.Ports = ports
59+
for _, fileContent := range fileContents {
60+
ports := GetPortFromFileGo(matchRegexRules, fileContent)
61+
if len(ports) > 0 {
62+
component.Ports = ports
63+
return
64+
}
5465
}
5566
}

pkg/apis/enricher/framework/go/go_detector.go

+11-23
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ package enricher
1313

1414
import (
1515
"context"
16-
"os"
17-
"path/filepath"
1816
"regexp"
1917
"strings"
2018

@@ -38,7 +36,11 @@ func DoGoPortsDetection(component *model.Component, ctx *context.Context) {
3836
if err != nil {
3937
return
4038
}
41-
39+
appFileInfos := utils.GenerateApplicationFileFromFilters(files, component.Path, ".go", ctx)
40+
fileContents, err := utils.GetApplicationFileContents(appFileInfos)
41+
if err != nil {
42+
return
43+
}
4244
matchRegexRules := model.PortMatchRules{
4345
MatchIndexRegexes: []model.PortMatchRule{
4446
{
@@ -58,9 +60,12 @@ func DoGoPortsDetection(component *model.Component, ctx *context.Context) {
5860
},
5961
}
6062

61-
ports := GetPortFromFilesGo(matchRegexRules, files)
62-
if len(ports) > 0 {
63-
component.Ports = ports
63+
for _, fileContent := range fileContents {
64+
ports := GetPortFromFileGo(matchRegexRules, fileContent)
65+
if len(ports) > 0 {
66+
component.Ports = ports
67+
return
68+
}
6469
}
6570
}
6671

@@ -135,20 +140,3 @@ func GetPortWithMatchIndexesGo(content string, matchIndexes []int, toBeReplaced
135140

136141
return -1
137142
}
138-
139-
// GetPortFromFilesGo loops through a list of paths and tries to find a port matching the
140-
// given set PortMatchRules
141-
func GetPortFromFilesGo(matchRegexRules model.PortMatchRules, files []string) []int {
142-
for _, file := range files {
143-
cleanFile := filepath.Clean(file)
144-
bytes, err := os.ReadFile(cleanFile)
145-
if err != nil {
146-
continue
147-
}
148-
ports := GetPortFromFileGo(matchRegexRules, string(bytes))
149-
if len(ports) > 0 {
150-
return ports
151-
}
152-
}
153-
return []int{}
154-
}

pkg/apis/enricher/framework/go/gofiber_detector.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ func (g GoFiberDetector) GetSupportedFrameworks() []string {
2626
return []string{"GoFiber"}
2727
}
2828

29+
func (g GoFiberDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
30+
files, err := utils.GetCachedFilePathsFromRoot(componentPath, ctx)
31+
if err != nil {
32+
return []model.ApplicationFileInfo{}
33+
}
34+
return utils.GenerateApplicationFileFromFilters(files, componentPath, ".go", ctx)
35+
}
36+
2937
// DoFrameworkDetection uses a tag to check for the framework name
3038
func (g GoFiberDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
3139
if hasFramework(goMod.Require, "github.com/gofiber/fiber") {
@@ -34,7 +42,7 @@ func (g GoFiberDetector) DoFrameworkDetection(language *model.Language, goMod *m
3442
}
3543

3644
func (g GoFiberDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
37-
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
45+
fileContents, err := utils.GetApplicationFileContents(g.GetApplicationFileInfos(component.Path, ctx))
3846
if err != nil {
3947
return
4048
}
@@ -47,8 +55,12 @@ func (g GoFiberDetector) DoPortsDetection(component *model.Component, ctx *conte
4755
},
4856
},
4957
}
50-
ports := GetPortFromFilesGo(matchRegexRules, files)
51-
if len(ports) > 0 {
52-
component.Ports = ports
58+
59+
for _, fileContent := range fileContents {
60+
ports := GetPortFromFileGo(matchRegexRules, fileContent)
61+
if len(ports) > 0 {
62+
component.Ports = ports
63+
return
64+
}
5365
}
5466
}

pkg/apis/enricher/framework/go/mux_detector.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ func (m MuxDetector) GetSupportedFrameworks() []string {
2626
return []string{"Mux"}
2727
}
2828

29+
func (m MuxDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
30+
files, err := utils.GetCachedFilePathsFromRoot(componentPath, ctx)
31+
if err != nil {
32+
return []model.ApplicationFileInfo{}
33+
}
34+
return utils.GenerateApplicationFileFromFilters(files, componentPath, ".go", ctx)
35+
}
36+
2937
// DoFrameworkDetection uses a tag to check for the framework name
3038
func (m MuxDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
3139
if hasFramework(goMod.Require, "github.com/gorilla/mux") {
@@ -34,7 +42,7 @@ func (m MuxDetector) DoFrameworkDetection(language *model.Language, goMod *modfi
3442
}
3543

3644
func (m MuxDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
37-
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
45+
fileContents, err := utils.GetApplicationFileContents(m.GetApplicationFileInfos(component.Path, ctx))
3846
if err != nil {
3947
return
4048
}
@@ -54,8 +62,11 @@ func (m MuxDetector) DoPortsDetection(component *model.Component, ctx *context.C
5462
},
5563
}
5664

57-
ports := GetPortFromFilesGo(matchRegexRules, files)
58-
if len(ports) > 0 {
59-
component.Ports = ports
65+
for _, fileContent := range fileContents {
66+
ports := GetPortFromFileGo(matchRegexRules, fileContent)
67+
if len(ports) > 0 {
68+
component.Ports = ports
69+
return
70+
}
6071
}
6172
}

pkg/apis/enricher/framework/java/java_detector.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"regexp"
1616
"strings"
1717

18+
"github.com/devfile/alizer/pkg/schema"
1819
"github.com/devfile/alizer/pkg/utils"
1920
)
2021

@@ -36,13 +37,8 @@ func hasFramework(configFile, groupId, artifactId string) (bool, error) {
3637

3738
// GetPortsForJBossFrameworks tries to detect any port information inside javaOpts of configuration
3839
// of a given profiles plugin
39-
func GetPortsForJBossFrameworks(pomFilePath, pluginArtifactId, pluginGroupId string) string {
40+
func GetPortsForJBossFrameworks(pom schema.Pom, pluginArtifactId string, pluginGroupId string) string {
4041
portPlaceholder := ""
41-
pom, err := utils.GetPomFileContent(pomFilePath)
42-
if err != nil {
43-
return portPlaceholder
44-
}
45-
4642
re := regexp.MustCompile(`jboss.https?.port=\d*`)
4743
// Check for port configuration inside profiles
4844
for _, profile := range pom.Profiles.Profile {

0 commit comments

Comments
 (0)