Skip to content

Commit

Permalink
Convert xml rules for containerless analyze (#359)
Browse files Browse the repository at this point in the history
convert xml rules for containerless analyze

Signed-off-by: Emily McMullan <[email protected]>
  • Loading branch information
eemcmullan authored Nov 12, 2024
1 parent ccb798a commit a2026af
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 20 deletions.
19 changes: 18 additions & 1 deletion cmd/analyze-bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ func (a *analyzeCommand) RunAnalysisContainerless(ctx context.Context) error {
}
defer analysisLog.Close()

// try to convert any xml rules
xmlTempDir, err := a.ConvertXMLContainerless()
if err != nil {
a.log.Error(err, "failed to convert xml rules")
return err
}
defer os.RemoveAll(xmlTempDir)
xmlDirEmpty, err := IsXMLDirEmpty(xmlTempDir)
if err != nil {
return err
}

// clean jdtls dirs after analysis
defer func() {
if err := a.cleanlsDirs(); err != nil {
a.log.Error(err, "failed to clean language server directories")
Expand Down Expand Up @@ -119,6 +132,10 @@ func (a *analyzeCommand) RunAnalysisContainerless(ctx context.Context) error {
if a.enableDefaultRulesets {
a.rules = append(a.rules, filepath.Join(a.kantraDir, RulesetsLocation))
}
if !xmlDirEmpty {
a.rules = append(a.rules, xmlTempDir)
}

for _, f := range a.rules {
a.log.Info("parsing rules for analysis", "rules", f)

Expand Down Expand Up @@ -502,7 +519,7 @@ func (a *analyzeCommand) DependencyOutputContainerless(ctx context.Context, prov
}

if depsFlat == nil && depsTree == nil {
a.log.Info("failed to get dependencies from all given providers")
a.log.V(4).Info("did not get dependencies from all given providers")
return
}
}
Expand Down
52 changes: 52 additions & 0 deletions cmd/shimconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path"
"path/filepath"
Expand All @@ -13,6 +15,9 @@ import (
"github.com/konveyor-ecosystem/kantra/pkg/container"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"

"github.com/fabianvf/windup-rulesets-yaml/pkg/conversion"
"github.com/fabianvf/windup-rulesets-yaml/pkg/windup"
)

type windupShimCommand struct {
Expand Down Expand Up @@ -181,3 +186,50 @@ func (w *windupShimCommand) Run(ctx context.Context) error {
}
return nil
}

func (a *analyzeCommand) ConvertXMLContainerless() (string, error) {
shimLogFilePath := filepath.Join(a.output, "shim.log")
shimLog, err := os.Create(shimLogFilePath)
if err != nil {
return "", err
}
defer shimLog.Close()
os.Stdout = shimLog

tempDir, err := os.MkdirTemp("", "analyze-rules-")
if err != nil {
a.log.V(1).Error(err, "failed creating temp dir", "dir", tempDir)
return "", err
}
a.log.V(7).Info("created temp directory for xml rules", "dir", tempDir)

for _, location := range a.rules {
rulesets := []windup.Ruleset{}
ruletests := []windup.Ruletest{}
err := filepath.WalkDir(location, walkXML(location, &rulesets, &ruletests))
if err != nil {
a.log.V(1).Error(err, "failed to get get xml rule")
}
_, err = conversion.ConvertWindupRulesetsToAnalyzer(rulesets, location, tempDir, true, false)
if err != nil {
log.Fatal(err)
}
}

return tempDir, nil
}

func walkXML(root string, rulesets *[]windup.Ruleset, rulestest *[]windup.Ruletest) fs.WalkDirFunc {
return func(path string, d fs.DirEntry, err error) error {
if !strings.HasSuffix(path, ".xml") {
return nil
}
if rulesets != nil {
ruleset := windup.ProcessWindupRuleset(path)
if ruleset != nil {
*rulesets = append(*rulesets, *ruleset)
}
}
return err
}
}
15 changes: 15 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,18 @@ func listOptionsFromLabels(sl []string, label string, out io.Writer) {
fmt.Fprintln(out, tech)
}
}

func IsXMLDirEmpty(dir string) (bool, error) {
f, err := os.Open(dir)
if err != nil {
return false, err
}
defer f.Close()

_, err = f.Readdir(1)
if err == io.EOF {
return true, nil
}

return false, err
}
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ require (
go.opentelemetry.io/otel/trace v1.22.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1

)

require (
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/antchfx/xpath v1.3.1 // indirect
github.com/bufbuild/protocompile v0.10.0 // indirect
github.com/cbroglie/mustache v1.3.0 // indirect
github.com/cbroglie/mustache v1.4.0 // indirect
github.com/containerd/typeurl/v2 v2.1.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/invopop/yaml v0.1.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/jhump/protoreflect v1.16.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/moby/buildkit v0.14.1 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
Expand Down Expand Up @@ -69,6 +69,7 @@ require (
github.com/antchfx/xmlquery v1.4.1 // indirect
github.com/bombsimon/logrusr/v3 v3.1.0
github.com/codingconcepts/env v0.0.0-20200821220118-a8fbf8d84482
github.com/fabianvf/windup-rulesets-yaml v0.5.3
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/konveyor/analyzer-lsp v0.5.3
github.com/konveyor/analyzer-lsp/external-providers/java-external-provider v0.0.0-20241107203043-172d684c168d
Expand Down
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ github.com/bool64/shared v0.1.5 h1:fp3eUhBsrSjNCQPcSdQqZxxh9bBwrYiZ+zOKFkM0/2E=
github.com/bool64/shared v0.1.5/go.mod h1:081yz68YC9jeFB3+Bbmno2RFWvGKv1lPKkMP6MHJlPs=
github.com/bufbuild/protocompile v0.10.0 h1:+jW/wnLMLxaCEG8AX9lD0bQ5v9h1RUiMKOBOT5ll9dM=
github.com/bufbuild/protocompile v0.10.0/go.mod h1:G9qQIQo0xZ6Uyj6CMNz0saGmx2so+KONo8/KrELABiY=
github.com/cbroglie/mustache v1.3.0 h1:sj24GVYl8G7MH4b3zaROGsZnF8X79JqtjMx8/6H/nXM=
github.com/cbroglie/mustache v1.3.0/go.mod h1:w58RIHjw/L7DPyRX2CcCTduNmcP1dvztaHP72ciSfh0=
github.com/cbroglie/mustache v1.4.0 h1:Azg0dVhxTml5me+7PsZ7WPrQq1Gkf3WApcHMjMprYoU=
github.com/cbroglie/mustache v1.4.0/go.mod h1:SS1FTIghy0sjse4DUVGV1k/40B1qE1XkD9DtDsHo9iM=
github.com/codingconcepts/env v0.0.0-20200821220118-a8fbf8d84482 h1:5/aEFreBh9hH/0G+33xtczJCvMaulqsm9nDuu2BZUEo=
github.com/codingconcepts/env v0.0.0-20200821220118-a8fbf8d84482/go.mod h1:TM9ug+H/2cI3EjyIDr5xKCkFGyNE59URgH1wu5NyU8E=
github.com/containerd/typeurl/v2 v2.1.1 h1:3Q4Pt7i8nYwy2KmQWIw2+1hTvwTE/6w9FqcttATPO/4=
github.com/containerd/typeurl/v2 v2.1.1/go.mod h1:IDp2JFvbwZ31H8dQbEIY7sDl2L3o3HZj1hsSQlywkQ0=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devfile/alizer v1.6.1 h1:5G0SHFAcFNTwqsaZu5pir2pBJ04hg/F6d/lfQeAs7YY=
github.com/devfile/alizer v1.6.1/go.mod h1:RHcz/nRPIlHNIfG3wTmVLrS8IyM+4CB841S/WKFXDiw=
github.com/fabianvf/windup-rulesets-yaml v0.5.3 h1:CiOdTNIggt2NHwSpuCAJWKO5/X7t+Hq9+qXZ7lziGK8=
github.com/fabianvf/windup-rulesets-yaml v0.5.3/go.mod h1:rv/Pzx5zMXKHbexpa8AeAI91Cb8Mw532jmx4HkgjGOc=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
Expand All @@ -46,11 +47,11 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre
github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ=
github.com/go-logr/zapr v1.3.0/go.mod h1:YKepepNBd1u/oyhd/yQmtjVXmm9uML4IXUgMOwR8/Gg=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g=
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicbw=
github.com/go-openapi/swag v0.22.8/go.mod h1:6QT22icPLEqAM/z/TChgb4WAveCHF92+2gF0CNjHpPI=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
Expand All @@ -72,8 +73,9 @@ github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJ
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/invopop/yaml v0.1.0 h1:YW3WGUoJEXYfzWBjn00zIlrw7brGVD0fUKRYDPAPhrc=
github.com/invopop/yaml v0.1.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/jhump/protoreflect v1.16.0 h1:54fZg+49widqXYQ0b+usAFHbMkBGR4PpXrsHc8+TBDg=
github.com/jhump/protoreflect v1.16.0/go.mod h1:oYPd7nPvcBw/5wlDfm/AVmU9zH9BgqGCI469pGxfj/8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand All @@ -85,9 +87,8 @@ github.com/konveyor/analyzer-lsp v0.5.3/go.mod h1:Gqj6MRUA2Jjjw19tUguJB6Bqj4dLwZ
github.com/konveyor/analyzer-lsp/external-providers/java-external-provider v0.0.0-20241107203043-172d684c168d h1:P9hRAD/k1cTdOYJBVDuHESSUIbPsXS3hB3T0msSIryA=
github.com/konveyor/analyzer-lsp/external-providers/java-external-provider v0.0.0-20241107203043-172d684c168d/go.mod h1:0OaNuH9HgJZbXPKmblcAfJ+4qyuS5Rc0dp3kmMvjtKk=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -108,8 +109,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
Expand Down Expand Up @@ -245,7 +246,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down

0 comments on commit a2026af

Please sign in to comment.