Skip to content

Commit

Permalink
feat(contrib/artifactory): enable xray build scan through project var…
Browse files Browse the repository at this point in the history
…iable (#6128)

Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin authored Apr 5, 2022
1 parent f9a5b4d commit 042861d
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"time"

"github.com/golang/protobuf/ptypes/empty"
"github.com/jfrog/jfrog-client-go/distribution/services"
Expand Down Expand Up @@ -34,7 +35,7 @@ func (actPlugin *artifactoryReleaseBundleDistributePlugin) Manifest(_ context.Co
}, nil
}

func (actPlugin *artifactoryReleaseBundleDistributePlugin) Run(_ context.Context, q *actionplugin.ActionQuery) (*actionplugin.ActionResult, error) {
func (actPlugin *artifactoryReleaseBundleDistributePlugin) Run(ctx context.Context, q *actionplugin.ActionQuery) (*actionplugin.ActionResult, error) {
name := q.GetOptions()["name"]
version := q.GetOptions()["version"]
url := q.GetOptions()["url"]
Expand Down Expand Up @@ -63,8 +64,11 @@ func (actPlugin *artifactoryReleaseBundleDistributePlugin) Run(_ context.Context
return actionplugin.Fail("missing Artifactory Distribution Token")
}

ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

log.SetLogger(log.NewLogger(log.INFO, os.Stdout))
distriClient, err := art.CreateDistributionClient(url, token)
distriClient, err := art.CreateDistributionClient(ctx, url, token)
if err != nil {
return actionplugin.Fail("unable to create distribution client: %v", err)
}
Expand Down
11 changes: 9 additions & 2 deletions contrib/integrations/artifactory/artifactory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package art

import (
"context"
"fmt"
"net/url"
"strings"
Expand Down Expand Up @@ -50,14 +51,17 @@ type DistribClient struct {
ServiceConfig config.Config
}

func CreateDistributionClient(url, token string) (DistribClient, error) {
func CreateDistributionClient(ctx context.Context, url, token string) (DistribClient, error) {
dtb := authdistrib.NewDistributionDetails()
dtb.SetUrl(strings.Replace(url, "/artifactory/", "/distribution/", -1))
dtb.SetAccessToken(token)
serviceConfig, err := config.NewConfigBuilder().
SetServiceDetails(dtb).
SetThreads(1).
SetDryRun(false).
SetContext(ctx).
SetHttpTimeout(60 * time.Second).
SetHttpRetries(5).
Build()
if err != nil {
return DistribClient{}, fmt.Errorf("unable to create service config: %v", err)
Expand All @@ -69,14 +73,17 @@ func CreateDistributionClient(url, token string) (DistribClient, error) {
return DistribClient{Dsm: dsm, ServiceConfig: serviceConfig}, nil
}

func CreateArtifactoryClient(url, token string) (artifactory.ArtifactoryServicesManager, error) {
func CreateArtifactoryClient(ctx context.Context, url, token string) (artifactory.ArtifactoryServicesManager, error) {
rtDetails := auth.NewArtifactoryDetails()
rtDetails.SetUrl(strings.TrimSuffix(url, "/") + "/") // ensure having '/' at the end
rtDetails.SetAccessToken(token)
serviceConfig, err := config.NewConfigBuilder().
SetServiceDetails(rtDetails).
SetThreads(1).
SetDryRun(false).
SetContext(ctx).
SetHttpTimeout(60 * time.Second).
SetHttpRetries(5).
Build()
if err != nil {
return nil, fmt.Errorf("unable to create service config: %v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/golang/protobuf/ptypes/empty"
"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/buildinfo"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/utils/log"

Expand Down Expand Up @@ -64,7 +65,7 @@ func (e *artifactoryBuildInfoPlugin) Manifest(_ context.Context, _ *empty.Empty)
}, nil
}

func (e *artifactoryBuildInfoPlugin) Run(_ context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
func (e *artifactoryBuildInfoPlugin) Run(ctx context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
artifactoryURL := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigURL)]
token := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigToken)]
tokenName := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigTokenName)]
Expand All @@ -76,11 +77,14 @@ func (e *artifactoryBuildInfoPlugin) Run(_ context.Context, opts *integrationplu
projectKey := opts.GetOptions()["cds.project"]
workflowName := opts.GetOptions()["cds.workflow"]

artiClient, err := art.CreateArtifactoryClient(artifactoryURL, token)
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

artiClient, err := art.CreateArtifactoryClient(ctx, artifactoryURL, token)
if err != nil {
return fail("unable to create artifactory client: %v", err)
}
log.SetLogger(log.NewLogger(log.ERROR, os.Stdout))
log.SetLogger(log.NewLogger(log.INFO, os.Stdout))

buildInfoName := fmt.Sprintf("%s/%s/%s", buildInfo, projectKey, workflowName)

Expand All @@ -92,6 +96,14 @@ func (e *artifactoryBuildInfoPlugin) Run(_ context.Context, opts *integrationplu
nodeRunURL := opts.GetOptions()["cds.ui.pipeline.run"]
runURL := nodeRunURL[0:strings.Index(nodeRunURL, "/node/")]

fmt.Printf("Creating Artifactory Build %s %s on project %s...\n", buildInfoName, version, artifactoryProjectKey)

// Get the build agent from env variable set by worker
workerName := os.Getenv("CDS_WORKER")
if workerName == "" {
workerName = "CDS"
}

buildInfoRequest := &buildinfo.BuildInfo{
Properties: map[string]string{},
Name: buildInfoName,
Expand All @@ -100,7 +112,7 @@ func (e *artifactoryBuildInfoPlugin) Run(_ context.Context, opts *integrationplu
Version: sdk.VERSION,
},
BuildAgent: &buildinfo.Agent{
Name: "CDS",
Name: workerName,
Version: sdk.VERSION,
},
ArtifactoryPrincipal: fmt.Sprintf("token:%s", tokenName),
Expand All @@ -111,10 +123,16 @@ func (e *artifactoryBuildInfoPlugin) Run(_ context.Context, opts *integrationplu
Modules: []buildinfo.Module{},
VcsList: make([]buildinfo.Vcs, 0),
}

gitUrl := opts.GetOptions()["git.url"]
if gitUrl == "" {
gitUrl = opts.GetOptions()["git.http_url"]
}

buildInfoRequest.VcsList = append(buildInfoRequest.VcsList, buildinfo.Vcs{
Branch: opts.GetOptions()["git.branch"],
Message: opts.GetOptions()["git.message"],
Url: opts.GetOptions()["git.http_url"],
Url: gitUrl,
Revision: opts.GetOptions()["git.hash"],
})

Expand All @@ -133,6 +151,24 @@ func (e *artifactoryBuildInfoPlugin) Run(_ context.Context, opts *integrationplu
if _, err := artiClient.PublishBuildInfo(buildInfoRequest, artifactoryProjectKey); err != nil {
return fail("unable to push build info: %v", err)
}

// Temporary code
if opts.GetOptions()["cds.proj.xray.enabled"] == "true" {
fmt.Printf("Triggering XRay Build %s %s scan...\n", buildInfoName, version)

// Scan build info
scanBuildRequest := services.NewXrayScanParams()
scanBuildRequest.BuildName = buildInfoRequest.Name
scanBuildRequest.BuildNumber = buildInfoRequest.Number
scanBuildRequest.ProjectKey = artifactoryProjectKey
scanBuildResponseBtes, err := artiClient.XrayScanBuild(scanBuildRequest)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(string(scanBuildResponseBtes))
}
}

return &integrationplugin.RunResult{
Status: sdk.StatusSuccess,
}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strconv"
"time"

"github.com/golang/protobuf/ptypes/empty"
"github.com/jfrog/jfrog-client-go/artifactory/services"
Expand Down Expand Up @@ -60,7 +61,7 @@ func (e *artifactoryDownloadArtifactPlugin) Manifest(_ context.Context, _ *empty
}, nil
}

func (e *artifactoryDownloadArtifactPlugin) Run(_ context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
func (e *artifactoryDownloadArtifactPlugin) Run(ctx context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
cdsRepo := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigCdsRepository)]
artifactoryURL := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigURL)]
token := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigToken)]
Expand All @@ -75,11 +76,14 @@ func (e *artifactoryDownloadArtifactPlugin) Run(_ context.Context, opts *integra
return fail("unable to read file permission %s: %v", permS, err)
}

artiClient, err := art.CreateArtifactoryClient(artifactoryURL, token)
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

artiClient, err := art.CreateArtifactoryClient(ctx, artifactoryURL, token)
if err != nil {
return fail("unable to create artifactory client: %v", err)
}
log.SetLogger(log.NewLogger(log.ERROR, os.Stdout))
log.SetLogger(log.NewLogger(log.INFO, os.Stdout))
fileutils.SetTempDirBase(opts.GetOptions()["cds.workspace"])

params := services.NewDownloadParams()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"regexp"
"strings"
"time"

"github.com/golang/protobuf/ptypes/empty"
"github.com/jfrog/jfrog-client-go/utils/log"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (e *artifactoryPromotePlugin) Manifest(_ context.Context, _ *empty.Empty) (
}, nil
}

func (e *artifactoryPromotePlugin) Run(_ context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
func (e *artifactoryPromotePlugin) Run(ctx context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
artifactoryURL := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigURL)]
token := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigToken)]

Expand All @@ -68,9 +69,12 @@ func (e *artifactoryPromotePlugin) Run(_ context.Context, opts *integrationplugi
return fail("unable to list run results: %v", err)
}

log.SetLogger(log.NewLogger(log.ERROR, os.Stdout))
log.SetLogger(log.NewLogger(log.INFO, os.Stdout))

artiClient, err := art.CreateArtifactoryClient(artifactoryURL, token)
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

artiClient, err := art.CreateArtifactoryClient(ctx, artifactoryURL, token)
if err != nil {
return fail("unable to create artifactory client: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"regexp"
"strings"
"time"

"github.com/golang/protobuf/ptypes/empty"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (e *artifactoryReleasePlugin) Manifest(_ context.Context, _ *empty.Empty) (
}, nil
}

func (e *artifactoryReleasePlugin) Run(_ context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
func (e *artifactoryReleasePlugin) Run(ctx context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
artifactoryURL := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigURL)]
distributionURL := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigDistributionURL)]
token := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigToken)]
Expand Down Expand Up @@ -85,7 +86,7 @@ func (e *artifactoryReleasePlugin) Run(_ context.Context, opts *integrationplugi
return fail("unable to list run results: %v", err)
}

log.SetLogger(log.NewLogger(log.ERROR, os.Stdout))
log.SetLogger(log.NewLogger(log.INFO, os.Stdout))
if distributionURL == "" {
fmt.Printf("Using %s to release\n", artifactoryURL)
distributionURL = artifactoryURL
Expand All @@ -94,13 +95,17 @@ func (e *artifactoryReleasePlugin) Run(_ context.Context, opts *integrationplugi
fmt.Println("Using distribution token to release")
releaseToken = token
}
distriClient, err := art.CreateDistributionClient(distributionURL, releaseToken)

ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

distriClient, err := art.CreateDistributionClient(ctx, distributionURL, releaseToken)
if err != nil {
return fail("unable to create distribution client: %v", err)
}

// Promotion
artiClient, err := art.CreateArtifactoryClient(artifactoryURL, token)
artiClient, err := art.CreateArtifactoryClient(ctx, artifactoryURL, token)
if err != nil {
return fail("unable to create artifactory client: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (e *artifactoryUploadArtifactPlugin) Manifest(_ context.Context, _ *empty.E
}, nil
}

func (e *artifactoryUploadArtifactPlugin) Run(_ context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
func (e *artifactoryUploadArtifactPlugin) Run(ctx context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
prefix := "cds.integration.artifact_manager"
cdsRepo := opts.GetOptions()[fmt.Sprintf("%s.%s", prefix, sdk.ArtifactoryConfigCdsRepository)]
artifactoryURL := opts.GetOptions()[fmt.Sprintf("%s.%s", prefix, sdk.ArtifactoryConfigURL)]
Expand All @@ -73,7 +73,10 @@ func (e *artifactoryUploadArtifactPlugin) Run(_ context.Context, opts *integrati
version := opts.GetOptions()["cds.version"]
buildInfo := opts.GetOptions()[fmt.Sprintf("%s.%s", prefix, sdk.ArtifactoryConfigBuildInfoPrefix)]

artiClient, err := art.CreateArtifactoryClient(artifactoryURL, token)
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

artiClient, err := art.CreateArtifactoryClient(ctx, artifactoryURL, token)
if err != nil {
return fail("unable to create artifactory client: %v", err)
}
Expand Down
24 changes: 21 additions & 3 deletions engine/vcs/bitbucketserver/bitbucketserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bitbucketserver

import (
"context"
"os"
"testing"

"github.com/ovh/cds/engine/cache"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/stretchr/testify/require"
)

// TestNew needs githubClientID and githubClientSecret
func TestNewClient(t *testing.T) {
ghConsummer := getNewConsumer(t)
assert.NotNil(t, ghConsummer)
Expand Down Expand Up @@ -56,7 +56,7 @@ func getAuthorizedClient(t *testing.T) sdk.VCSAuthorizedClient {
redisPassword := cfg["redisPassword"]

if consumerKey == "" && privateKey == "" {
t.Logf("Unable to read github configuration. Skipping this tests.")
t.Logf("Unable to read bitbucket configuration. Skipping this tests.")
t.SkipNow()
}

Expand All @@ -71,7 +71,7 @@ func getAuthorizedClient(t *testing.T) sdk.VCSAuthorizedClient {
return cli
}

func TestClientAuthorizeToken(t *testing.T) {
func TestClientAuthorizeRedirect(t *testing.T) {
consumer := getNewConsumer(t)
token, url, err := consumer.AuthorizeRedirect(context.Background())
t.Logf("token: %s", token)
Expand All @@ -85,6 +85,24 @@ func TestClientAuthorizeToken(t *testing.T) {
require.NoError(t, err)
}

func TestClientAuthorizeToken(t *testing.T) {
token := os.Getenv("TOKEN")
verifier := os.Getenv("VERIFIER")

if token == "" || verifier == "" {
t.SkipNow()
}

consumer := getNewConsumer(t)
accesstoken, accesstokenSecret, err := consumer.AuthorizeToken(context.Background(), token, verifier)
require.NoError(t, err)
assert.NotEmpty(t, accesstoken)
assert.NotEmpty(t, accesstokenSecret)

t.Logf("accesstoken: %s", accesstoken)
t.Logf("accesstokenSecret: %s", accesstokenSecret)
}

func TestAuthorizedClient(t *testing.T) {
bitbucketClient := getAuthorizedClient(t)
assert.NotNil(t, bitbucketClient)
Expand Down

0 comments on commit 042861d

Please sign in to comment.