Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detectExecuteScan : Changes to include user group and handle build fails #1775

Merged
merged 26 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c3b1482
changes to detectExec before master merge
shenoygi Jun 17, 2020
84e08ca
merge with master
shenoygi Jun 17, 2020
4e89416
Merge remote-tracking branch 'upstream/master'
shenoygi Jun 24, 2020
564cac4
changes for detectExecuteScan
shenoygi Jun 24, 2020
9ca47c1
Merge remote-tracking branch 'upstream/master'
shenoygi Jun 25, 2020
1297fdd
self generated code added
shenoygi Jun 25, 2020
4b5c972
fix syntax errors and update docu
shenoygi Jun 25, 2020
324d3fa
added unit tests for fail and Group
shenoygi Jun 25, 2020
aa51b0e
fix failOn bug
shenoygi Jun 30, 2020
ad0cdb4
Merge remote-tracking branch 'upstream/master'
shenoygi Jun 30, 2020
43ccb26
Merge remote-tracking branch 'upstream/master'
shenoygi Jul 8, 2020
9aeeb36
merge upstream/master 22.07
shenoygi Jul 22, 2020
7f2267f
Merge branch 'master' into master
shenoygi Jul 22, 2020
32202f4
add Groups as string array
shenoygi Jul 23, 2020
ea7d31f
add Groups as string array
shenoygi Jul 23, 2020
5e658e4
Merge branch 'master' of https://github.com/shenoygi/jenkins-library
shenoygi Jul 23, 2020
7980b2e
Merge remote-tracking branch 'upstream/master'
shenoygi Jul 23, 2020
c3ccfe2
tests and validation for groups, failOn
shenoygi Jul 23, 2020
5902057
reset docu files as they wll be generated
shenoygi Jul 27, 2020
0ce1e26
Updated docs and added more tests
shenoygi Jul 27, 2020
e72a785
documentation md files should not be changed
shenoygi Jul 27, 2020
55f0b11
Handle merge conflicts from PR 1845
shenoygi Jul 27, 2020
c69389e
Handle merge conflicts from PR 1845
shenoygi Jul 27, 2020
2b73576
fix merge errors
shenoygi Jul 27, 2020
4e32226
Merge branch 'master' into master
OliverNocon Jul 28, 2020
9d59057
Merge branch 'master' into master
OliverNocon Jul 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions cmd/detectExecuteScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,26 @@ func addDetectArgs(args []string, config detectExecuteScanOptions) []string {

args = append(args, fmt.Sprintf("--blackduck.url=%v", config.ServerURL))
args = append(args, fmt.Sprintf("--blackduck.api.token=%v", config.APIToken))
// ProjectNames, VersionName, GroupName etc can contain spaces and need to be escaped using double quotes in CLI
// Hence the string need to be surrounded by \"
args = append(args, fmt.Sprintf("--detect.project.name=\\\"%v\\\"", config.ProjectName))
OliverNocon marked this conversation as resolved.
Show resolved Hide resolved
args = append(args, fmt.Sprintf("--detect.project.version.name=\\\"%v\\\"", detectVersionName))

// Groups parameter is added only when there is atleast one non-empty groupname provided
if len(config.Groups) > 0 && len(config.Groups[0]) > 0 {
args = append(args, fmt.Sprintf("--detect.project.user.groups=\\\"%v\\\"", strings.Join(config.Groups, "\\\",\\\"")))
OliverNocon marked this conversation as resolved.
Show resolved Hide resolved
}

// Atleast 1, non-empty category to fail on must be provided
if len(config.FailOn) > 0 && len(config.FailOn[0]) > 0 {
args = append(args, fmt.Sprintf("--detect.policy.check.fail.on.severities=%v", strings.Join(config.FailOn, ",")))
}

args = append(args, fmt.Sprintf("--detect.project.name=%v", config.ProjectName))
args = append(args, fmt.Sprintf("--detect.project.version.name=%v", detectVersionName))
codeLocation := config.CodeLocation
if len(codeLocation) == 0 && len(config.ProjectName) > 0 {
codeLocation = fmt.Sprintf("%v/%v", config.ProjectName, detectVersionName)
}
args = append(args, fmt.Sprintf("--detect.code.location.name=%v", codeLocation))
args = append(args, fmt.Sprintf("--detect.code.location.name=\\\"%v\\\"", codeLocation))

if sliceUtils.ContainsString(config.Scanners, "signature") {
args = append(args, fmt.Sprintf("--detect.blackduck.signature.scanner.paths=%v", strings.Join(config.ScanPaths, ",")))
Expand Down
30 changes: 26 additions & 4 deletions cmd/detectExecuteScan_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 38 additions & 8 deletions cmd/detectExecuteScan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func TestRunDetect(t *testing.T) {

assert.Equal(t, ".", s.Dir, "Wrong execution directory used")
assert.Equal(t, "/bin/bash", s.Shell[0], "Bash shell expected")
expectedScript := "bash <(curl -s https://detect.synopsys.com/detect.sh) --blackduck.url= --blackduck.api.token= --detect.project.name= --detect.project.version.name= --detect.code.location.name="
expectedScript := "bash <(curl -s https://detect.synopsys.com/detect.sh) --blackduck.url= --blackduck.api.token= --detect.project.name=\\\"\\\" --detect.project.version.name=\\\"\\\" --detect.code.location.name=\\\"\\\""
assert.Equal(t, expectedScript, s.Calls[0])
})

t.Run("failure case", func(t *testing.T) {
var hasFailed bool
log.Entry().Logger.ExitFunc = func(int) { hasFailed = true }

s := mock.ShellMockRunner{ShouldFailOnCommand: map[string]error{"bash <(curl -s https://detect.synopsys.com/detect.sh) --blackduck.url= --blackduck.api.token= --detect.project.name= --detect.project.version.name= --detect.code.location.name=": fmt.Errorf("Test Error")}}
s := mock.ShellMockRunner{ShouldFailOnCommand: map[string]error{"bash <(curl -s https://detect.synopsys.com/detect.sh) --blackduck.url= --blackduck.api.token= --detect.project.name=\\\"\\\" --detect.project.version.name=\\\"\\\" --detect.code.location.name=\\\"\\\"": fmt.Errorf("Test Error")}}
runDetect(detectExecuteScanOptions{}, &s)
assert.True(t, hasFailed, "expected command to exit with fatal")
})
Expand Down Expand Up @@ -57,9 +57,9 @@ func TestAddDetectArgs(t *testing.T) {
"--scan2=2",
"--blackduck.url=https://server.url",
"--blackduck.api.token=apiToken",
"--detect.project.name=testName",
"--detect.project.version.name=1.0",
"--detect.code.location.name=testName/1.0",
"--detect.project.name=\\\"testName\\\"",
"--detect.project.version.name=\\\"1.0\\\"",
"--detect.code.location.name=\\\"testName/1.0\\\"",
"--detect.blackduck.signature.scanner.paths=path1,path2",
},
},
Expand All @@ -72,16 +72,46 @@ func TestAddDetectArgs(t *testing.T) {
Version: "1.0",
VersioningModel: "major-minor",
CodeLocation: "testLocation",
FailOn: []string{"BLOCKER", "MAJOR"},
Scanners: []string{"source"},
ScanPaths: []string{"path1", "path2"},
Groups: []string{"testGroup"},
},
expected: []string{
"--testProp1=1",
"--blackduck.url=https://server.url",
"--blackduck.api.token=apiToken",
"--detect.project.name=testName",
"--detect.project.version.name=1.0",
"--detect.code.location.name=testLocation",
"--detect.project.name=\\\"testName\\\"",
"--detect.project.version.name=\\\"1.0\\\"",
"--detect.project.user.groups=\\\"testGroup\\\"",
"--detect.policy.check.fail.on.severities=BLOCKER,MAJOR",
"--detect.code.location.name=\\\"testLocation\\\"",
"--detect.source.path=path1",
},
},
{
args: []string{"--testProp1=1"},
options: detectExecuteScanOptions{
ServerURL: "https://server.url",
APIToken: "apiToken",
ProjectName: "testName",
CodeLocation: "testLocation",
FailOn: []string{"BLOCKER", "MAJOR"},
Scanners: []string{"source"},
ScanPaths: []string{"path1", "path2"},
Groups: []string{"testGroup", "testGroup2"},
Version: "1.0",
VersioningModel: "major-minor",
},
expected: []string{
"--testProp1=1",
"--blackduck.url=https://server.url",
"--blackduck.api.token=apiToken",
"--detect.project.name=\\\"testName\\\"",
"--detect.project.version.name=\\\"1.0\\\"",
"--detect.project.user.groups=\\\"testGroup\\\",\\\"testGroup2\\\"",
"--detect.policy.check.fail.on.severities=BLOCKER,MAJOR",
"--detect.code.location.name=\\\"testLocation\\\"",
"--detect.source.path=path1",
},
},
Expand Down
41 changes: 37 additions & 4 deletions resources/metadata/detect.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
metadata:
name: detectExecuteScan
description: Executes Synopsis Detect scan
longDescription: |-
This step executes [Synopsis Detect](https://synopsys.atlassian.net/wiki/spaces/INTDOCS/pages/62423113/Synopsys+Detect) scans.
description: Executes Synopsys Detect scan
longDescription: |
This step executes [Synopsys Detect](https://synopsys.atlassian.net/wiki/spaces/INTDOCS/pages/62423113/Synopsys+Detect) scans.
Synopsys Detect command line utlity can be used to run various scans including BlackDuck and Polaris scans. This step allows users to run BlackDuck scans by default.
Comment on lines +3 to +6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the docs are concatenated I would omit the first sentence.

Suggested change
description: Executes Synopsys Detect scan
longDescription: |
This step executes [Synopsys Detect](https://synopsys.atlassian.net/wiki/spaces/INTDOCS/pages/62423113/Synopsys+Detect) scans.
Synopsys Detect command line utlity can be used to run various scans including BlackDuck and Polaris scans. This step allows users to run BlackDuck scans by default.
description: Executes Synopsys Detect scan
longDescription: |
[Synopsys Detect](https://synopsys.atlassian.net/wiki/spaces/INTDOCS/pages/62423113/Synopsys+Detect) command line utlity can be used to run various scans including BlackDuck and Polaris scans. This step allows users to run BlackDuck scans by default.

Please configure your BlackDuck server Url using the serverUrl parameter and the API token of your user using the apiToken parameter for this step.
spec:
inputs:
resources:
Expand Down Expand Up @@ -86,7 +88,6 @@ spec:
- --blackduck.signature.scanner.memory=4096
- --blackduck.timeout=6000
- --blackduck.trust.cert=true
- --detect.policy.check.fail.on.severities=BLOCKER,CRITICAL,MAJOR
- --detect.report.timeout=4800
- --logging.level.com.synopsys.integration=DEBUG
scope:
Expand All @@ -103,6 +104,38 @@ spec:
- PARAMETERS
- STAGES
- STEPS
- name: groups
description: Users groups to be assigned for the Project
aliases:
- name: detect/groups
type: '[]string'
mandatory: false
scope:
- PARAMETERS
- STAGES
- STEPS
- name: failOn
description: Mark the current build as fail based on the policy categories applied.
longDescription: |
A list of policies can be provided which will be applied after the scan is completed. These policies if violated will mark the build/scan result as failed.
The list of accepted valed can be found at https://blackducksoftware.github.io/synopsys-detect/latest/properties/configuration/project/#fail-on-policy-violation-severities
aliases:
- name: detect/failOn
type: '[]string'
mandatory: false
default:
- BLOCKER
possibleValues:
- ALL
- BLOCKER
- CRITICAL
- MAJOR
- MINOR
- NONE
scope:
- PARAMETERS
- STAGES
- STEPS
- name: version
aliases:
- name: projectVersion
Expand Down