Skip to content

Commit

Permalink
Fixed getting oc version for v3 (jfrog#278)
Browse files Browse the repository at this point in the history
* Fixed getting OpenShift CLI version for v3.

* Improve comments.

* Fix typos.
  • Loading branch information
asafgabai authored Dec 12, 2021
1 parent 7c86303 commit 62df3b3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
24 changes: 23 additions & 1 deletion artifactory/commands/oc/startbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ func getOcVersion(executablePath string) (string, error) {
cmdArgs := []string{"version", "-o=json"}
outputBytes, err := exec.Command(executablePath, cmdArgs...).Output()
if err != nil {
return "", errorutils.CheckError(convertExitError(err))
// If an error occurred, maybe it's because the '-o' flag is not supported in OpenShift CLI v3 and below.
// Try executing this command without this flag.
return getOldOcVersion(executablePath)
}
var versionRes ocVersionResponse
err = json.Unmarshal(outputBytes, &versionRes)
Expand All @@ -226,6 +228,26 @@ func getOcVersion(executablePath string) (string, error) {
return versionRes.ClientVersion.GitVersion, nil
}

// Running 'oc version' without the '-o=json' flag that is not supported in OpenShift CLI v3 and below.
func getOldOcVersion(executablePath string) (string, error) {
outputBytes, err := exec.Command(executablePath, "version").Output()
if err != nil {
return "", errorutils.CheckError(convertExitError(err))
}
// In OpenShift CLI v3 the output of 'oc version' looks like this:
// oc v3.0.0
// kubernetes v1.11.0
// [...]
// Get the first line of the output
scanner := bufio.NewScanner(strings.NewReader(string(outputBytes)))
scanner.Scan()
firstLine := scanner.Text()
if !strings.HasPrefix(firstLine, "oc v") {
return "", errorutils.CheckErrorf("Could not parse OpenShift CLI version. JFrog CLI oc start-build command requires OpenShift CLI version " + minSupportedOcVersion + " or higher.")
}
return strings.TrimPrefix(firstLine, "oc "), nil
}

func convertExitError(err error) error {
if exitError, ok := err.(*exec.ExitError); ok {
return errors.New(string(exitError.Stderr))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exclude (
github.com/pkg/sftp v1.10.1
)

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.6.6-0.20211209072237-e2731f45f02a
replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.6.6-0.20211209124304-3d49a83d81f0

replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v0.1.5-0.20211209071650-c5f4d2e581c3

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ github.com/jfrog/build-info-go v0.1.5-0.20211209071650-c5f4d2e581c3 h1:QtPavjNqJ
github.com/jfrog/build-info-go v0.1.5-0.20211209071650-c5f4d2e581c3/go.mod h1:zP8ZaXsofjGRIVnStLHqAI0doqEZC6I/IjRJWeL60lE=
github.com/jfrog/gofrog v1.0.7-0.20211128152632-e218c460d703 h1:xig3nr0zrZHgpK9G50aJgmMTDUnJWEiVf7WwDm5V+oo=
github.com/jfrog/gofrog v1.0.7-0.20211128152632-e218c460d703/go.mod h1:9YN5v4LlsCfLIXpwQnzSf1wVtgjdHM20FzuIu58RMI4=
github.com/jfrog/jfrog-client-go v1.6.6-0.20211209072237-e2731f45f02a h1:VN0WpXMWyhSiAHeO/Jys+leA9B560lhe+9fCoh37N5M=
github.com/jfrog/jfrog-client-go v1.6.6-0.20211209072237-e2731f45f02a/go.mod h1:n0s30clEJyh6NywfsygQM53WP0/oytlvE72TqMLHKIw=
github.com/jfrog/jfrog-client-go v1.6.6-0.20211209124304-3d49a83d81f0 h1:I/ILev2bT8AtFEZU9/OWJBpAti+Qo6yL3fdvRuDwOYE=
github.com/jfrog/jfrog-client-go v1.6.6-0.20211209124304-3d49a83d81f0/go.mod h1:56x9yhaNvoYC2wEZSfDD1GkDmTirBE97O8DfrXkq17U=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
Expand Down
20 changes: 10 additions & 10 deletions xray/commands/scan/buildscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (bsc *BuildScanCommand) Run() (err error) {
}
defer func() {
if failBuild {
// deferred so if build summery fails, it will still return a fail build error if needed
// deferred so if build summary fails, it will still return a fail build error if needed
if err != nil {
log.Error(err)
}
Expand All @@ -102,7 +102,7 @@ func (bsc *BuildScanCommand) Run() (err error) {
}()

if bsc.includeVulnerabilities {
// if vulnerabilities flag is true, get vulnerabilities from xray with build-summery and print to output
// if vulnerabilities flag is true, get vulnerabilities from xray with build-summary and print to output
log.Info("Getting the build-summary from Xray...")
err = bsc.runBuildSummaryAndPrintResults(xrayManager, params)
if err != nil {
Expand Down Expand Up @@ -150,24 +150,24 @@ func convertIssuesToVulnerabilities(issues []services.Issue, params services.Xra
return vulnerabilities
}

func getCvesField(summeryCves []services.SummeryCve) []services.Cve {
// The build-summery API response includes both the score and the vector. We're taking the score only
func getCvesField(summaryCves []services.SummeryCve) []services.Cve {
// The build-summary API response includes both the score and the vector. We're taking the score only
// Example: "4.0/CVSS:2.0/AV:N/AC:L/Au:S/C:N/I:N/A:P" >> "4.0"
var cves []services.Cve
for _, summeryCve := range summeryCves {
for _, summaryCve := range summaryCves {
cve := services.Cve{
Id: summeryCve.Id,
CvssV2Score: strings.Split(summeryCve.CvssV2Score, "/")[0],
CvssV3Score: strings.Split(summeryCve.CvssV3Score, "/")[0],
Id: summaryCve.Id,
CvssV2Score: strings.Split(summaryCve.CvssV2Score, "/")[0],
CvssV3Score: strings.Split(summaryCve.CvssV3Score, "/")[0],
}
cves = append(cves, cve)
}
return cves
}

func getComponentsField(summeryComponents []services.SummeryComponent, impactPaths []string, buildName string) map[string]services.Component {
func getComponentsField(summaryComponents []services.SummeryComponent, impactPaths []string, buildName string) map[string]services.Component {
components := map[string]services.Component{}
for _, component := range summeryComponents {
for _, component := range summaryComponents {
componentImpactPaths := getComponentImpactPaths(component.ComponentId, buildName, impactPaths)
if len(componentImpactPaths) > 0 {
components[component.ComponentId] = services.Component{
Expand Down

0 comments on commit 62df3b3

Please sign in to comment.