diff --git a/go.mod b/go.mod index 2e5f72b6..d0418bad 100644 --- a/go.mod +++ b/go.mod @@ -10,8 +10,8 @@ require ( github.com/jfrog/froggit-go v1.16.2 github.com/jfrog/gofrog v1.7.6 github.com/jfrog/jfrog-apps-config v1.0.1 - github.com/jfrog/jfrog-cli-core/v2 v2.57.0 - github.com/jfrog/jfrog-client-go v1.48.2 + github.com/jfrog/jfrog-cli-core/v2 v2.57.2 + github.com/jfrog/jfrog-client-go v1.48.4 github.com/magiconair/properties v1.8.7 github.com/owenrumney/go-sarif/v2 v2.3.0 github.com/stretchr/testify v1.10.0 diff --git a/go.sum b/go.sum index eb30857d..107ac694 100644 --- a/go.sum +++ b/go.sum @@ -127,10 +127,10 @@ github.com/jfrog/gofrog v1.7.6 h1:QmfAiRzVyaI7JYGsB7cxfAJePAZTzFz0gRWZSE27c6s= github.com/jfrog/gofrog v1.7.6/go.mod h1:ntr1txqNOZtHplmaNd7rS4f8jpA5Apx8em70oYEe7+4= github.com/jfrog/jfrog-apps-config v1.0.1 h1:mtv6k7g8A8BVhlHGlSveapqf4mJfonwvXYLipdsOFMY= github.com/jfrog/jfrog-apps-config v1.0.1/go.mod h1:8AIIr1oY9JuH5dylz2S6f8Ym2MaadPLR6noCBO4C22w= -github.com/jfrog/jfrog-cli-core/v2 v2.57.0 h1:3ON0J6Sjc2+4HZrzh4eSbdciXx3sJsJUIJ3TPQXh/5c= -github.com/jfrog/jfrog-cli-core/v2 v2.57.0/go.mod h1:SThaC/fniC96oN8YgCsHjvOxp5rBM7IppuIybn1oxT0= -github.com/jfrog/jfrog-client-go v1.48.2 h1:YVAIiNWuBEa4NbWL54I+YzvXHaxoHDk532USDKTvgLU= -github.com/jfrog/jfrog-client-go v1.48.2/go.mod h1:1a7bmQHkRmPEza9wva2+WVrYzrGbosrMymq57kyG5gU= +github.com/jfrog/jfrog-cli-core/v2 v2.57.2 h1:2shy1CRWm/8yf6WWfVyAW3AdmryQiI73Tkhfb62vgPE= +github.com/jfrog/jfrog-cli-core/v2 v2.57.2/go.mod h1:sgi0gw96J00Yzx2cKG5xTG/x9XD0YiJbglJOnXUeaD0= +github.com/jfrog/jfrog-client-go v1.48.4 h1:uXvBr2ebFKpBRUhWgC9TSSJe32IbSYGlbDp9tDzBcaY= +github.com/jfrog/jfrog-client-go v1.48.4/go.mod h1:2ySOMva54L3EYYIlCBYBTcTgqfrrQ19gtpA/MWfA/ec= github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= diff --git a/utils/validations/test_mocks.go b/utils/validations/test_mocks.go index a1123b77..1dac6f90 100644 --- a/utils/validations/test_mocks.go +++ b/utils/validations/test_mocks.go @@ -69,7 +69,8 @@ func XscServer(t *testing.T, params MockServerParams) (*httptest.Server, *config func getXscServerApiHandler(t *testing.T, params MockServerParams) func(w http.ResponseWriter, r *http.Request) { apiUrlPart := "api/v1/" - if xscutils.IsXscXrayInnerService(params.XrayVersion) { + var isXrayAfterXscMigration bool + if isXrayAfterXscMigration = xscutils.IsXscXrayInnerService(params.XrayVersion); isXrayAfterXscMigration { apiUrlPart = "" } return func(w http.ResponseWriter, r *http.Request) { @@ -153,7 +154,22 @@ func XrayServer(t *testing.T, params MockServerParams) (*httptest.Server, *confi } } } - if !xscutils.IsXscXrayInnerService(params.XrayVersion) { + + isXrayAfterXscMigration := xscutils.IsXscXrayInnerService(params.XrayVersion) + if strings.Contains(r.RequestURI, "/xsc/profile_repos") && isXrayAfterXscMigration { + assert.Equal(t, http.MethodPost, r.Method) + w.WriteHeader(http.StatusOK) + content, err := os.ReadFile("../../tests/testdata/other/configProfile/configProfileExample.json") + if !assert.NoError(t, err) { + return + } + _, err = w.Write(content) + if !assert.NoError(t, err) { + return + } + } + + if !isXrayAfterXscMigration { return } getXscServerApiHandler(t, params)(w, r) diff --git a/utils/xsc/configprofile.go b/utils/xsc/configprofile.go index d1b7693b..eb1066d4 100644 --- a/utils/xsc/configprofile.go +++ b/utils/xsc/configprofile.go @@ -8,18 +8,37 @@ import ( "github.com/jfrog/jfrog-client-go/xsc/services" ) -func GetConfigProfile(xrayVersion, xscVersion string, serverDetails *config.ServerDetails, profileName string) (*services.ConfigProfile, error) { +func GetConfigProfileByName(xrayVersion, xscVersion string, serverDetails *config.ServerDetails, profileName string) (*services.ConfigProfile, error) { + // If Xray version >= 1.107.13 (After Xsc migration to Xray), xscVersion is 3.999.999 and will always suffice this condition if err := clientutils.ValidateMinimumVersion(clientutils.Xsc, xscVersion, services.ConfigProfileMinXscVersion); err != nil { - log.Info("Minimal Xsc version required to utilize config profile is '%s'. All configurations will be induced from provided Env vars and files") + log.Info(fmt.Sprintf("Minimal Xsc version required to utilize config profile by url is '%s'. All configurations will be induced from provided Env vars and files", services.ConfigProfileMinXscVersion)) return nil, err } + xscService, err := CreateXscService(xrayVersion, serverDetails) if err != nil { return nil, err } - configProfile, err := xscService.GetConfigProfile(profileName) + configProfile, err := xscService.GetConfigProfileByName(profileName) if err != nil { err = fmt.Errorf("failed to get config profile '%s': %q", profileName, err) } return configProfile, err } + +func GetConfigProfileByUrl(xrayVersion string, serverDetails *config.ServerDetails, cloneRepoUrl string) (*services.ConfigProfile, error) { + if err := clientutils.ValidateMinimumVersion(clientutils.Xray, xrayVersion, services.ConfigProfileByUrlMinXrayVersion); err != nil { + log.Info(fmt.Sprintf("Minimal Xray version required to utilize config profile by url is '%s'. All configurations will be induced from provided Env vars and files", services.ConfigProfileByUrlMinXrayVersion)) + return nil, err + } + xscService, err := CreateXscService(xrayVersion, serverDetails) + if err != nil { + return nil, err + } + + configProfile, err := xscService.GetConfigProfileByUrl(cloneRepoUrl) + if err != nil { + err = fmt.Errorf("failed to get config profile for url '%s': %q", serverDetails.Url, err) + } + return configProfile, err +} diff --git a/utils/xsc/configprofile_test.go b/utils/xsc/configprofile_test.go index 0e52a560..7e1473a0 100644 --- a/utils/xsc/configprofile_test.go +++ b/utils/xsc/configprofile_test.go @@ -11,33 +11,78 @@ import ( "github.com/stretchr/testify/assert" ) -func TestGetConfigProfile(t *testing.T) { +const testRepoUrl = "https://github.com/jfrog/test-repository.git" + +func TestGetConfigProfileByName(t *testing.T) { testCases := []struct { name string mockParams validations.MockServerParams expectError bool }{ { - name: "Deprecated XSC service", + name: "Deprecated XSC service - Valid request", mockParams: validations.MockServerParams{XrayVersion: "3.0.0", XscVersion: services.ConfigProfileMinXscVersion}, }, { - name: "Xsc as inner service in Xray", + name: "Deprecated XSC service - Xsc version too low - error expected", + mockParams: validations.MockServerParams{XrayVersion: "3.0.0", XscVersion: "1.0.0"}, + expectError: true, + }, + { + name: "Xsc as inner service in Xray - valid request", mockParams: validations.MockServerParams{XrayVersion: xscutils.MinXrayVersionXscTransitionToXray, XscVersion: services.ConfigProfileMinXscVersion}, }, + } + + for _, testcase := range testCases { + t.Run(testcase.name, func(t *testing.T) { + mockServer, serverDetails := validations.XscServer(t, testcase.mockParams) + defer mockServer.Close() + + configProfile, err := GetConfigProfileByName(testcase.mockParams.XrayVersion, testcase.mockParams.XscVersion, serverDetails, validations.TestConfigProfileName) + if testcase.expectError { + assert.Error(t, err) + assert.Nil(t, configProfile) + return + } + // Validate results + assert.NoError(t, err) + + profileFileContent, err := os.ReadFile("../../tests/testdata/other/configProfile/configProfileExample.json") + assert.NoError(t, err) + + var configProfileForComparison services.ConfigProfile + err = json.Unmarshal(profileFileContent, &configProfileForComparison) + assert.NoError(t, err) + + assert.Equal(t, &configProfileForComparison, configProfile) + }) + } +} + +func TestGetConfigProfileByUrl(t *testing.T) { + testCases := []struct { + name string + mockParams validations.MockServerParams + expectError bool + }{ { - name: "Expected error - Xsc version too low", - mockParams: validations.MockServerParams{XrayVersion: xscutils.MinXrayVersionXscTransitionToXray, XscVersion: "1.0.0"}, + name: "Xray version too low - error expected", + mockParams: validations.MockServerParams{XrayVersion: "3.108.0"}, expectError: true, }, + { + name: "Valid request", + mockParams: validations.MockServerParams{XrayVersion: services.ConfigProfileByUrlMinXrayVersion}, + }, } for _, testcase := range testCases { t.Run(testcase.name, func(t *testing.T) { - mockServer, serverDetails := validations.XscServer(t, testcase.mockParams) + mockServer, serverDetails := validations.XrayServer(t, testcase.mockParams) defer mockServer.Close() - configProfile, err := GetConfigProfile(testcase.mockParams.XrayVersion, testcase.mockParams.XscVersion, serverDetails, validations.TestConfigProfileName) + configProfile, err := GetConfigProfileByUrl(testcase.mockParams.XrayVersion, serverDetails, testRepoUrl) if testcase.expectError { assert.Error(t, err) assert.Nil(t, configProfile)