Skip to content

Commit

Permalink
added interface implementations and logic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Dec 10, 2024
1 parent dbc8829 commit 19dbaac
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
10 changes: 8 additions & 2 deletions xray/services/xsc/xsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 8 additions & 2 deletions xsc/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
47 changes: 40 additions & 7 deletions xsc/services/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand All @@ -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
}

0 comments on commit 19dbaac

Please sign in to comment.