From 2ada8c2660f6a446b9ab39131a285fed4766beed Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 2 May 2024 09:02:32 +0300 Subject: [PATCH 01/69] update gpt-wrapper to 1.0.0 --- go.mod | 8 ++++---- go.sum | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index d7e07d698..305a74769 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.22.2 require ( github.com/MakeNowJust/heredoc v1.0.0 - github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c + github.com/checkmarxDev/gpt-wrapper v1.0.0 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 @@ -27,7 +27,7 @@ require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect - github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect @@ -38,8 +38,8 @@ require ( github.com/subosito/gotenv v1.6.0 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect go.uber.org/atomic v1.9.0 // indirect - go.uber.org/multierr v1.9.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect golang.org/x/sys v0.19.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 4d6ca44d0..e2758a255 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c h1:oKI4C1dXYpi0B8pltDDzp1ZRiyeILv5enbp9h4ASQ3s= github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c/go.mod h1:l+0rISRGaps2HWkpvKbYPE1nsNx28vBj6bKorEm1M5o= +github.com/checkmarxDev/gpt-wrapper v1.0.0 h1:fGcS8fhoj7DQa4mpIXZgAkcwQgCSBewDIpr1CrLjZQM= +github.com/checkmarxDev/gpt-wrapper v1.0.0/go.mod h1:OUWAPguJcW9mfE3jlt+izj5ZzejRfW0DiO8juAvukQU= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -39,6 +41,7 @@ github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQw github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= 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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -66,6 +69,7 @@ github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMV github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -82,10 +86,12 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ= golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= From 9c8ba1cb11ea661607a76027037959434ae96a48 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 12 May 2024 15:04:24 +0300 Subject: [PATCH 02/69] send requests to AzureAI using gen ai wrapper 1.0.1 --- .golangci.yml | 2 +- go.mod | 10 +++++---- go.sum | 31 +++++++++++++------------- internal/commands/chat-kics.go | 33 ++++++++++++++++++++++------ internal/commands/chat-sast.go | 26 ++++++++++++++++------ internal/commands/util/maskSecret.go | 4 ++-- internal/params/flags.go | 1 + internal/wrappers/chat-http.go | 6 ++--- internal/wrappers/chat.go | 6 ++--- internal/wrappers/mock/chat-mock.go | 8 +++---- 10 files changed, 80 insertions(+), 47 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index ba0a3c767..5b883709b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,7 +8,7 @@ linters-settings: - $gostd - github.com/checkmarx/ast-cli/internal - github.com/spf13/viper - - github.com/checkmarxDev/gpt-wrapper + - github.com/Checkmarx/gen-ai-wrapper - github.com/spf13/cobra - github.com/pkg/errors - github.com/google diff --git a/go.mod b/go.mod index 305a74769..aadd4f6c3 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/checkmarx/ast-cli go 1.22.2 require ( + github.com/Checkmarx/gen-ai-wrapper v1.0.1 github.com/MakeNowJust/heredoc v1.0.0 - github.com/checkmarxDev/gpt-wrapper v1.0.0 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 @@ -22,7 +22,7 @@ require ( require ( github.com/fsnotify/fsnotify v1.7.0 // indirect - github.com/google/go-cmp v0.5.9 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/magiconair/properties v1.8.7 // indirect @@ -34,13 +34,15 @@ require ( github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/stretchr/testify v1.9.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect - go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect + golang.org/x/net v0.22.0 // indirect golang.org/x/sys v0.19.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa // indirect + google.golang.org/grpc v1.63.2 // indirect + google.golang.org/protobuf v1.33.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index e2758a255..e20f853f5 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,7 @@ +github.com/Checkmarx/gen-ai-wrapper v1.0.1 h1:MwIndXVTqFLwrhjbaXO1FqpgvExgZ41mkLf/dujqoQw= +github.com/Checkmarx/gen-ai-wrapper v1.0.1/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= -github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c h1:oKI4C1dXYpi0B8pltDDzp1ZRiyeILv5enbp9h4ASQ3s= -github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c/go.mod h1:l+0rISRGaps2HWkpvKbYPE1nsNx28vBj6bKorEm1M5o= -github.com/checkmarxDev/gpt-wrapper v1.0.0 h1:fGcS8fhoj7DQa4mpIXZgAkcwQgCSBewDIpr1CrLjZQM= -github.com/checkmarxDev/gpt-wrapper v1.0.0/go.mod h1:OUWAPguJcW9mfE3jlt+izj5ZzejRfW0DiO8juAvukQU= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -17,8 +15,8 @@ github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keL github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 h1:EcQR3gusLHN46TAD+G+EbaaqJArt5vHhNpXAa12PQf4= github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -39,8 +37,7 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4= github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw= -github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= -github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeBAsoHo= github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -70,7 +67,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= @@ -82,21 +78,24 @@ github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJ github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80/go.mod h1:iFyPdL66DjUD96XmzVL3ZntbzcflLnznH0fr99w5VqE= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= -go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= -go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= -go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw= golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ= -golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= +golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa h1:RBgMaUMP+6soRkik4VoN8ojR2nex2TqZwjSSogic+eo= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= +google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= +google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 25e2461af..633813120 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -4,14 +4,14 @@ import ( "fmt" "os" + "github.com/Checkmarx/gen-ai-wrapper/pkg/connector" + "github.com/Checkmarx/gen-ai-wrapper/pkg/message" + "github.com/Checkmarx/gen-ai-wrapper/pkg/role" + "github.com/Checkmarx/gen-ai-wrapper/pkg/wrapper" "github.com/checkmarx/ast-cli/internal/commands/util/printer" "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" "github.com/checkmarx/ast-cli/internal/wrappers" - "github.com/checkmarxDev/gpt-wrapper/pkg/connector" - "github.com/checkmarxDev/gpt-wrapper/pkg/message" - "github.com/checkmarxDev/gpt-wrapper/pkg/role" - "github.com/checkmarxDev/gpt-wrapper/pkg/wrapper" "github.com/google/uuid" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -39,8 +39,13 @@ const userInputFormat = `The user question is: // dropLen number of messages to drop when limit is reached, 4 due to 2 from prompt, 1 from user question, 1 from reply const dropLen = 4 +// chatModel model to use when calling the Checkmarx AzureAI +const azureAiChatModel = "GPT4" + const FileErrorFormat = "It seems that %s is not available for AI Guided Remediation. Please ensure that you have opened the correct workspace or the relevant file." +const azureAiRoute = "/api/ai-proxy" + type OutputModel struct { ConversationID string `json:"conversationId"` Response []string `json:"response"` @@ -59,6 +64,7 @@ func ChatKicsSubCommand(chatWrapper wrappers.ChatWrapper) *cobra.Command { chatKicsCmd.Flags().String(params.ChatConversationID, "", "ID of existing conversation") chatKicsCmd.Flags().String(params.ChatUserInput, "", "User question") chatKicsCmd.Flags().String(params.ChatModel, "", "OpenAI model version") + chatKicsCmd.Flags().Bool(params.ChatAzureAI, false, "Use Azure AI") chatKicsCmd.Flags().String(params.ChatKicsResultFile, "", "IaC result code file") chatKicsCmd.Flags().String(params.ChatKicsResultLine, "", "IaC result line") chatKicsCmd.Flags().String(params.ChatKicsResultSeverity, "", "IaC result severity") @@ -76,16 +82,29 @@ func ChatKicsSubCommand(chatWrapper wrappers.ChatWrapper) *cobra.Command { func runChatKics(chatKicsWrapper wrappers.ChatWrapper) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { - chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) chatConversationID, _ := cmd.Flags().GetString(params.ChatConversationID) - chatModel, _ := cmd.Flags().GetString(params.ChatModel) + + chatAzureAI, _ := cmd.Flags().GetBool(params.ChatAzureAI) chatResultFile, _ := cmd.Flags().GetString(params.ChatKicsResultFile) chatResultLine, _ := cmd.Flags().GetString(params.ChatKicsResultLine) chatResultSeverity, _ := cmd.Flags().GetString(params.ChatKicsResultSeverity) chatResultVulnerability, _ := cmd.Flags().GetString(params.ChatKicsResultVulnerability) userInput, _ := cmd.Flags().GetString(params.ChatUserInput) - statefulWrapper := wrapper.NewStatefulWrapper(connector.NewFileSystemConnector(""), chatAPIKey, chatModel, dropLen, 0) + conn := connector.NewFileSystemConnector("") + + var statefulWrapper wrapper.StatefulWrapper + + if chatAzureAI { + customerToken, _ := wrappers.GetAccessToken() + azureAiEndPoint, _ := wrappers.GetURL(azureAiRoute, customerToken) + + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, azureAiEndPoint, customerToken, azureAiChatModel, dropLen, 0) + } else { + chatModel, _ := cmd.Flags().GetString(params.ChatModel) + chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) + statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) + } if chatConversationID == "" { chatConversationID = statefulWrapper.GenerateId().String() diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 88ceb623f..14ef6c043 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -4,14 +4,14 @@ import ( "fmt" "strconv" + "github.com/Checkmarx/gen-ai-wrapper/pkg/connector" + "github.com/Checkmarx/gen-ai-wrapper/pkg/message" + "github.com/Checkmarx/gen-ai-wrapper/pkg/role" + "github.com/Checkmarx/gen-ai-wrapper/pkg/wrapper" "github.com/checkmarx/ast-cli/internal/commands/util/printer" "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" "github.com/checkmarx/ast-cli/internal/wrappers" - "github.com/checkmarxDev/gpt-wrapper/pkg/connector" - "github.com/checkmarxDev/gpt-wrapper/pkg/message" - "github.com/checkmarxDev/gpt-wrapper/pkg/role" - "github.com/checkmarxDev/gpt-wrapper/pkg/wrapper" "github.com/google/uuid" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -35,6 +35,7 @@ func ChatSastSubCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers chatSastCmd.Flags().String(params.ChatConversationID, "", "ID of existing conversation") chatSastCmd.Flags().String(params.ChatUserInput, "", "User question") chatSastCmd.Flags().String(params.ChatModel, "", "OpenAI model version") + chatSastCmd.Flags().Bool(params.ChatAzureAI, false, "Use Azure AI") chatSastCmd.Flags().String(params.ChatSastScanResultsFile, "", "Results file in JSON format containing SAST scan results") chatSastCmd.Flags().String(params.ChatSastSourceDir, "", "Source code root directory relevant for the results file") chatSastCmd.Flags().String(params.ChatSastResultID, "", "ID of the result to remediate") @@ -52,14 +53,25 @@ func runChatSast(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.Tenant if !isAiGuidedRemediationEnabled(tenantWrapper) { return outputError(cmd, uuid.Nil, errors.Errorf(AiGuidedRemediationDisabledError)) } - chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) chatConversationID, _ := cmd.Flags().GetString(params.ChatConversationID) - chatModel, _ := cmd.Flags().GetString(params.ChatModel) + chatAzureAI, _ := cmd.Flags().GetBool(params.ChatAzureAI) scanResultsFile, _ := cmd.Flags().GetString(params.ChatSastScanResultsFile) sourceDir, _ := cmd.Flags().GetString(params.ChatSastSourceDir) sastResultID, _ := cmd.Flags().GetString(params.ChatSastResultID) - statefulWrapper := wrapper.NewStatefulWrapper(connector.NewFileSystemConnector(""), chatAPIKey, chatModel, dropLen, 0) + conn := connector.NewFileSystemConnector("") + + var statefulWrapper wrapper.StatefulWrapper + if chatAzureAI { + customerToken, _ := wrappers.GetAccessToken() + azureAiEndPoint, _ := wrappers.GetURL(azureAiRoute, customerToken) + + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, azureAiEndPoint, customerToken, azureAiChatModel, dropLen, 0) + } else { + chatModel, _ := cmd.Flags().GetString(params.ChatModel) + chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) + statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) + } newConversation := false var userInput string diff --git a/internal/commands/util/maskSecret.go b/internal/commands/util/maskSecret.go index ef894bf09..99ad8db06 100644 --- a/internal/commands/util/maskSecret.go +++ b/internal/commands/util/maskSecret.go @@ -3,12 +3,12 @@ package util import ( "os" + "github.com/Checkmarx/gen-ai-wrapper/pkg/connector" + "github.com/Checkmarx/gen-ai-wrapper/pkg/wrapper" "github.com/MakeNowJust/heredoc" "github.com/checkmarx/ast-cli/internal/commands/util/printer" "github.com/checkmarx/ast-cli/internal/params" "github.com/checkmarx/ast-cli/internal/wrappers" - "github.com/checkmarxDev/gpt-wrapper/pkg/connector" - "github.com/checkmarxDev/gpt-wrapper/pkg/wrapper" "github.com/pkg/errors" "github.com/spf13/cobra" ) diff --git a/internal/params/flags.go b/internal/params/flags.go index 4d3c6b707..6b46825db 100644 --- a/internal/params/flags.go +++ b/internal/params/flags.go @@ -168,6 +168,7 @@ const ( ChatConversationID = "conversation-id" ChatUserInput = "user-input" ChatModel = "model" + ChatAzureAI = "azure-ai" // Chat (Kics) ChatKicsResultFile = "result-file" diff --git a/internal/wrappers/chat-http.go b/internal/wrappers/chat-http.go index 35c6f70cb..6d27af1ef 100644 --- a/internal/wrappers/chat-http.go +++ b/internal/wrappers/chat-http.go @@ -1,9 +1,9 @@ package wrappers import ( - gptWrapperMaskedSecret "github.com/checkmarxDev/gpt-wrapper/pkg/maskedSecret" - gptWrapperMessage "github.com/checkmarxDev/gpt-wrapper/pkg/message" - gptWrapper "github.com/checkmarxDev/gpt-wrapper/pkg/wrapper" + gptWrapperMaskedSecret "github.com/Checkmarx/gen-ai-wrapper/pkg/maskedSecret" + gptWrapperMessage "github.com/Checkmarx/gen-ai-wrapper/pkg/message" + gptWrapper "github.com/Checkmarx/gen-ai-wrapper/pkg/wrapper" "github.com/google/uuid" ) diff --git a/internal/wrappers/chat.go b/internal/wrappers/chat.go index 3cdb62f25..087e2afcb 100644 --- a/internal/wrappers/chat.go +++ b/internal/wrappers/chat.go @@ -1,9 +1,9 @@ package wrappers import ( - gptWrapperMaskedSecret "github.com/checkmarxDev/gpt-wrapper/pkg/maskedSecret" - gptWrapperMessage "github.com/checkmarxDev/gpt-wrapper/pkg/message" - gptWrapper "github.com/checkmarxDev/gpt-wrapper/pkg/wrapper" + gptWrapperMaskedSecret "github.com/Checkmarx/gen-ai-wrapper/pkg/maskedSecret" + gptWrapperMessage "github.com/Checkmarx/gen-ai-wrapper/pkg/message" + gptWrapper "github.com/Checkmarx/gen-ai-wrapper/pkg/wrapper" "github.com/google/uuid" ) diff --git a/internal/wrappers/mock/chat-mock.go b/internal/wrappers/mock/chat-mock.go index ad3540828..96d2d8941 100644 --- a/internal/wrappers/mock/chat-mock.go +++ b/internal/wrappers/mock/chat-mock.go @@ -1,10 +1,10 @@ package mock import ( - gptWrapperMaskedSecret "github.com/checkmarxDev/gpt-wrapper/pkg/maskedSecret" - gptWrapperMessage "github.com/checkmarxDev/gpt-wrapper/pkg/message" - gptWrapperRole "github.com/checkmarxDev/gpt-wrapper/pkg/role" - gptWrapper "github.com/checkmarxDev/gpt-wrapper/pkg/wrapper" + gptWrapperMaskedSecret "github.com/Checkmarx/gen-ai-wrapper/pkg/maskedSecret" + gptWrapperMessage "github.com/Checkmarx/gen-ai-wrapper/pkg/message" + gptWrapperRole "github.com/Checkmarx/gen-ai-wrapper/pkg/role" + gptWrapper "github.com/Checkmarx/gen-ai-wrapper/pkg/wrapper" "github.com/google/uuid" ) From 2cd4edcbb87040b939ffc57a452bbbc45c67966e Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Sun, 26 May 2024 14:07:15 +0300 Subject: [PATCH 03/69] change names to checkmarx ai --- internal/commands/chat-kics.go | 19 +++++++++---------- internal/commands/chat-sast.go | 12 ++++++------ internal/params/flags.go | 2 +- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 633813120..30e123109 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -39,12 +39,11 @@ const userInputFormat = `The user question is: // dropLen number of messages to drop when limit is reached, 4 due to 2 from prompt, 1 from user question, 1 from reply const dropLen = 4 -// chatModel model to use when calling the Checkmarx AzureAI -const azureAiChatModel = "GPT4" - const FileErrorFormat = "It seems that %s is not available for AI Guided Remediation. Please ensure that you have opened the correct workspace or the relevant file." -const azureAiRoute = "/api/ai-proxy" +// chatModel model to use when calling the CheckmarxAI +const checkmarxAiChatModel = "GPT4" +const checkmarxAiRoute = "/api/ai-proxy" type OutputModel struct { ConversationID string `json:"conversationId"` @@ -64,7 +63,7 @@ func ChatKicsSubCommand(chatWrapper wrappers.ChatWrapper) *cobra.Command { chatKicsCmd.Flags().String(params.ChatConversationID, "", "ID of existing conversation") chatKicsCmd.Flags().String(params.ChatUserInput, "", "User question") chatKicsCmd.Flags().String(params.ChatModel, "", "OpenAI model version") - chatKicsCmd.Flags().Bool(params.ChatAzureAI, false, "Use Azure AI") + chatKicsCmd.Flags().Bool(params.ChatCheckmarxAI, false, "Use Checkmarx AI") chatKicsCmd.Flags().String(params.ChatKicsResultFile, "", "IaC result code file") chatKicsCmd.Flags().String(params.ChatKicsResultLine, "", "IaC result line") chatKicsCmd.Flags().String(params.ChatKicsResultSeverity, "", "IaC result severity") @@ -84,24 +83,24 @@ func runChatKics(chatKicsWrapper wrappers.ChatWrapper) func(cmd *cobra.Command, return func(cmd *cobra.Command, args []string) error { chatConversationID, _ := cmd.Flags().GetString(params.ChatConversationID) - chatAzureAI, _ := cmd.Flags().GetBool(params.ChatAzureAI) + chatCheckmarxAI, _ := cmd.Flags().GetBool(params.ChatCheckmarxAI) chatResultFile, _ := cmd.Flags().GetString(params.ChatKicsResultFile) chatResultLine, _ := cmd.Flags().GetString(params.ChatKicsResultLine) chatResultSeverity, _ := cmd.Flags().GetString(params.ChatKicsResultSeverity) chatResultVulnerability, _ := cmd.Flags().GetString(params.ChatKicsResultVulnerability) userInput, _ := cmd.Flags().GetString(params.ChatUserInput) + chatModel, _ := cmd.Flags().GetString(params.ChatModel) conn := connector.NewFileSystemConnector("") var statefulWrapper wrapper.StatefulWrapper - if chatAzureAI { + if chatCheckmarxAI { customerToken, _ := wrappers.GetAccessToken() - azureAiEndPoint, _ := wrappers.GetURL(azureAiRoute, customerToken) + azureAiEndPoint, _ := wrappers.GetURL(checkmarxAiRoute, customerToken) - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, azureAiEndPoint, customerToken, azureAiChatModel, dropLen, 0) + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, azureAiEndPoint, customerToken, checkmarxAiChatModel, dropLen, 0) } else { - chatModel, _ := cmd.Flags().GetString(params.ChatModel) chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) } diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 14ef6c043..ee2280720 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -35,7 +35,7 @@ func ChatSastSubCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers chatSastCmd.Flags().String(params.ChatConversationID, "", "ID of existing conversation") chatSastCmd.Flags().String(params.ChatUserInput, "", "User question") chatSastCmd.Flags().String(params.ChatModel, "", "OpenAI model version") - chatSastCmd.Flags().Bool(params.ChatAzureAI, false, "Use Azure AI") + chatSastCmd.Flags().Bool(params.ChatCheckmarxAI, false, "Use Checkmarx AI") chatSastCmd.Flags().String(params.ChatSastScanResultsFile, "", "Results file in JSON format containing SAST scan results") chatSastCmd.Flags().String(params.ChatSastSourceDir, "", "Source code root directory relevant for the results file") chatSastCmd.Flags().String(params.ChatSastResultID, "", "ID of the result to remediate") @@ -54,21 +54,21 @@ func runChatSast(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.Tenant return outputError(cmd, uuid.Nil, errors.Errorf(AiGuidedRemediationDisabledError)) } chatConversationID, _ := cmd.Flags().GetString(params.ChatConversationID) - chatAzureAI, _ := cmd.Flags().GetBool(params.ChatAzureAI) + chatCheckmarxAI, _ := cmd.Flags().GetBool(params.ChatCheckmarxAI) scanResultsFile, _ := cmd.Flags().GetString(params.ChatSastScanResultsFile) sourceDir, _ := cmd.Flags().GetString(params.ChatSastSourceDir) sastResultID, _ := cmd.Flags().GetString(params.ChatSastResultID) + chatModel, _ := cmd.Flags().GetString(params.ChatModel) conn := connector.NewFileSystemConnector("") var statefulWrapper wrapper.StatefulWrapper - if chatAzureAI { + if chatCheckmarxAI { customerToken, _ := wrappers.GetAccessToken() - azureAiEndPoint, _ := wrappers.GetURL(azureAiRoute, customerToken) + azureAiEndPoint, _ := wrappers.GetURL(checkmarxAiRoute, customerToken) + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, azureAiEndPoint, customerToken, checkmarxAiChatModel, dropLen, 0) - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, azureAiEndPoint, customerToken, azureAiChatModel, dropLen, 0) } else { - chatModel, _ := cmd.Flags().GetString(params.ChatModel) chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) } diff --git a/internal/params/flags.go b/internal/params/flags.go index 50dcf954e..c376f78f4 100644 --- a/internal/params/flags.go +++ b/internal/params/flags.go @@ -169,7 +169,7 @@ const ( ChatConversationID = "conversation-id" ChatUserInput = "user-input" ChatModel = "model" - ChatAzureAI = "azure-ai" + ChatCheckmarxAI = "checkmarx-ai" // Chat (Kics) ChatKicsResultFile = "result-file" From 184d080e64b567e6c9a216c854cb14f38eb6426a Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Mon, 27 May 2024 14:52:37 +0300 Subject: [PATCH 04/69] fix linter --- internal/commands/chat-sast.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index ee2280720..2ad5975dd 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -67,7 +67,6 @@ func runChatSast(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.Tenant customerToken, _ := wrappers.GetAccessToken() azureAiEndPoint, _ := wrappers.GetURL(checkmarxAiRoute, customerToken) statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, azureAiEndPoint, customerToken, checkmarxAiChatModel, dropLen, 0) - } else { chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) From 1877d2fc6c84ddba1dce278cf49d6facdfabafdd Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Mon, 27 May 2024 14:55:55 +0300 Subject: [PATCH 05/69] update net package --- go.mod | 3 ++- go.sum | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d6aa0baf1..04bbaa732 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( golang.org/x/crypto v0.22.0 golang.org/x/text v0.14.0 gotest.tools v2.2.0+incompatible + ) require ( @@ -41,7 +42,7 @@ require ( github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect - golang.org/x/net v0.22.0 // indirect + golang.org/x/net v0.23.0 // indirect golang.org/x/sys v0.19.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa // indirect google.golang.org/grpc v1.63.2 // indirect diff --git a/go.sum b/go.sum index e20f853f5..9115e64ad 100644 --- a/go.sum +++ b/go.sum @@ -84,8 +84,8 @@ golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw= golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ= -golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= -golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= From 0f475f4712d127a5e7b7e92d08d438009c11b823 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Mon, 27 May 2024 15:05:04 +0300 Subject: [PATCH 06/69] fix variable name --- internal/commands/chat-kics.go | 4 ++-- internal/commands/chat-sast.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 30e123109..836d6c7ae 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -97,9 +97,9 @@ func runChatKics(chatKicsWrapper wrappers.ChatWrapper) func(cmd *cobra.Command, if chatCheckmarxAI { customerToken, _ := wrappers.GetAccessToken() - azureAiEndPoint, _ := wrappers.GetURL(checkmarxAiRoute, customerToken) + CheckmarxAiEndPoint, _ := wrappers.GetURL(checkmarxAiRoute, customerToken) - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, azureAiEndPoint, customerToken, checkmarxAiChatModel, dropLen, 0) + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, CheckmarxAiEndPoint, customerToken, checkmarxAiChatModel, dropLen, 0) } else { chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 2ad5975dd..c61da9d5c 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -65,8 +65,8 @@ func runChatSast(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.Tenant var statefulWrapper wrapper.StatefulWrapper if chatCheckmarxAI { customerToken, _ := wrappers.GetAccessToken() - azureAiEndPoint, _ := wrappers.GetURL(checkmarxAiRoute, customerToken) - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, azureAiEndPoint, customerToken, checkmarxAiChatModel, dropLen, 0) + CheckmarxAiEndPoint, _ := wrappers.GetURL(checkmarxAiRoute, customerToken) + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, CheckmarxAiEndPoint, customerToken, checkmarxAiChatModel, dropLen, 0) } else { chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) From 7d7a3f31ddb7d8551127aaea9e29726bef34b2ae Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Wed, 5 Jun 2024 10:59:38 +0300 Subject: [PATCH 07/69] update code to latest gpt wrapper --- go.mod | 2 +- go.sum | 4 + internal/commands/chat-kics.go | 79 ++++++++++++---- internal/commands/chat-sast.go | 121 ++++++++++++++++++++---- internal/commands/chat.go | 11 ++- internal/params/flags.go | 1 - internal/wrappers/chat-http.go | 7 ++ internal/wrappers/chat.go | 1 + internal/wrappers/client.go | 7 +- internal/wrappers/feature-flags-http.go | 2 +- internal/wrappers/mock/chat-mock.go | 10 ++ 11 files changed, 200 insertions(+), 45 deletions(-) diff --git a/go.mod b/go.mod index 04bbaa732..b35425882 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/checkmarx/ast-cli go 1.22.3 require ( - github.com/Checkmarx/gen-ai-wrapper v1.0.1 + github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352 github.com/MakeNowJust/heredoc v1.0.0 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 diff --git a/go.sum b/go.sum index 9115e64ad..e3a003608 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ github.com/Checkmarx/gen-ai-wrapper v1.0.1 h1:MwIndXVTqFLwrhjbaXO1FqpgvExgZ41mkLf/dujqoQw= github.com/Checkmarx/gen-ai-wrapper v1.0.1/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604074408-3a640add0c20 h1:9okGTw//C0y01pyzy7ISP/0SiOH6G1PzG4kEPHWx3aA= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604074408-3a640add0c20/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352 h1:jxRmRax3Stz7+64Rh83u4rQDa1duYcfsNrUdk9wxyFU= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 836d6c7ae..998086bdf 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -43,27 +43,29 @@ const FileErrorFormat = "It seems that %s is not available for AI Guided Remedia // chatModel model to use when calling the CheckmarxAI const checkmarxAiChatModel = "GPT4" -const checkmarxAiRoute = "/api/ai-proxy" +const aiProxyRoute = "/api/ai-proxy" +const tenantIDClaimKey = "tenant_id" +const guidedRemediationFeatureNameKics = "cli-guided-remediation-kics" +const guidedRemediationFeatureNameSast = "cli-guided-remediation-sast" type OutputModel struct { ConversationID string `json:"conversationId"` Response []string `json:"response"` } -func ChatKicsSubCommand(chatWrapper wrappers.ChatWrapper) *cobra.Command { +func ChatKicsSubCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) *cobra.Command { chatKicsCmd := &cobra.Command{ Use: "kics", Short: "Chat about KICS result with OpenAI models", Long: "Chat about KICS result with OpenAI models", Hidden: true, - RunE: runChatKics(chatWrapper), + RunE: runChatKics(chatWrapper, tenantWrapper), } chatKicsCmd.Flags().String(params.ChatAPIKey, "", "OpenAI API key") chatKicsCmd.Flags().String(params.ChatConversationID, "", "ID of existing conversation") chatKicsCmd.Flags().String(params.ChatUserInput, "", "User question") chatKicsCmd.Flags().String(params.ChatModel, "", "OpenAI model version") - chatKicsCmd.Flags().Bool(params.ChatCheckmarxAI, false, "Use Checkmarx AI") chatKicsCmd.Flags().String(params.ChatKicsResultFile, "", "IaC result code file") chatKicsCmd.Flags().String(params.ChatKicsResultLine, "", "IaC result line") chatKicsCmd.Flags().String(params.ChatKicsResultSeverity, "", "IaC result severity") @@ -79,32 +81,40 @@ func ChatKicsSubCommand(chatWrapper wrappers.ChatWrapper) *cobra.Command { return chatKicsCmd } -func runChatKics(chatKicsWrapper wrappers.ChatWrapper) func(cmd *cobra.Command, args []string) error { +func runChatKics( + chatKicsWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper, +) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { chatConversationID, _ := cmd.Flags().GetString(params.ChatConversationID) - - chatCheckmarxAI, _ := cmd.Flags().GetBool(params.ChatCheckmarxAI) chatResultFile, _ := cmd.Flags().GetString(params.ChatKicsResultFile) chatResultLine, _ := cmd.Flags().GetString(params.ChatKicsResultLine) chatResultSeverity, _ := cmd.Flags().GetString(params.ChatKicsResultSeverity) chatResultVulnerability, _ := cmd.Flags().GetString(params.ChatKicsResultVulnerability) userInput, _ := cmd.Flags().GetString(params.ChatUserInput) - chatModel, _ := cmd.Flags().GetString(params.ChatModel) + + azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantWrapper) + checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantWrapper) conn := connector.NewFileSystemConnector("") var statefulWrapper wrapper.StatefulWrapper - - if chatCheckmarxAI { - customerToken, _ := wrappers.GetAccessToken() - CheckmarxAiEndPoint, _ := wrappers.GetURL(checkmarxAiRoute, customerToken) - - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, CheckmarxAiEndPoint, customerToken, checkmarxAiChatModel, dropLen, 0) + customerToken, _ := wrappers.GetAccessToken() + tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) + + if azureAiEnabled || checkmarxAiEnabled { + aiProxyEndPoint, _ := wrappers.GetURL(aiProxyRoute, customerToken) + var model string + if azureAiEnabled { + model, _ = GetAzureAiModel(tenantWrapper) + } else { + model = checkmarxAiChatModel + } + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) // todo: check final interface } else { + chatModel, _ := cmd.Flags().GetString(params.ChatModel) chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) } - if chatConversationID == "" { chatConversationID = statefulWrapper.GenerateId().String() } @@ -122,9 +132,42 @@ func runChatKics(chatKicsWrapper wrappers.ChatWrapper) func(cmd *cobra.Command, } newMessages := buildMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) - response, err := chatKicsWrapper.Call(statefulWrapper, id, newMessages) - if err != nil { - return outputError(cmd, id, err) + + var response []message.Message + if azureAiEnabled { + azureAiEndPoint, _ := GetAzureAiEndPoint(tenantWrapper) + azureAiApiKey, _ := GetAzureAiAPIKey(tenantWrapper) + metadata := message.MetaData{ + TenantID: tenantID, + RequestID: "???", + UserAgent: params.DefaultAgent, + Feature: guidedRemediationFeatureNameKics, + ExternalModel: &message.ExternalAzure{ + Endpoint: azureAiEndPoint, + ApiKey: azureAiApiKey, + }, + } + response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) + if err != nil { + return outputError(cmd, id, err) + } + } else if checkmarxAiEnabled { + metadata := message.MetaData{ + TenantID: tenantID, + RequestID: "???", + UserAgent: params.DefaultAgent, + Feature: guidedRemediationFeatureNameKics, + ExternalModel: nil, + } + response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) + if err != nil { + return outputError(cmd, id, err) + } + } else { + response, err = chatKicsWrapper.Call(statefulWrapper, id, newMessages) + if err != nil { + return outputError(cmd, id, err) + } } responseContent := getMessageContents(response) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index c61da9d5c..7be66e3bf 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -35,7 +35,6 @@ func ChatSastSubCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers chatSastCmd.Flags().String(params.ChatConversationID, "", "ID of existing conversation") chatSastCmd.Flags().String(params.ChatUserInput, "", "User question") chatSastCmd.Flags().String(params.ChatModel, "", "OpenAI model version") - chatSastCmd.Flags().Bool(params.ChatCheckmarxAI, false, "Use Checkmarx AI") chatSastCmd.Flags().String(params.ChatSastScanResultsFile, "", "Results file in JSON format containing SAST scan results") chatSastCmd.Flags().String(params.ChatSastSourceDir, "", "Source code root directory relevant for the results file") chatSastCmd.Flags().String(params.ChatSastResultID, "", "ID of the result to remediate") @@ -48,26 +47,37 @@ func ChatSastSubCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers return chatSastCmd } -func runChatSast(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) func(cmd *cobra.Command, args []string) error { +func runChatSast( + chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper, +) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { if !isAiGuidedRemediationEnabled(tenantWrapper) { return outputError(cmd, uuid.Nil, errors.Errorf(AiGuidedRemediationDisabledError)) } chatConversationID, _ := cmd.Flags().GetString(params.ChatConversationID) - chatCheckmarxAI, _ := cmd.Flags().GetBool(params.ChatCheckmarxAI) scanResultsFile, _ := cmd.Flags().GetString(params.ChatSastScanResultsFile) sourceDir, _ := cmd.Flags().GetString(params.ChatSastSourceDir) sastResultID, _ := cmd.Flags().GetString(params.ChatSastResultID) - chatModel, _ := cmd.Flags().GetString(params.ChatModel) + azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantWrapper) + checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantWrapper) conn := connector.NewFileSystemConnector("") var statefulWrapper wrapper.StatefulWrapper - if chatCheckmarxAI { - customerToken, _ := wrappers.GetAccessToken() - CheckmarxAiEndPoint, _ := wrappers.GetURL(checkmarxAiRoute, customerToken) - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, CheckmarxAiEndPoint, customerToken, checkmarxAiChatModel, dropLen, 0) + customerToken, _ := wrappers.GetAccessToken() + tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) + + if azureAiEnabled || checkmarxAiEnabled { + aiProxyEndPoint, _ := wrappers.GetURL(aiProxyRoute, customerToken) + var model string + if azureAiEnabled { + model, _ = GetAzureAiModel(tenantWrapper) + } else { + model = checkmarxAiChatModel + } + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) // todo: check final interface } else { + chatModel, _ := cmd.Flags().GetString(params.ChatModel) chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) } @@ -113,9 +123,41 @@ func runChatSast(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.Tenant }) } - response, err := chatWrapper.Call(statefulWrapper, id, newMessages) - if err != nil { - return outputError(cmd, id, err) + var response []message.Message + if azureAiEnabled { + azureAiEndPoint, _ := GetAzureAiEndPoint(tenantWrapper) + azureAiAPIKey, _ := GetAzureAiAPIKey(tenantWrapper) + metadata := message.MetaData{ + TenantID: tenantID, + RequestID: "???", + UserAgent: params.DefaultAgent, + Feature: guidedRemediationFeatureNameSast, + ExternalModel: &message.ExternalAzure{ + Endpoint: azureAiEndPoint, + ApiKey: azureAiAPIKey, + }, + } + response, err = chatWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) + if err != nil { + return outputError(cmd, id, err) + } + } else if checkmarxAiEnabled { + metadata := message.MetaData{ + TenantID: tenantID, + RequestID: "???", + UserAgent: params.DefaultAgent, + Feature: guidedRemediationFeatureNameSast, + ExternalModel: nil, + } + response, err = chatWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) + if err != nil { + return outputError(cmd, id, err) + } + } else { + response, err = chatWrapper.Call(statefulWrapper, id, newMessages) + if err != nil { + return outputError(cmd, id, err) + } } responseContent := getMessageContents(response) @@ -129,23 +171,66 @@ func runChatSast(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.Tenant } } -func isAiGuidedRemediationEnabled(tenantWrapper wrappers.TenantConfigurationWrapper) bool { +func GetTenantConfiguration(tenantWrapper wrappers.TenantConfigurationWrapper, configKey string) (string, error) { tenantConfigurationResponse, errorModel, err := tenantWrapper.GetTenantConfiguration() if err != nil { - return false + return "", err } if errorModel != nil { - return false + return "", errors.New(errorModel.Message) } if tenantConfigurationResponse != nil { for _, resp := range *tenantConfigurationResponse { - if resp.Key == AiGuidedRemediationEnabled { - isEnabled, _ := strconv.ParseBool(resp.Value) - return isEnabled + if resp.Key == configKey { + return resp.Value, nil } } } - return false + return "", errors.New(configKey + " not found") +} + +func GetTenantConfigurationBool(tenantWrapper wrappers.TenantConfigurationWrapper, configKey string) (bool, error) { + value, err := GetTenantConfiguration(tenantWrapper, configKey) + if err != nil { + return false, err + } + return strconv.ParseBool(value) +} + +func isAiGuidedRemediationEnabled(tenantWrapper wrappers.TenantConfigurationWrapper) bool { + isEnabled, err := GetTenantConfigurationBool(tenantWrapper, AiGuidedRemediationEnabled) + if err != nil { + return false + } + return isEnabled +} + +func isAzureAiGuidedRemediationEnabled(tenantWrapper wrappers.TenantConfigurationWrapper) bool { + isEnabled, err := GetTenantConfigurationBool(tenantWrapper, AzureAiGuidedRemediationEnabled) + if err != nil { + return false + } + return isEnabled +} + +func isCheckmarxAiGuidedRemediationEnabled(tenantWrapper wrappers.TenantConfigurationWrapper) bool { + isEnabled, err := GetTenantConfigurationBool(tenantWrapper, CheckmarxAiGuidedRemediationEnabled) + if err != nil { + return false + } + return isEnabled +} + +func GetAzureAiEndPoint(tenantWrapper wrappers.TenantConfigurationWrapper) (string, error) { + return GetTenantConfiguration(tenantWrapper, AzureAiEndPoint) +} + +func GetAzureAiModel(tenantWrapper wrappers.TenantConfigurationWrapper) (string, error) { + return GetTenantConfiguration(tenantWrapper, AzureAiModel) +} + +func GetAzureAiAPIKey(tenantWrapper wrappers.TenantConfigurationWrapper) (string, error) { + return GetTenantConfiguration(tenantWrapper, AzureAiApiKey) } func buildPrompt(scanResultsFile, sastResultID, sourceDir string) (systemPrompt, userPrompt string, err error) { diff --git a/internal/commands/chat.go b/internal/commands/chat.go index 28c205462..8a30f715f 100644 --- a/internal/commands/chat.go +++ b/internal/commands/chat.go @@ -6,8 +6,13 @@ import ( ) const ( - ConversationIDErrorFormat = "Invalid conversation ID %s" - AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" + ConversationIDErrorFormat = "Invalid conversation ID %s" + AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" + AzureAiGuidedRemediationEnabled = "scan.config.plugins.azureAiGuidedRemediation" // todo: check final value + AzureAiEndPoint = "scan.config.plugins.azureAiEndPoint" // todo: check final value + AzureAiApiKey = "scan.config.plugins.azureAiApiKey" // todo: check final value + AzureAiModel = "scan.config.plugins.azureAiModel" // todo: check final value + CheckmarxAiGuidedRemediationEnabled = "scan.config.plugins.checkmarxAiGuidedRemediation" // todo: check final value // final value ) func NewChatCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) *cobra.Command { @@ -17,7 +22,7 @@ func NewChatCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.Ten Long: "Chat with OpenAI models regarding KICS or SAST results", Hidden: true, } - chatKicsCmd := ChatKicsSubCommand(chatWrapper) + chatKicsCmd := ChatKicsSubCommand(chatWrapper, tenantWrapper) chatSastCmd := ChatSastSubCommand(chatWrapper, tenantWrapper) chatCmd.AddCommand(chatKicsCmd, chatSastCmd) diff --git a/internal/params/flags.go b/internal/params/flags.go index c376f78f4..a9394d818 100644 --- a/internal/params/flags.go +++ b/internal/params/flags.go @@ -169,7 +169,6 @@ const ( ChatConversationID = "conversation-id" ChatUserInput = "user-input" ChatModel = "model" - ChatCheckmarxAI = "checkmarx-ai" // Chat (Kics) ChatKicsResultFile = "result-file" diff --git a/internal/wrappers/chat-http.go b/internal/wrappers/chat-http.go index 6d27af1ef..b4f0ff0f5 100644 --- a/internal/wrappers/chat-http.go +++ b/internal/wrappers/chat-http.go @@ -18,6 +18,13 @@ func (c ChatHTTPWrapper) Call(w gptWrapper.StatefulWrapper, id uuid.UUID, messag return w.Call(id, messages) } +func (c ChatHTTPWrapper) SecureCall(w gptWrapper.StatefulWrapper, id uuid.UUID, messages []gptWrapperMessage.Message, metaData *gptWrapperMessage.MetaData, cxAuth string) ( + []gptWrapperMessage.Message, + error, +) { + return w.SecureCall(cxAuth, metaData, id, messages) +} + func NewChatWrapper() ChatWrapper { return ChatHTTPWrapper{} } diff --git a/internal/wrappers/chat.go b/internal/wrappers/chat.go index 087e2afcb..f9428587f 100644 --- a/internal/wrappers/chat.go +++ b/internal/wrappers/chat.go @@ -9,5 +9,6 @@ import ( type ChatWrapper interface { Call(gptWrapper.StatefulWrapper, uuid.UUID, []gptWrapperMessage.Message) ([]gptWrapperMessage.Message, error) + SecureCall(gptWrapper.StatefulWrapper, uuid.UUID, []gptWrapperMessage.Message, *gptWrapperMessage.MetaData, string) ([]gptWrapperMessage.Message, error) MaskSecrets(gptWrapper.StatefulWrapper, string) (*gptWrapperMaskedSecret.MaskedEntry, error) } diff --git a/internal/wrappers/client.go b/internal/wrappers/client.go index 7bd286be9..2ac3856fe 100644 --- a/internal/wrappers/client.go +++ b/internal/wrappers/client.go @@ -35,6 +35,7 @@ const ( tryPrintOffset = 2 retryLimitPrintOffset = 1 MissingURI = "When using client-id and client-secret please provide base-uri or base-auth-uri" + BaseUriisEmpty = "Base URI is empty" MissingTenant = "Failed to authenticate - please provide tenant" jwtError = "Error retrieving %s from jwt token" basicFormat = "Basic %s" @@ -659,7 +660,7 @@ func getAuthURI() (string, error) { apiKey := viper.GetString(commonParams.AstAPIKey) if len(apiKey) > 0 { logger.PrintIfVerbose("Base Auth URI - Extract from API KEY") - authURI, err = extractFromTokenClaims(apiKey, audienceClaimKey) + authURI, err = ExtractFromTokenClaims(apiKey, audienceClaimKey) if err != nil { return "", err } @@ -707,7 +708,7 @@ func GetURL(path, accessToken string) (string, error) { override := viper.GetBool(commonParams.ApikeyOverrideFlag) if accessToken != "" { logger.PrintIfVerbose("Base URI - Extract from JWT token") - cleanURL, err = extractFromTokenClaims(accessToken, baseURLKey) + cleanURL, err = ExtractFromTokenClaims(accessToken, baseURLKey) if err != nil { return "", err } @@ -728,7 +729,7 @@ func GetURL(path, accessToken string) (string, error) { return fmt.Sprintf("%s/%s", cleanURL, path), nil } -func extractFromTokenClaims(accessToken, claim string) (string, error) { +func ExtractFromTokenClaims(accessToken, claim string) (string, error) { var value string token, _, err := new(jwt.Parser).ParseUnverified(accessToken, jwt.MapClaims{}) if err != nil { diff --git a/internal/wrappers/feature-flags-http.go b/internal/wrappers/feature-flags-http.go index c06e24c0d..10d9959e5 100644 --- a/internal/wrappers/feature-flags-http.go +++ b/internal/wrappers/feature-flags-http.go @@ -27,7 +27,7 @@ func (f FeatureFlagsHTTPWrapper) GetAll() (*FeatureFlagsResponseModel, error) { return nil, tokenError } - tenantIDFromClaims, extractError := extractFromTokenClaims(accessToken, tenantIDClaimKey) + tenantIDFromClaims, extractError := ExtractFromTokenClaims(accessToken, tenantIDClaimKey) if extractError != nil { return nil, extractError } diff --git a/internal/wrappers/mock/chat-mock.go b/internal/wrappers/mock/chat-mock.go index 96d2d8941..efbffa095 100644 --- a/internal/wrappers/mock/chat-mock.go +++ b/internal/wrappers/mock/chat-mock.go @@ -28,3 +28,13 @@ func (c ChatMockWrapper) Call(_ gptWrapper.StatefulWrapper, _ uuid.UUID, _ []gpt Content: "Mock message", }}, nil } + +func (c ChatMockWrapper) SecureCall(w gptWrapper.StatefulWrapper, id uuid.UUID, messages []gptWrapperMessage.Message, metaData *gptWrapperMessage.MetaData, cxAuth string) ( + []gptWrapperMessage.Message, + error, +) { + return []gptWrapperMessage.Message{{ + Role: gptWrapperRole.Assistant, + Content: "Mock message", + }}, nil +} From e2458c8bfbed9dfd9aaa857791ec8680c46d0384 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Wed, 5 Jun 2024 11:58:48 +0300 Subject: [PATCH 08/69] reduce calls to configuration service --- internal/commands/chat-kics.go | 17 ++++++---- internal/commands/chat-sast.go | 58 +++++++++++++++++++--------------- internal/commands/chat.go | 2 +- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 998086bdf..a73de50de 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -92,8 +92,13 @@ func runChatKics( chatResultVulnerability, _ := cmd.Flags().GetString(params.ChatKicsResultVulnerability) userInput, _ := cmd.Flags().GetString(params.ChatUserInput) - azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantWrapper) - checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantWrapper) + tenantConfigurationResponses, err := GetTenantConfigurationResponses(tenantWrapper) + if err != nil { + return outputError(cmd, uuid.Nil, err) + } + + azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) + checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) conn := connector.NewFileSystemConnector("") @@ -105,7 +110,7 @@ func runChatKics( aiProxyEndPoint, _ := wrappers.GetURL(aiProxyRoute, customerToken) var model string if azureAiEnabled { - model, _ = GetAzureAiModel(tenantWrapper) + model, _ = GetAzureAiModel(tenantConfigurationResponses) } else { model = checkmarxAiChatModel } @@ -135,8 +140,8 @@ func runChatKics( var response []message.Message if azureAiEnabled { - azureAiEndPoint, _ := GetAzureAiEndPoint(tenantWrapper) - azureAiApiKey, _ := GetAzureAiAPIKey(tenantWrapper) + azureAiEndPoint, _ := GetAzureAiEndPoint(tenantConfigurationResponses) + azureAiAPIKey, _ := GetAzureAiAPIKey(tenantConfigurationResponses) metadata := message.MetaData{ TenantID: tenantID, RequestID: "???", @@ -144,7 +149,7 @@ func runChatKics( Feature: guidedRemediationFeatureNameKics, ExternalModel: &message.ExternalAzure{ Endpoint: azureAiEndPoint, - ApiKey: azureAiApiKey, + ApiKey: azureAiAPIKey, }, } response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 7be66e3bf..c1aed9c23 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -51,15 +51,19 @@ func runChatSast( chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper, ) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { - if !isAiGuidedRemediationEnabled(tenantWrapper) { + tenantConfigurationResponses, err := GetTenantConfigurationResponses(tenantWrapper) + if err != nil { + return outputError(cmd, uuid.Nil, err) + } + if !isAiGuidedRemediationEnabled(tenantConfigurationResponses) { return outputError(cmd, uuid.Nil, errors.Errorf(AiGuidedRemediationDisabledError)) } chatConversationID, _ := cmd.Flags().GetString(params.ChatConversationID) scanResultsFile, _ := cmd.Flags().GetString(params.ChatSastScanResultsFile) sourceDir, _ := cmd.Flags().GetString(params.ChatSastSourceDir) sastResultID, _ := cmd.Flags().GetString(params.ChatSastResultID) - azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantWrapper) - checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantWrapper) + azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) + checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) conn := connector.NewFileSystemConnector("") @@ -71,7 +75,7 @@ func runChatSast( aiProxyEndPoint, _ := wrappers.GetURL(aiProxyRoute, customerToken) var model string if azureAiEnabled { - model, _ = GetAzureAiModel(tenantWrapper) + model, _ = GetAzureAiModel(tenantConfigurationResponses) } else { model = checkmarxAiChatModel } @@ -125,8 +129,8 @@ func runChatSast( var response []message.Message if azureAiEnabled { - azureAiEndPoint, _ := GetAzureAiEndPoint(tenantWrapper) - azureAiAPIKey, _ := GetAzureAiAPIKey(tenantWrapper) + azureAiEndPoint, _ := GetAzureAiEndPoint(tenantConfigurationResponses) + azureAiAPIKey, _ := GetAzureAiAPIKey(tenantConfigurationResponses) metadata := message.MetaData{ TenantID: tenantID, RequestID: "???", @@ -171,16 +175,20 @@ func runChatSast( } } -func GetTenantConfiguration(tenantWrapper wrappers.TenantConfigurationWrapper, configKey string) (string, error) { +func GetTenantConfigurationResponses(tenantWrapper wrappers.TenantConfigurationWrapper) (*[]*wrappers.TenantConfigurationResponse, error) { tenantConfigurationResponse, errorModel, err := tenantWrapper.GetTenantConfiguration() if err != nil { - return "", err + return nil, err } if errorModel != nil { - return "", errors.New(errorModel.Message) + return nil, errors.New(errorModel.Message) } - if tenantConfigurationResponse != nil { - for _, resp := range *tenantConfigurationResponse { + return tenantConfigurationResponse, nil +} + +func GetTenantConfiguration(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, configKey string) (string, error) { + if tenantConfigurationResponses != nil { + for _, resp := range *tenantConfigurationResponses { if resp.Key == configKey { return resp.Value, nil } @@ -189,48 +197,48 @@ func GetTenantConfiguration(tenantWrapper wrappers.TenantConfigurationWrapper, c return "", errors.New(configKey + " not found") } -func GetTenantConfigurationBool(tenantWrapper wrappers.TenantConfigurationWrapper, configKey string) (bool, error) { - value, err := GetTenantConfiguration(tenantWrapper, configKey) +func GetTenantConfigurationBool(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, configKey string) (bool, error) { + value, err := GetTenantConfiguration(tenantConfigurationResponses, configKey) if err != nil { return false, err } return strconv.ParseBool(value) } -func isAiGuidedRemediationEnabled(tenantWrapper wrappers.TenantConfigurationWrapper) bool { - isEnabled, err := GetTenantConfigurationBool(tenantWrapper, AiGuidedRemediationEnabled) +func isAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { + isEnabled, err := GetTenantConfigurationBool(tenantConfigurationResponses, AiGuidedRemediationEnabled) if err != nil { return false } return isEnabled } -func isAzureAiGuidedRemediationEnabled(tenantWrapper wrappers.TenantConfigurationWrapper) bool { - isEnabled, err := GetTenantConfigurationBool(tenantWrapper, AzureAiGuidedRemediationEnabled) +func isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { + isEnabled, err := GetTenantConfigurationBool(tenantConfigurationResponses, AzureAiGuidedRemediationEnabled) if err != nil { return false } return isEnabled } -func isCheckmarxAiGuidedRemediationEnabled(tenantWrapper wrappers.TenantConfigurationWrapper) bool { - isEnabled, err := GetTenantConfigurationBool(tenantWrapper, CheckmarxAiGuidedRemediationEnabled) +func isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { + isEnabled, err := GetTenantConfigurationBool(tenantConfigurationResponses, CheckmarxAiGuidedRemediationEnabled) if err != nil { return false } return isEnabled } -func GetAzureAiEndPoint(tenantWrapper wrappers.TenantConfigurationWrapper) (string, error) { - return GetTenantConfiguration(tenantWrapper, AzureAiEndPoint) +func GetAzureAiEndPoint(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (string, error) { + return GetTenantConfiguration(tenantConfigurationResponses, AzureAiEndPoint) } -func GetAzureAiModel(tenantWrapper wrappers.TenantConfigurationWrapper) (string, error) { - return GetTenantConfiguration(tenantWrapper, AzureAiModel) +func GetAzureAiModel(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (string, error) { + return GetTenantConfiguration(tenantConfigurationResponses, AzureAiModel) } -func GetAzureAiAPIKey(tenantWrapper wrappers.TenantConfigurationWrapper) (string, error) { - return GetTenantConfiguration(tenantWrapper, AzureAiApiKey) +func GetAzureAiAPIKey(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (string, error) { + return GetTenantConfiguration(tenantConfigurationResponses, AzureAiAPIKey) } func buildPrompt(scanResultsFile, sastResultID, sourceDir string) (systemPrompt, userPrompt string, err error) { diff --git a/internal/commands/chat.go b/internal/commands/chat.go index 8a30f715f..a34f2d2f6 100644 --- a/internal/commands/chat.go +++ b/internal/commands/chat.go @@ -10,7 +10,7 @@ const ( AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" AzureAiGuidedRemediationEnabled = "scan.config.plugins.azureAiGuidedRemediation" // todo: check final value AzureAiEndPoint = "scan.config.plugins.azureAiEndPoint" // todo: check final value - AzureAiApiKey = "scan.config.plugins.azureAiApiKey" // todo: check final value + AzureAiAPIKey = "scan.config.plugins.azureAiApiKey" // todo: check final value AzureAiModel = "scan.config.plugins.azureAiModel" // todo: check final value CheckmarxAiGuidedRemediationEnabled = "scan.config.plugins.checkmarxAiGuidedRemediation" // todo: check final value // final value ) From 664248b526eee84fdb150254ee0cea76a3a68be0 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Thu, 6 Jun 2024 10:05:53 +0300 Subject: [PATCH 09/69] shorten method --- internal/commands/chat-kics.go | 25 +++--------------- internal/commands/chat-sast.go | 46 +++++++++++++++++++--------------- internal/wrappers/chat-http.go | 4 +-- 3 files changed, 31 insertions(+), 44 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index a73de50de..8b9ec2097 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -4,10 +4,8 @@ import ( "fmt" "os" - "github.com/Checkmarx/gen-ai-wrapper/pkg/connector" "github.com/Checkmarx/gen-ai-wrapper/pkg/message" "github.com/Checkmarx/gen-ai-wrapper/pkg/role" - "github.com/Checkmarx/gen-ai-wrapper/pkg/wrapper" "github.com/checkmarx/ast-cli/internal/commands/util/printer" "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" @@ -100,26 +98,8 @@ func runChatKics( azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) - conn := connector.NewFileSystemConnector("") - - var statefulWrapper wrapper.StatefulWrapper - customerToken, _ := wrappers.GetAccessToken() - tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) - - if azureAiEnabled || checkmarxAiEnabled { - aiProxyEndPoint, _ := wrappers.GetURL(aiProxyRoute, customerToken) - var model string - if azureAiEnabled { - model, _ = GetAzureAiModel(tenantConfigurationResponses) - } else { - model = checkmarxAiChatModel - } - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) // todo: check final interface - } else { - chatModel, _ := cmd.Flags().GetString(params.ChatModel) - chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) - statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) - } + statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) + if chatConversationID == "" { chatConversationID = statefulWrapper.GenerateId().String() } @@ -137,6 +117,7 @@ func runChatKics( } newMessages := buildMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) + tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) var response []message.Message if azureAiEnabled { diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index c1aed9c23..a0f1de71b 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -65,26 +65,7 @@ func runChatSast( azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) - conn := connector.NewFileSystemConnector("") - - var statefulWrapper wrapper.StatefulWrapper - customerToken, _ := wrappers.GetAccessToken() - tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) - - if azureAiEnabled || checkmarxAiEnabled { - aiProxyEndPoint, _ := wrappers.GetURL(aiProxyRoute, customerToken) - var model string - if azureAiEnabled { - model, _ = GetAzureAiModel(tenantConfigurationResponses) - } else { - model = checkmarxAiChatModel - } - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) // todo: check final interface - } else { - chatModel, _ := cmd.Flags().GetString(params.ChatModel) - chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) - statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) - } + statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) newConversation := false var userInput string @@ -127,6 +108,8 @@ func runChatSast( }) } + tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) + var response []message.Message if azureAiEnabled { azureAiEndPoint, _ := GetAzureAiEndPoint(tenantConfigurationResponses) @@ -175,6 +158,29 @@ func runChatSast( } } +func CreateStatefulWrapper(cmd *cobra.Command, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (wrapper.StatefulWrapper, string) { + conn := connector.NewFileSystemConnector("") + + var statefulWrapper wrapper.StatefulWrapper + customerToken, _ := wrappers.GetAccessToken() + + if azureAiEnabled || checkmarxAiEnabled { + aiProxyEndPoint, _ := wrappers.GetURL(aiProxyRoute, customerToken) + var model string + if azureAiEnabled { + model, _ = GetAzureAiModel(tenantConfigurationResponses) + } else { + model = checkmarxAiChatModel + } + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) // todo: check final interface + } else { + chatModel, _ := cmd.Flags().GetString(params.ChatModel) + chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) + statefulWrapper = wrapper.NewStatefulWrapper(conn, chatAPIKey, chatModel, dropLen, 0) + } + return statefulWrapper, customerToken +} + func GetTenantConfigurationResponses(tenantWrapper wrappers.TenantConfigurationWrapper) (*[]*wrappers.TenantConfigurationResponse, error) { tenantConfigurationResponse, errorModel, err := tenantWrapper.GetTenantConfiguration() if err != nil { diff --git a/internal/wrappers/chat-http.go b/internal/wrappers/chat-http.go index b4f0ff0f5..3312766c5 100644 --- a/internal/wrappers/chat-http.go +++ b/internal/wrappers/chat-http.go @@ -18,11 +18,11 @@ func (c ChatHTTPWrapper) Call(w gptWrapper.StatefulWrapper, id uuid.UUID, messag return w.Call(id, messages) } -func (c ChatHTTPWrapper) SecureCall(w gptWrapper.StatefulWrapper, id uuid.UUID, messages []gptWrapperMessage.Message, metaData *gptWrapperMessage.MetaData, cxAuth string) ( +func (c ChatHTTPWrapper) SecureCall(w gptWrapper.StatefulWrapper, historyId uuid.UUID, messages []gptWrapperMessage.Message, metaData *gptWrapperMessage.MetaData, cxAuth string) ( []gptWrapperMessage.Message, error, ) { - return w.SecureCall(cxAuth, metaData, id, messages) + return w.SecureCall(cxAuth, metaData, historyId, messages) } func NewChatWrapper() ChatWrapper { From b898439aceb9352b4a4af3e3225e5a6f59c69420 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Thu, 6 Jun 2024 10:54:11 +0300 Subject: [PATCH 10/69] fix lint --- internal/commands/chat-kics.go | 2 +- internal/commands/chat-sast.go | 6 +++--- internal/wrappers/chat-http.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 8b9ec2097..2d6869fd2 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -99,7 +99,7 @@ func runChatKics( checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) - + if chatConversationID == "" { chatConversationID = statefulWrapper.GenerateId().String() } diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index a0f1de71b..c66b5ba9a 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -158,11 +158,11 @@ func runChatSast( } } -func CreateStatefulWrapper(cmd *cobra.Command, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (wrapper.StatefulWrapper, string) { +func CreateStatefulWrapper(cmd *cobra.Command, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) ( + statefulWrapper wrapper.StatefulWrapper, customerToken string) { conn := connector.NewFileSystemConnector("") - var statefulWrapper wrapper.StatefulWrapper - customerToken, _ := wrappers.GetAccessToken() + customerToken, _ = wrappers.GetAccessToken() if azureAiEnabled || checkmarxAiEnabled { aiProxyEndPoint, _ := wrappers.GetURL(aiProxyRoute, customerToken) diff --git a/internal/wrappers/chat-http.go b/internal/wrappers/chat-http.go index 3312766c5..ad9118de7 100644 --- a/internal/wrappers/chat-http.go +++ b/internal/wrappers/chat-http.go @@ -18,11 +18,11 @@ func (c ChatHTTPWrapper) Call(w gptWrapper.StatefulWrapper, id uuid.UUID, messag return w.Call(id, messages) } -func (c ChatHTTPWrapper) SecureCall(w gptWrapper.StatefulWrapper, historyId uuid.UUID, messages []gptWrapperMessage.Message, metaData *gptWrapperMessage.MetaData, cxAuth string) ( +func (c ChatHTTPWrapper) SecureCall(w gptWrapper.StatefulWrapper, historyID uuid.UUID, messages []gptWrapperMessage.Message, metaData *gptWrapperMessage.MetaData, cxAuth string) ( []gptWrapperMessage.Message, error, ) { - return w.SecureCall(cxAuth, metaData, historyId, messages) + return w.SecureCall(cxAuth, metaData, historyID, messages) } func NewChatWrapper() ChatWrapper { From bf51859aa8b246fb8c0875245b06ffda44e4eae5 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Mon, 10 Jun 2024 15:33:41 +0300 Subject: [PATCH 11/69] added request id and logs --- internal/commands/chat-kics.go | 9 +++++++-- internal/commands/chat-sast.go | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 2d6869fd2..48bfc2bd8 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -118,6 +118,7 @@ func runChatKics( newMessages := buildMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) + requestID := statefulWrapper.GenerateId().String() var response []message.Message if azureAiEnabled { @@ -125,7 +126,7 @@ func runChatKics( azureAiAPIKey, _ := GetAzureAiAPIKey(tenantConfigurationResponses) metadata := message.MetaData{ TenantID: tenantID, - RequestID: "???", + RequestID: requestID, UserAgent: params.DefaultAgent, Feature: guidedRemediationFeatureNameKics, ExternalModel: &message.ExternalAzure{ @@ -133,6 +134,8 @@ func runChatKics( ApiKey: azureAiAPIKey, }, } + logger.PrintIfVerbose("Sending message to Azure AI model for KICS guided remediation. RequestID: " + requestID) + response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { return outputError(cmd, id, err) @@ -140,16 +143,18 @@ func runChatKics( } else if checkmarxAiEnabled { metadata := message.MetaData{ TenantID: tenantID, - RequestID: "???", + RequestID: requestID, UserAgent: params.DefaultAgent, Feature: guidedRemediationFeatureNameKics, ExternalModel: nil, } + logger.PrintIfVerbose("Sending message to Checkmarx AI model for KICS guided remediation. RequestID: " + requestID) response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { return outputError(cmd, id, err) } } else { + logger.PrintIfVerbose("Sending message to ChatGPT model for KICS guided remediation. RequestID: " + requestID) response, err = chatKicsWrapper.Call(statefulWrapper, id, newMessages) if err != nil { return outputError(cmd, id, err) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index c66b5ba9a..9b86c349a 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -109,6 +109,7 @@ func runChatSast( } tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) + requestID := statefulWrapper.GenerateId().String() var response []message.Message if azureAiEnabled { @@ -116,7 +117,7 @@ func runChatSast( azureAiAPIKey, _ := GetAzureAiAPIKey(tenantConfigurationResponses) metadata := message.MetaData{ TenantID: tenantID, - RequestID: "???", + RequestID: requestID, UserAgent: params.DefaultAgent, Feature: guidedRemediationFeatureNameSast, ExternalModel: &message.ExternalAzure{ @@ -124,6 +125,8 @@ func runChatSast( ApiKey: azureAiAPIKey, }, } + + logger.PrintIfVerbose("Sending message to Azure AI model for SAST guided remediation. RequestID: " + requestID) response, err = chatWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { return outputError(cmd, id, err) @@ -131,16 +134,18 @@ func runChatSast( } else if checkmarxAiEnabled { metadata := message.MetaData{ TenantID: tenantID, - RequestID: "???", + RequestID: requestID, UserAgent: params.DefaultAgent, Feature: guidedRemediationFeatureNameSast, ExternalModel: nil, } + logger.PrintIfVerbose("Sending message to Checkmarx AI model for SAST guided remediation. RequestID: " + requestID) response, err = chatWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { return outputError(cmd, id, err) } } else { + logger.PrintIfVerbose("Sending message to ChatGPT model for SAST guided remediation. RequestID: " + requestID) response, err = chatWrapper.Call(statefulWrapper, id, newMessages) if err != nil { return outputError(cmd, id, err) From 65b438ef36e837bc84ce7775eaf008dc66700f90 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Thu, 13 Jun 2024 13:28:57 +0300 Subject: [PATCH 12/69] Add tests --- internal/commands/chat-kics.go | 8 +- internal/commands/chat-kics_test.go | 56 ++++++++++++- internal/commands/chat-sast.go | 21 +++-- internal/commands/chat-sast_test.go | 112 ++++++++++++++++++++++++++ internal/commands/chat.go | 2 +- internal/wrappers/mock/chat-mock.go | 6 +- internal/wrappers/mock/tenant-mock.go | 4 + 7 files changed, 194 insertions(+), 15 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 48bfc2bd8..7156e8e8b 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -70,7 +70,6 @@ func ChatKicsSubCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers chatKicsCmd.Flags().String(params.ChatKicsResultVulnerability, "", "IaC result vulnerability name") _ = chatKicsCmd.MarkFlagRequired(params.ChatUserInput) - _ = chatKicsCmd.MarkFlagRequired(params.ChatAPIKey) _ = chatKicsCmd.MarkFlagRequired(params.ChatKicsResultFile) _ = chatKicsCmd.MarkFlagRequired(params.ChatKicsResultLine) _ = chatKicsCmd.MarkFlagRequired(params.ChatKicsResultSeverity) @@ -97,6 +96,7 @@ func runChatKics( azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) + chatGptEnabled := isChatGPTAiGuidedRemediationEnabled(tenantConfigurationResponses) statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) @@ -123,7 +123,6 @@ func runChatKics( var response []message.Message if azureAiEnabled { azureAiEndPoint, _ := GetAzureAiEndPoint(tenantConfigurationResponses) - azureAiAPIKey, _ := GetAzureAiAPIKey(tenantConfigurationResponses) metadata := message.MetaData{ TenantID: tenantID, RequestID: requestID, @@ -131,7 +130,6 @@ func runChatKics( Feature: guidedRemediationFeatureNameKics, ExternalModel: &message.ExternalAzure{ Endpoint: azureAiEndPoint, - ApiKey: azureAiAPIKey, }, } logger.PrintIfVerbose("Sending message to Azure AI model for KICS guided remediation. RequestID: " + requestID) @@ -153,12 +151,14 @@ func runChatKics( if err != nil { return outputError(cmd, id, err) } - } else { + } else if chatGptEnabled { logger.PrintIfVerbose("Sending message to ChatGPT model for KICS guided remediation. RequestID: " + requestID) response, err = chatKicsWrapper.Call(statefulWrapper, id, newMessages) if err != nil { return outputError(cmd, id, err) } + } else { + return outputError(cmd, id, errors.New(AllOptionsDisabledError)) } responseContent := getMessageContents(response) diff --git a/internal/commands/chat-kics_test.go b/internal/commands/chat-kics_test.go index e743a4f72..ab854d91d 100644 --- a/internal/commands/chat-kics_test.go +++ b/internal/commands/chat-kics_test.go @@ -2,6 +2,8 @@ package commands import ( "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers" + "github.com/checkmarx/ast-cli/internal/wrappers/mock" "io" "strings" "testing" @@ -49,7 +51,6 @@ func TestChatKicsInvalidFile(t *testing.T) { func TestChatKicsCorrectResponse(t *testing.T) { buffer, err := executeRedirectedTestCommand("chat", "kics", "--conversation-id", uuid.New().String(), - "--chat-apikey", "apiKey", "--user-input", "userInput", "--result-file", "./data/Dockerfile", "--result-line", "0", @@ -61,3 +62,56 @@ func TestChatKicsCorrectResponse(t *testing.T) { s := strings.ToLower(string(output)) assert.Assert(t, strings.Contains(s, "mock"), s) } + +func TestChatKicsAzureAICorrectResponse(t *testing.T) { + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{ + { + Key: "scan.config.plugins.ideScans", + Value: "true", + }, + { + Key: "scan.config.plugins.azureAiGuidedRemediation", + Value: "true", + }, + } + buffer, err := executeRedirectedTestCommand("chat", "kics", + "--conversation-id", uuid.New().String(), + "--user-input", "userInput", + "--result-file", "./data/Dockerfile", + "--result-line", "0", + "--result-severity", "LOW", + "--result-vulnerability", "Vulnerability") + assert.NilError(t, err) + output, err := io.ReadAll(buffer) + assert.NilError(t, err) + s := strings.ToLower(string(output)) + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} + assert.Assert(t, strings.Contains(s, "mock message from securecall with externalmodel: externalmodel is not nil"), s) +} + +func TestChatKicsCheckmarxAICorrectResponse(t *testing.T) { + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{ + { + Key: "scan.config.plugins.ideScans", + Value: "true", + }, + { + Key: "scan.config.plugins.checkmarxAiGuidedRemediation", + Value: "true", + }, + } + buffer, err := executeRedirectedTestCommand("chat", "kics", + "--conversation-id", uuid.New().String(), + "--chat-apikey", "apiKey", + "--user-input", "userInput", + "--result-file", "./data/Dockerfile", + "--result-line", "0", + "--result-severity", "LOW", + "--result-vulnerability", "Vulnerability") + assert.NilError(t, err) + output, err := io.ReadAll(buffer) + assert.NilError(t, err) + s := strings.ToLower(string(output)) + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} + assert.Assert(t, strings.Contains(s, "mock message from securecall with externalmodel: externalmodel is nil"), s) +} diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 9b86c349a..52dbb4571 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -21,6 +21,7 @@ const ScanResultsFileErrorFormat = "Error reading and parsing scan results %s" const CreatePromptErrorFormat = "Error creating prompt for result ID %s" const UserInputRequiredErrorFormat = "%s is required when %s is provided" const AiGuidedRemediationDisabledError = "The AI Guided Remediation is disabled in your tenant account" +const AllOptionsDisabledError = "All AI Guided Remediation options are disabled in your tenant account" // check final value func ChatSastSubCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) *cobra.Command { chatSastCmd := &cobra.Command{ @@ -39,7 +40,6 @@ func ChatSastSubCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers chatSastCmd.Flags().String(params.ChatSastSourceDir, "", "Source code root directory relevant for the results file") chatSastCmd.Flags().String(params.ChatSastResultID, "", "ID of the result to remediate") - _ = chatSastCmd.MarkFlagRequired(params.ChatAPIKey) _ = chatSastCmd.MarkFlagRequired(params.ChatSastScanResultsFile) _ = chatSastCmd.MarkFlagRequired(params.ChatSastSourceDir) _ = chatSastCmd.MarkFlagRequired(params.ChatSastResultID) @@ -64,6 +64,7 @@ func runChatSast( sastResultID, _ := cmd.Flags().GetString(params.ChatSastResultID) azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) + chatGptEnabled := isChatGPTAiGuidedRemediationEnabled(tenantConfigurationResponses) statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) @@ -114,7 +115,6 @@ func runChatSast( var response []message.Message if azureAiEnabled { azureAiEndPoint, _ := GetAzureAiEndPoint(tenantConfigurationResponses) - azureAiAPIKey, _ := GetAzureAiAPIKey(tenantConfigurationResponses) metadata := message.MetaData{ TenantID: tenantID, RequestID: requestID, @@ -122,7 +122,6 @@ func runChatSast( Feature: guidedRemediationFeatureNameSast, ExternalModel: &message.ExternalAzure{ Endpoint: azureAiEndPoint, - ApiKey: azureAiAPIKey, }, } @@ -144,12 +143,14 @@ func runChatSast( if err != nil { return outputError(cmd, id, err) } - } else { + } else if chatGptEnabled { logger.PrintIfVerbose("Sending message to ChatGPT model for SAST guided remediation. RequestID: " + requestID) response, err = chatWrapper.Call(statefulWrapper, id, newMessages) if err != nil { return outputError(cmd, id, err) } + } else { + return outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)) } responseContent := getMessageContents(response) @@ -240,6 +241,14 @@ func isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrap return isEnabled } +func isChatGPTAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { + isEnabled, err := GetTenantConfigurationBool(tenantConfigurationResponses, ChatGPTGuidedRemediationEnabled) + if err != nil { + return false + } + return isEnabled +} + func GetAzureAiEndPoint(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (string, error) { return GetTenantConfiguration(tenantConfigurationResponses, AzureAiEndPoint) } @@ -248,10 +257,6 @@ func GetAzureAiModel(tenantConfigurationResponses *[]*wrappers.TenantConfigurati return GetTenantConfiguration(tenantConfigurationResponses, AzureAiModel) } -func GetAzureAiAPIKey(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (string, error) { - return GetTenantConfiguration(tenantConfigurationResponses, AzureAiAPIKey) -} - func buildPrompt(scanResultsFile, sastResultID, sourceDir string) (systemPrompt, userPrompt string, err error) { scanResults, err := ReadResultsSAST(scanResultsFile) if err != nil { diff --git a/internal/commands/chat-sast_test.go b/internal/commands/chat-sast_test.go index 6673a8e34..95e4e0313 100644 --- a/internal/commands/chat-sast_test.go +++ b/internal/commands/chat-sast_test.go @@ -122,6 +122,60 @@ func TestChatSastFirstMessageCorrectResponse(t *testing.T) { assert.Assert(t, strings.Contains(s, "mock"), s) } +func TestChatSastAzureAIFirstMessageCorrectResponse(t *testing.T) { + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{ + { + Key: "scan.config.plugins.ideScans", + Value: "true", + }, + { + Key: "scan.config.plugins.azureAiGuidedRemediation", + Value: "true", + }, + { + Key: "scan.config.plugins.aiGuidedRemediation", + Value: "true", + }, + } + buffer, err := executeRedirectedTestCommand("chat", "sast", + "--scan-results-file", "./data/cx_result.json", + "--source-dir", "./data", + "--sast-result-id", "13588362") + assert.NilError(t, err) + output, err := io.ReadAll(buffer) + assert.NilError(t, err) + s := strings.ToLower(string(output)) + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} + assert.Assert(t, strings.Contains(s, "mock"), s) +} + +func TestChatSastCheckmarxAIFirstMessageCorrectResponse(t *testing.T) { + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{ + { + Key: "scan.config.plugins.ideScans", + Value: "true", + }, + { + Key: "scan.config.plugins.checkmarxAiGuidedRemediation", + Value: "true", + }, + { + Key: "scan.config.plugins.aiGuidedRemediation", + Value: "true", + }, + } + buffer, err := executeRedirectedTestCommand("chat", "sast", + "--scan-results-file", "./data/cx_result.json", + "--source-dir", "./data", + "--sast-result-id", "13588362") + assert.NilError(t, err) + output, err := io.ReadAll(buffer) + assert.NilError(t, err) + s := strings.ToLower(string(output)) + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} + assert.Assert(t, strings.Contains(s, "mock"), s) +} + func TestChatSastSecondMessageCorrectResponse(t *testing.T) { buffer, err := executeRedirectedTestCommand("chat", "sast", "--chat-apikey", "apiKey", @@ -136,3 +190,61 @@ func TestChatSastSecondMessageCorrectResponse(t *testing.T) { s := strings.ToLower(string(output)) assert.Assert(t, strings.Contains(s, "mock"), s) } + +func TestChatSastAzureAISecondMessageCorrectResponse(t *testing.T) { + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{ + { + Key: "scan.config.plugins.ideScans", + Value: "true", + }, + { + Key: "scan.config.plugins.azureAiGuidedRemediation", + Value: "true", + }, + { + Key: "scan.config.plugins.aiGuidedRemediation", + Value: "true", + }, + } + buffer, err := executeRedirectedTestCommand("chat", "sast", + "--scan-results-file", "./data/cx_result.json", + "--source-dir", "./data", + "--sast-result-id", "13588362", + "--conversation-id", uuid.New().String(), + "--user-input", "userInput") + assert.NilError(t, err) + output, err := io.ReadAll(buffer) + assert.NilError(t, err) + s := strings.ToLower(string(output)) + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} + assert.Assert(t, strings.Contains(s, "mock"), s) +} + +func TestChatSastCheckmarxAISecondMessageCorrectResponse(t *testing.T) { + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{ + { + Key: "scan.config.plugins.ideScans", + Value: "true", + }, + { + Key: "scan.config.plugins.checkmarxAiGuidedRemediation", + Value: "true", + }, + { + Key: "scan.config.plugins.aiGuidedRemediation", + Value: "true", + }, + } + buffer, err := executeRedirectedTestCommand("chat", "sast", + "--scan-results-file", "./data/cx_result.json", + "--source-dir", "./data", + "--sast-result-id", "13588362", + "--conversation-id", uuid.New().String(), + "--user-input", "userInput") + assert.NilError(t, err) + output, err := io.ReadAll(buffer) + assert.NilError(t, err) + s := strings.ToLower(string(output)) + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} + assert.Assert(t, strings.Contains(s, "mock"), s) +} diff --git a/internal/commands/chat.go b/internal/commands/chat.go index a34f2d2f6..37cbad9e6 100644 --- a/internal/commands/chat.go +++ b/internal/commands/chat.go @@ -8,9 +8,9 @@ import ( const ( ConversationIDErrorFormat = "Invalid conversation ID %s" AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" + ChatGPTGuidedRemediationEnabled = "scan.config.plugins.chatGPTGuidedRemediation" // todo: check final value AzureAiGuidedRemediationEnabled = "scan.config.plugins.azureAiGuidedRemediation" // todo: check final value AzureAiEndPoint = "scan.config.plugins.azureAiEndPoint" // todo: check final value - AzureAiAPIKey = "scan.config.plugins.azureAiApiKey" // todo: check final value AzureAiModel = "scan.config.plugins.azureAiModel" // todo: check final value CheckmarxAiGuidedRemediationEnabled = "scan.config.plugins.checkmarxAiGuidedRemediation" // todo: check final value // final value ) diff --git a/internal/wrappers/mock/chat-mock.go b/internal/wrappers/mock/chat-mock.go index efbffa095..06fc00bb3 100644 --- a/internal/wrappers/mock/chat-mock.go +++ b/internal/wrappers/mock/chat-mock.go @@ -33,8 +33,12 @@ func (c ChatMockWrapper) SecureCall(w gptWrapper.StatefulWrapper, id uuid.UUID, []gptWrapperMessage.Message, error, ) { + externalModelState := "externalModel is not nil" + if metaData.ExternalModel == nil { + externalModelState = "externalModel is nil" + } return []gptWrapperMessage.Message{{ Role: gptWrapperRole.Assistant, - Content: "Mock message", + Content: "Mock message from SecureCall with externalModel: " + externalModelState, }}, nil } diff --git a/internal/wrappers/mock/tenant-mock.go b/internal/wrappers/mock/tenant-mock.go index 3ef26e66a..887f9e486 100644 --- a/internal/wrappers/mock/tenant-mock.go +++ b/internal/wrappers/mock/tenant-mock.go @@ -22,6 +22,10 @@ func (t TenantConfigurationMockWrapper) GetTenantConfiguration() ( Key: "scan.config.plugins.aiGuidedRemediation", Value: "true", }, + { + Key: "scan.config.plugins.chatGPTGuidedRemediation", + Value: "true", + }, } } return &TenantConfiguration, nil, nil From 5d99028355764ad1db59b7f610d0d06ddaba6795 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Thu, 13 Jun 2024 13:42:40 +0300 Subject: [PATCH 13/69] mod tidy --- go.mod | 6 ------ go.sum | 4 ---- 2 files changed, 10 deletions(-) diff --git a/go.mod b/go.mod index bbaccb946..78c68bf86 100644 --- a/go.mod +++ b/go.mod @@ -41,17 +41,11 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect - go.uber.org/atomic v1.9.0 // indirect - go.uber.org/multierr v1.9.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect - golang.org/x/net v0.23.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect golang.org/x/net v0.23.0 // indirect golang.org/x/sys v0.19.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa // indirect - google.golang.org/grpc v1.63.2 // indirect - google.golang.org/protobuf v1.33.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index e3a003608..18b148d92 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,3 @@ -github.com/Checkmarx/gen-ai-wrapper v1.0.1 h1:MwIndXVTqFLwrhjbaXO1FqpgvExgZ41mkLf/dujqoQw= -github.com/Checkmarx/gen-ai-wrapper v1.0.1/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604074408-3a640add0c20 h1:9okGTw//C0y01pyzy7ISP/0SiOH6G1PzG4kEPHWx3aA= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604074408-3a640add0c20/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352 h1:jxRmRax3Stz7+64Rh83u4rQDa1duYcfsNrUdk9wxyFU= github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= From 69e2d7a0b6c033b88a32d9c4a8ea8e378f48f176 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Thu, 13 Jun 2024 13:44:50 +0300 Subject: [PATCH 14/69] Fix error --- internal/wrappers/feature-flags-http.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/wrappers/feature-flags-http.go b/internal/wrappers/feature-flags-http.go index 40908f783..b1aa13b74 100644 --- a/internal/wrappers/feature-flags-http.go +++ b/internal/wrappers/feature-flags-http.go @@ -98,7 +98,7 @@ func (f FeatureFlagsHTTPWrapper) getTenantDetails() (params map[string]string, c return nil, 0, tokenError } - tenantIDFromClaims, extractError := extractFromTokenClaims(accessToken, tenantIDClaimKey) + tenantIDFromClaims, extractError := ExtractFromTokenClaims(accessToken, tenantIDClaimKey) if extractError != nil { return nil, 0, extractError } From 08bdf29f6467a17acba664dbe10757f99be27065 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Thu, 13 Jun 2024 14:37:12 +0300 Subject: [PATCH 15/69] try to fix lint error --- internal/commands/chat-kics_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/commands/chat-kics_test.go b/internal/commands/chat-kics_test.go index ab854d91d..5af2977de 100644 --- a/internal/commands/chat-kics_test.go +++ b/internal/commands/chat-kics_test.go @@ -2,12 +2,12 @@ package commands import ( "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers" - "github.com/checkmarx/ast-cli/internal/wrappers/mock" "io" "strings" "testing" + "github.com/checkmarx/ast-cli/internal/wrappers" + "github.com/checkmarx/ast-cli/internal/wrappers/mock" "github.com/google/uuid" "gotest.tools/assert" ) From 2ccf9e15086500a0c54f7f9bc95673519aa6ff93 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Thu, 13 Jun 2024 17:53:23 +0300 Subject: [PATCH 16/69] fix case where api key is not available in kics chat --- internal/commands/chat-kics.go | 25 ++++++++++++++++++------- internal/commands/chat-sast.go | 7 +++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 7156e8e8b..5e6e15dd6 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -89,14 +89,25 @@ func runChatKics( chatResultVulnerability, _ := cmd.Flags().GetString(params.ChatKicsResultVulnerability) userInput, _ := cmd.Flags().GetString(params.ChatUserInput) - tenantConfigurationResponses, err := GetTenantConfigurationResponses(tenantWrapper) - if err != nil { - return outputError(cmd, uuid.Nil, err) - } + var chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool + var tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse + + if !isCxOneApiKeyAvailable() { + chatGptEnabled = true + azureAiEnabled = false + checkmarxAiEnabled = false + logger.Printf("CxOne API key is not available, ChatGPT model will be used for guided remediation.") + } else { + var err error + tenantConfigurationResponses, err = GetTenantConfigurationResponses(tenantWrapper) + if err != nil { + return outputError(cmd, uuid.Nil, err) + } - azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) - checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) - chatGptEnabled := isChatGPTAiGuidedRemediationEnabled(tenantConfigurationResponses) + azureAiEnabled = isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) + checkmarxAiEnabled = isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) + chatGptEnabled = isChatGPTAiGuidedRemediationEnabled(tenantConfigurationResponses) + } statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 52dbb4571..19345a18a 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "github.com/spf13/viper" "strconv" "github.com/Checkmarx/gen-ai-wrapper/pkg/connector" @@ -11,6 +12,7 @@ import ( "github.com/checkmarx/ast-cli/internal/commands/util/printer" "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" + commonParams "github.com/checkmarx/ast-cli/internal/params" "github.com/checkmarx/ast-cli/internal/wrappers" "github.com/google/uuid" "github.com/pkg/errors" @@ -225,6 +227,11 @@ func isAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.Tena return isEnabled } +func isCxOneApiKeyAvailable() bool { + apiKey := viper.GetString(commonParams.AstAPIKey) + return apiKey != "" +} + func isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { isEnabled, err := GetTenantConfigurationBool(tenantConfigurationResponses, AzureAiGuidedRemediationEnabled) if err != nil { From f2348a3186618885bab19023107ebeb2226317d6 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Sun, 16 Jun 2024 08:58:11 +0300 Subject: [PATCH 17/69] fix lint warnings --- internal/commands/chat-kics.go | 2 +- internal/commands/chat-sast.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 5e6e15dd6..d949c5162 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -92,7 +92,7 @@ func runChatKics( var chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool var tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse - if !isCxOneApiKeyAvailable() { + if !isCxOneAPIKeyAvailable() { chatGptEnabled = true azureAiEnabled = false checkmarxAiEnabled = false diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 19345a18a..6d0165999 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -12,7 +12,6 @@ import ( "github.com/checkmarx/ast-cli/internal/commands/util/printer" "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" - commonParams "github.com/checkmarx/ast-cli/internal/params" "github.com/checkmarx/ast-cli/internal/wrappers" "github.com/google/uuid" "github.com/pkg/errors" @@ -227,8 +226,8 @@ func isAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.Tena return isEnabled } -func isCxOneApiKeyAvailable() bool { - apiKey := viper.GetString(commonParams.AstAPIKey) +func isCxOneAPIKeyAvailable() bool { + apiKey := viper.GetString(params.AstAPIKey) return apiKey != "" } From ddc3d0938d26090bb7bd7dc28820e6acb421e9b0 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Sun, 16 Jun 2024 08:59:29 +0300 Subject: [PATCH 18/69] fix lint errors --- internal/commands/chat-sast.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 6d0165999..79fe156a6 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "github.com/spf13/viper" "strconv" "github.com/Checkmarx/gen-ai-wrapper/pkg/connector" @@ -16,6 +15,7 @@ import ( "github.com/google/uuid" "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/viper" ) const ScanResultsFileErrorFormat = "Error reading and parsing scan results %s" From f2a3c929959ff6e4db47a188f8ed428e21d828eb Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Sun, 16 Jun 2024 11:50:09 +0300 Subject: [PATCH 19/69] Fix unit tests --- internal/commands/chat-kics_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/commands/chat-kics_test.go b/internal/commands/chat-kics_test.go index 5af2977de..787840500 100644 --- a/internal/commands/chat-kics_test.go +++ b/internal/commands/chat-kics_test.go @@ -2,6 +2,8 @@ package commands import ( "fmt" + "github.com/checkmarx/ast-cli/internal/params" + "github.com/spf13/viper" "io" "strings" "testing" @@ -74,6 +76,8 @@ func TestChatKicsAzureAICorrectResponse(t *testing.T) { Value: "true", }, } + viper.Set(params.AstAPIKey, "SomeKey") + buffer, err := executeRedirectedTestCommand("chat", "kics", "--conversation-id", uuid.New().String(), "--user-input", "userInput", @@ -85,7 +89,10 @@ func TestChatKicsAzureAICorrectResponse(t *testing.T) { output, err := io.ReadAll(buffer) assert.NilError(t, err) s := strings.ToLower(string(output)) + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} + viper.Set(params.AstAPIKey, "") + assert.Assert(t, strings.Contains(s, "mock message from securecall with externalmodel: externalmodel is not nil"), s) } @@ -100,6 +107,8 @@ func TestChatKicsCheckmarxAICorrectResponse(t *testing.T) { Value: "true", }, } + viper.Set(params.AstAPIKey, "SomeKey") + buffer, err := executeRedirectedTestCommand("chat", "kics", "--conversation-id", uuid.New().String(), "--chat-apikey", "apiKey", @@ -112,6 +121,9 @@ func TestChatKicsCheckmarxAICorrectResponse(t *testing.T) { output, err := io.ReadAll(buffer) assert.NilError(t, err) s := strings.ToLower(string(output)) + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} + viper.Set(params.AstAPIKey, "") + assert.Assert(t, strings.Contains(s, "mock message from securecall with externalmodel: externalmodel is nil"), s) } From 2c17e58cccdec22acb0432bd9a8258f67bcc105d Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Sun, 16 Jun 2024 11:53:41 +0300 Subject: [PATCH 20/69] fix lint --- internal/commands/chat-kics_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/commands/chat-kics_test.go b/internal/commands/chat-kics_test.go index 787840500..998be879a 100644 --- a/internal/commands/chat-kics_test.go +++ b/internal/commands/chat-kics_test.go @@ -2,12 +2,12 @@ package commands import ( "fmt" - "github.com/checkmarx/ast-cli/internal/params" "github.com/spf13/viper" "io" "strings" "testing" + params "github.com/checkmarx/ast-cli/internal/params" "github.com/checkmarx/ast-cli/internal/wrappers" "github.com/checkmarx/ast-cli/internal/wrappers/mock" "github.com/google/uuid" From d57355c654829abf5402eafe9f895058ebfb6607 Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Sun, 16 Jun 2024 11:54:21 +0300 Subject: [PATCH 21/69] fix lints --- internal/commands/chat-kics_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/commands/chat-kics_test.go b/internal/commands/chat-kics_test.go index 998be879a..bcfda306a 100644 --- a/internal/commands/chat-kics_test.go +++ b/internal/commands/chat-kics_test.go @@ -2,15 +2,15 @@ package commands import ( "fmt" - "github.com/spf13/viper" "io" "strings" "testing" - params "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/params" "github.com/checkmarx/ast-cli/internal/wrappers" "github.com/checkmarx/ast-cli/internal/wrappers/mock" "github.com/google/uuid" + "github.com/spf13/viper" "gotest.tools/assert" ) From 348e1a981f814ba7786cf9003a2c00c9d0f9650c Mon Sep 17 00:00:00 2001 From: Avi Sabzerou Date: Sun, 16 Jun 2024 12:03:11 +0300 Subject: [PATCH 22/69] reset api key to previous value in tests --- internal/commands/chat-kics_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/commands/chat-kics_test.go b/internal/commands/chat-kics_test.go index bcfda306a..94c85f009 100644 --- a/internal/commands/chat-kics_test.go +++ b/internal/commands/chat-kics_test.go @@ -76,6 +76,7 @@ func TestChatKicsAzureAICorrectResponse(t *testing.T) { Value: "true", }, } + origAPIKey := viper.GetString(params.AstAPIKey) viper.Set(params.AstAPIKey, "SomeKey") buffer, err := executeRedirectedTestCommand("chat", "kics", @@ -91,7 +92,7 @@ func TestChatKicsAzureAICorrectResponse(t *testing.T) { s := strings.ToLower(string(output)) mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} - viper.Set(params.AstAPIKey, "") + viper.Set(params.AstAPIKey, origAPIKey) assert.Assert(t, strings.Contains(s, "mock message from securecall with externalmodel: externalmodel is not nil"), s) } @@ -107,6 +108,7 @@ func TestChatKicsCheckmarxAICorrectResponse(t *testing.T) { Value: "true", }, } + origAPIKey := viper.GetString(params.AstAPIKey) viper.Set(params.AstAPIKey, "SomeKey") buffer, err := executeRedirectedTestCommand("chat", "kics", @@ -123,7 +125,7 @@ func TestChatKicsCheckmarxAICorrectResponse(t *testing.T) { s := strings.ToLower(string(output)) mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} - viper.Set(params.AstAPIKey, "") + viper.Set(params.AstAPIKey, origAPIKey) assert.Assert(t, strings.Contains(s, "mock message from securecall with externalmodel: externalmodel is nil"), s) } From 5e618394e18d807d450e0d1ad678102af9ad1fbe Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Tue, 18 Jun 2024 10:18:37 +0300 Subject: [PATCH 23/69] fix route according to the target --- internal/commands/chat-kics.go | 28 +++++++---------------- internal/commands/chat-sast.go | 42 ++++++++++------------------------ 2 files changed, 20 insertions(+), 50 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index d949c5162..012a257cf 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -41,7 +41,8 @@ const FileErrorFormat = "It seems that %s is not available for AI Guided Remedia // chatModel model to use when calling the CheckmarxAI const checkmarxAiChatModel = "GPT4" -const aiProxyRoute = "/api/ai-proxy" +const aiProxyAzureAIRoute = "/api/ai-proxy/redirect/externalAzure" +const aiProxyCheckmarxAIRoute = "/api/ai-proxy/redirect/azure" const tenantIDClaimKey = "tenant_id" const guidedRemediationFeatureNameKics = "cli-guided-remediation-kics" const guidedRemediationFeatureNameSast = "cli-guided-remediation-sast" @@ -132,32 +133,19 @@ func runChatKics( requestID := statefulWrapper.GenerateId().String() var response []message.Message - if azureAiEnabled { - azureAiEndPoint, _ := GetAzureAiEndPoint(tenantConfigurationResponses) + if azureAiEnabled || checkmarxAiEnabled { metadata := message.MetaData{ TenantID: tenantID, RequestID: requestID, UserAgent: params.DefaultAgent, Feature: guidedRemediationFeatureNameKics, - ExternalModel: &message.ExternalAzure{ - Endpoint: azureAiEndPoint, - }, } - logger.PrintIfVerbose("Sending message to Azure AI model for KICS guided remediation. RequestID: " + requestID) + if azureAiEnabled { + logger.PrintIfVerbose("Sending message to Azure AI model for KICS guided remediation. RequestID: " + requestID) - response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) - if err != nil { - return outputError(cmd, id, err) - } - } else if checkmarxAiEnabled { - metadata := message.MetaData{ - TenantID: tenantID, - RequestID: requestID, - UserAgent: params.DefaultAgent, - Feature: guidedRemediationFeatureNameKics, - ExternalModel: nil, + } else { + logger.PrintIfVerbose("Sending message to Checkmarx AI model for KICS guided remediation. RequestID: " + requestID) } - logger.PrintIfVerbose("Sending message to Checkmarx AI model for KICS guided remediation. RequestID: " + requestID) response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { return outputError(cmd, id, err) @@ -169,7 +157,7 @@ func runChatKics( return outputError(cmd, id, err) } } else { - return outputError(cmd, id, errors.New(AllOptionsDisabledError)) + return outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)) } responseContent := getMessageContents(response) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 79fe156a6..26fc2eafe 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -114,32 +114,19 @@ func runChatSast( requestID := statefulWrapper.GenerateId().String() var response []message.Message - if azureAiEnabled { - azureAiEndPoint, _ := GetAzureAiEndPoint(tenantConfigurationResponses) + if azureAiEnabled || checkmarxAiEnabled { metadata := message.MetaData{ TenantID: tenantID, RequestID: requestID, UserAgent: params.DefaultAgent, Feature: guidedRemediationFeatureNameSast, - ExternalModel: &message.ExternalAzure{ - Endpoint: azureAiEndPoint, - }, } + if azureAiEnabled { + logger.PrintIfVerbose("Sending message to Azure AI model for SAST guided remediation. RequestID: " + requestID) - logger.PrintIfVerbose("Sending message to Azure AI model for SAST guided remediation. RequestID: " + requestID) - response, err = chatWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) - if err != nil { - return outputError(cmd, id, err) - } - } else if checkmarxAiEnabled { - metadata := message.MetaData{ - TenantID: tenantID, - RequestID: requestID, - UserAgent: params.DefaultAgent, - Feature: guidedRemediationFeatureNameSast, - ExternalModel: nil, + } else { + logger.PrintIfVerbose("Sending message to Checkmarx AI model for SAST guided remediation. RequestID: " + requestID) } - logger.PrintIfVerbose("Sending message to Checkmarx AI model for SAST guided remediation. RequestID: " + requestID) response, err = chatWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { return outputError(cmd, id, err) @@ -171,14 +158,13 @@ func CreateStatefulWrapper(cmd *cobra.Command, azureAiEnabled, checkmarxAiEnable customerToken, _ = wrappers.GetAccessToken() - if azureAiEnabled || checkmarxAiEnabled { - aiProxyEndPoint, _ := wrappers.GetURL(aiProxyRoute, customerToken) - var model string - if azureAiEnabled { - model, _ = GetAzureAiModel(tenantConfigurationResponses) - } else { - model = checkmarxAiChatModel - } + if azureAiEnabled { + aiProxyEndPoint, _ := wrappers.GetURL(aiProxyAzureAIRoute, customerToken) + model, _ := GetAzureAiModel(tenantConfigurationResponses) + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) // todo: check final interface + } else if checkmarxAiEnabled { + aiProxyEndPoint, _ := wrappers.GetURL(aiProxyCheckmarxAIRoute, customerToken) + model := checkmarxAiChatModel statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) // todo: check final interface } else { chatModel, _ := cmd.Flags().GetString(params.ChatModel) @@ -255,10 +241,6 @@ func isChatGPTAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappe return isEnabled } -func GetAzureAiEndPoint(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (string, error) { - return GetTenantConfiguration(tenantConfigurationResponses, AzureAiEndPoint) -} - func GetAzureAiModel(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (string, error) { return GetTenantConfiguration(tenantConfigurationResponses, AzureAiModel) } From 61c7df3c0654871806ed96f3d55cc9bd9b0bb4ff Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Tue, 18 Jun 2024 10:21:00 +0300 Subject: [PATCH 24/69] fix lint --- internal/commands/chat-kics.go | 1 - internal/commands/chat-sast.go | 1 - 2 files changed, 2 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 012a257cf..34f0cf290 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -142,7 +142,6 @@ func runChatKics( } if azureAiEnabled { logger.PrintIfVerbose("Sending message to Azure AI model for KICS guided remediation. RequestID: " + requestID) - } else { logger.PrintIfVerbose("Sending message to Checkmarx AI model for KICS guided remediation. RequestID: " + requestID) } diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 26fc2eafe..fe3a5db6e 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -123,7 +123,6 @@ func runChatSast( } if azureAiEnabled { logger.PrintIfVerbose("Sending message to Azure AI model for SAST guided remediation. RequestID: " + requestID) - } else { logger.PrintIfVerbose("Sending message to Checkmarx AI model for SAST guided remediation. RequestID: " + requestID) } From e1bdac0c3b534ba4b311f3b74efeef73258f10cd Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 19 Jun 2024 10:50:58 +0300 Subject: [PATCH 25/69] update to latest gpt wrapper --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 78c68bf86..fab84cdd1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/checkmarx/ast-cli go 1.22.4 require ( - github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352 + github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240619053350-1df5b4050291 github.com/MakeNowJust/heredoc v1.0.0 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 diff --git a/go.sum b/go.sum index 18b148d92..bdbaadc50 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352 h1:jxRmRax3Stz7+64Rh83u4rQDa1duYcfsNrUdk9wxyFU= github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240619053350-1df5b4050291 h1:EafjD2GOcdCcUusFPOHjjwppqOFPeU7MQBy+jYRx8vs= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240619053350-1df5b4050291/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= From a5192e7ea59f6cdbd1571f674d6bb9c000ec71cb Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 19 Jun 2024 10:53:59 +0300 Subject: [PATCH 26/69] fix tests --- internal/commands/chat-kics.go | 6 +++--- internal/commands/chat-kics_test.go | 4 ++-- internal/commands/chat-sast.go | 6 +++--- internal/commands/chat-sast_test.go | 4 ++-- internal/wrappers/mock/chat-mock.go | 6 +----- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 34f0cf290..eb085069c 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -141,16 +141,16 @@ func runChatKics( Feature: guidedRemediationFeatureNameKics, } if azureAiEnabled { - logger.PrintIfVerbose("Sending message to Azure AI model for KICS guided remediation. RequestID: " + requestID) + logger.Printf("Sending message to Azure AI model for KICS guided remediation. RequestID: " + requestID) } else { - logger.PrintIfVerbose("Sending message to Checkmarx AI model for KICS guided remediation. RequestID: " + requestID) + logger.Printf("Sending message to Checkmarx AI model for KICS guided remediation. RequestID: " + requestID) } response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { return outputError(cmd, id, err) } } else if chatGptEnabled { - logger.PrintIfVerbose("Sending message to ChatGPT model for KICS guided remediation. RequestID: " + requestID) + logger.Printf("Sending message to ChatGPT model for KICS guided remediation. RequestID: " + requestID) response, err = chatKicsWrapper.Call(statefulWrapper, id, newMessages) if err != nil { return outputError(cmd, id, err) diff --git a/internal/commands/chat-kics_test.go b/internal/commands/chat-kics_test.go index 94c85f009..df32021eb 100644 --- a/internal/commands/chat-kics_test.go +++ b/internal/commands/chat-kics_test.go @@ -94,7 +94,7 @@ func TestChatKicsAzureAICorrectResponse(t *testing.T) { mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} viper.Set(params.AstAPIKey, origAPIKey) - assert.Assert(t, strings.Contains(s, "mock message from securecall with externalmodel: externalmodel is not nil"), s) + assert.Assert(t, strings.Contains(s, "mock message from securecall"), s) } func TestChatKicsCheckmarxAICorrectResponse(t *testing.T) { @@ -127,5 +127,5 @@ func TestChatKicsCheckmarxAICorrectResponse(t *testing.T) { mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} viper.Set(params.AstAPIKey, origAPIKey) - assert.Assert(t, strings.Contains(s, "mock message from securecall with externalmodel: externalmodel is nil"), s) + assert.Assert(t, strings.Contains(s, "mock message from securecall"), s) } diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index fe3a5db6e..19a36bc5a 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -122,16 +122,16 @@ func runChatSast( Feature: guidedRemediationFeatureNameSast, } if azureAiEnabled { - logger.PrintIfVerbose("Sending message to Azure AI model for SAST guided remediation. RequestID: " + requestID) + logger.Printf("Sending message to Azure AI model for SAST guided remediation. RequestID: " + requestID) } else { - logger.PrintIfVerbose("Sending message to Checkmarx AI model for SAST guided remediation. RequestID: " + requestID) + logger.Printf("Sending message to Checkmarx AI model for SAST guided remediation. RequestID: " + requestID) } response, err = chatWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { return outputError(cmd, id, err) } } else if chatGptEnabled { - logger.PrintIfVerbose("Sending message to ChatGPT model for SAST guided remediation. RequestID: " + requestID) + logger.Printf("Sending message to ChatGPT model for SAST guided remediation. RequestID: " + requestID) response, err = chatWrapper.Call(statefulWrapper, id, newMessages) if err != nil { return outputError(cmd, id, err) diff --git a/internal/commands/chat-sast_test.go b/internal/commands/chat-sast_test.go index 95e4e0313..6d299f9d1 100644 --- a/internal/commands/chat-sast_test.go +++ b/internal/commands/chat-sast_test.go @@ -217,7 +217,7 @@ func TestChatSastAzureAISecondMessageCorrectResponse(t *testing.T) { assert.NilError(t, err) s := strings.ToLower(string(output)) mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} - assert.Assert(t, strings.Contains(s, "mock"), s) + assert.Assert(t, strings.Contains(s, "mock message from securecall"), s) } func TestChatSastCheckmarxAISecondMessageCorrectResponse(t *testing.T) { @@ -246,5 +246,5 @@ func TestChatSastCheckmarxAISecondMessageCorrectResponse(t *testing.T) { assert.NilError(t, err) s := strings.ToLower(string(output)) mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{} - assert.Assert(t, strings.Contains(s, "mock"), s) + assert.Assert(t, strings.Contains(s, "mock message from securecall"), s) } diff --git a/internal/wrappers/mock/chat-mock.go b/internal/wrappers/mock/chat-mock.go index 06fc00bb3..ee97da9db 100644 --- a/internal/wrappers/mock/chat-mock.go +++ b/internal/wrappers/mock/chat-mock.go @@ -33,12 +33,8 @@ func (c ChatMockWrapper) SecureCall(w gptWrapper.StatefulWrapper, id uuid.UUID, []gptWrapperMessage.Message, error, ) { - externalModelState := "externalModel is not nil" - if metaData.ExternalModel == nil { - externalModelState = "externalModel is nil" - } return []gptWrapperMessage.Message{{ Role: gptWrapperRole.Assistant, - Content: "Mock message from SecureCall with externalModel: " + externalModelState, + Content: "Mock message from SecureCall", }}, nil } From 2b1af8fe19c614fd027bfed52d6052b6f1e73b3c Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 19 Jun 2024 14:08:28 +0300 Subject: [PATCH 27/69] fix routes --- internal/commands/chat-kics.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index eb085069c..40eec92e9 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -41,8 +41,8 @@ const FileErrorFormat = "It seems that %s is not available for AI Guided Remedia // chatModel model to use when calling the CheckmarxAI const checkmarxAiChatModel = "GPT4" -const aiProxyAzureAIRoute = "/api/ai-proxy/redirect/externalAzure" -const aiProxyCheckmarxAIRoute = "/api/ai-proxy/redirect/azure" +const aiProxyAzureAIRoute = "api/ai-proxy/redirect/externalAzure" +const aiProxyCheckmarxAIRoute = "api/ai-proxy/redirect/azure" const tenantIDClaimKey = "tenant_id" const guidedRemediationFeatureNameKics = "cli-guided-remediation-kics" const guidedRemediationFeatureNameSast = "cli-guided-remediation-sast" From e8ffdfd429b23e0db4d84d883ac5b4319c5d2fcb Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 23 Jun 2024 10:38:59 +0300 Subject: [PATCH 28/69] upgrade gpt wrapper --- go.mod | 2 +- go.sum | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index fab84cdd1..a93edb639 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/checkmarx/ast-cli go 1.22.4 require ( - github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240619053350-1df5b4050291 + github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb github.com/MakeNowJust/heredoc v1.0.0 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 diff --git a/go.sum b/go.sum index bdbaadc50..dcc57808b 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,12 @@ github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352 h1:jxRm github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240619053350-1df5b4050291 h1:EafjD2GOcdCcUusFPOHjjwppqOFPeU7MQBy+jYRx8vs= github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240619053350-1df5b4050291/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240620113214-fb742c9e7674 h1:kjlDESWsWXtBUIMqgLJ+uvA9+cRElMGc+BIMX6IqKlk= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240620113214-fb742c9e7674/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623071108-c6dd30dd6815 h1:AsuynchsJYFJXkxliEXjxW2WFFRAJtPUMOXN7G8aqyA= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623071108-c6dd30dd6815/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb h1:6dlsDwdf7H7aA/4CynxCaHF7wzTiV89QrWsUOQ9Sk1g= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= From 7e7b9659a3c364c0a7cf3b6f5087e6cd3e14b575 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 23 Jun 2024 10:41:32 +0300 Subject: [PATCH 29/69] fix tenant id --- internal/commands/chat-kics.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 40eec92e9..b765895c6 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -3,6 +3,7 @@ package commands import ( "fmt" "os" + "strings" "github.com/Checkmarx/gen-ai-wrapper/pkg/message" "github.com/Checkmarx/gen-ai-wrapper/pkg/role" @@ -130,6 +131,11 @@ func runChatKics( newMessages := buildMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) + // remove from tenant id all the string before :: + if !strings.Contains(tenantID, "::") { + tenantID = tenantID[strings.LastIndex(tenantID, "::")+2:] + } + requestID := statefulWrapper.GenerateId().String() var response []message.Message From 26ce4d7af1bdc03b8b8016cb3272105101eaf9a9 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 23 Jun 2024 10:42:20 +0300 Subject: [PATCH 30/69] handle tenant format --- internal/commands/chat-sast.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 19a36bc5a..299556d42 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -3,6 +3,7 @@ package commands import ( "fmt" "strconv" + "strings" "github.com/Checkmarx/gen-ai-wrapper/pkg/connector" "github.com/Checkmarx/gen-ai-wrapper/pkg/message" @@ -111,6 +112,9 @@ func runChatSast( } tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) + if !strings.Contains(tenantID, "::") { + tenantID = tenantID[strings.LastIndex(tenantID, "::")+2:] + } requestID := statefulWrapper.GenerateId().String() var response []message.Message From c71cac8e635365f2117575afbd7bcc5af5072eaa Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 23 Jun 2024 10:46:41 +0300 Subject: [PATCH 31/69] fix stupid mistake --- internal/commands/chat-kics.go | 2 +- internal/commands/chat-sast.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index b765895c6..554cad76e 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -132,7 +132,7 @@ func runChatKics( newMessages := buildMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) // remove from tenant id all the string before :: - if !strings.Contains(tenantID, "::") { + if strings.Contains(tenantID, "::") { tenantID = tenantID[strings.LastIndex(tenantID, "::")+2:] } diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 299556d42..5660a3d0a 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -112,7 +112,7 @@ func runChatSast( } tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) - if !strings.Contains(tenantID, "::") { + if strings.Contains(tenantID, "::") { tenantID = tenantID[strings.LastIndex(tenantID, "::")+2:] } requestID := statefulWrapper.GenerateId().String() From 2b0b56ffca766d6314c46eb36b5428a3557090c9 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 23 Jun 2024 12:42:49 +0300 Subject: [PATCH 32/69] add response to test error --- test/integration/chat_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/chat_test.go b/test/integration/chat_test.go index a9b1bfc2e..7d672787d 100644 --- a/test/integration/chat_test.go +++ b/test/integration/chat_test.go @@ -26,7 +26,7 @@ func TestChatKicsInvalidAPIKey(t *testing.T) { assert.NilError(t, err) outputModel := commands.OutputModel{} unmarshall(t, respBuffer, &outputModel, "Reading results should pass") - assert.Assert(t, strings.Contains(outputModel.Response[0], "Incorrect API key provided"), "Expecting incorrect api key error") + assert.Assert(t, strings.Contains(outputModel.Response[0], "Incorrect API key provided"), "Expecting incorrect api key error. Got: "+outputModel.Response[0]) } func TestChatSastInvalidAPIKey(t *testing.T) { @@ -41,5 +41,5 @@ func TestChatSastInvalidAPIKey(t *testing.T) { assert.NilError(t, err) outputModel := commands.OutputModel{} unmarshall(t, respBuffer, &outputModel, "Reading results should pass") - assert.Assert(t, strings.Contains(outputModel.Response[0], "Incorrect API key provided"), "Expecting incorrect api key error") + assert.Assert(t, strings.Contains(outputModel.Response[0], "Incorrect API key provided"), "Expecting incorrect api key error. Got: "+outputModel.Response[0]) } From 5e2d32d78ac4a8b982b539153893fcc3a7dbb370 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 15 Aug 2024 16:57:17 +0300 Subject: [PATCH 33/69] fix the checkmarx ai chat model --- internal/commands/chat-kics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 554cad76e..308697d39 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -41,7 +41,7 @@ const dropLen = 4 const FileErrorFormat = "It seems that %s is not available for AI Guided Remediation. Please ensure that you have opened the correct workspace or the relevant file." // chatModel model to use when calling the CheckmarxAI -const checkmarxAiChatModel = "GPT4" +const checkmarxAiChatModel = "gpt-4" const aiProxyAzureAIRoute = "api/ai-proxy/redirect/externalAzure" const aiProxyCheckmarxAIRoute = "api/ai-proxy/redirect/azure" const tenantIDClaimKey = "tenant_id" From 254323cedb487a919d8366f92594e1e2b2f2712c Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Tue, 20 Aug 2024 12:57:00 +0300 Subject: [PATCH 34/69] finish merge with go mod tidy --- go.mod | 6 ++---- go.sum | 17 ++--------------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index 9aab7fc54..5ca5cb04e 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,10 @@ module github.com/checkmarx/ast-cli go 1.22.5 require ( - github.com/CheckmarxDev/containers-resolver v1.0.6 + github.com/Checkmarx/gen-ai-prompts v0.0.0-20240807143411-708ceec12b63 github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb + github.com/CheckmarxDev/containers-resolver v1.0.6 github.com/MakeNowJust/heredoc v1.0.0 - github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c - github.com/Checkmarx/gen-ai-prompts v0.0.0-20240807143411-708ceec12b63 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 @@ -167,7 +166,6 @@ require ( github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect - github.com/pelletier/go-toml/v2 v2.2.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/moby/docker-image-spec v1.3.1 // indirect github.com/moby/locker v1.0.1 // indirect diff --git a/go.sum b/go.sum index 390a70244..e541443bc 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,3 @@ -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352 h1:jxRmRax3Stz7+64Rh83u4rQDa1duYcfsNrUdk9wxyFU= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240604122751-06e7809dc352/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240619053350-1df5b4050291 h1:EafjD2GOcdCcUusFPOHjjwppqOFPeU7MQBy+jYRx8vs= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240619053350-1df5b4050291/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240620113214-fb742c9e7674 h1:kjlDESWsWXtBUIMqgLJ+uvA9+cRElMGc+BIMX6IqKlk= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240620113214-fb742c9e7674/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623071108-c6dd30dd6815 h1:AsuynchsJYFJXkxliEXjxW2WFFRAJtPUMOXN7G8aqyA= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623071108-c6dd30dd6815/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb h1:6dlsDwdf7H7aA/4CynxCaHF7wzTiV89QrWsUOQ9Sk1g= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= @@ -72,6 +62,8 @@ github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/Checkmarx/gen-ai-prompts v0.0.0-20240807143411-708ceec12b63 h1:SCuTcE+CFvgjbIxUNL8rsdB2sAhfuNx85HvxImKta3g= github.com/Checkmarx/gen-ai-prompts v0.0.0-20240807143411-708ceec12b63/go.mod h1:MI6lfLerXU+5eTV/EPTDavgnV3owz3GPT4g/msZBWPo= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb h1:6dlsDwdf7H7aA/4CynxCaHF7wzTiV89QrWsUOQ9Sk1g= +github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= github.com/CheckmarxDev/containers-resolver v1.0.6 h1:Y0CKTR5tlw0YV+nQpz44kF0sZxWwCyvgYtjOukfYm0E= github.com/CheckmarxDev/containers-resolver v1.0.6/go.mod h1:S3m6qscOWqaJJw56hR/hZxBVdcZRn8AnRGU/6jtONI4= github.com/CycloneDX/cyclonedx-go v0.8.0 h1:FyWVj6x6hoJrui5uRQdYZcSievw3Z32Z88uYzG/0D6M= @@ -195,8 +187,6 @@ github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg= github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s= github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE= -github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c h1:oKI4C1dXYpi0B8pltDDzp1ZRiyeILv5enbp9h4ASQ3s= -github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c/go.mod h1:l+0rISRGaps2HWkpvKbYPE1nsNx28vBj6bKorEm1M5o= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -719,8 +709,6 @@ github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4= github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw= -github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeBAsoHo= -github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 h1:kMlmsLSbjkikxQJ1IPwaM+7LJ9ltFu/fi8CRzvSnQmA= github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= @@ -887,7 +875,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= From cbc1911143d302d4e60358e2938670844a738ad7 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Tue, 20 Aug 2024 12:57:13 +0300 Subject: [PATCH 35/69] arrange configuration fields --- internal/commands/chat-sast.go | 2 +- internal/commands/chat.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 621192a91..b9ba7be6d 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -236,7 +236,7 @@ func isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrap } func isChatGPTAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { - isEnabled, err := GetTenantConfigurationBool(tenantConfigurationResponses, ChatGPTGuidedRemediationEnabled) + isEnabled, err := GetTenantConfigurationBool(tenantConfigurationResponses, OpenAiGuidedRemediationEnabled) if err != nil { return false } diff --git a/internal/commands/chat.go b/internal/commands/chat.go index 37cbad9e6..7f5fa5ae1 100644 --- a/internal/commands/chat.go +++ b/internal/commands/chat.go @@ -8,11 +8,11 @@ import ( const ( ConversationIDErrorFormat = "Invalid conversation ID %s" AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" - ChatGPTGuidedRemediationEnabled = "scan.config.plugins.chatGPTGuidedRemediation" // todo: check final value + OpenAiGuidedRemediationEnabled = "scan.config.plugins.openAiGuidedRemediation" // todo: check final value AzureAiGuidedRemediationEnabled = "scan.config.plugins.azureAiGuidedRemediation" // todo: check final value AzureAiEndPoint = "scan.config.plugins.azureAiEndPoint" // todo: check final value AzureAiModel = "scan.config.plugins.azureAiModel" // todo: check final value - CheckmarxAiGuidedRemediationEnabled = "scan.config.plugins.checkmarxAiGuidedRemediation" // todo: check final value // final value + CheckmarxAiGuidedRemediationEnabled = "scan.config.plugins.checkmarxAiGuidedRemediation" // todo: check final value ) func NewChatCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) *cobra.Command { From b5357d149194184dff1469ddeee248b22f59e510 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Tue, 20 Aug 2024 15:42:49 +0300 Subject: [PATCH 36/69] arranging the configuration fields --- internal/commands/chat-kics.go | 2 +- internal/commands/chat-sast.go | 13 ++++++++----- internal/commands/chat.go | 10 +++++----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 308697d39..f88ca57d8 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -108,7 +108,7 @@ func runChatKics( azureAiEnabled = isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled = isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) - chatGptEnabled = isChatGPTAiGuidedRemediationEnabled(tenantConfigurationResponses) + chatGptEnabled = isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses) } statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index b9ba7be6d..d6fa891c9 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -65,7 +65,7 @@ func runChatSast( sastResultID, _ := cmd.Flags().GetString(params.ChatSastResultID) azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) - chatGptEnabled := isChatGPTAiGuidedRemediationEnabled(tenantConfigurationResponses) + chatGptEnabled := isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses) statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) @@ -220,26 +220,29 @@ func isCxOneAPIKeyAvailable() bool { } func isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { - isEnabled, err := GetTenantConfigurationBool(tenantConfigurationResponses, AzureAiGuidedRemediationEnabled) + engine, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationEngine) if err != nil { return false } + isEnabled := engine == AiGuidedRemediationAzureAiValue return isEnabled } func isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { - isEnabled, err := GetTenantConfigurationBool(tenantConfigurationResponses, CheckmarxAiGuidedRemediationEnabled) + engine, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationEngine) if err != nil { return false } + isEnabled := engine == AiGuidedRemediationCheckmarxAiValue return isEnabled } -func isChatGPTAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { - isEnabled, err := GetTenantConfigurationBool(tenantConfigurationResponses, OpenAiGuidedRemediationEnabled) +func isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { + engine, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationEngine) if err != nil { return false } + isEnabled := engine == AiGuidedRemediationOpenAiValue return isEnabled } diff --git a/internal/commands/chat.go b/internal/commands/chat.go index 7f5fa5ae1..af61ca57a 100644 --- a/internal/commands/chat.go +++ b/internal/commands/chat.go @@ -8,11 +8,11 @@ import ( const ( ConversationIDErrorFormat = "Invalid conversation ID %s" AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" - OpenAiGuidedRemediationEnabled = "scan.config.plugins.openAiGuidedRemediation" // todo: check final value - AzureAiGuidedRemediationEnabled = "scan.config.plugins.azureAiGuidedRemediation" // todo: check final value - AzureAiEndPoint = "scan.config.plugins.azureAiEndPoint" // todo: check final value - AzureAiModel = "scan.config.plugins.azureAiModel" // todo: check final value - CheckmarxAiGuidedRemediationEnabled = "scan.config.plugins.checkmarxAiGuidedRemediation" // todo: check final value + AiGuidedRemediationEngine = "scan.config.plugins.aiGuidedRemediation.aiEngine" // todo: check final value + AiGuidedRemediationOpenAiValue = "openai" + AiGuidedRemediationAzureAiValue = "azureai" + AiGuidedRemediationCheckmarxAiValue = "checkmarxai" + AzureAiModel = "scan.config.plugins.aiGuidedRemediation.azureAi.model" // todo: check final value ) func NewChatCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) *cobra.Command { From 4b9ac87e1c0c1299c05842d549ec806b69587cbe Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 21 Aug 2024 13:51:32 +0300 Subject: [PATCH 37/69] compare lowercase --- internal/commands/chat-sast.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index d6fa891c9..8c67546f7 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -224,7 +224,7 @@ func isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers if err != nil { return false } - isEnabled := engine == AiGuidedRemediationAzureAiValue + isEnabled := strings.EqualFold(engine, AiGuidedRemediationAzureAiValue) return isEnabled } @@ -233,7 +233,7 @@ func isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrap if err != nil { return false } - isEnabled := engine == AiGuidedRemediationCheckmarxAiValue + isEnabled := strings.EqualFold(engine, AiGuidedRemediationCheckmarxAiValue) return isEnabled } @@ -242,7 +242,7 @@ func isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers. if err != nil { return false } - isEnabled := engine == AiGuidedRemediationOpenAiValue + isEnabled := strings.EqualFold(engine, AiGuidedRemediationOpenAiValue) return isEnabled } From c44738d8b19b28460272c29b81292777c931dbf3 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 21 Aug 2024 17:07:20 +0300 Subject: [PATCH 38/69] Update configuration service field names --- internal/commands/chat.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/commands/chat.go b/internal/commands/chat.go index af61ca57a..0e884fd1b 100644 --- a/internal/commands/chat.go +++ b/internal/commands/chat.go @@ -8,11 +8,11 @@ import ( const ( ConversationIDErrorFormat = "Invalid conversation ID %s" AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" - AiGuidedRemediationEngine = "scan.config.plugins.aiGuidedRemediation.aiEngine" // todo: check final value + AiGuidedRemediationEngine = "scan.config.plugins.aiGuidedRemediationAiEngine" // todo: check final value AiGuidedRemediationOpenAiValue = "openai" AiGuidedRemediationAzureAiValue = "azureai" AiGuidedRemediationCheckmarxAiValue = "checkmarxai" - AzureAiModel = "scan.config.plugins.aiGuidedRemediation.azureAi.model" // todo: check final value + AzureAiModel = "scan.config.plugins.aiGuidedRemediationAzureAiModel" // todo: check final value ) func NewChatCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) *cobra.Command { From be969bf3d82d7f92e2a729ff9ae4a8d72c5f912f Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Tue, 3 Sep 2024 09:46:11 +0300 Subject: [PATCH 39/69] refactor kics method to make it shorter --- internal/commands/chat-kics.go | 125 ++++++++++++++++++++------------- 1 file changed, 76 insertions(+), 49 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index f88ca57d8..7f4108220 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + gptWrapper "github.com/Checkmarx/gen-ai-wrapper/pkg/wrapper" "os" "strings" @@ -94,25 +95,16 @@ func runChatKics( var chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool var tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse - if !isCxOneAPIKeyAvailable() { - chatGptEnabled = true - azureAiEnabled = false - checkmarxAiEnabled = false - logger.Printf("CxOne API key is not available, ChatGPT model will be used for guided remediation.") - } else { - var err error - tenantConfigurationResponses, err = GetTenantConfigurationResponses(tenantWrapper) - if err != nil { - return outputError(cmd, uuid.Nil, err) - } - - azureAiEnabled = isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) - checkmarxAiEnabled = isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) - chatGptEnabled = isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses) + chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, err2, done := + getEngineSelection(cmd, chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, tenantWrapper) + if done { + return err2 } statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) + tenantID := getTenantId(customerToken) + if chatConversationID == "" { chatConversationID = statefulWrapper.GenerateId().String() } @@ -130,43 +122,12 @@ func runChatKics( } newMessages := buildMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) - tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) - // remove from tenant id all the string before :: - if strings.Contains(tenantID, "::") { - tenantID = tenantID[strings.LastIndex(tenantID, "::")+2:] - } - requestID := statefulWrapper.GenerateId().String() - - var response []message.Message - if azureAiEnabled || checkmarxAiEnabled { - metadata := message.MetaData{ - TenantID: tenantID, - RequestID: requestID, - UserAgent: params.DefaultAgent, - Feature: guidedRemediationFeatureNameKics, - } - if azureAiEnabled { - logger.Printf("Sending message to Azure AI model for KICS guided remediation. RequestID: " + requestID) - } else { - logger.Printf("Sending message to Checkmarx AI model for KICS guided remediation. RequestID: " + requestID) - } - response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) - if err != nil { - return outputError(cmd, id, err) - } - } else if chatGptEnabled { - logger.Printf("Sending message to ChatGPT model for KICS guided remediation. RequestID: " + requestID) - response, err = chatKicsWrapper.Call(statefulWrapper, id, newMessages) - if err != nil { - return outputError(cmd, id, err) - } - } else { - return outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)) + responseContent, err3, done2 := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, id, newMessages, customerToken, chatGptEnabled) + if done2 { + return err3 } - responseContent := getMessageContents(response) - return printer.Print(cmd.OutOrStdout(), &OutputModel{ ConversationID: id.String(), Response: responseContent, @@ -174,6 +135,72 @@ func runChatKics( } } +func getTenantId(customerToken string) string { + tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) + // remove from tenant id all the string before :: + if strings.Contains(tenantID, "::") { + tenantID = tenantID[strings.LastIndex(tenantID, "::")+2:] + } + return tenantID +} + +func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, + chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, chatGptEnabled bool) ([]string, error, bool) { + requestID := statefulWrapper.GenerateId().String() + + var response []message.Message + var err error + + if azureAiEnabled || checkmarxAiEnabled { + metadata := message.MetaData{ + TenantID: tenantID, + RequestID: requestID, + UserAgent: params.DefaultAgent, + Feature: guidedRemediationFeatureNameKics, + } + if azureAiEnabled { + logger.Printf("Sending message to Azure AI model for KICS guided remediation. RequestID: " + requestID) + } else { + logger.Printf("Sending message to Checkmarx AI model for KICS guided remediation. RequestID: " + requestID) + } + response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) + if err != nil { + return nil, outputError(cmd, id, err), true + } + } else if chatGptEnabled { + logger.Printf("Sending message to ChatGPT model for KICS guided remediation. RequestID: " + requestID) + response, err = chatKicsWrapper.Call(statefulWrapper, id, newMessages) + if err != nil { + return nil, outputError(cmd, id, err), true + } + } else { + return nil, outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)), true + } + + responseContent := getMessageContents(response) + return responseContent, nil, false +} + +func getEngineSelection(cmd *cobra.Command, chatGptEnabled bool, azureAiEnabled bool, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, tenantWrapper wrappers.TenantConfigurationWrapper) (bool, bool, bool, *[]*wrappers.TenantConfigurationResponse, error, bool) { + if !isCxOneAPIKeyAvailable() { + chatGptEnabled = true + azureAiEnabled = false + checkmarxAiEnabled = false + logger.Printf("CxOne API key is not available, ChatGPT model will be used for guided remediation.") + } else { + var err error + tenantConfigurationResponses, err = GetTenantConfigurationResponses(tenantWrapper) + if err != nil { + return false, false, false, nil, outputError(cmd, uuid.Nil, err), true + } + + azureAiEnabled = isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) + checkmarxAiEnabled = isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) + chatGptEnabled = isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses) + } + return chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, nil, false +} + func buildMessages(chatResultCode []byte, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput string) []message.Message { var newMessages []message.Message From 14b33802599e19ad90ed221546e6c15974ef53e1 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Tue, 3 Sep 2024 10:45:57 +0300 Subject: [PATCH 40/69] refactor method of kics to be shorter --- internal/commands/chat-kics.go | 54 ++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 7f4108220..17cafd3e1 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -103,16 +103,11 @@ func runChatKics( statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) - tenantID := getTenantId(customerToken) + tenantID := getTenantID(customerToken) - if chatConversationID == "" { - chatConversationID = statefulWrapper.GenerateId().String() - } - - id, err := uuid.Parse(chatConversationID) - if err != nil { - logger.PrintIfVerbose(err.Error()) - return outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)) + id, err4, done3 := getKicsConversationId(cmd, chatConversationID, statefulWrapper) + if done3 { + return err4 } chatResultCode, err := os.ReadFile(chatResultFile) @@ -121,9 +116,10 @@ func runChatKics( return outputError(cmd, id, errors.Errorf(FileErrorFormat, chatResultFile)) } - newMessages := buildMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) + newMessages := buildKicsMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) - responseContent, err3, done2 := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, id, newMessages, customerToken, chatGptEnabled) + responseContent, done2, err3 := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, + id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameKics) if done2 { return err3 } @@ -135,7 +131,20 @@ func runChatKics( } } -func getTenantId(customerToken string) string { +func getKicsConversationId(cmd *cobra.Command, chatConversationID string, statefulWrapper gptWrapper.StatefulWrapper) (uuid.UUID, error, bool) { + if chatConversationID == "" { + chatConversationID = statefulWrapper.GenerateId().String() + } + + id, err := uuid.Parse(chatConversationID) + if err != nil { + logger.PrintIfVerbose(err.Error()) + return uuid.UUID{}, outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)), true + } + return id, err, false +} + +func getTenantID(customerToken string) string { tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) // remove from tenant id all the string before :: if strings.Contains(tenantID, "::") { @@ -145,7 +154,8 @@ func getTenantId(customerToken string) string { } func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, - chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, chatGptEnabled bool) ([]string, error, bool) { + chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, chatGptEnabled bool, + featureName string) ([]string, bool, error) { requestID := statefulWrapper.GenerateId().String() var response []message.Message @@ -156,29 +166,29 @@ func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, TenantID: tenantID, RequestID: requestID, UserAgent: params.DefaultAgent, - Feature: guidedRemediationFeatureNameKics, + Feature: featureName, } if azureAiEnabled { - logger.Printf("Sending message to Azure AI model for KICS guided remediation. RequestID: " + requestID) + logger.Printf("Sending message to Azure AI model for " + featureName + " guided remediation. RequestID: " + requestID) } else { - logger.Printf("Sending message to Checkmarx AI model for KICS guided remediation. RequestID: " + requestID) + logger.Printf("Sending message to Checkmarx AI model for " + featureName + " guided remediation. RequestID: " + requestID) } response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { - return nil, outputError(cmd, id, err), true + return nil, true, outputError(cmd, id, err) } } else if chatGptEnabled { - logger.Printf("Sending message to ChatGPT model for KICS guided remediation. RequestID: " + requestID) + logger.Printf("Sending message to ChatGPT model for " + featureName + " guided remediation. RequestID: " + requestID) response, err = chatKicsWrapper.Call(statefulWrapper, id, newMessages) if err != nil { - return nil, outputError(cmd, id, err), true + return nil, true, outputError(cmd, id, err) } } else { - return nil, outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)), true + return nil, true, outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)) } responseContent := getMessageContents(response) - return responseContent, nil, false + return responseContent, false, nil } func getEngineSelection(cmd *cobra.Command, chatGptEnabled bool, azureAiEnabled bool, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, tenantWrapper wrappers.TenantConfigurationWrapper) (bool, bool, bool, *[]*wrappers.TenantConfigurationResponse, error, bool) { @@ -201,7 +211,7 @@ func getEngineSelection(cmd *cobra.Command, chatGptEnabled bool, azureAiEnabled return chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, nil, false } -func buildMessages(chatResultCode []byte, +func buildKicsMessages(chatResultCode []byte, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput string) []message.Message { var newMessages []message.Message newMessages = append(newMessages, message.Message{ From a5a1aa80cbb20a49223196764c33071e51ed0c5d Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Tue, 3 Sep 2024 10:46:07 +0300 Subject: [PATCH 41/69] refactor method of sast to be shorter --- internal/commands/chat-sast.go | 127 +++++++++++++++------------------ 1 file changed, 58 insertions(+), 69 deletions(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 8c67546f7..11d54d743 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -69,82 +69,24 @@ func runChatSast( statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) - newConversation := false - var userInput string - if chatConversationID == "" { - newConversation = true - chatConversationID = statefulWrapper.GenerateId().String() - } else { - userInput, _ = cmd.Flags().GetString(params.ChatUserInput) - if userInput == "" { - msg := fmt.Sprintf(UserInputRequiredErrorFormat, params.ChatUserInput, params.ChatConversationID) - logger.PrintIfVerbose(msg) - return outputError(cmd, uuid.Nil, errors.Errorf(msg)) - } - } - - id, err := uuid.Parse(chatConversationID) - if err != nil { - logger.PrintIfVerbose(err.Error()) - return outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)) - } + tenantID := getTenantID(customerToken) - var newMessages []message.Message - if newConversation { - systemPrompt, userPrompt, e := sastchat.BuildPrompt(scanResultsFile, sastResultID, sourceDir) - if e != nil { - logger.PrintIfVerbose(e.Error()) - return outputError(cmd, id, e) - } - newMessages = append(newMessages, message.Message{ - Role: role.System, - Content: systemPrompt, - }, message.Message{ - Role: role.User, - Content: userPrompt, - }) - } else { - newMessages = append(newMessages, message.Message{ - Role: role.User, - Content: userInput, - }) + newConversation, userInput, id, err4, done3 := getSastConversationDetails(cmd, chatConversationID, statefulWrapper) + if done3 { + return err4 } - tenantID, _ := wrappers.ExtractFromTokenClaims(customerToken, tenantIDClaimKey) - if strings.Contains(tenantID, "::") { - tenantID = tenantID[strings.LastIndex(tenantID, "::")+2:] + newMessages, err2, done := buildSastMessages(cmd, newConversation, scanResultsFile, sastResultID, sourceDir, id, userInput) + if done { + return err2 } - requestID := statefulWrapper.GenerateId().String() - var response []message.Message - if azureAiEnabled || checkmarxAiEnabled { - metadata := message.MetaData{ - TenantID: tenantID, - RequestID: requestID, - UserAgent: params.DefaultAgent, - Feature: guidedRemediationFeatureNameSast, - } - if azureAiEnabled { - logger.Printf("Sending message to Azure AI model for SAST guided remediation. RequestID: " + requestID) - } else { - logger.Printf("Sending message to Checkmarx AI model for SAST guided remediation. RequestID: " + requestID) - } - response, err = chatWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) - if err != nil { - return outputError(cmd, id, err) - } - } else if chatGptEnabled { - logger.Printf("Sending message to ChatGPT model for SAST guided remediation. RequestID: " + requestID) - response, err = chatWrapper.Call(statefulWrapper, id, newMessages) - if err != nil { - return outputError(cmd, id, err) - } - } else { - return outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)) + responseContent, done2, err3 := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatWrapper, + id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameSast) + if done2 { + return err3 } - responseContent := getMessageContents(response) - responseContent = sastchat.AddDescriptionForIdentifier(responseContent) return printer.Print(cmd.OutOrStdout(), &OutputModel{ @@ -154,6 +96,53 @@ func runChatSast( } } +func getSastConversationDetails(cmd *cobra.Command, chatConversationID string, statefulWrapper wrapper.StatefulWrapper) (bool, string, uuid.UUID, error, bool) { + newConversation := false + var userInput string + if chatConversationID == "" { + newConversation = true + chatConversationID = statefulWrapper.GenerateId().String() + } else { + userInput, _ = cmd.Flags().GetString(params.ChatUserInput) + if userInput == "" { + msg := fmt.Sprintf(UserInputRequiredErrorFormat, params.ChatUserInput, params.ChatConversationID) + logger.PrintIfVerbose(msg) + return false, "", uuid.UUID{}, outputError(cmd, uuid.Nil, errors.Errorf(msg)), true + } + } + + id, err := uuid.Parse(chatConversationID) + if err != nil { + logger.PrintIfVerbose(err.Error()) + return false, "", uuid.UUID{}, outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)), true + } + return newConversation, userInput, id, nil, false +} + +func buildSastMessages(cmd *cobra.Command, newConversation bool, scanResultsFile string, sastResultID string, sourceDir string, id uuid.UUID, userInput string) ([]message.Message, error, bool) { + var newMessages []message.Message + if newConversation { + systemPrompt, userPrompt, e := sastchat.BuildPrompt(scanResultsFile, sastResultID, sourceDir) + if e != nil { + logger.PrintIfVerbose(e.Error()) + return nil, outputError(cmd, id, e), true + } + newMessages = append(newMessages, message.Message{ + Role: role.System, + Content: systemPrompt, + }, message.Message{ + Role: role.User, + Content: userPrompt, + }) + } else { + newMessages = append(newMessages, message.Message{ + Role: role.User, + Content: userInput, + }) + } + return newMessages, nil, false +} + func CreateStatefulWrapper(cmd *cobra.Command, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) ( statefulWrapper wrapper.StatefulWrapper, customerToken string) { conn := connector.NewFileSystemConnector("") From d5a912162b4e4056eb999b5a7a720e83f2159c71 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 8 Sep 2024 12:24:23 +0300 Subject: [PATCH 42/69] remove todo comments --- internal/commands/chat-sast.go | 4 ++-- internal/commands/chat.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 11d54d743..9295885fb 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -152,11 +152,11 @@ func CreateStatefulWrapper(cmd *cobra.Command, azureAiEnabled, checkmarxAiEnable if azureAiEnabled { aiProxyEndPoint, _ := wrappers.GetURL(aiProxyAzureAIRoute, customerToken) model, _ := GetAzureAiModel(tenantConfigurationResponses) - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) // todo: check final interface + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) } else if checkmarxAiEnabled { aiProxyEndPoint, _ := wrappers.GetURL(aiProxyCheckmarxAIRoute, customerToken) model := checkmarxAiChatModel - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) // todo: check final interface + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) } else { chatModel, _ := cmd.Flags().GetString(params.ChatModel) chatAPIKey, _ := cmd.Flags().GetString(params.ChatAPIKey) diff --git a/internal/commands/chat.go b/internal/commands/chat.go index 0e884fd1b..8a86c6255 100644 --- a/internal/commands/chat.go +++ b/internal/commands/chat.go @@ -8,11 +8,11 @@ import ( const ( ConversationIDErrorFormat = "Invalid conversation ID %s" AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" - AiGuidedRemediationEngine = "scan.config.plugins.aiGuidedRemediationAiEngine" // todo: check final value + AiGuidedRemediationEngine = "scan.config.plugins.aiGuidedRemediationAiEngine" AiGuidedRemediationOpenAiValue = "openai" AiGuidedRemediationAzureAiValue = "azureai" AiGuidedRemediationCheckmarxAiValue = "checkmarxai" - AzureAiModel = "scan.config.plugins.aiGuidedRemediationAzureAiModel" // todo: check final value + AzureAiModel = "scan.config.plugins.aiGuidedRemediationAzureAiModel" ) func NewChatCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) *cobra.Command { From 8eb02feb469f1f05f8b03c56a32e3ae7dd6c27cf Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 8 Sep 2024 12:25:47 +0300 Subject: [PATCH 43/69] remove comment --- internal/commands/chat-sast.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 9295885fb..ed58dc869 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -22,7 +22,7 @@ import ( const UserInputRequiredErrorFormat = "%s is required when %s is provided" const AiGuidedRemediationDisabledError = "The AI Guided Remediation is disabled in your tenant account" -const AllOptionsDisabledError = "All AI Guided Remediation options are disabled in your tenant account" // check final value +const AllOptionsDisabledError = "All AI Guided Remediation options are disabled in your tenant account" func ChatSastSubCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) *cobra.Command { chatSastCmd := &cobra.Command{ From 1acb486ec39f9dcc680a434810979e0998ffc03d Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 8 Sep 2024 12:36:18 +0300 Subject: [PATCH 44/69] move urls to binds --- internal/commands/chat-kics.go | 2 -- internal/commands/chat-sast.go | 10 ++++++++-- internal/params/binds.go | 2 ++ internal/params/envs.go | 2 ++ internal/params/keys.go | 2 ++ 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 17cafd3e1..d9af805f8 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -43,8 +43,6 @@ const FileErrorFormat = "It seems that %s is not available for AI Guided Remedia // chatModel model to use when calling the CheckmarxAI const checkmarxAiChatModel = "gpt-4" -const aiProxyAzureAIRoute = "api/ai-proxy/redirect/externalAzure" -const aiProxyCheckmarxAIRoute = "api/ai-proxy/redirect/azure" const tenantIDClaimKey = "tenant_id" const guidedRemediationFeatureNameKics = "cli-guided-remediation-kics" const guidedRemediationFeatureNameSast = "cli-guided-remediation-sast" diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index ed58dc869..b03c4b7dd 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -96,7 +96,11 @@ func runChatSast( } } -func getSastConversationDetails(cmd *cobra.Command, chatConversationID string, statefulWrapper wrapper.StatefulWrapper) (bool, string, uuid.UUID, error, bool) { +func getSastConversationDetails( + cmd *cobra.Command, + chatConversationID string, + statefulWrapper wrapper.StatefulWrapper, +) (bool, string, uuid.UUID, error, bool) { newConversation := false var userInput string if chatConversationID == "" { @@ -119,7 +123,7 @@ func getSastConversationDetails(cmd *cobra.Command, chatConversationID string, s return newConversation, userInput, id, nil, false } -func buildSastMessages(cmd *cobra.Command, newConversation bool, scanResultsFile string, sastResultID string, sourceDir string, id uuid.UUID, userInput string) ([]message.Message, error, bool) { +func buildSastMessages(cmd *cobra.Command, newConversation bool, scanResultsFile, sastResultID, sourceDir string, id uuid.UUID, userInput string) ([]message.Message, error, bool) { var newMessages []message.Message if newConversation { systemPrompt, userPrompt, e := sastchat.BuildPrompt(scanResultsFile, sastResultID, sourceDir) @@ -150,10 +154,12 @@ func CreateStatefulWrapper(cmd *cobra.Command, azureAiEnabled, checkmarxAiEnable customerToken, _ = wrappers.GetAccessToken() if azureAiEnabled { + aiProxyAzureAIRoute := viper.GetString(params.AiProxyAzureAiRouteKey) aiProxyEndPoint, _ := wrappers.GetURL(aiProxyAzureAIRoute, customerToken) model, _ := GetAzureAiModel(tenantConfigurationResponses) statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) } else if checkmarxAiEnabled { + aiProxyCheckmarxAIRoute := viper.GetString(params.AiProxyCheckmarxAiRouteKey) aiProxyEndPoint, _ := wrappers.GetURL(aiProxyCheckmarxAIRoute, customerToken) model := checkmarxAiChatModel statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) diff --git a/internal/params/binds.go b/internal/params/binds.go index 5f4b3131b..def15d806 100644 --- a/internal/params/binds.go +++ b/internal/params/binds.go @@ -63,4 +63,6 @@ var EnvVarsBinds = []struct { {AccessManagementPathKey, AccessManagementPathEnv, "api/access-management"}, {ByorPathKey, ByorPathEnv, "api/byor"}, {VorpalPortKey, VorpalPortEnv, ""}, + {AiProxyAzureAiRouteKey, AiProxyAzureAiRouteEnv, "api/ai-proxy/redirect/externalAzure"}, + {AiProxyCheckmarxAiRouteKey, AiProxyCheckmarxAiRouteEnv, "api/ai-proxy/redirect/azure"}, } diff --git a/internal/params/envs.go b/internal/params/envs.go index a776100f2..c355a85c4 100644 --- a/internal/params/envs.go +++ b/internal/params/envs.go @@ -62,4 +62,6 @@ const ( ByorPathEnv = "CX_BYOR_PATH" IgnoreProxyEnv = "CX_IGNORE_PROXY" VorpalPortEnv = "CX_VORPAL_PORT" + AiProxyAzureAiRouteEnv = "CX_AIPROXY_AZUREAI_ROUTE" + AiProxyCheckmarxAiRouteEnv = "CX_AIPROXY_CHECKMARXAI_ROUTE" ) diff --git a/internal/params/keys.go b/internal/params/keys.go index 8dcb84e95..613951282 100644 --- a/internal/params/keys.go +++ b/internal/params/keys.go @@ -62,4 +62,6 @@ var ( AccessManagementPathKey = strings.ToLower(AccessManagementPathEnv) ByorPathKey = strings.ToLower(ByorPathEnv) VorpalPortKey = strings.ToLower(VorpalPortEnv) + AiProxyAzureAiRouteKey = strings.ToLower(AiProxyAzureAiRouteEnv) + AiProxyCheckmarxAiRouteKey = strings.ToLower(AiProxyCheckmarxAiRouteEnv) ) From a46f73b4a261958d8ef3204cd0fad65ffcc2c5dd Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 10:55:55 +0300 Subject: [PATCH 45/69] add names to return values --- internal/commands/chat-sast.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index b03c4b7dd..0a91b89d8 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -100,9 +100,8 @@ func getSastConversationDetails( cmd *cobra.Command, chatConversationID string, statefulWrapper wrapper.StatefulWrapper, -) (bool, string, uuid.UUID, error, bool) { +) (isNewConversation bool, userInput string, conversationId uuid.UUID, err error, done bool) { newConversation := false - var userInput string if chatConversationID == "" { newConversation = true chatConversationID = statefulWrapper.GenerateId().String() From 8cf7770678c17c5f3cefbb6975d71356379ba6f4 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 11:18:42 +0300 Subject: [PATCH 46/69] fix lints --- internal/commands/chat-kics.go | 29 +++++++++++++---------------- internal/commands/chat-sast.go | 23 ++++++++++------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index d9af805f8..fe4016b83 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -2,12 +2,12 @@ package commands import ( "fmt" - gptWrapper "github.com/Checkmarx/gen-ai-wrapper/pkg/wrapper" "os" "strings" "github.com/Checkmarx/gen-ai-wrapper/pkg/message" "github.com/Checkmarx/gen-ai-wrapper/pkg/role" + gptWrapper "github.com/Checkmarx/gen-ai-wrapper/pkg/wrapper" "github.com/checkmarx/ast-cli/internal/commands/util/printer" "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" @@ -90,11 +90,8 @@ func runChatKics( chatResultVulnerability, _ := cmd.Flags().GetString(params.ChatKicsResultVulnerability) userInput, _ := cmd.Flags().GetString(params.ChatUserInput) - var chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool - var tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse - - chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, err2, done := - getEngineSelection(cmd, chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, tenantWrapper) + chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, done, err2 := + getEngineSelection(cmd, tenantWrapper) if done { return err2 } @@ -103,7 +100,7 @@ func runChatKics( tenantID := getTenantID(customerToken) - id, err4, done3 := getKicsConversationId(cmd, chatConversationID, statefulWrapper) + id, done3, err4 := getKicsConversationId(cmd, chatConversationID, statefulWrapper) if done3 { return err4 } @@ -129,7 +126,7 @@ func runChatKics( } } -func getKicsConversationId(cmd *cobra.Command, chatConversationID string, statefulWrapper gptWrapper.StatefulWrapper) (uuid.UUID, error, bool) { +func getKicsConversationId(cmd *cobra.Command, chatConversationID string, statefulWrapper gptWrapper.StatefulWrapper) (uuid.UUID, bool, error) { if chatConversationID == "" { chatConversationID = statefulWrapper.GenerateId().String() } @@ -137,9 +134,9 @@ func getKicsConversationId(cmd *cobra.Command, chatConversationID string, statef id, err := uuid.Parse(chatConversationID) if err != nil { logger.PrintIfVerbose(err.Error()) - return uuid.UUID{}, outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)), true + return uuid.UUID{}, true, outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)) } - return id, err, false + return id, false, err } func getTenantID(customerToken string) string { @@ -153,11 +150,10 @@ func getTenantID(customerToken string) string { func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, chatGptEnabled bool, - featureName string) ([]string, bool, error) { + featureName string) (responseContent []string, done bool, err error) { requestID := statefulWrapper.GenerateId().String() var response []message.Message - var err error if azureAiEnabled || checkmarxAiEnabled { metadata := message.MetaData{ @@ -185,11 +181,12 @@ func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, return nil, true, outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)) } - responseContent := getMessageContents(response) + responseContent = getMessageContents(response) return responseContent, false, nil } -func getEngineSelection(cmd *cobra.Command, chatGptEnabled bool, azureAiEnabled bool, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, tenantWrapper wrappers.TenantConfigurationWrapper) (bool, bool, bool, *[]*wrappers.TenantConfigurationResponse, error, bool) { +func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) (chatGptEnabled bool, azureAiEnabled bool, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, done bool, err error) { + if !isCxOneAPIKeyAvailable() { chatGptEnabled = true azureAiEnabled = false @@ -199,14 +196,14 @@ func getEngineSelection(cmd *cobra.Command, chatGptEnabled bool, azureAiEnabled var err error tenantConfigurationResponses, err = GetTenantConfigurationResponses(tenantWrapper) if err != nil { - return false, false, false, nil, outputError(cmd, uuid.Nil, err), true + return false, false, false, nil, true, outputError(cmd, uuid.Nil, err) } azureAiEnabled = isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled = isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) chatGptEnabled = isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses) } - return chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, nil, false + return chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, false, nil } func buildKicsMessages(chatResultCode []byte, diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 0a91b89d8..90de71189 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -71,12 +71,12 @@ func runChatSast( tenantID := getTenantID(customerToken) - newConversation, userInput, id, err4, done3 := getSastConversationDetails(cmd, chatConversationID, statefulWrapper) + newConversation, userInput, id, done3, err4 := getSastConversationDetails(cmd, chatConversationID, statefulWrapper) if done3 { return err4 } - newMessages, err2, done := buildSastMessages(cmd, newConversation, scanResultsFile, sastResultID, sourceDir, id, userInput) + newMessages, done, err2 := buildSastMessages(cmd, newConversation, scanResultsFile, sastResultID, sourceDir, id, userInput) if done { return err2 } @@ -96,11 +96,8 @@ func runChatSast( } } -func getSastConversationDetails( - cmd *cobra.Command, - chatConversationID string, - statefulWrapper wrapper.StatefulWrapper, -) (isNewConversation bool, userInput string, conversationId uuid.UUID, err error, done bool) { +func getSastConversationDetails(cmd *cobra.Command, chatConversationID string, statefulWrapper wrapper.StatefulWrapper) ( + isNewConversation bool, userInput string, conversationID uuid.UUID, done bool, err error) { newConversation := false if chatConversationID == "" { newConversation = true @@ -110,25 +107,25 @@ func getSastConversationDetails( if userInput == "" { msg := fmt.Sprintf(UserInputRequiredErrorFormat, params.ChatUserInput, params.ChatConversationID) logger.PrintIfVerbose(msg) - return false, "", uuid.UUID{}, outputError(cmd, uuid.Nil, errors.Errorf(msg)), true + return false, "", uuid.UUID{}, true, outputError(cmd, uuid.Nil, errors.Errorf(msg)) } } id, err := uuid.Parse(chatConversationID) if err != nil { logger.PrintIfVerbose(err.Error()) - return false, "", uuid.UUID{}, outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)), true + return false, "", uuid.UUID{}, true, outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)) } - return newConversation, userInput, id, nil, false + return newConversation, userInput, id, false, nil } -func buildSastMessages(cmd *cobra.Command, newConversation bool, scanResultsFile, sastResultID, sourceDir string, id uuid.UUID, userInput string) ([]message.Message, error, bool) { +func buildSastMessages(cmd *cobra.Command, newConversation bool, scanResultsFile, sastResultID, sourceDir string, id uuid.UUID, userInput string) ([]message.Message, bool, error) { var newMessages []message.Message if newConversation { systemPrompt, userPrompt, e := sastchat.BuildPrompt(scanResultsFile, sastResultID, sourceDir) if e != nil { logger.PrintIfVerbose(e.Error()) - return nil, outputError(cmd, id, e), true + return nil, true, outputError(cmd, id, e) } newMessages = append(newMessages, message.Message{ Role: role.System, @@ -143,7 +140,7 @@ func buildSastMessages(cmd *cobra.Command, newConversation bool, scanResultsFile Content: userInput, }) } - return newMessages, nil, false + return newMessages, false, nil } func CreateStatefulWrapper(cmd *cobra.Command, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) ( From 669e7adfb7ea6ad98fb2baa8da2c3dfe825dc626 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 11:29:43 +0300 Subject: [PATCH 47/69] fix lints --- internal/commands/chat-kics.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index fe4016b83..e28571eed 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -100,7 +100,7 @@ func runChatKics( tenantID := getTenantID(customerToken) - id, done3, err4 := getKicsConversationId(cmd, chatConversationID, statefulWrapper) + id, done3, err4 := getKicsConversationID(cmd, chatConversationID, statefulWrapper) if done3 { return err4 } @@ -126,7 +126,7 @@ func runChatKics( } } -func getKicsConversationId(cmd *cobra.Command, chatConversationID string, statefulWrapper gptWrapper.StatefulWrapper) (uuid.UUID, bool, error) { +func getKicsConversationID(cmd *cobra.Command, chatConversationID string, statefulWrapper gptWrapper.StatefulWrapper) (uuid.UUID, bool, error) { if chatConversationID == "" { chatConversationID = statefulWrapper.GenerateId().String() } @@ -185,7 +185,7 @@ func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, return responseContent, false, nil } -func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) (chatGptEnabled bool, azureAiEnabled bool, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, done bool, err error) { +func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) (chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, done bool, err error) { if !isCxOneAPIKeyAvailable() { chatGptEnabled = true From bbdfb88ad701fbc777ccc75674459a01a97d7616 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 11:43:39 +0300 Subject: [PATCH 48/69] fix lints --- internal/commands/chat-kics.go | 40 +++++++++++++++++----------------- internal/commands/chat-sast.go | 32 +++++++++++++-------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index e28571eed..50ea3ddd4 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -90,19 +90,19 @@ func runChatKics( chatResultVulnerability, _ := cmd.Flags().GetString(params.ChatKicsResultVulnerability) userInput, _ := cmd.Flags().GetString(params.ChatUserInput) - chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, done, err2 := + chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, err := getEngineSelection(cmd, tenantWrapper) - if done { - return err2 + if err != nil { + return err } statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) tenantID := getTenantID(customerToken) - id, done3, err4 := getKicsConversationID(cmd, chatConversationID, statefulWrapper) - if done3 { - return err4 + id, err := getKicsConversationID(cmd, chatConversationID, statefulWrapper) + if err != nil { + return err } chatResultCode, err := os.ReadFile(chatResultFile) @@ -113,10 +113,10 @@ func runChatKics( newMessages := buildKicsMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) - responseContent, done2, err3 := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, + responseContent, err := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameKics) - if done2 { - return err3 + if err != nil { + return err } return printer.Print(cmd.OutOrStdout(), &OutputModel{ @@ -126,7 +126,7 @@ func runChatKics( } } -func getKicsConversationID(cmd *cobra.Command, chatConversationID string, statefulWrapper gptWrapper.StatefulWrapper) (uuid.UUID, bool, error) { +func getKicsConversationID(cmd *cobra.Command, chatConversationID string, statefulWrapper gptWrapper.StatefulWrapper) (uuid.UUID, error) { if chatConversationID == "" { chatConversationID = statefulWrapper.GenerateId().String() } @@ -134,9 +134,9 @@ func getKicsConversationID(cmd *cobra.Command, chatConversationID string, statef id, err := uuid.Parse(chatConversationID) if err != nil { logger.PrintIfVerbose(err.Error()) - return uuid.UUID{}, true, outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)) + return uuid.UUID{}, outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)) } - return id, false, err + return id, err } func getTenantID(customerToken string) string { @@ -150,7 +150,7 @@ func getTenantID(customerToken string) string { func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, chatGptEnabled bool, - featureName string) (responseContent []string, done bool, err error) { + featureName string) (responseContent []string, err error) { requestID := statefulWrapper.GenerateId().String() var response []message.Message @@ -169,23 +169,23 @@ func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, } response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { - return nil, true, outputError(cmd, id, err) + return nil, outputError(cmd, id, err) } } else if chatGptEnabled { logger.Printf("Sending message to ChatGPT model for " + featureName + " guided remediation. RequestID: " + requestID) response, err = chatKicsWrapper.Call(statefulWrapper, id, newMessages) if err != nil { - return nil, true, outputError(cmd, id, err) + return nil, outputError(cmd, id, err) } } else { - return nil, true, outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)) + return nil, outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)) } responseContent = getMessageContents(response) - return responseContent, false, nil + return responseContent, nil } -func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) (chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, done bool, err error) { +func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) (chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, err error) { if !isCxOneAPIKeyAvailable() { chatGptEnabled = true @@ -196,14 +196,14 @@ func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigu var err error tenantConfigurationResponses, err = GetTenantConfigurationResponses(tenantWrapper) if err != nil { - return false, false, false, nil, true, outputError(cmd, uuid.Nil, err) + return false, false, false, nil, outputError(cmd, uuid.Nil, err) } azureAiEnabled = isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled = isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) chatGptEnabled = isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses) } - return chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, false, nil + return chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, nil } func buildKicsMessages(chatResultCode []byte, diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 90de71189..bd81cdf5a 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -71,20 +71,20 @@ func runChatSast( tenantID := getTenantID(customerToken) - newConversation, userInput, id, done3, err4 := getSastConversationDetails(cmd, chatConversationID, statefulWrapper) - if done3 { - return err4 + newConversation, userInput, id, err := getSastConversationDetails(cmd, chatConversationID, statefulWrapper) + if err != nil { + return err } - newMessages, done, err2 := buildSastMessages(cmd, newConversation, scanResultsFile, sastResultID, sourceDir, id, userInput) - if done { - return err2 + newMessages, err := buildSastMessages(cmd, newConversation, scanResultsFile, sastResultID, sourceDir, id, userInput) + if err != nil { + return err } - responseContent, done2, err3 := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatWrapper, + responseContent, err := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatWrapper, id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameSast) - if done2 { - return err3 + if err != nil { + return err } responseContent = sastchat.AddDescriptionForIdentifier(responseContent) @@ -97,7 +97,7 @@ func runChatSast( } func getSastConversationDetails(cmd *cobra.Command, chatConversationID string, statefulWrapper wrapper.StatefulWrapper) ( - isNewConversation bool, userInput string, conversationID uuid.UUID, done bool, err error) { + isNewConversation bool, userInput string, conversationID uuid.UUID, err error) { newConversation := false if chatConversationID == "" { newConversation = true @@ -107,25 +107,25 @@ func getSastConversationDetails(cmd *cobra.Command, chatConversationID string, s if userInput == "" { msg := fmt.Sprintf(UserInputRequiredErrorFormat, params.ChatUserInput, params.ChatConversationID) logger.PrintIfVerbose(msg) - return false, "", uuid.UUID{}, true, outputError(cmd, uuid.Nil, errors.Errorf(msg)) + return false, "", uuid.UUID{}, outputError(cmd, uuid.Nil, errors.Errorf(msg)) } } id, err := uuid.Parse(chatConversationID) if err != nil { logger.PrintIfVerbose(err.Error()) - return false, "", uuid.UUID{}, true, outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)) + return false, "", uuid.UUID{}, outputError(cmd, id, errors.Errorf(ConversationIDErrorFormat, chatConversationID)) } - return newConversation, userInput, id, false, nil + return newConversation, userInput, id, nil } -func buildSastMessages(cmd *cobra.Command, newConversation bool, scanResultsFile, sastResultID, sourceDir string, id uuid.UUID, userInput string) ([]message.Message, bool, error) { +func buildSastMessages(cmd *cobra.Command, newConversation bool, scanResultsFile, sastResultID, sourceDir string, id uuid.UUID, userInput string) ([]message.Message, error) { var newMessages []message.Message if newConversation { systemPrompt, userPrompt, e := sastchat.BuildPrompt(scanResultsFile, sastResultID, sourceDir) if e != nil { logger.PrintIfVerbose(e.Error()) - return nil, true, outputError(cmd, id, e) + return nil, outputError(cmd, id, e) } newMessages = append(newMessages, message.Message{ Role: role.System, @@ -140,7 +140,7 @@ func buildSastMessages(cmd *cobra.Command, newConversation bool, scanResultsFile Content: userInput, }) } - return newMessages, false, nil + return newMessages, nil } func CreateStatefulWrapper(cmd *cobra.Command, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) ( From 1fcf17113374174be3818b9fe15aefbf202684ec Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 11:46:05 +0300 Subject: [PATCH 49/69] fix lint --- internal/commands/chat-kics.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 50ea3ddd4..e3f8222db 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -185,7 +185,8 @@ func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, return responseContent, nil } -func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) (chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, err error) { +func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) ( + chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, err error) { if !isCxOneAPIKeyAvailable() { chatGptEnabled = true From 6993b5a8637dcc4bb58f4002da999f88ceaa3fc6 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 11:47:54 +0300 Subject: [PATCH 50/69] fix lint --- internal/commands/chat-kics.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index e3f8222db..411e53d3d 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -185,8 +185,8 @@ func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, return responseContent, nil } -func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) ( - chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, err error) { +func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) (chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool, + tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, err error) { if !isCxOneAPIKeyAvailable() { chatGptEnabled = true From adf14d95363aad18cefa74acf480165eb6eb57ec Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 11:51:05 +0300 Subject: [PATCH 51/69] fix lint --- internal/commands/chat-kics.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 411e53d3d..a72aa8645 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -187,7 +187,6 @@ func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) (chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, err error) { - if !isCxOneAPIKeyAvailable() { chatGptEnabled = true azureAiEnabled = false From 06efbc44f46c4c0e07741f70858d64672c76d25d Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 14:33:09 +0300 Subject: [PATCH 52/69] fix some tests --- internal/commands/chat-kics_test.go | 8 ++++++ internal/commands/chat-sast_test.go | 44 +++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/internal/commands/chat-kics_test.go b/internal/commands/chat-kics_test.go index df32021eb..c110cee33 100644 --- a/internal/commands/chat-kics_test.go +++ b/internal/commands/chat-kics_test.go @@ -75,6 +75,10 @@ func TestChatKicsAzureAICorrectResponse(t *testing.T) { Key: "scan.config.plugins.azureAiGuidedRemediation", Value: "true", }, + { + Key: "scan.config.plugins.aiGuidedRemediationAiEngine", + Value: "azureai", + }, } origAPIKey := viper.GetString(params.AstAPIKey) viper.Set(params.AstAPIKey, "SomeKey") @@ -107,6 +111,10 @@ func TestChatKicsCheckmarxAICorrectResponse(t *testing.T) { Key: "scan.config.plugins.checkmarxAiGuidedRemediation", Value: "true", }, + { + Key: "scan.config.plugins.aiGuidedRemediationAiEngine", + Value: "checkmarxai", + }, } origAPIKey := viper.GetString(params.AstAPIKey) viper.Set(params.AstAPIKey, "SomeKey") diff --git a/internal/commands/chat-sast_test.go b/internal/commands/chat-sast_test.go index 6418fddd5..447eca86e 100644 --- a/internal/commands/chat-sast_test.go +++ b/internal/commands/chat-sast_test.go @@ -112,6 +112,20 @@ func TestChatSastInvalidSourceDir(t *testing.T) { } func TestChatSastFirstMessageCorrectResponse(t *testing.T) { + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{ + { + Key: "scan.config.plugins.ideScans", + Value: "true", + }, + { + Key: "scan.config.plugins.aiGuidedRemediationAiEngine", + Value: "openai", + }, + { + Key: "scan.config.plugins.aiGuidedRemediation", + Value: "true", + }, + } buffer, err := executeRedirectedTestCommand("chat", "sast", "--chat-apikey", "apiKey", "--scan-results-file", "./data/cx_result.json", @@ -131,8 +145,8 @@ func TestChatSastAzureAIFirstMessageCorrectResponse(t *testing.T) { Value: "true", }, { - Key: "scan.config.plugins.azureAiGuidedRemediation", - Value: "true", + Key: "scan.config.plugins.aiGuidedRemediationAiEngine", + Value: "azureai", }, { Key: "scan.config.plugins.aiGuidedRemediation", @@ -158,8 +172,8 @@ func TestChatSastCheckmarxAIFirstMessageCorrectResponse(t *testing.T) { Value: "true", }, { - Key: "scan.config.plugins.checkmarxAiGuidedRemediation", - Value: "true", + Key: "scan.config.plugins.aiGuidedRemediationAiEngine", + Value: "checkmarxai", }, { Key: "scan.config.plugins.aiGuidedRemediation", @@ -179,6 +193,20 @@ func TestChatSastCheckmarxAIFirstMessageCorrectResponse(t *testing.T) { } func TestChatSastSecondMessageCorrectResponse(t *testing.T) { + mock.TenantConfiguration = []*wrappers.TenantConfigurationResponse{ + { + Key: "scan.config.plugins.ideScans", + Value: "true", + }, + { + Key: "scan.config.plugins.aiGuidedRemediationAiEngine", + Value: "openai", + }, + { + Key: "scan.config.plugins.aiGuidedRemediation", + Value: "true", + }, + } buffer, err := executeRedirectedTestCommand("chat", "sast", "--chat-apikey", "apiKey", "--scan-results-file", "./data/cx_result.json", @@ -200,8 +228,8 @@ func TestChatSastAzureAISecondMessageCorrectResponse(t *testing.T) { Value: "true", }, { - Key: "scan.config.plugins.azureAiGuidedRemediation", - Value: "true", + Key: "scan.config.plugins.aiGuidedRemediationAiEngine", + Value: "azureai", }, { Key: "scan.config.plugins.aiGuidedRemediation", @@ -229,8 +257,8 @@ func TestChatSastCheckmarxAISecondMessageCorrectResponse(t *testing.T) { Value: "true", }, { - Key: "scan.config.plugins.checkmarxAiGuidedRemediation", - Value: "true", + Key: "scan.config.plugins.aiGuidedRemediationAiEngine", + Value: "checkmarxai", }, { Key: "scan.config.plugins.aiGuidedRemediation", From c82b5e7d41755f967438136ca91aa56dfc78ea97 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 16:39:49 +0300 Subject: [PATCH 53/69] fix test --- internal/commands/chat-kics.go | 8 ++++---- internal/commands/chat-sast.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index a72aa8645..a50b4f5ab 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -116,7 +116,7 @@ func runChatKics( responseContent, err := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameKics) if err != nil { - return err + return outputError(cmd, id, err) } return printer.Print(cmd.OutOrStdout(), &OutputModel{ @@ -169,16 +169,16 @@ func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, } response, err = chatKicsWrapper.SecureCall(statefulWrapper, id, newMessages, &metadata, customerToken) if err != nil { - return nil, outputError(cmd, id, err) + return nil, err } } else if chatGptEnabled { logger.Printf("Sending message to ChatGPT model for " + featureName + " guided remediation. RequestID: " + requestID) response, err = chatKicsWrapper.Call(statefulWrapper, id, newMessages) if err != nil { - return nil, outputError(cmd, id, err) + return nil, err } } else { - return nil, outputError(cmd, uuid.Nil, errors.Errorf(AllOptionsDisabledError)) + return nil, errors.Errorf(AllOptionsDisabledError) } responseContent = getMessageContents(response) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index bd81cdf5a..59898af71 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -84,7 +84,7 @@ func runChatSast( responseContent, err := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatWrapper, id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameSast) if err != nil { - return err + return outputError(cmd, id, err) } responseContent = sastchat.AddDescriptionForIdentifier(responseContent) From 7d0a157273fbff9f8a20eacd2fc4a0c504db00d3 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 16:47:00 +0300 Subject: [PATCH 54/69] fix lint warning --- internal/commands/chat-kics.go | 7 ++----- internal/commands/chat-sast.go | 3 +-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index a50b4f5ab..8976ace1d 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -113,8 +113,7 @@ func runChatKics( newMessages := buildKicsMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) - responseContent, err := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, - id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameKics) + responseContent, err := sendRequest(statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameKics) if err != nil { return outputError(cmd, id, err) } @@ -148,9 +147,7 @@ func getTenantID(customerToken string) string { return tenantID } -func sendRequest(cmd *cobra.Command, statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, - chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, chatGptEnabled bool, - featureName string) (responseContent []string, err error) { +func sendRequest(statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, chatGptEnabled bool, featureName string) (responseContent []string, err error) { requestID := statefulWrapper.GenerateId().String() var response []message.Message diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 59898af71..c9907a7e1 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -81,8 +81,7 @@ func runChatSast( return err } - responseContent, err := sendRequest(cmd, statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatWrapper, - id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameSast) + responseContent, err := sendRequest(statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatWrapper, id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameSast) if err != nil { return outputError(cmd, id, err) } From 3a0d96b6a345aa46dd219cf9efe71b1f30803a4b Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 16:50:56 +0300 Subject: [PATCH 55/69] fix lints --- internal/commands/chat-kics.go | 6 ++++-- internal/commands/chat-sast.go | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 8976ace1d..506d40535 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -113,7 +113,8 @@ func runChatKics( newMessages := buildKicsMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) - responseContent, err := sendRequest(statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameKics) + responseContent, err := sendRequest(statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, id, + newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameKics) if err != nil { return outputError(cmd, id, err) } @@ -147,7 +148,8 @@ func getTenantID(customerToken string) string { return tenantID } -func sendRequest(statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, chatGptEnabled bool, featureName string) (responseContent []string, err error) { +func sendRequest(statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, + chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, chatGptEnabled bool, featureName string) (responseContent []string, err error) { requestID := statefulWrapper.GenerateId().String() var response []message.Message diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index c9907a7e1..f58d23f74 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -81,7 +81,8 @@ func runChatSast( return err } - responseContent, err := sendRequest(statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatWrapper, id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameSast) + responseContent, err := sendRequest(statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, + chatWrapper, id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameSast) if err != nil { return outputError(cmd, id, err) } From b08c684f3532a82b40007ee35caa7b4737e66317 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 12 Sep 2024 16:53:46 +0300 Subject: [PATCH 56/69] fix lint --- internal/commands/chat-kics.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 506d40535..4178102d3 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -149,7 +149,8 @@ func getTenantID(customerToken string) string { } func sendRequest(statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, - chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, chatGptEnabled bool, featureName string) (responseContent []string, err error) { + chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, + chatGptEnabled bool, featureName string) (responseContent []string, err error) { requestID := statefulWrapper.GenerateId().String() var response []message.Message From ae0a7d57c0417d25a1aa35f566390faa423e4e77 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 15 Sep 2024 09:33:57 +0300 Subject: [PATCH 57/69] made default selection to be chatgpt --- internal/commands/chat-kics.go | 23 ++++++++--------------- internal/commands/chat-sast.go | 5 +---- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 4178102d3..d2453a468 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -90,8 +90,7 @@ func runChatKics( chatResultVulnerability, _ := cmd.Flags().GetString(params.ChatKicsResultVulnerability) userInput, _ := cmd.Flags().GetString(params.ChatUserInput) - chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, err := - getEngineSelection(cmd, tenantWrapper) + azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, err := getEngineSelection(cmd, tenantWrapper) if err != nil { return err } @@ -113,8 +112,7 @@ func runChatKics( newMessages := buildKicsMessages(chatResultCode, chatResultVulnerability, chatResultLine, chatResultSeverity, userInput) - responseContent, err := sendRequest(statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, id, - newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameKics) + responseContent, err := sendRequest(statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatKicsWrapper, id, newMessages, customerToken, guidedRemediationFeatureNameKics) if err != nil { return outputError(cmd, id, err) } @@ -148,9 +146,8 @@ func getTenantID(customerToken string) string { return tenantID } -func sendRequest(statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, - chatKicsWrapper wrappers.ChatWrapper, id uuid.UUID, newMessages []message.Message, customerToken string, - chatGptEnabled bool, featureName string) (responseContent []string, err error) { +func sendRequest(statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool, checkmarxAiEnabled bool, tenantID string, chatKicsWrapper wrappers.ChatWrapper, + id uuid.UUID, newMessages []message.Message, customerToken string, featureName string) (responseContent []string, err error) { requestID := statefulWrapper.GenerateId().String() var response []message.Message @@ -171,24 +168,21 @@ func sendRequest(statefulWrapper gptWrapper.StatefulWrapper, azureAiEnabled bool if err != nil { return nil, err } - } else if chatGptEnabled { + } else { // if chatgpt is enabled or no engine is enabled logger.Printf("Sending message to ChatGPT model for " + featureName + " guided remediation. RequestID: " + requestID) response, err = chatKicsWrapper.Call(statefulWrapper, id, newMessages) if err != nil { return nil, err } - } else { - return nil, errors.Errorf(AllOptionsDisabledError) } responseContent = getMessageContents(response) return responseContent, nil } -func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) (chatGptEnabled, azureAiEnabled, checkmarxAiEnabled bool, +func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigurationWrapper) (azureAiEnabled, checkmarxAiEnabled bool, tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse, err error) { if !isCxOneAPIKeyAvailable() { - chatGptEnabled = true azureAiEnabled = false checkmarxAiEnabled = false logger.Printf("CxOne API key is not available, ChatGPT model will be used for guided remediation.") @@ -196,14 +190,13 @@ func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigu var err error tenantConfigurationResponses, err = GetTenantConfigurationResponses(tenantWrapper) if err != nil { - return false, false, false, nil, outputError(cmd, uuid.Nil, err) + return false, false, nil, outputError(cmd, uuid.Nil, err) } azureAiEnabled = isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled = isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) - chatGptEnabled = isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses) } - return chatGptEnabled, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, nil + return azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, nil } func buildKicsMessages(chatResultCode []byte, diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index f58d23f74..cfcb357d0 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -22,7 +22,6 @@ import ( const UserInputRequiredErrorFormat = "%s is required when %s is provided" const AiGuidedRemediationDisabledError = "The AI Guided Remediation is disabled in your tenant account" -const AllOptionsDisabledError = "All AI Guided Remediation options are disabled in your tenant account" func ChatSastSubCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) *cobra.Command { chatSastCmd := &cobra.Command{ @@ -65,7 +64,6 @@ func runChatSast( sastResultID, _ := cmd.Flags().GetString(params.ChatSastResultID) azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) - chatGptEnabled := isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses) statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) @@ -81,8 +79,7 @@ func runChatSast( return err } - responseContent, err := sendRequest(statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, - chatWrapper, id, newMessages, customerToken, chatGptEnabled, guidedRemediationFeatureNameSast) + responseContent, err := sendRequest(statefulWrapper, azureAiEnabled, checkmarxAiEnabled, tenantID, chatWrapper, id, newMessages, customerToken, guidedRemediationFeatureNameSast) if err != nil { return outputError(cmd, id, err) } From d43b5b6c1de649f4e4bc541dc7c40749102b724a Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 15 Sep 2024 09:35:41 +0300 Subject: [PATCH 58/69] remove isOpenAiGuidedRemediationEnabled --- internal/commands/chat-sast.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index cfcb357d0..b5a67c396 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -225,15 +225,6 @@ func isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrap return isEnabled } -func isOpenAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { - engine, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationEngine) - if err != nil { - return false - } - isEnabled := strings.EqualFold(engine, AiGuidedRemediationOpenAiValue) - return isEnabled -} - func GetAzureAiModel(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (string, error) { return GetTenantConfiguration(tenantConfigurationResponses, AzureAiModel) } From fbb213fa3af21385511f8bd1b5c6d4c74ac086da Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Sun, 15 Sep 2024 12:38:36 +0300 Subject: [PATCH 59/69] update to gen-ai-wrapper 1.0.2 --- go.mod | 8 ++++---- go.sum | 14 ++++++-------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 693d39fd8..81d40c7d3 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.22.7 require ( github.com/Checkmarx/gen-ai-prompts v0.0.0-20240807143411-708ceec12b63 - github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb + github.com/Checkmarx/gen-ai-wrapper v1.0.2 github.com/CheckmarxDev/containers-resolver v1.0.10 github.com/MakeNowJust/heredoc v1.0.0 github.com/golang-jwt/jwt v3.2.2+incompatible @@ -16,9 +16,10 @@ require ( github.com/mssola/user_agent v0.6.0 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.8.1 - github.com/spf13/viper v1.18.2 + github.com/spf13/viper v1.19.0 github.com/stretchr/testify v1.9.0 github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 + github.com/xeipuuv/gojsonschema v1.2.0 golang.org/x/crypto v0.26.0 golang.org/x/sync v0.8.0 golang.org/x/text v0.17.0 @@ -190,7 +191,7 @@ require ( github.com/opencontainers/selinux v1.11.0 // indirect github.com/pborman/indent v1.2.1 // indirect github.com/pelletier/go-toml v1.9.5 // indirect - github.com/pelletier/go-toml/v2 v2.2.1 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pierrec/lz4/v4 v4.1.21 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect @@ -232,7 +233,6 @@ require ( github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect - github.com/xeipuuv/gojsonschema v1.2.0 // indirect github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect github.com/xlab/treeprint v1.2.0 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect diff --git a/go.sum b/go.sum index c566c511c..e094d77e0 100644 --- a/go.sum +++ b/go.sum @@ -62,8 +62,8 @@ github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/Checkmarx/gen-ai-prompts v0.0.0-20240807143411-708ceec12b63 h1:SCuTcE+CFvgjbIxUNL8rsdB2sAhfuNx85HvxImKta3g= github.com/Checkmarx/gen-ai-prompts v0.0.0-20240807143411-708ceec12b63/go.mod h1:MI6lfLerXU+5eTV/EPTDavgnV3owz3GPT4g/msZBWPo= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb h1:6dlsDwdf7H7aA/4CynxCaHF7wzTiV89QrWsUOQ9Sk1g= -github.com/Checkmarx/gen-ai-wrapper v1.0.2-0.20240623072149-3ad82f7be6fb/go.mod h1:Lu4bJHWIHPrh1eyfJN0BVvTdHC3C3X4aGtBxekYXtGM= +github.com/Checkmarx/gen-ai-wrapper v1.0.2 h1:T6X40+4hYnwfDsvkjWs9VIcE6s1O+8DUu0+sDdCY3GI= +github.com/Checkmarx/gen-ai-wrapper v1.0.2/go.mod h1:xwRLefezwNNnRGu1EjGS6wNiR9FVV/eP9D+oXwLViVM= github.com/CheckmarxDev/containers-resolver v1.0.10 h1:Co9tKzvcQYtmAP/iendcBcUHIZRwiCEQhSXigTXQ4xM= github.com/CheckmarxDev/containers-resolver v1.0.10/go.mod h1:i9ZTKip7/EuzXxlW1FdGzAdWooAy0fwzkuwFBJnvcE4= github.com/CycloneDX/cyclonedx-go v0.9.0 h1:inaif7qD8bivyxp7XLgxUYtOXWtDez7+j72qKTMQTb8= @@ -195,8 +195,6 @@ github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXD github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw= github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4= github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ= -github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c h1:oKI4C1dXYpi0B8pltDDzp1ZRiyeILv5enbp9h4ASQ3s= -github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c/go.mod h1:l+0rISRGaps2HWkpvKbYPE1nsNx28vBj6bKorEm1M5o= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -759,8 +757,8 @@ github.com/pborman/indent v1.2.1/go.mod h1:FitS+t35kIYtB5xWTZAPhnmrxcciEEOdbyrrp github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= -github.com/pelletier/go-toml/v2 v2.2.1 h1:9TA9+T8+8CUCO2+WYnDLCgrYi9+omqKXyjDtosvtEhg= -github.com/pelletier/go-toml/v2 v2.2.1/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI= @@ -881,8 +879,8 @@ github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0 github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.10.0/go.mod h1:SoyBPwAtKDzypXNDFKN5kzH7ppppbGZtls1UpIy5AsM= -github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= -github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= +github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= From fad54bcaa739335f4922b1657d8f14612d119777 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Mon, 16 Sep 2024 18:31:04 +0300 Subject: [PATCH 60/69] Add integration test for kics azure ai wrong api --- internal/wrappers/mock/tenant-mock.go | 8 +++- test/integration/chat_test.go | 55 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/internal/wrappers/mock/tenant-mock.go b/internal/wrappers/mock/tenant-mock.go index 887f9e486..4cacbd990 100644 --- a/internal/wrappers/mock/tenant-mock.go +++ b/internal/wrappers/mock/tenant-mock.go @@ -23,10 +23,14 @@ func (t TenantConfigurationMockWrapper) GetTenantConfiguration() ( Value: "true", }, { - Key: "scan.config.plugins.chatGPTGuidedRemediation", - Value: "true", + Key: "scan.config.plugins.aiGuidedRemediationAiEngine", + Value: "azureai", }, } } return &TenantConfiguration, nil, nil } + +func (t TenantConfigurationMockWrapper) SetTenantConfiguration(response []*wrappers.TenantConfigurationResponse) { + TenantConfiguration = response +} diff --git a/test/integration/chat_test.go b/test/integration/chat_test.go index 7d672787d..675b77d19 100644 --- a/test/integration/chat_test.go +++ b/test/integration/chat_test.go @@ -3,14 +3,21 @@ package integration import ( + "bytes" "strings" "testing" "github.com/checkmarx/ast-cli/internal/commands" + "github.com/checkmarx/ast-cli/internal/wrappers" + "github.com/checkmarx/ast-cli/internal/wrappers/mock" "github.com/google/uuid" "gotest.tools/assert" ) +const ( + INCORRECT_API_ERROR = "Error Code: 401, Access denied due to invalid subscription key or wrong API endpoint" +) + func TestChatKicsInvalidAPIKey(t *testing.T) { args := []string{ "chat", "kics", @@ -43,3 +50,51 @@ func TestChatSastInvalidAPIKey(t *testing.T) { unmarshall(t, respBuffer, &outputModel, "Reading results should pass") assert.Assert(t, strings.Contains(outputModel.Response[0], "Incorrect API key provided"), "Expecting incorrect api key error. Got: "+outputModel.Response[0]) } + +func TestChatKicsAzureAIInvalidAPIKey(t *testing.T) { + createASTIntegrationTestCommand(t) + mockConfig := []*wrappers.TenantConfigurationResponse{ + { + Key: "scan.config.plugins.ideScans", + Value: "true", + }, + { + Key: "scan.config.plugins.azureAiGuidedRemediation", + Value: "true", + }, + { + Key: "scan.config.plugins.aiGuidedRemediationAiEngine", + Value: "azureai", + }, + } + + mockTenant := mock.TenantConfigurationMockWrapper{} + mockTenant.SetTenantConfiguration(mockConfig) + + args := []string{ + "chat", "kics", + "--conversation-id", uuid.New().String(), + "--chat-apikey", "invalidApiKey", + "--user-input", "userInput", + "--result-file", "./data/Dockerfile", + "--result-line", "0", + "--result-severity", "LOW", + "--result-vulnerability", "Vulnerability", + } + + response := RunKicsChatForTest(t, mockTenant, args...) + assert.Assert(t, strings.Contains(response.Response[0], INCORRECT_API_ERROR), "Expecting incorrect api key error. Got: "+response.Response[0]) + +} + +func RunKicsChatForTest(t *testing.T, tenantWrapper mock.TenantConfigurationMockWrapper, args ...string) commands.OutputModel { + outputBuffer := bytes.NewBufferString("") + cmd := commands.ChatKicsSubCommand(wrappers.NewChatWrapper(), tenantWrapper) + cmd.SetArgs(args) + cmd.SetOut(outputBuffer) + err := cmd.Execute() + assert.NilError(t, err) + outputModel := commands.OutputModel{} + unmarshall(t, outputBuffer, &outputModel, "Reading results should pass") + return outputModel +} From e308dffb303448c17d26c52841673a25cdc2395f Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 18 Sep 2024 13:19:51 +0300 Subject: [PATCH 61/69] add log to the failure --- test/integration/chat_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/integration/chat_test.go b/test/integration/chat_test.go index 675b77d19..7b6c926b7 100644 --- a/test/integration/chat_test.go +++ b/test/integration/chat_test.go @@ -82,12 +82,12 @@ func TestChatKicsAzureAIInvalidAPIKey(t *testing.T) { "--result-vulnerability", "Vulnerability", } - response := RunKicsChatForTest(t, mockTenant, args...) - assert.Assert(t, strings.Contains(response.Response[0], INCORRECT_API_ERROR), "Expecting incorrect api key error. Got: "+response.Response[0]) + response, responseString := RunKicsChatForTest(t, mockTenant, args...) + assert.Assert(t, strings.Contains(response.Response[0], INCORRECT_API_ERROR), "Expecting incorrect api key error. Got: "+responseString) } -func RunKicsChatForTest(t *testing.T, tenantWrapper mock.TenantConfigurationMockWrapper, args ...string) commands.OutputModel { +func RunKicsChatForTest(t *testing.T, tenantWrapper mock.TenantConfigurationMockWrapper, args ...string) (commands.OutputModel, string) { outputBuffer := bytes.NewBufferString("") cmd := commands.ChatKicsSubCommand(wrappers.NewChatWrapper(), tenantWrapper) cmd.SetArgs(args) @@ -96,5 +96,6 @@ func RunKicsChatForTest(t *testing.T, tenantWrapper mock.TenantConfigurationMock assert.NilError(t, err) outputModel := commands.OutputModel{} unmarshall(t, outputBuffer, &outputModel, "Reading results should pass") - return outputModel + outputString := outputBuffer.String() + return outputModel, outputString } From 040649f651d06d3c650e8b7c0bed0ca6eb913de8 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 18 Sep 2024 13:45:22 +0300 Subject: [PATCH 62/69] Skip test since it's not in production yet --- test/integration/chat_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/chat_test.go b/test/integration/chat_test.go index 7b6c926b7..eb0cfd7f6 100644 --- a/test/integration/chat_test.go +++ b/test/integration/chat_test.go @@ -52,6 +52,7 @@ func TestChatSastInvalidAPIKey(t *testing.T) { } func TestChatKicsAzureAIInvalidAPIKey(t *testing.T) { + t.Skip("Skipping this test since not all services are deployed to production yet") createASTIntegrationTestCommand(t) mockConfig := []*wrappers.TenantConfigurationResponse{ { From d4d392b937020b367b584d501f22754537858821 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Mon, 23 Sep 2024 08:49:52 +0300 Subject: [PATCH 63/69] remove unneeded model configuration --- internal/commands/chat-sast.go | 7 +------ internal/commands/chat.go | 2 -- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index b5a67c396..7e9345672 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -149,8 +149,7 @@ func CreateStatefulWrapper(cmd *cobra.Command, azureAiEnabled, checkmarxAiEnable if azureAiEnabled { aiProxyAzureAIRoute := viper.GetString(params.AiProxyAzureAiRouteKey) aiProxyEndPoint, _ := wrappers.GetURL(aiProxyAzureAIRoute, customerToken) - model, _ := GetAzureAiModel(tenantConfigurationResponses) - statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, model, dropLen, 0) + statefulWrapper, _ = wrapper.NewStatefulWrapperNew(conn, aiProxyEndPoint, customerToken, "", dropLen, 0) } else if checkmarxAiEnabled { aiProxyCheckmarxAIRoute := viper.GetString(params.AiProxyCheckmarxAiRouteKey) aiProxyEndPoint, _ := wrappers.GetURL(aiProxyCheckmarxAIRoute, customerToken) @@ -225,10 +224,6 @@ func isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrap return isEnabled } -func GetAzureAiModel(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) (string, error) { - return GetTenantConfiguration(tenantConfigurationResponses, AzureAiModel) -} - func getMessageContents(response []message.Message) []string { var responseContent []string for _, r := range response { diff --git a/internal/commands/chat.go b/internal/commands/chat.go index 8a86c6255..795394c2f 100644 --- a/internal/commands/chat.go +++ b/internal/commands/chat.go @@ -9,10 +9,8 @@ const ( ConversationIDErrorFormat = "Invalid conversation ID %s" AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" AiGuidedRemediationEngine = "scan.config.plugins.aiGuidedRemediationAiEngine" - AiGuidedRemediationOpenAiValue = "openai" AiGuidedRemediationAzureAiValue = "azureai" AiGuidedRemediationCheckmarxAiValue = "checkmarxai" - AzureAiModel = "scan.config.plugins.aiGuidedRemediationAzureAiModel" ) func NewChatCommand(chatWrapper wrappers.ChatWrapper, tenantWrapper wrappers.TenantConfigurationWrapper) *cobra.Command { From ac5abb425d41d430cb3c2c5ffda4508afba91941 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Thu, 10 Oct 2024 09:38:42 +0300 Subject: [PATCH 64/69] fix cr comments --- internal/params/binds.go | 1 - internal/params/envs.go | 1 - internal/params/keys.go | 1 - internal/wrappers/client.go | 1 - 4 files changed, 4 deletions(-) diff --git a/internal/params/binds.go b/internal/params/binds.go index c16ada789..681dd65f3 100644 --- a/internal/params/binds.go +++ b/internal/params/binds.go @@ -62,7 +62,6 @@ var EnvVarsBinds = []struct { {PolicyEvaluationPathKey, PolicyEvaluationPathEnv, "api/policy_management_service_uri/evaluation"}, {AccessManagementPathKey, AccessManagementPathEnv, "api/access-management"}, {ByorPathKey, ByorPathEnv, "api/byor"}, - {VorpalPortKey, VorpalPortEnv, ""}, {AiProxyAzureAiRouteKey, AiProxyAzureAiRouteEnv, "api/ai-proxy/redirect/externalAzure"}, {AiProxyCheckmarxAiRouteKey, AiProxyCheckmarxAiRouteEnv, "api/ai-proxy/redirect/azure"}, {ASCAPortKey, ASCAPortEnv, ""}, diff --git a/internal/params/envs.go b/internal/params/envs.go index fe4794f52..6db6457d2 100644 --- a/internal/params/envs.go +++ b/internal/params/envs.go @@ -61,7 +61,6 @@ const ( AccessManagementPathEnv = "CX_ACCESS_MANAGEMENT_PATH" ByorPathEnv = "CX_BYOR_PATH" IgnoreProxyEnv = "CX_IGNORE_PROXY" - VorpalPortEnv = "CX_VORPAL_PORT" AiProxyAzureAiRouteEnv = "CX_AIPROXY_AZUREAI_ROUTE" AiProxyCheckmarxAiRouteEnv = "CX_AIPROXY_CHECKMARXAI_ROUTE" ASCAPortEnv = "CX_ASCA_PORT" diff --git a/internal/params/keys.go b/internal/params/keys.go index 51246b018..002cc5ee6 100644 --- a/internal/params/keys.go +++ b/internal/params/keys.go @@ -61,7 +61,6 @@ var ( PolicyEvaluationPathKey = strings.ToLower(PolicyEvaluationPathEnv) AccessManagementPathKey = strings.ToLower(AccessManagementPathEnv) ByorPathKey = strings.ToLower(ByorPathEnv) - VorpalPortKey = strings.ToLower(VorpalPortEnv) AiProxyAzureAiRouteKey = strings.ToLower(AiProxyAzureAiRouteEnv) AiProxyCheckmarxAiRouteKey = strings.ToLower(AiProxyCheckmarxAiRouteEnv) ASCAPortKey = strings.ToLower(ASCAPortEnv) diff --git a/internal/wrappers/client.go b/internal/wrappers/client.go index 659ef347b..74b839006 100644 --- a/internal/wrappers/client.go +++ b/internal/wrappers/client.go @@ -35,7 +35,6 @@ const ( tryPrintOffset = 2 retryLimitPrintOffset = 1 MissingURI = "When using client-id and client-secret please provide base-uri or base-auth-uri" - BaseUriisEmpty = "Base URI is empty" MissingTenant = "Failed to authenticate - please provide tenant" jwtError = "Error retrieving %s from jwt token" basicFormat = "Basic %s" From ea6987c85fed7172fda1de4b9988e6cbbddf8c29 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Tue, 5 Nov 2024 13:09:45 +0200 Subject: [PATCH 65/69] merge from master --- go.mod | 1 - 1 file changed, 1 deletion(-) diff --git a/go.mod b/go.mod index b5a5428c1..2eaf6651a 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/CheckmarxDev/containers-resolver v1.0.14 github.com/MakeNowJust/heredoc v1.0.0 github.com/bouk/monkey v1.0.0 - github.com/checkmarxDev/gpt-wrapper v0.0.0-20230721160222-85da2fd1cc4c github.com/golang-jwt/jwt v3.2.2+incompatible github.com/gomarkdown/markdown v0.0.0-20241102151059-6bc1ffdc6e8c github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 From b8550c83bc9f3db012a915ef65b874ec0c0e438c Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 13 Nov 2024 10:30:21 +0200 Subject: [PATCH 66/69] Workaround for engine configuration --- internal/commands/chat-sast.go | 27 ++++++++++++++++++++++----- internal/commands/chat.go | 11 +++++++---- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 7e9345672..1525dbe92 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -62,7 +62,7 @@ func runChatSast( scanResultsFile, _ := cmd.Flags().GetString(params.ChatSastScanResultsFile) sourceDir, _ := cmd.Flags().GetString(params.ChatSastSourceDir) sastResultID, _ := cmd.Flags().GetString(params.ChatSastResultID) - azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) + azureAiEnabled := isAzureAiGuidedRemediationEnabledWorkaround(tenantConfigurationResponses) checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) @@ -206,13 +206,30 @@ func isCxOneAPIKeyAvailable() bool { return apiKey != "" } -func isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { - engine, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationEngine) +// func isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { +// engine, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationEngine) +// if err != nil { +// return false +// } +// isEnabled := strings.EqualFold(engine, AiGuidedRemediationAzureAiValue) +// return isEnabled +//} + +func isAzureAiGuidedRemediationEnabledWorkaround(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { + apiKey, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationAzureAiAPIKey) if err != nil { return false } - isEnabled := strings.EqualFold(engine, AiGuidedRemediationAzureAiValue) - return isEnabled + endpoint, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationAzureAiEndpoint) + if err != nil { + return false + } + deploymentName, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationAzureAiDeploymentName) + if err != nil { + return false + } + + return len(apiKey) > 0 && len(endpoint) > 0 && len(deploymentName) > 0 } func isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { diff --git a/internal/commands/chat.go b/internal/commands/chat.go index 795394c2f..542bd30d6 100644 --- a/internal/commands/chat.go +++ b/internal/commands/chat.go @@ -6,10 +6,13 @@ import ( ) const ( - ConversationIDErrorFormat = "Invalid conversation ID %s" - AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" - AiGuidedRemediationEngine = "scan.config.plugins.aiGuidedRemediationAiEngine" - AiGuidedRemediationAzureAiValue = "azureai" + ConversationIDErrorFormat = "Invalid conversation ID %s" + AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" + AiGuidedRemediationEngine = "scan.config.plugins.aiGuidedRemediationAiEngine" + AiGuidedRemediationAzureAiDeploymentName = "scan.config.plugins.aiGuidedRemediationAzureAiDeploymentName" + AiGuidedRemediationAzureAiEndpoint = "scan.config.plugins.aiGuidedRemediationAzureAiEndpoint" + AiGuidedRemediationAzureAiAPIKey = "scan.config.plugins.aiGuidedRemediationAzureAiApiKey" + // AiGuidedRemediationAzureAiValue = "azureai" AiGuidedRemediationCheckmarxAiValue = "checkmarxai" ) From a3b7c4dc0d0147712b99cb73e33e11181f58990a Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 13 Nov 2024 10:33:37 +0200 Subject: [PATCH 67/69] fix method call --- internal/commands/chat-kics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index d2453a468..0f1af7ce4 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -193,7 +193,7 @@ func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigu return false, false, nil, outputError(cmd, uuid.Nil, err) } - azureAiEnabled = isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) + azureAiEnabled = isAzureAiGuidedRemediationEnabledWorkaround(tenantConfigurationResponses) checkmarxAiEnabled = isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) } return azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, nil From 8738703bd2a3c4ebe925ce0b68945b786338855f Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 13 Nov 2024 11:03:37 +0200 Subject: [PATCH 68/69] Revert "fix method call" This reverts commit a3b7c4dc0d0147712b99cb73e33e11181f58990a. --- internal/commands/chat-kics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/commands/chat-kics.go b/internal/commands/chat-kics.go index 0f1af7ce4..d2453a468 100644 --- a/internal/commands/chat-kics.go +++ b/internal/commands/chat-kics.go @@ -193,7 +193,7 @@ func getEngineSelection(cmd *cobra.Command, tenantWrapper wrappers.TenantConfigu return false, false, nil, outputError(cmd, uuid.Nil, err) } - azureAiEnabled = isAzureAiGuidedRemediationEnabledWorkaround(tenantConfigurationResponses) + azureAiEnabled = isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled = isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) } return azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses, nil From 59444829295c7f75788fca2c12ad0e7349470bc6 Mon Sep 17 00:00:00 2001 From: avisab-cx Date: Wed, 13 Nov 2024 11:03:41 +0200 Subject: [PATCH 69/69] Revert "Workaround for engine configuration" This reverts commit b8550c83bc9f3db012a915ef65b874ec0c0e438c. --- internal/commands/chat-sast.go | 27 +++++---------------------- internal/commands/chat.go | 11 ++++------- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/internal/commands/chat-sast.go b/internal/commands/chat-sast.go index 1525dbe92..7e9345672 100644 --- a/internal/commands/chat-sast.go +++ b/internal/commands/chat-sast.go @@ -62,7 +62,7 @@ func runChatSast( scanResultsFile, _ := cmd.Flags().GetString(params.ChatSastScanResultsFile) sourceDir, _ := cmd.Flags().GetString(params.ChatSastSourceDir) sastResultID, _ := cmd.Flags().GetString(params.ChatSastResultID) - azureAiEnabled := isAzureAiGuidedRemediationEnabledWorkaround(tenantConfigurationResponses) + azureAiEnabled := isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses) checkmarxAiEnabled := isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses) statefulWrapper, customerToken := CreateStatefulWrapper(cmd, azureAiEnabled, checkmarxAiEnabled, tenantConfigurationResponses) @@ -206,30 +206,13 @@ func isCxOneAPIKeyAvailable() bool { return apiKey != "" } -// func isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { -// engine, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationEngine) -// if err != nil { -// return false -// } -// isEnabled := strings.EqualFold(engine, AiGuidedRemediationAzureAiValue) -// return isEnabled -//} - -func isAzureAiGuidedRemediationEnabledWorkaround(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { - apiKey, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationAzureAiAPIKey) - if err != nil { - return false - } - endpoint, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationAzureAiEndpoint) - if err != nil { - return false - } - deploymentName, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationAzureAiDeploymentName) +func isAzureAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { + engine, err := GetTenantConfiguration(tenantConfigurationResponses, AiGuidedRemediationEngine) if err != nil { return false } - - return len(apiKey) > 0 && len(endpoint) > 0 && len(deploymentName) > 0 + isEnabled := strings.EqualFold(engine, AiGuidedRemediationAzureAiValue) + return isEnabled } func isCheckmarxAiGuidedRemediationEnabled(tenantConfigurationResponses *[]*wrappers.TenantConfigurationResponse) bool { diff --git a/internal/commands/chat.go b/internal/commands/chat.go index 542bd30d6..795394c2f 100644 --- a/internal/commands/chat.go +++ b/internal/commands/chat.go @@ -6,13 +6,10 @@ import ( ) const ( - ConversationIDErrorFormat = "Invalid conversation ID %s" - AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" - AiGuidedRemediationEngine = "scan.config.plugins.aiGuidedRemediationAiEngine" - AiGuidedRemediationAzureAiDeploymentName = "scan.config.plugins.aiGuidedRemediationAzureAiDeploymentName" - AiGuidedRemediationAzureAiEndpoint = "scan.config.plugins.aiGuidedRemediationAzureAiEndpoint" - AiGuidedRemediationAzureAiAPIKey = "scan.config.plugins.aiGuidedRemediationAzureAiApiKey" - // AiGuidedRemediationAzureAiValue = "azureai" + ConversationIDErrorFormat = "Invalid conversation ID %s" + AiGuidedRemediationEnabled = "scan.config.plugins.aiGuidedRemediation" + AiGuidedRemediationEngine = "scan.config.plugins.aiGuidedRemediationAiEngine" + AiGuidedRemediationAzureAiValue = "azureai" AiGuidedRemediationCheckmarxAiValue = "checkmarxai" )