Skip to content

Commit

Permalink
Specify files for port detection for every detector (#28)
Browse files Browse the repository at this point in the history
* Update dotnet detector with universal port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Align go detectors to universal port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* TMP update detector

Signed-off-by: thepetk <thepetk@gmail.com>

* Update various java frameworks

Signed-off-by: thepetk <thepetk@gmail.com>

* Finalize quarkus port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Update spring port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Update Vertx port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Use consistent naming for enricher models

Signed-off-by: thepetk <thepetk@gmail.com>

* Update wildfly port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Update angular port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Add filtering to js files for express enricher

Signed-off-by: thepetk <thepetk@gmail.com>

* Update next port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Update nuxt port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Update react port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Update svelte js port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Update vue js port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Sort models

Signed-off-by: thepetk <thepetk@gmail.com>

* Update laravel port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Refactor django enricher

Signed-off-by: thepetk <thepetk@gmail.com>

* Loop for every application file found during port detection process

Signed-off-by: thepetk <thepetk@gmail.com>

* Refactor flask enricher

Signed-off-by: thepetk <thepetk@gmail.com>

* Fix vertx port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Remove unused model

Signed-off-by: thepetk <thepetk@gmail.com>

* Add comments to all func in detector.go

Signed-off-by: thepetk <thepetk@gmail.com>

* Improve detector_test coverage

Signed-off-by: thepetk <thepetk@gmail.com>

* Add comments to model.go

Signed-off-by: thepetk <thepetk@gmail.com>

* Update documentation for port detection

Signed-off-by: thepetk <thepetk@gmail.com>

* Update docs/public/port_detection.md

Signed-off-by: thepetk <thepetk@gmail.com>

Co-authored-by: Armel Soro <armel@rm3l.org>
Signed-off-by: thepetk <thepetk@gmail.com>

* Update docs/public/port_detection.md

Signed-off-by: thepetk <thepetk@gmail.com>

Co-authored-by: Armel Soro <armel@rm3l.org>
Signed-off-by: thepetk <thepetk@gmail.com>

* Update docs/public/port_detection.md

Signed-off-by: thepetk <thepetk@gmail.com>

Co-authored-by: Armel Soro <armel@rm3l.org>
Signed-off-by: thepetk <thepetk@gmail.com>

* Return nil on not implemented GetApplicationFileInfos

Signed-off-by: thepetk <thepetk@gmail.com>

---------

Signed-off-by: thepetk <thepetk@gmail.com>
Co-authored-by: Armel Soro <armel@rm3l.org>
  • Loading branch information
thepetk and rm3l authored Sep 22, 2023
1 parent 060a15e commit e486794
Show file tree
Hide file tree
Showing 31 changed files with 1,344 additions and 451 deletions.
6 changes: 6 additions & 0 deletions docs/public/port_detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ services:
### Port detection with frameworks
### Java Frameworks

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.

#### Micronaut

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.
Expand Down Expand Up @@ -179,6 +181,8 @@ Alizer searches inside the `pom.xml` file to find any configuration inside the p

### Javascript Frameworks

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.

### Angular

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

### GoLang Frameworks

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.

#### Beego

Alizer parses the `conf/app.conf` file looking for the `httpport` variable
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/enricher/framework/dotnet/dotnet_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func (d DotNetDetector) GetSupportedFrameworks() []string {
return []string{""}
}

func (d DotNetDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
// not implemented yet
return []model.ApplicationFileInfo{}
}

// DoFrameworkDetection uses configFilePath to check for the name of the framework
func (d DotNetDetector) DoFrameworkDetection(language *model.Language, configFilePath string) {
framework := getFrameworks(configFilePath)
Expand All @@ -52,6 +57,7 @@ func (d DotNetDetector) DoFrameworkDetection(language *model.Language, configFil
}

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

func getFrameworks(configFilePath string) string {
Expand Down
28 changes: 20 additions & 8 deletions pkg/apis/enricher/framework/go/beego_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ func (b BeegoDetector) GetSupportedFrameworks() []string {
return []string{"Beego"}
}

func (b BeegoDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
return []model.ApplicationFileInfo{
{
Context: ctx,
Root: componentPath,
Dir: "conf",
File: "app.conf",
},
}
}

// DoFrameworkDetection uses a tag to check for the framework name
func (b BeegoDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/beego/beego") {
Expand All @@ -40,15 +51,16 @@ type ApplicationPropertiesFile struct {

// DoPortsDetection searches for the port in conf/app.conf
func (b BeegoDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
bytes, err := utils.ReadAnyApplicationFile(component.Path, []model.ApplicationFileInfo{
{
Dir: "conf",
File: "app.conf",
},
}, ctx)
fileContents, err := utils.GetApplicationFileContents(b.GetApplicationFileInfos(component.Path, ctx))
if err != nil {
return
}
re := regexp.MustCompile(`httpport\s*=\s*(\d+)`)
component.Ports = utils.FindAllPortsSubmatch(re, string(bytes), 1)
for _, fileContent := range fileContents {
re := regexp.MustCompile(`httpport\s*=\s*(\d+)`)
ports := utils.FindAllPortsSubmatch(re, fileContent, 1)
if len(ports) > 0 {
component.Ports = ports
return
}
}
}
20 changes: 15 additions & 5 deletions pkg/apis/enricher/framework/go/echo_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (e EchoDetector) GetSupportedFrameworks() []string {
return []string{"Echo"}
}

func (e EchoDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
files, err := utils.GetCachedFilePathsFromRoot(componentPath, ctx)
if err != nil {
return []model.ApplicationFileInfo{}
}
return utils.GenerateApplicationFileFromFilters(files, componentPath, ".go", ctx)
}

// DoFrameworkDetection uses a tag to check for the framework name
func (e EchoDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/labstack/echo") {
Expand All @@ -34,11 +42,10 @@ func (e EchoDetector) DoFrameworkDetection(language *model.Language, goMod *modf
}

func (e EchoDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
fileContents, err := utils.GetApplicationFileContents(e.GetApplicationFileInfos(component.Path, ctx))
if err != nil {
return
}

matchRegexRules := model.PortMatchRules{
MatchIndexRegexes: []model.PortMatchRule{
{
Expand All @@ -58,8 +65,11 @@ func (e EchoDetector) DoPortsDetection(component *model.Component, ctx *context.
},
}

ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
for _, fileContent := range fileContents {
ports := GetPortFromFileGo(matchRegexRules, fileContent)
if len(ports) > 0 {
component.Ports = ports
return
}
}
}
19 changes: 15 additions & 4 deletions pkg/apis/enricher/framework/go/fasthttp_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (f FastHttpDetector) GetSupportedFrameworks() []string {
return []string{"FastHttp"}
}

func (f FastHttpDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
files, err := utils.GetCachedFilePathsFromRoot(componentPath, ctx)
if err != nil {
return []model.ApplicationFileInfo{}
}
return utils.GenerateApplicationFileFromFilters(files, componentPath, ".go", ctx)
}

// DoFrameworkDetection uses a tag to check for the framework name
func (f FastHttpDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/valyala/fasthttp") {
Expand All @@ -34,7 +42,7 @@ func (f FastHttpDetector) DoFrameworkDetection(language *model.Language, goMod *
}

func (f FastHttpDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
fileContents, err := utils.GetApplicationFileContents(f.GetApplicationFileInfos(component.Path, ctx))
if err != nil {
return
}
Expand All @@ -47,8 +55,11 @@ func (f FastHttpDetector) DoPortsDetection(component *model.Component, ctx *cont
},
},
}
ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
for _, fileContent := range fileContents {
ports := GetPortFromFileGo(matchRegexRules, fileContent)
if len(ports) > 0 {
component.Ports = ports
return
}
}
}
19 changes: 15 additions & 4 deletions pkg/apis/enricher/framework/go/gin_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (g GinDetector) GetSupportedFrameworks() []string {
return []string{"Gin"}
}

func (g GinDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
files, err := utils.GetCachedFilePathsFromRoot(componentPath, ctx)
if err != nil {
return []model.ApplicationFileInfo{}
}
return utils.GenerateApplicationFileFromFilters(files, componentPath, ".go", ctx)
}

// DoFrameworkDetection uses a tag to check for the framework name
func (g GinDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/gin-gonic/gin") {
Expand All @@ -34,7 +42,7 @@ func (g GinDetector) DoFrameworkDetection(language *model.Language, goMod *modfi
}

func (g GinDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
fileContents, err := utils.GetApplicationFileContents(g.GetApplicationFileInfos(component.Path, ctx))
if err != nil {
return
}
Expand All @@ -48,8 +56,11 @@ func (g GinDetector) DoPortsDetection(component *model.Component, ctx *context.C
},
}

ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
for _, fileContent := range fileContents {
ports := GetPortFromFileGo(matchRegexRules, fileContent)
if len(ports) > 0 {
component.Ports = ports
return
}
}
}
34 changes: 11 additions & 23 deletions pkg/apis/enricher/framework/go/go_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ package enricher

import (
"context"
"os"
"path/filepath"
"regexp"
"strings"

Expand All @@ -38,7 +36,11 @@ func DoGoPortsDetection(component *model.Component, ctx *context.Context) {
if err != nil {
return
}

appFileInfos := utils.GenerateApplicationFileFromFilters(files, component.Path, ".go", ctx)
fileContents, err := utils.GetApplicationFileContents(appFileInfos)
if err != nil {
return
}
matchRegexRules := model.PortMatchRules{
MatchIndexRegexes: []model.PortMatchRule{
{
Expand All @@ -58,9 +60,12 @@ func DoGoPortsDetection(component *model.Component, ctx *context.Context) {
},
}

ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
for _, fileContent := range fileContents {
ports := GetPortFromFileGo(matchRegexRules, fileContent)
if len(ports) > 0 {
component.Ports = ports
return
}
}
}

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

return -1
}

// GetPortFromFilesGo loops through a list of paths and tries to find a port matching the
// given set PortMatchRules
func GetPortFromFilesGo(matchRegexRules model.PortMatchRules, files []string) []int {
for _, file := range files {
cleanFile := filepath.Clean(file)
bytes, err := os.ReadFile(cleanFile)
if err != nil {
continue
}
ports := GetPortFromFileGo(matchRegexRules, string(bytes))
if len(ports) > 0 {
return ports
}
}
return []int{}
}
20 changes: 16 additions & 4 deletions pkg/apis/enricher/framework/go/gofiber_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (g GoFiberDetector) GetSupportedFrameworks() []string {
return []string{"GoFiber"}
}

func (g GoFiberDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
files, err := utils.GetCachedFilePathsFromRoot(componentPath, ctx)
if err != nil {
return []model.ApplicationFileInfo{}
}
return utils.GenerateApplicationFileFromFilters(files, componentPath, ".go", ctx)
}

// DoFrameworkDetection uses a tag to check for the framework name
func (g GoFiberDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/gofiber/fiber") {
Expand All @@ -34,7 +42,7 @@ func (g GoFiberDetector) DoFrameworkDetection(language *model.Language, goMod *m
}

func (g GoFiberDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
fileContents, err := utils.GetApplicationFileContents(g.GetApplicationFileInfos(component.Path, ctx))
if err != nil {
return
}
Expand All @@ -47,8 +55,12 @@ func (g GoFiberDetector) DoPortsDetection(component *model.Component, ctx *conte
},
},
}
ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports

for _, fileContent := range fileContents {
ports := GetPortFromFileGo(matchRegexRules, fileContent)
if len(ports) > 0 {
component.Ports = ports
return
}
}
}
19 changes: 15 additions & 4 deletions pkg/apis/enricher/framework/go/mux_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (m MuxDetector) GetSupportedFrameworks() []string {
return []string{"Mux"}
}

func (m MuxDetector) GetApplicationFileInfos(componentPath string, ctx *context.Context) []model.ApplicationFileInfo {
files, err := utils.GetCachedFilePathsFromRoot(componentPath, ctx)
if err != nil {
return []model.ApplicationFileInfo{}
}
return utils.GenerateApplicationFileFromFilters(files, componentPath, ".go", ctx)
}

// DoFrameworkDetection uses a tag to check for the framework name
func (m MuxDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/gorilla/mux") {
Expand All @@ -34,7 +42,7 @@ func (m MuxDetector) DoFrameworkDetection(language *model.Language, goMod *modfi
}

func (m MuxDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
fileContents, err := utils.GetApplicationFileContents(m.GetApplicationFileInfos(component.Path, ctx))
if err != nil {
return
}
Expand All @@ -54,8 +62,11 @@ func (m MuxDetector) DoPortsDetection(component *model.Component, ctx *context.C
},
}

ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
for _, fileContent := range fileContents {
ports := GetPortFromFileGo(matchRegexRules, fileContent)
if len(ports) > 0 {
component.Ports = ports
return
}
}
}
8 changes: 2 additions & 6 deletions pkg/apis/enricher/framework/java/java_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"regexp"
"strings"

"github.com/devfile/alizer/pkg/schema"
"github.com/devfile/alizer/pkg/utils"
)

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

// GetPortsForJBossFrameworks tries to detect any port information inside javaOpts of configuration
// of a given profiles plugin
func GetPortsForJBossFrameworks(pomFilePath, pluginArtifactId, pluginGroupId string) string {
func GetPortsForJBossFrameworks(pom schema.Pom, pluginArtifactId string, pluginGroupId string) string {
portPlaceholder := ""
pom, err := utils.GetPomFileContent(pomFilePath)
if err != nil {
return portPlaceholder
}

re := regexp.MustCompile(`jboss.https?.port=\d*`)
// Check for port configuration inside profiles
for _, profile := range pom.Profiles.Profile {
Expand Down
Loading

0 comments on commit e486794

Please sign in to comment.