Skip to content

Commit

Permalink
Config profile with repo API implementation (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman authored Dec 22, 2024
1 parent ccdab05 commit d0f1598
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 18 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
20 changes: 18 additions & 2 deletions utils/validations/test_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 22 additions & 3 deletions utils/xsc/configprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
59 changes: 52 additions & 7 deletions utils/xsc/configprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d0f1598

Please sign in to comment.