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

added logic of fetching golang private packages for whitesource step #4595

Merged
merged 14 commits into from
Oct 6, 2023
Merged
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions cmd/detectExecuteScan.go
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ import (
bd "github.com/SAP/jenkins-library/pkg/blackduck"
"github.com/SAP/jenkins-library/pkg/command"
piperGithub "github.com/SAP/jenkins-library/pkg/github"
"github.com/SAP/jenkins-library/pkg/golang"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/maven"
@@ -138,6 +139,14 @@ func detectExecuteScan(config detectExecuteScanOptions, _ *telemetry.CustomData,
if err != nil {
log.Entry().WithError(err).Warning("Failed to get GitHub client")
}

if config.PrivateModules == "" && config.PrivateModulesGitToken != "" {
//configuring go private packages
if err := golang.PrepareGolangPrivatePackages("detectExecuteStep", config.PrivateModules, config.PrivateModulesGitToken); err != nil {
log.Entry().Warningf("couldn't set private packages for golang, error: %s", err.Error())
}
}

utils := newDetectUtils(client)
if err := runDetect(ctx, config, utils, influx); err != nil {
log.Entry().
36 changes: 36 additions & 0 deletions cmd/detectExecuteScan_generated.go

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

2 changes: 1 addition & 1 deletion cmd/golangBuild_generated.go

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

8 changes: 8 additions & 0 deletions cmd/whitesourceExecuteScan.go
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ import (

"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/format"
"github.com/SAP/jenkins-library/pkg/golang"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/npm"
"github.com/SAP/jenkins-library/pkg/piperutils"
@@ -157,6 +158,13 @@ func whitesourceExecuteScan(config ScanOptions, _ *telemetry.CustomData, commonP
}

func runWhitesourceExecuteScan(ctx context.Context, config *ScanOptions, scan *ws.Scan, utils whitesourceUtils, sys whitesource, commonPipelineEnvironment *whitesourceExecuteScanCommonPipelineEnvironment, influx *whitesourceExecuteScanInflux) error {
if config != nil && config.PrivateModules != "" && config.PrivateModulesGitToken != "" {
//configuring go private packages
if err := golang.PrepareGolangPrivatePackages("WhitesourceExecuteStep", config.PrivateModules, config.PrivateModulesGitToken); err != nil {
log.Entry().Warningf("couldn't set private packages for golang, error: %s", err.Error())
}
}

if err := resolveAggregateProjectName(config, scan, sys); err != nil {
return errors.Wrapf(err, "failed to resolve and aggregate project name")
}
36 changes: 36 additions & 0 deletions cmd/whitesourceExecuteScan_generated.go

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

43 changes: 43 additions & 0 deletions pkg/golang/golang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package golang

import (
"fmt"
"os"
"strings"

"github.com/SAP/jenkins-library/pkg/command"
)

type utilsBundle struct {
command.Command
}

// prepare golang private packages for whitesource and blackduck(detectExecuteScan)
func PrepareGolangPrivatePackages(stepName, privateModules, privateModulesGitToken string) error {
utils := &utilsBundle{
Command: command.Command{
StepName: stepName,
},
}
os.Setenv("GOPRIVATE", privateModules)
err := gitConfigurationForPrivateModules(privateModules, privateModulesGitToken, utils)
if err != nil {
return err
}
return nil
}

func gitConfigurationForPrivateModules(privateMod string, token string, utils *utilsBundle) error {
privateMod = strings.ReplaceAll(privateMod, "/*", "")
privateMod = strings.ReplaceAll(privateMod, "*.", "")
modules := strings.Split(privateMod, ",")
for _, v := range modules {
authenticatedRepoURL := fmt.Sprintf("https://%s@%s", token, v)
repoBaseURL := fmt.Sprintf("https://%s", v)
err := utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoBaseURL)
if err != nil {
return err
}
}
return nil
}
29 changes: 29 additions & 0 deletions resources/metadata/detectExecuteScan.yaml
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@ spec:
- name: githubTokenCredentialsId
description: Jenkins 'Secret text' credentials ID containing token to authenticate to GitHub.
type: jenkins
- name: golangPrivateModulesGitTokenCredentialsId
description: Jenkins 'Username with password' credentials ID containing username/password for http access to your git repos where your go private modules are stored.
type: jenkins
params:
- name: token
aliases:
@@ -489,6 +492,32 @@ spec:
- PARAMETERS
- STAGES
- STEPS
- name: privateModules
type: "string"
description: Tells go which modules shall be considered to be private (by setting [GOPRIVATE](https://pkg.go.dev/cmd/go#hdr-Configuration_for_downloading_non_public_code)).
scope:
- GENERAL
- STEPS
- STAGES
- PARAMETERS
alias:
- goprivate
- name: privateModulesGitToken
description: GitHub personal access token as per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line.
scope:
- GENERAL
- PARAMETERS
- STAGES
- STEPS
type: string
secret: true
resourceRef:
- name: golangPrivateModulesGitTokenCredentialsId
type: secret
param: password
- type: vaultSecret
name: golangPrivateModulesGitTokenVaultSecret
default: golang
outputs:
resources:
- name: influx
1 change: 1 addition & 0 deletions resources/metadata/golangBuild.yaml
Original file line number Diff line number Diff line change
@@ -209,6 +209,7 @@ spec:
type: "string"
description: Tells go which modules shall be considered to be private (by setting [GOPRIVATE](https://pkg.go.dev/cmd/go#hdr-Configuration_for_downloading_non_public_code)).
scope:
- GENERAL
- STEPS
- STAGES
- PARAMETERS
29 changes: 29 additions & 0 deletions resources/metadata/whitesourceExecuteScan.yaml
Original file line number Diff line number Diff line change
@@ -38,6 +38,9 @@ spec:
- name: githubTokenCredentialsId
description: Jenkins 'Secret text' credentials ID containing token to authenticate to GitHub.
type: jenkins
- name: golangPrivateModulesGitTokenCredentialsId
description: Jenkins 'Username with password' credentials ID containing username/password for http access to your git repos where your go private modules are stored.
type: jenkins
params:
- name: agentDownloadUrl
type: string
@@ -597,6 +600,32 @@ spec:
- PARAMETERS
- STAGES
- STEPS
- name: privateModules
type: "string"
description: Tells go which modules shall be considered to be private (by setting [GOPRIVATE](https://pkg.go.dev/cmd/go#hdr-Configuration_for_downloading_non_public_code)).
scope:
- GENERAL
- STEPS
- STAGES
- PARAMETERS
alias:
- goprivate
- name: privateModulesGitToken
description: GitHub personal access token as per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line.
scope:
- GENERAL
- PARAMETERS
- STAGES
- STEPS
type: string
secret: true
resourceRef:
- name: golangPrivateModulesGitTokenCredentialsId
type: secret
param: password
- type: vaultSecret
name: golangPrivateModulesGitTokenVaultSecret
default: golang
resources:
- name: buildDescriptor
type: stash
3 changes: 2 additions & 1 deletion vars/detectExecuteScan.groovy
Original file line number Diff line number Diff line change
@@ -12,7 +12,8 @@ void call(Map parameters = [:]) {
parameters = DownloadCacheUtils.injectDownloadCacheInParameters(script, parameters, BuildTool.MAVEN)
List credentials = [
[type: 'token', id: 'detectTokenCredentialsId', env: ['PIPER_token']],
[type: 'token', id: 'githubTokenCredentialsId', env: ['PIPER_githubToken']]
[type: 'token', id: 'githubTokenCredentialsId', env: ['PIPER_githubToken']],
[type: 'usernamePassword', id: 'golangPrivateModulesGitTokenCredentialsId', env: ['PIPER_privateModulesGitUsername', 'PIPER_privateModulesGitToken']]
]
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)
}
1 change: 1 addition & 0 deletions vars/whitesourceExecuteScan.groovy
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ void call(Map parameters = [:]) {
[type: 'token', id: 'userTokenCredentialsId', env: ['PIPER_userToken']],
[type: 'token', id: 'githubTokenCredentialsId', env: ['PIPER_githubToken']],
[type: 'file', id: 'dockerConfigJsonCredentialsId', env: ['PIPER_dockerConfigJSON']],
[type: 'usernamePassword', id: 'golangPrivateModulesGitTokenCredentialsId', env: ['PIPER_privateModulesGitUsername', 'PIPER_privateModulesGitToken']]
]
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)
}