From dbc8829cd839f1155da8a7084243b9ca5e098682 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Tue, 10 Dec 2024 09:44:10 +0200 Subject: [PATCH 01/16] added a new method to interface GetConfigProfileByUrl and changed name GetConfigProfile -> GetConfigProfileByName --- xsc/service.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xsc/service.go b/xsc/service.go index 6094b5b0d..9fccf85da 100644 --- a/xsc/service.go +++ b/xsc/service.go @@ -15,6 +15,8 @@ type XscService interface { UpdateAnalyticsGeneralEvent(event services.XscAnalyticsGeneralEventFinalize) error // GetAnalyticsGeneralEvent returns general event that match the msi provided. GetAnalyticsGeneralEvent(msi string) (*services.XscAnalyticsGeneralEvent, error) - // GetConfigProfile returns the configuration profile that match the profile name provided. - GetConfigProfile(profileName string) (*services.ConfigProfile, error) + // GetConfigProfileByName returns the configuration profile that match the profile name provided. + GetConfigProfileByName(profileName string) (*services.ConfigProfile, error) + // GetConfigProfileByUrl returns the configuration profile related to the provided repository url. + GetConfigProfileByUrl(profileUrl string) (*services.ConfigProfile, error) } From 19dbaac6b9139a54989be5503c0af9ee6ad74468 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Tue, 10 Dec 2024 09:45:13 +0200 Subject: [PATCH 02/16] added interface implementations and logic implementation --- xray/services/xsc/xsc.go | 10 +++++++-- xsc/manager.go | 10 +++++++-- xsc/services/profile.go | 47 ++++++++++++++++++++++++++++++++++------ 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/xray/services/xsc/xsc.go b/xray/services/xsc/xsc.go index cc5119a99..1e3003ed4 100644 --- a/xray/services/xsc/xsc.go +++ b/xray/services/xsc/xsc.go @@ -47,8 +47,14 @@ func (xs *XscInnerService) GetAnalyticsGeneralEvent(msi string) (*services.XscAn return eventService.GetGeneralEvent(msi) } -func (xs *XscInnerService) GetConfigProfile(profileName string) (*services.ConfigProfile, error) { +func (xs *XscInnerService) GetConfigProfileByName(profileName string) (*services.ConfigProfile, error) { configProfileService := services.NewConfigurationProfileService(xs.client) configProfileService.XrayDetails = xs.XrayDetails - return configProfileService.GetConfigurationProfile(profileName) + return configProfileService.GetConfigurationProfileByName(profileName) +} + +func (xs *XscInnerService) GetConfigProfileByUrl(repoUrl string) (*services.ConfigProfile, error) { + configProfileService := services.NewConfigurationProfileService(xs.client) + configProfileService.XrayDetails = xs.XrayDetails + return configProfileService.GetConfigurationProfileByUrl(repoUrl) } diff --git a/xsc/manager.go b/xsc/manager.go index 04423674b..70eebc00e 100644 --- a/xsc/manager.go +++ b/xsc/manager.go @@ -77,8 +77,14 @@ func (sm *XscServicesManager) GetAnalyticsGeneralEvent(msi string) (*services.Xs return eventService.GetGeneralEvent(msi) } -func (sm *XscServicesManager) GetConfigProfile(profileName string) (*services.ConfigProfile, error) { +func (sm *XscServicesManager) GetConfigProfileByName(profileName string) (*services.ConfigProfile, error) { configProfileService := services.NewConfigurationProfileService(sm.client) configProfileService.XscDetails = sm.config.GetServiceDetails() - return configProfileService.GetConfigurationProfile(profileName) + return configProfileService.GetConfigurationProfileByName(profileName) +} + +// TODO is it really required? +func (sm *XscServicesManager) GetConfigProfileByUrl(profileUrl string) (*services.ConfigProfile, error) { + // Empty implementation required for alignment with interface + return nil, nil } diff --git a/xsc/services/profile.go b/xsc/services/profile.go index 2db33d7d7..3e1968491 100644 --- a/xsc/services/profile.go +++ b/xsc/services/profile.go @@ -13,9 +13,11 @@ import ( ) const ( - ConfigProfileMinXscVersion = "1.11.0" - xscConfigProfileApi = "profile" - xscDeprecatedConfigProfileApiSuffix = "api/v1/" + xscConfigProfileApi + ConfigProfileMinXscVersion = "1.11.0" // TODO eran start here - we no longer check for xsc version. fix usages + tests so now we only verify this XRAY version + ConfigProfileByUrlMinXrayVersion = "3.110.0" + xscConfigProfileByNameApi = "profile" + xscConfigProfileByUrlApi = "profile_repos" + xscDeprecatedConfigProfileApiSuffix = "api/v1/" + xscConfigProfileByNameApi ) type ConfigurationProfileService struct { @@ -100,10 +102,10 @@ type ServicesScannerConfig struct { ExcludePatterns []string `json:"exclude_patterns,omitempty"` } -func (cp *ConfigurationProfileService) sendConfigProfileRequest(profileName string) (url string, resp *http.Response, body []byte, err error) { +func (cp *ConfigurationProfileService) sendConfigProfileByNameRequest(profileName string) (url string, resp *http.Response, body []byte, err error) { if cp.XrayDetails != nil { httpDetails := cp.XrayDetails.CreateHttpClientDetails() - url = fmt.Sprintf("%s%s%s/%s", utils.AddTrailingSlashIfNeeded(cp.XrayDetails.GetUrl()), xscutils.XscInXraySuffix, xscConfigProfileApi, profileName) + url = fmt.Sprintf("%s%s%s/%s", utils.AddTrailingSlashIfNeeded(cp.XrayDetails.GetUrl()), xscutils.XscInXraySuffix, xscConfigProfileByNameApi, profileName) resp, body, _, err = cp.client.SendGet(url, true, &httpDetails) return } @@ -114,8 +116,8 @@ func (cp *ConfigurationProfileService) sendConfigProfileRequest(profileName stri return } -func (cp *ConfigurationProfileService) GetConfigurationProfile(profileName string) (*ConfigProfile, error) { - url, res, body, err := cp.sendConfigProfileRequest(profileName) +func (cp *ConfigurationProfileService) GetConfigurationProfileByName(profileName string) (*ConfigProfile, error) { + url, res, body, err := cp.sendConfigProfileByNameRequest(profileName) if err != nil { return nil, fmt.Errorf("failed to send GET query to '%s': %q", url, err) } @@ -127,3 +129,34 @@ func (cp *ConfigurationProfileService) GetConfigurationProfile(profileName strin err = errorutils.CheckError(json.Unmarshal(body, &profile)) return &profile, err } + +func (cp *ConfigurationProfileService) sendConfigProfileByUrlRequest(repoUrl string) (url string, resp *http.Response, body []byte, err error) { + if cp.XrayDetails != nil { + httpDetails := cp.XrayDetails.CreateHttpClientDetails() + url = fmt.Sprintf("%s%s%s", utils.AddTrailingSlashIfNeeded(cp.XrayDetails.GetUrl()), xscutils.XscInXraySuffix, xscConfigProfileByUrlApi) + requestContent := []byte(fmt.Sprintf("{\"repo_url\":\"%s\"}", repoUrl)) + resp, body, err = cp.client.SendPost(url, requestContent, &httpDetails) + return + } + // Backward compatibility + httpDetails := cp.XscDetails.CreateHttpClientDetails() + url = fmt.Sprintf("%s%s/%s", utils.AddTrailingSlashIfNeeded(cp.XscDetails.GetUrl()), xscDeprecatedConfigProfileApiSuffix, xscConfigProfileByUrlApi) + requestContent := []byte(fmt.Sprintf("{\"repo_url\":\"%s\"}", repoUrl)) + resp, body, err = cp.client.SendPost(url, requestContent, &httpDetails) + return +} + +// TODO eran write test similar to byName func above +func (cp *ConfigurationProfileService) GetConfigurationProfileByUrl(url string) (*ConfigProfile, error) { + url, res, body, err := cp.sendConfigProfileByUrlRequest(url) + if err != nil { + return nil, fmt.Errorf("failed to send POST query to '%s': %q", url, err) + } + if err = errorutils.CheckResponseStatusWithBody(res, body, http.StatusOK); err != nil { + return nil, err + } + + var profile ConfigProfile + err = errorutils.CheckError(json.Unmarshal(body, &profile)) + return &profile, err +} From a11abc6c0c6b5fd990fcd7fd722b54c0720b9dcf Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Tue, 10 Dec 2024 09:45:28 +0200 Subject: [PATCH 03/16] adjusted test according to changes --- tests/xscconfigprofile_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/xscconfigprofile_test.go b/tests/xscconfigprofile_test.go index 44c5932ba..fd3cbf669 100644 --- a/tests/xscconfigprofile_test.go +++ b/tests/xscconfigprofile_test.go @@ -11,13 +11,13 @@ import ( "testing" ) -func TestGetConfigurationProfile(t *testing.T) { +func TestGetConfigurationProfileByName(t *testing.T) { initXscTest(t, services.ConfigProfileMinXscVersion) mockServer, configProfileService := createXscMockServerForConfigProfile(t) defer mockServer.Close() - configProfile, err := configProfileService.GetConfigurationProfile("default-test-profile") + configProfile, err := configProfileService.GetConfigurationProfileByName("default-test-profile") assert.NoError(t, err) profileFileContent, err := os.ReadFile("testdata/configprofile/configProfileExample.json") From 3ee757f8edf68378587e8f5e99b8243c816a9c02 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Tue, 10 Dec 2024 16:20:12 +0200 Subject: [PATCH 04/16] added ConfigProfileRepository struct to ConfigProfile to support the new format --- xsc/services/profile.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/xsc/services/profile.go b/xsc/services/profile.go index 3e1968491..0dd4f998d 100644 --- a/xsc/services/profile.go +++ b/xsc/services/profile.go @@ -31,10 +31,18 @@ func NewConfigurationProfileService(client *jfroghttpclient.JfrogHttpClient) *Co } type ConfigProfile struct { - ProfileName string `json:"profile_name"` - FrogbotConfig FrogbotConfig `json:"frogbot_config,omitempty"` - Modules []Module `json:"modules"` - IsDefault bool `json:"is_default,omitempty"` + ProfileName string `json:"profile_name"` + Repository ConfigProfileRepository `json:"repository"` + FrogbotConfig FrogbotConfig `json:"frogbot_config,omitempty"` + Modules []Module `json:"modules"` + IsDefault bool `json:"is_default,omitempty"` +} + +type ConfigProfileRepository struct { + Id string + Name string + Url string + Provider string } type FrogbotConfig struct { From 22db4c2715cf2f628854b070392e35e114077007 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Wed, 11 Dec 2024 13:43:22 +0200 Subject: [PATCH 05/16] deleted comment --- xsc/services/profile.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xsc/services/profile.go b/xsc/services/profile.go index 0dd4f998d..cfaef0637 100644 --- a/xsc/services/profile.go +++ b/xsc/services/profile.go @@ -13,7 +13,7 @@ import ( ) const ( - ConfigProfileMinXscVersion = "1.11.0" // TODO eran start here - we no longer check for xsc version. fix usages + tests so now we only verify this XRAY version + ConfigProfileMinXscVersion = "1.11.0" ConfigProfileByUrlMinXrayVersion = "3.110.0" xscConfigProfileByNameApi = "profile" xscConfigProfileByUrlApi = "profile_repos" From a04d80618d900eb2b3895b8dc6aff2ff0352d077 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Wed, 11 Dec 2024 13:44:28 +0200 Subject: [PATCH 06/16] updated initXscTest verification check to match the new xsc to Xray migration --- .../configProfileWithRepoExample.json | 55 +++++++++++++++++++ tests/xsc_test.go | 45 +++++++++++---- tests/xscanalyticsevent_test.go | 2 +- tests/xsclogerrorevent_test.go | 2 +- 4 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 tests/testdata/configprofile/configProfileWithRepoExample.json diff --git a/tests/testdata/configprofile/configProfileWithRepoExample.json b/tests/testdata/configprofile/configProfileWithRepoExample.json new file mode 100644 index 000000000..250b7add0 --- /dev/null +++ b/tests/testdata/configprofile/configProfileWithRepoExample.json @@ -0,0 +1,55 @@ +{ + "profile_name": "default-profile-with-repo", + "repository": { + "id": "my-repo-id-123", + "name": "my-repo", + "url": "https://github.com/myUser/my-repo.git", + "provider": "github" + }, + "frogbot_config": { + "email_author": "my-user@jfrog.com", + "aggregate_fixes": true, + "avoid_previous_pr_comments_deletion": true, + "branch_name_template": "frogbot-${IMPACTED_PACKAGE}-${BRANCH_NAME_HASH}", + "pr_title_template": "[🐸 Frogbot] Upgrade {IMPACTED_PACKAGE} to {FIX_VERSION}", + "pr_comment_title": "Frogbot notes:", + "commit_message_template": "Upgrade {IMPACTED_PACKAGE} to {FIX_VERSION}", + "show_secrets_as_pr_comment": false + }, + "modules": [ + { + "module_name": "default-module", + "path_from_root": ".", + "releases_repo": "nuget-remote", + "analyzer_manager_version": "1.8.1", + "additional_paths_for_module": ["lib1", "utils/lib2"], + "exclude_paths": ["**/.git/**", "**/*test*/**", "**/*venv*/**", "**/*node_modules*/**", "**/target/**"], + "scan_config": { + "scan_timeout": 600, + "exclude_pattern": "*.md", + "enable_sca_scan": true, + "enable_contextual_analysis_scan": true, + "sast_scanner_config": { + "enable_sast_scan": true + }, + "secrets_scanner_config": { + "enable_secrets_scan": true + }, + "iac_scanner_config": { + "enable_iac_scan": true + }, + "applications_scanner_config": { + "enable_applications_scan": true + }, + "services_scanner_config": { + "enable_services_scan": true + } + }, + "protected_branches": ["main", "master"], + "include_exclude_mode": 0, + "include_exclude_pattern": "*test*", + "report_analytics": true + } + ], + "is_default": true +} \ No newline at end of file diff --git a/tests/xsc_test.go b/tests/xsc_test.go index bc2178a31..500c1da87 100644 --- a/tests/xsc_test.go +++ b/tests/xsc_test.go @@ -6,7 +6,7 @@ import ( ) func TestXscVersion(t *testing.T) { - initXscTest(t, "") + initXscTest(t, "", "") version, err := GetXscDetails().GetVersion() if err != nil { t.Error(err) @@ -16,21 +16,42 @@ func TestXscVersion(t *testing.T) { } } -func initXscTest(t *testing.T, minVersion string) { +func initXscTest(t *testing.T, minXscVersion string, minXrayVersion string) { if !*TestXsc { t.Skip("Skipping xsc test. To run xsc test add the '-test.xsc=true' option.") } - validateXscVersion(t, minVersion) + validateXscVersion(t, minXscVersion, minXrayVersion) } -func validateXscVersion(t *testing.T, minVersion string) { - // Validate active XSC server. - version, err := GetXscDetails().GetVersion() - if err != nil { - t.Skip(err) + +// This func validates minimal Xsc version. +// Since Xsc was migrated into Xray from version 3.107.13, we need to check minimal Xray version from this version forward instead of Xsc version. +// For features that are available before the migration we pass minXscVersion to check. If the utilized Xray version >= 3.107.13, the returned Xsc version will always suffice the check. +// For features that were introduced only after the migration we pass only minXrayVersion to check and can leave minXscVersion blank. +func validateXscVersion(t *testing.T, minXscVersion string, minXrayVersion string) { + if minXscVersion != "" { + // If minXscVersion is provided we assume we have a Xray version BEFORE Xsc migration to it (i.e. prior to 3.107.13) + // In this case we want to validate active Xsc server + minimal required Xsc version + currentXscVersion, err := GetXscDetails().GetVersion() + if err != nil { + t.Skip(err) + } + // Validate minimum XSC version. + err = clientUtils.ValidateMinimumVersion(clientUtils.Xsc, currentXscVersion, minXscVersion) + if err != nil { + t.Skip(err) + } } - // Validate minimum XSC version. - err = clientUtils.ValidateMinimumVersion(clientUtils.Xsc, version, minVersion) - if err != nil { - t.Skip(err) + + if minXrayVersion != "" { + // If minXrayVersion is provided we assume we have a Xray version AFTER Xsc migration to it (3.107.13+) + // In this case we want to validate minimal required Xray version only + currentXrayVersion, err := GetXrayDetails().GetVersion() + if err != nil { + t.Skip(err) + } + err = clientUtils.ValidateMinimumVersion(clientUtils.Xsc, currentXrayVersion, minXscVersion) + if err != nil { + t.Skip(err) + } } } diff --git a/tests/xscanalyticsevent_test.go b/tests/xscanalyticsevent_test.go index bfe83792d..0aa223e5b 100644 --- a/tests/xscanalyticsevent_test.go +++ b/tests/xscanalyticsevent_test.go @@ -67,7 +67,7 @@ func isValidUUID(str string) bool { func initXscEventTest(t *testing.T) (xscDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) { var err error - initXscTest(t, services.AnalyticsMetricsMinXscVersion) + initXscTest(t, services.AnalyticsMetricsMinXscVersion, "") xscDetails = GetXscDetails() client, err = jfroghttpclient.JfrogClientBuilder(). SetClientCertPath(xscDetails.GetClientCertPath()). diff --git a/tests/xsclogerrorevent_test.go b/tests/xsclogerrorevent_test.go index c56273633..ff441bb2f 100644 --- a/tests/xsclogerrorevent_test.go +++ b/tests/xsclogerrorevent_test.go @@ -13,7 +13,7 @@ import ( const errorMessageContentForTest = "THIS IS NOT A REAL ERROR! This Error is posted as part of TestXscSendLogErrorEvent test" func TestXscSendLogErrorEvent(t *testing.T) { - initXscTest(t, services.LogErrorMinXscVersion) + initXscTest(t, services.LogErrorMinXscVersion, "") mockServer, logErrorService := createXscMockServerForLogEvent(t) defer mockServer.Close() From c53fa7e9c3704b1882bcf0f989789b1f7921618a Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Wed, 11 Dec 2024 13:45:21 +0200 Subject: [PATCH 07/16] Added test for GetConfigurationProfileByUrl --- tests/xscconfigprofile_test.go | 67 +++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/tests/xscconfigprofile_test.go b/tests/xscconfigprofile_test.go index fd3cbf669..a2ebfff9a 100644 --- a/tests/xscconfigprofile_test.go +++ b/tests/xscconfigprofile_test.go @@ -2,22 +2,32 @@ package tests import ( "encoding/json" + "fmt" "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" + "github.com/jfrog/jfrog-client-go/utils" "github.com/jfrog/jfrog-client-go/xsc/services" + xscutils "github.com/jfrog/jfrog-client-go/xsc/services/utils" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "net/http" "net/http/httptest" "os" + "strings" "testing" ) +const configProfileWithoutRepo = "default-test-profile" + func TestGetConfigurationProfileByName(t *testing.T) { - initXscTest(t, services.ConfigProfileMinXscVersion) + initXscTest(t, services.ConfigProfileMinXscVersion, "") + + xrayVersion, err := GetXrayDetails().GetVersion() + require.NoError(t, err) - mockServer, configProfileService := createXscMockServerForConfigProfile(t) + mockServer, configProfileService := createXscMockServerForConfigProfile(t, xrayVersion) defer mockServer.Close() - configProfile, err := configProfileService.GetConfigurationProfileByName("default-test-profile") + configProfile, err := configProfileService.GetConfigurationProfileByName(configProfileWithoutRepo) assert.NoError(t, err) profileFileContent, err := os.ReadFile("testdata/configprofile/configProfileExample.json") @@ -28,15 +38,55 @@ func TestGetConfigurationProfileByName(t *testing.T) { assert.Equal(t, &configProfileForComparison, configProfile) } -func createXscMockServerForConfigProfile(t *testing.T) (mockServer *httptest.Server, configProfileService *services.ConfigurationProfileService) { +func TestGetConfigurationProfileByUrl(t *testing.T) { + initXscTest(t, "", services.ConfigProfileByUrlMinXrayVersion) + + // Verifying minimal xray version required for feature and test + xrayVersion, err := GetXrayDetails().GetVersion() + require.NoError(t, err) + err = utils.ValidateMinimumVersion(utils.Xray, xrayVersion, services.ConfigProfileByUrlMinXrayVersion) + if err != nil { + t.Skip(fmt.Sprintf("Skipping GetConfigurationProfileByName test since current Xray version is %s while minimal required version for the feature is %s", xrayVersion, services.ConfigProfileByUrlMinXrayVersion)) + } + + mockServer, configProfileService := createXscMockServerForConfigProfile(t, xrayVersion) + defer mockServer.Close() + + configProfile, err := configProfileService.GetConfigurationProfileByUrl(mockServer.URL) + assert.NoError(t, err) + + profileFileContent, err := os.ReadFile("testdata/configprofile/configProfileWithRepoExample.json") + assert.NoError(t, err) + var configProfileForComparison services.ConfigProfile + err = json.Unmarshal(profileFileContent, &configProfileForComparison) + assert.NoError(t, err) + assert.Equal(t, &configProfileForComparison, configProfile) + +} + +func createXscMockServerForConfigProfile(t *testing.T, xrayVersion string) (mockServer *httptest.Server, configProfileService *services.ConfigurationProfileService) { mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.RequestURI == "/xsc/api/v1/profile/default-test-profile" && r.Method == http.MethodGet { + apiUrlPart := "api/v1/" + var isXrayAfterXscMigration bool + if isXrayAfterXscMigration = xscutils.IsXscXrayInnerService(xrayVersion); isXrayAfterXscMigration { + apiUrlPart = "" + } + + switch { + case strings.Contains(r.RequestURI, "/xsc/"+apiUrlPart+"profile/"+configProfileWithoutRepo): w.WriteHeader(http.StatusOK) content, err := os.ReadFile("testdata/configprofile/configProfileExample.json") assert.NoError(t, err) _, err = w.Write(content) assert.NoError(t, err) - } else { + + case strings.Contains(r.RequestURI, "xray/api/v1/xsc/profile_repos") && isXrayAfterXscMigration: + w.WriteHeader(http.StatusOK) + content, err := os.ReadFile("testdata/configprofile/configProfileWithRepoExample.json") + assert.NoError(t, err) + _, err = w.Write(content) + assert.NoError(t, err) + default: assert.Fail(t, "received an unexpected request") } })) @@ -45,10 +95,15 @@ func createXscMockServerForConfigProfile(t *testing.T) (mockServer *httptest.Ser xscDetails.SetUrl(mockServer.URL + "/xsc") xscDetails.SetAccessToken("") + xrayDetails := GetXrayDetails() + xrayDetails.SetUrl(mockServer.URL + "/xray") + xrayDetails.SetAccessToken("") + client, err := jfroghttpclient.JfrogClientBuilder().Build() assert.NoError(t, err) configProfileService = services.NewConfigurationProfileService(client) configProfileService.XscDetails = xscDetails + configProfileService.XrayDetails = xrayDetails return } From 69ae63f37137b7c7cc6e84be9fefb5b7fbeb949c Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Wed, 11 Dec 2024 13:53:30 +0200 Subject: [PATCH 08/16] fix test --- tests/xscconfigprofile_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/xscconfigprofile_test.go b/tests/xscconfigprofile_test.go index a2ebfff9a..60047e828 100644 --- a/tests/xscconfigprofile_test.go +++ b/tests/xscconfigprofile_test.go @@ -2,9 +2,7 @@ package tests import ( "encoding/json" - "fmt" "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" - "github.com/jfrog/jfrog-client-go/utils" "github.com/jfrog/jfrog-client-go/xsc/services" xscutils "github.com/jfrog/jfrog-client-go/xsc/services/utils" "github.com/stretchr/testify/assert" @@ -41,13 +39,8 @@ func TestGetConfigurationProfileByName(t *testing.T) { func TestGetConfigurationProfileByUrl(t *testing.T) { initXscTest(t, "", services.ConfigProfileByUrlMinXrayVersion) - // Verifying minimal xray version required for feature and test xrayVersion, err := GetXrayDetails().GetVersion() require.NoError(t, err) - err = utils.ValidateMinimumVersion(utils.Xray, xrayVersion, services.ConfigProfileByUrlMinXrayVersion) - if err != nil { - t.Skip(fmt.Sprintf("Skipping GetConfigurationProfileByName test since current Xray version is %s while minimal required version for the feature is %s", xrayVersion, services.ConfigProfileByUrlMinXrayVersion)) - } mockServer, configProfileService := createXscMockServerForConfigProfile(t, xrayVersion) defer mockServer.Close() From b958a9e8dcde5190404f851c0da3f5516d760ff1 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Wed, 11 Dec 2024 14:17:21 +0200 Subject: [PATCH 09/16] fix initXsc test to capture non-available Xsc server --- tests/xsc_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/xsc_test.go b/tests/xsc_test.go index 500c1da87..a4b2e5657 100644 --- a/tests/xsc_test.go +++ b/tests/xsc_test.go @@ -28,14 +28,15 @@ func initXscTest(t *testing.T, minXscVersion string, minXrayVersion string) { // For features that are available before the migration we pass minXscVersion to check. If the utilized Xray version >= 3.107.13, the returned Xsc version will always suffice the check. // For features that were introduced only after the migration we pass only minXrayVersion to check and can leave minXscVersion blank. func validateXscVersion(t *testing.T, minXscVersion string, minXrayVersion string) { + // Validate active Xsc server + currentXscVersion, err := GetXscDetails().GetVersion() + if err != nil { + t.Skip(err) + } + if minXscVersion != "" { // If minXscVersion is provided we assume we have a Xray version BEFORE Xsc migration to it (i.e. prior to 3.107.13) - // In this case we want to validate active Xsc server + minimal required Xsc version - currentXscVersion, err := GetXscDetails().GetVersion() - if err != nil { - t.Skip(err) - } - // Validate minimum XSC version. + // In this case we want to validate minimal required Xsc version err = clientUtils.ValidateMinimumVersion(clientUtils.Xsc, currentXscVersion, minXscVersion) if err != nil { t.Skip(err) From d5551ba0ded73f77777c014f1892e16a37b979f4 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Wed, 11 Dec 2024 14:20:43 +0200 Subject: [PATCH 10/16] remove comment and fix mock func --- xsc/manager.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xsc/manager.go b/xsc/manager.go index 70eebc00e..30521df52 100644 --- a/xsc/manager.go +++ b/xsc/manager.go @@ -83,8 +83,7 @@ func (sm *XscServicesManager) GetConfigProfileByName(profileName string) (*servi return configProfileService.GetConfigurationProfileByName(profileName) } -// TODO is it really required? -func (sm *XscServicesManager) GetConfigProfileByUrl(profileUrl string) (*services.ConfigProfile, error) { +func (sm *XscServicesManager) GetConfigProfileByUrl(_ string) (*services.ConfigProfile, error) { // Empty implementation required for alignment with interface return nil, nil } From 13cf89baa7510e588529582431a3a268c781b0f6 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Thu, 12 Dec 2024 10:41:06 +0200 Subject: [PATCH 11/16] fixing test files --- tests/testdata/configprofile/configProfileWithRepoExample.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testdata/configprofile/configProfileWithRepoExample.json b/tests/testdata/configprofile/configProfileWithRepoExample.json index 250b7add0..a31c7a392 100644 --- a/tests/testdata/configprofile/configProfileWithRepoExample.json +++ b/tests/testdata/configprofile/configProfileWithRepoExample.json @@ -3,7 +3,7 @@ "repository": { "id": "my-repo-id-123", "name": "my-repo", - "url": "https://github.com/myUser/my-repo.git", + "url": "https://github.com/myUser/my-repo", "provider": "github" }, "frogbot_config": { From 32a628b5043204ed3cb384592c3103408328f3bb Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Thu, 12 Dec 2024 17:46:00 +0200 Subject: [PATCH 12/16] added comment to check --- xsc/services/profile.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xsc/services/profile.go b/xsc/services/profile.go index cfaef0637..754ecf819 100644 --- a/xsc/services/profile.go +++ b/xsc/services/profile.go @@ -154,7 +154,6 @@ func (cp *ConfigurationProfileService) sendConfigProfileByUrlRequest(repoUrl str return } -// TODO eran write test similar to byName func above func (cp *ConfigurationProfileService) GetConfigurationProfileByUrl(url string) (*ConfigProfile, error) { url, res, body, err := cp.sendConfigProfileByUrlRequest(url) if err != nil { @@ -165,6 +164,6 @@ func (cp *ConfigurationProfileService) GetConfigurationProfileByUrl(url string) } var profile ConfigProfile - err = errorutils.CheckError(json.Unmarshal(body, &profile)) + err = errorutils.CheckError(json.Unmarshal(body, &profile)) // TODO eran check about ConfigProfile structure. should we expect the repository info INSIDE the returned config profile? if not delete the ConfigProfileRepository part from frogbot return &profile, err } From b659ba69227fc8ace17982b1187b2262aefb75c6 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Sun, 15 Dec 2024 11:25:48 +0200 Subject: [PATCH 13/16] deleting Repository context from config profile (not needed inside frogbot/cli) --- xsc/services/profile.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/xsc/services/profile.go b/xsc/services/profile.go index 754ecf819..9c1e2c43d 100644 --- a/xsc/services/profile.go +++ b/xsc/services/profile.go @@ -31,18 +31,10 @@ func NewConfigurationProfileService(client *jfroghttpclient.JfrogHttpClient) *Co } type ConfigProfile struct { - ProfileName string `json:"profile_name"` - Repository ConfigProfileRepository `json:"repository"` - FrogbotConfig FrogbotConfig `json:"frogbot_config,omitempty"` - Modules []Module `json:"modules"` - IsDefault bool `json:"is_default,omitempty"` -} - -type ConfigProfileRepository struct { - Id string - Name string - Url string - Provider string + ProfileName string `json:"profile_name"` + FrogbotConfig FrogbotConfig `json:"frogbot_config,omitempty"` + Modules []Module `json:"modules"` + IsDefault bool `json:"is_default,omitempty"` } type FrogbotConfig struct { From 65f3ee76b6c5424e54b84557f2446d3e0ed279a9 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Sun, 15 Dec 2024 11:35:30 +0200 Subject: [PATCH 14/16] fix test according to latest update on the expected response on POST profile_repos (will not include repository info) --- .../configProfileWithRepoExample.json | 55 ------------------- tests/xscconfigprofile_test.go | 8 +-- 2 files changed, 4 insertions(+), 59 deletions(-) delete mode 100644 tests/testdata/configprofile/configProfileWithRepoExample.json diff --git a/tests/testdata/configprofile/configProfileWithRepoExample.json b/tests/testdata/configprofile/configProfileWithRepoExample.json deleted file mode 100644 index a31c7a392..000000000 --- a/tests/testdata/configprofile/configProfileWithRepoExample.json +++ /dev/null @@ -1,55 +0,0 @@ -{ - "profile_name": "default-profile-with-repo", - "repository": { - "id": "my-repo-id-123", - "name": "my-repo", - "url": "https://github.com/myUser/my-repo", - "provider": "github" - }, - "frogbot_config": { - "email_author": "my-user@jfrog.com", - "aggregate_fixes": true, - "avoid_previous_pr_comments_deletion": true, - "branch_name_template": "frogbot-${IMPACTED_PACKAGE}-${BRANCH_NAME_HASH}", - "pr_title_template": "[🐸 Frogbot] Upgrade {IMPACTED_PACKAGE} to {FIX_VERSION}", - "pr_comment_title": "Frogbot notes:", - "commit_message_template": "Upgrade {IMPACTED_PACKAGE} to {FIX_VERSION}", - "show_secrets_as_pr_comment": false - }, - "modules": [ - { - "module_name": "default-module", - "path_from_root": ".", - "releases_repo": "nuget-remote", - "analyzer_manager_version": "1.8.1", - "additional_paths_for_module": ["lib1", "utils/lib2"], - "exclude_paths": ["**/.git/**", "**/*test*/**", "**/*venv*/**", "**/*node_modules*/**", "**/target/**"], - "scan_config": { - "scan_timeout": 600, - "exclude_pattern": "*.md", - "enable_sca_scan": true, - "enable_contextual_analysis_scan": true, - "sast_scanner_config": { - "enable_sast_scan": true - }, - "secrets_scanner_config": { - "enable_secrets_scan": true - }, - "iac_scanner_config": { - "enable_iac_scan": true - }, - "applications_scanner_config": { - "enable_applications_scan": true - }, - "services_scanner_config": { - "enable_services_scan": true - } - }, - "protected_branches": ["main", "master"], - "include_exclude_mode": 0, - "include_exclude_pattern": "*test*", - "report_analytics": true - } - ], - "is_default": true -} \ No newline at end of file diff --git a/tests/xscconfigprofile_test.go b/tests/xscconfigprofile_test.go index 60047e828..a4e947135 100644 --- a/tests/xscconfigprofile_test.go +++ b/tests/xscconfigprofile_test.go @@ -48,7 +48,7 @@ func TestGetConfigurationProfileByUrl(t *testing.T) { configProfile, err := configProfileService.GetConfigurationProfileByUrl(mockServer.URL) assert.NoError(t, err) - profileFileContent, err := os.ReadFile("testdata/configprofile/configProfileWithRepoExample.json") + profileFileContent, err := os.ReadFile("testdata/configprofile/configProfileExample.json") assert.NoError(t, err) var configProfileForComparison services.ConfigProfile err = json.Unmarshal(profileFileContent, &configProfileForComparison) @@ -66,16 +66,16 @@ func createXscMockServerForConfigProfile(t *testing.T, xrayVersion string) (mock } switch { - case strings.Contains(r.RequestURI, "/xsc/"+apiUrlPart+"profile/"+configProfileWithoutRepo): + case strings.Contains(r.RequestURI, "/xsc/"+apiUrlPart+"profile/"+configProfileWithoutRepo) && r.Method == http.MethodGet: w.WriteHeader(http.StatusOK) content, err := os.ReadFile("testdata/configprofile/configProfileExample.json") assert.NoError(t, err) _, err = w.Write(content) assert.NoError(t, err) - case strings.Contains(r.RequestURI, "xray/api/v1/xsc/profile_repos") && isXrayAfterXscMigration: + case strings.Contains(r.RequestURI, "xray/api/v1/xsc/profile_repos") && r.Method == http.MethodPost && isXrayAfterXscMigration: w.WriteHeader(http.StatusOK) - content, err := os.ReadFile("testdata/configprofile/configProfileWithRepoExample.json") + content, err := os.ReadFile("testdata/configprofile/configProfileExample.json") assert.NoError(t, err) _, err = w.Write(content) assert.NoError(t, err) From faa1f7e8673ca308dbb65ccd66bc50e28fa29bf9 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Sun, 15 Dec 2024 12:32:07 +0200 Subject: [PATCH 15/16] removed comment --- xsc/services/profile.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xsc/services/profile.go b/xsc/services/profile.go index 9c1e2c43d..23184e1a5 100644 --- a/xsc/services/profile.go +++ b/xsc/services/profile.go @@ -156,6 +156,6 @@ func (cp *ConfigurationProfileService) GetConfigurationProfileByUrl(url string) } var profile ConfigProfile - err = errorutils.CheckError(json.Unmarshal(body, &profile)) // TODO eran check about ConfigProfile structure. should we expect the repository info INSIDE the returned config profile? if not delete the ConfigProfileRepository part from frogbot + err = errorutils.CheckError(json.Unmarshal(body, &profile)) return &profile, err } From 9d652642daf87d1726a1fad24f160ad0d7d0b4ad Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Sun, 15 Dec 2024 17:34:52 +0200 Subject: [PATCH 16/16] fix PR --- tests/xsc_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/xsc_test.go b/tests/xsc_test.go index a4b2e5657..7b3e9dcc1 100644 --- a/tests/xsc_test.go +++ b/tests/xsc_test.go @@ -46,11 +46,12 @@ func validateXscVersion(t *testing.T, minXscVersion string, minXrayVersion strin if minXrayVersion != "" { // If minXrayVersion is provided we assume we have a Xray version AFTER Xsc migration to it (3.107.13+) // In this case we want to validate minimal required Xray version only - currentXrayVersion, err := GetXrayDetails().GetVersion() + var currentXrayVersion string + currentXrayVersion, err = GetXrayDetails().GetVersion() if err != nil { t.Skip(err) } - err = clientUtils.ValidateMinimumVersion(clientUtils.Xsc, currentXrayVersion, minXscVersion) + err = clientUtils.ValidateMinimumVersion(clientUtils.Xsc, currentXrayVersion, minXrayVersion) if err != nil { t.Skip(err) }