From 19dbaac6b9139a54989be5503c0af9ee6ad74468 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Tue, 10 Dec 2024 09:45:13 +0200 Subject: [PATCH] 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 +}