Skip to content

Commit 79a5c4c

Browse files
authored
Frogbot improvements (#5)
* Add support for xrUrl and rtUrl * Add missing trailing slash to urls * Update frogbot.yml * Add missing error handling
1 parent fb887fc commit 79a5c4c

File tree

3 files changed

+74
-43
lines changed

3 files changed

+74
-43
lines changed

Diff for: .github/workflows/frogbot.yml

+8-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ jobs:
2323
- name: Scan Pull Request
2424
env:
2525
FROGBOT_JF_USER: ${{ secrets.FROGBOT_JF_USER }}
26-
FROGBOT_JF_URL: ${{ secrets.FROGBOT_JF_URL }}
26+
FROGBOT_JF_URL: ${{ secrets.FROGBOT_JF_XRAY_URL }}
27+
FROGBOT_JF_XRAY_URL: ${{ secrets.FROGBOT_JF_URL }}
28+
FROGBOT_JF_ARTIFACTORY_URL: ${{ secrets.FROGBOT_JF_ARTIFACTORY_URL }}
2729
FROGBOT_JF_PASSWORD: ${{ secrets.FROGBOT_JF_PASSWORD }}
28-
FROGBOT_GIT_OWNER: $GITHUB_REPOSITORY_OWNER
29-
FROGBOT_GIT_REPO: ${{ github.repository }}
30-
FROGBOT_GIT_TOKEN: ${{secrets.GITHUB_TOKEN}}
31-
FROGBOT_GIT_BASE_BRANCH: $GITHUB_BASE_REF
30+
FROGBOT_GIT_OWNER: ${{ github.repository_owner }}
31+
FROGBOT_GIT_REPO: ${{ github.event.repository.name }}
32+
FROGBOT_GIT_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
FROGBOT_GIT_BASE_BRANCH: ${{ github.base_ref}}
3234
FROGBOT_PR: ${{ github.event.number }}
35+
JFROG_CLI_LOG_LEVEL: DEBUG
3336
run: ./frogbot scan-pull-request

Diff for: README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ Automated dependencies scanning using JFrog Xray.
99

1010
## Usage
1111

12-
- [Overview](#overview)
13-
- [Usage](#usage)
12+
- [FrogBot](#frogbot)
13+
- [Project Status](#project-status)
14+
- [Usage](#usage)
1415
- [Using Frogbot with GitHub Actions](#using-frogbot-with-github-actions)
1516
- [Using Frogbot with GitLab CI](#using-frogbot-with-gitlab-ci)
1617
- [Using Frogbot with Jenkins](#using-frogbot-with-jenkins)
1718
- [Download Frogbot through Artifactory](#download-frogbot-through-artifactory)
1819
- [Building and Testing the Sources](#building-and-testing-the-sources)
1920
- [Build Frogbot](#build-frogbot)
20-
- [Tests](#test)
21+
- [Tests](#tests)
2122
- [Code Contributions](#code-contributions)
2223
- [Release Notes](#release-notes)
2324

Diff for: main.go

+62-35
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"path"
78
"strconv"
89

910
"github.com/jfrog/frogbot/icons"
@@ -23,15 +24,17 @@ import (
2324
const (
2425
frogbotVersion = "0.0.0"
2526
// Env
26-
jfrogUser = "FROGBOT_JF_USER"
27-
jfrogUrl = "FROGBOT_JF_URL"
28-
jfrogPassword = "FROGBOT_JF_PASSWORD"
29-
jfrogToken = "FROGBOT_JF_TOKEN"
30-
gitRepoOwner = "FROGBOT_GIT_OWNER"
31-
gitRepo = "FROGBOT_GIT_REPO"
32-
gitToken = "FROGBOT_GIT_TOKEN"
33-
gitBaseBranch = "FROGBOT_GIT_BASE_BRANCH"
34-
prID = "FROGBOT_PR"
27+
jfrogUser = "FROGBOT_JF_USER"
28+
jfrogUrl = "FROGBOT_JF_URL"
29+
jfrogXrayUrl = "FROGBOT_JF_XRAY_URL"
30+
jfrogArtifactoryUrl = "FROGBOT_JF_ARTIFACTORY_URL"
31+
jfrogPassword = "FROGBOT_JF_PASSWORD"
32+
jfrogToken = "FROGBOT_JF_TOKEN"
33+
gitRepoOwner = "FROGBOT_GIT_OWNER"
34+
gitRepo = "FROGBOT_GIT_REPO"
35+
gitToken = "FROGBOT_GIT_TOKEN"
36+
gitBaseBranch = "FROGBOT_GIT_BASE_BRANCH"
37+
prID = "FROGBOT_PR"
3538
)
3639

3740
func main() {
@@ -79,61 +82,82 @@ func scanPullRequest(c *clitool.Context) error {
7982

8083
// Audit PR code
8184
// TODO - fill contex according to env/flags
82-
xrayScanParams := services.XrayGraphScanParams{}
85+
xrayScanParams := services.XrayGraphScanParams{IncludeVulnerabilities: true}
8386
wd, err := os.Getwd()
8487
if err != nil {
8588
return err
8689
}
90+
clientLog.Info("Auditing " + wd)
8791
currentScan, err := runAudit(xrayScanParams, &server, wd)
92+
if err != nil {
93+
return err
94+
}
8895
// Audit target code
96+
clientLog.Info("Auditing " + repo + " " + baseBranch)
8997
previousScan, err := auditTarget(client, xrayScanParams, &server, repoOwner, repo, baseBranch)
9098
if err != nil {
9199
return err
92100
}
93101
// Get only the new issues added by this PR
94-
violations := getNewViolations(previousScan[0], currentScan[0]) // TODO - handle array of scan results!
102+
var vulnerabilitiesRows []xrayutils.VulnerabilityRow
103+
// TODO - handle array of scan results!
104+
if len(currentScan[0].Violations) > 0 {
105+
vulnerabilitiesRows = getNewViolations(previousScan[0], currentScan[0])
106+
} else if len(currentScan[0].Vulnerabilities) > 0 {
107+
vulnerabilitiesRows = getNewVulnerabilities(previousScan[0], currentScan[0])
108+
}
95109
// Comment frogbot message on the PR
96-
message := createPullRequestMessage(violations)
110+
message := createPullRequestMessage(vulnerabilitiesRows)
97111
return client.AddPullRequestComment(context.Background(), repoOwner, repo, message, pullRequestID)
98112

99113
}
100114

101115
func extractParamsFromEnv() (server coreconfig.ServerDetails, repoOwner, token, repo, baseBranch string, pullRequestID int, err error) {
102-
url, exists := os.LookupEnv(jfrogUrl)
103-
if !exists {
104-
err = fmt.Errorf("%s is missing", jfrogUrl)
105-
return
116+
url := os.Getenv(jfrogUrl)
117+
xrUrl := os.Getenv(jfrogXrayUrl)
118+
rtUrl := os.Getenv(jfrogArtifactoryUrl)
119+
if xrUrl != "" && rtUrl != "" {
120+
server.XrayUrl = xrUrl
121+
server.ArtifactoryUrl = rtUrl
122+
} else {
123+
if url == "" {
124+
err = fmt.Errorf("%s or %s and %s are missing", url, xrUrl, rtUrl)
125+
return
126+
}
127+
server.Url = url
128+
server.XrayUrl = url + "/xray/"
129+
server.ArtifactoryUrl = path.Join(url, "artifactory") + "/"
106130
}
107-
server.Url = url
108-
password, passwordExists := os.LookupEnv(jfrogPassword)
109-
user, userExists := os.LookupEnv(jfrogUser)
110-
if passwordExists && userExists {
131+
132+
password := os.Getenv(jfrogPassword)
133+
user := os.Getenv(jfrogUser)
134+
if password != "" && user != "" {
111135
server.User = user
112136
server.Password = password
113-
} else if accessToken, exists := os.LookupEnv(jfrogToken); exists {
137+
} else if accessToken := os.Getenv(jfrogToken); accessToken != "" {
114138
server.AccessToken = accessToken
115139
} else {
116140
err = fmt.Errorf("%s and %s or %s are missing", jfrogUser, jfrogPassword, jfrogToken)
117141
return
118142
}
119-
if repoOwner, exists = os.LookupEnv(gitRepoOwner); !exists {
143+
if repoOwner = os.Getenv(gitRepoOwner); repoOwner == "" {
120144
err = fmt.Errorf("%s is missing", gitRepoOwner)
121145
return
122146
}
123-
if repo, exists = os.LookupEnv(gitRepo); !exists {
147+
if repo = os.Getenv(gitRepo); repo == "" {
124148
err = fmt.Errorf("%s is missing", gitRepo)
125149
return
126150
}
127-
if token, exists = os.LookupEnv(gitToken); !exists {
151+
if token = os.Getenv(gitToken); token == "" {
128152
err = fmt.Errorf("%s is missing", gitToken)
129153
return
130154
}
131-
if baseBranch, exists = os.LookupEnv(gitBaseBranch); !exists {
155+
if baseBranch = os.Getenv(gitBaseBranch); baseBranch == "" {
132156
err = fmt.Errorf("%s is missing", gitBaseBranch)
133157
return
134158
}
135-
pullRequestIDString, exists := os.LookupEnv(prID)
136-
if !exists {
159+
pullRequestIDString := os.Getenv(prID)
160+
if pullRequestIDString == "" {
137161
err = fmt.Errorf("%s is missing", prID)
138162
return
139163
}
@@ -169,8 +193,14 @@ func auditTarget(client vcsclient.VcsClient, xrayScanParams services.XrayGraphSc
169193
if err != nil {
170194
return
171195
}
196+
clientLog.Debug("Created temp working directory: " + tempWorkdir)
172197
defer fileutils.RemoveTempDir(tempWorkdir)
198+
clientLog.Debug(fmt.Sprintf("Downloading %s/%s , branch:%s to:%s", owner, repo, branch, tempWorkdir))
173199
err = client.DownloadRepository(context.Background(), owner, repo, branch, tempWorkdir)
200+
if err != nil {
201+
return
202+
}
203+
clientLog.Debug("Downloaded target repository")
174204
return runAudit(xrayScanParams, server, tempWorkdir)
175205
}
176206

@@ -218,22 +248,19 @@ func getNewVulnerabilities(previousScan, currentScan services.ScanResponse) (new
218248
}
219249

220250
func GetUniqueID(vulnerability xrayutils.VulnerabilityRow) string {
221-
return vulnerability.IssueId + vulnerability.Components[0].Name
251+
return vulnerability.ImpactedPackageName + vulnerability.ImpactedPackageVersion + vulnerability.IssueId
222252

223253
}
224254

225255
func createPullRequestMessage(vulnerabilitiesRows []xrayutils.VulnerabilityRow) string {
226256
if len(vulnerabilitiesRows) == 0 {
227257
return icons.GetIconTag(icons.NoVulnerabilityBannerSource)
228258
}
229-
tableHeder := `| SEVERITY | IMPACTED PACKAGE | IMPACTED PACKAGE VERSION | FIXED VERSIONS | COMPONENT | COMPONENT VERSION | CVE
230-
:--: | -- | -- | -- | -- | :--: | --`
231-
tableContent := `
232-
233-
234-
`
259+
tableHeder := "\n| SEVERITY | IMPACTED PACKAGE | IMPACTED PACKAGE VERSION | FIXED VERSIONS | COMPONENT | COMPONENT VERSION | CVE\n" +
260+
":--: | -- | -- | -- | -- | :--: | --"
261+
var tableContent string
235262
for _, vulnerability := range vulnerabilitiesRows {
236-
tableContent += fmt.Sprintf("| %s | %s | %s | %s | %s | %s | %s \n", icons.GetIconTag(icons.GetIconSource(vulnerability.Severity)), vulnerability.ImpactedPackageName,
263+
tableContent += fmt.Sprintf("\n| %s | %s | %s | %s | %s | %s | %s ", icons.GetIconTag(icons.GetIconSource(vulnerability.Severity)), vulnerability.ImpactedPackageName,
237264
vulnerability.ImpactedPackageVersion, vulnerability.FixedVersions, vulnerability.Components[0].Name, vulnerability.Components[0].Version, vulnerability.Cves[0].Id)
238265
}
239266
return icons.GetIconTag(icons.VulnerabilitiesBannerSource) + tableHeder + tableContent

0 commit comments

Comments
 (0)