Skip to content

Commit

Permalink
Merge branch 'dev' into frogbot-golang.org/x/net-13b661a2386933d18e2c…
Browse files Browse the repository at this point in the history
…3167e5761c88
  • Loading branch information
EyalDelarea authored Jan 7, 2025
2 parents 6b43f7c + de902d8 commit e9a1a39
Show file tree
Hide file tree
Showing 24 changed files with 510 additions and 127 deletions.
18 changes: 10 additions & 8 deletions artifactory/services/utils/tests/xray/consts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package xray

import xrayServices "github.com/jfrog/jfrog-client-go/xray/services"
import (
xscServices "github.com/jfrog/jfrog-client-go/xsc/services"
)

const ScanResponse = `
{
Expand Down Expand Up @@ -1437,15 +1439,15 @@ const XscGitInfoResponse = `{"multi_scan_id": "3472b4e2-bddc-11ee-a9c9-acde48001

const XscGitInfoBadResponse = `"failed create git info request: git_repo_url field must contain value"`

var GitInfoContextWithMinimalRequiredFields = xrayServices.XscGitInfoContext{
GitRepoUrl: "https://git.jfrog.info/projects/XSC/repos/xsc-service",
BranchName: "feature/XRAY-123-cool-feature",
CommitHash: "acc5e24e69a-d3c1-4022-62eb-69e4a1e5",
var GitInfoContextWithMinimalRequiredFields = xscServices.XscGitInfoContext{
GitRepoHttpsCloneUrl: "https://git.jfrog.info/projects/XSC/repos/xsc-service",
BranchName: "feature/XRAY-123-cool-feature",
LastCommitHash: "acc5e24e69a-d3c1-4022-62eb-69e4a1e5",
}

var GitInfoContextWithMissingFields = xrayServices.XscGitInfoContext{
GitRepoUrl: "https://git.jfrog.info/projects/XSC/repos/xsc-service",
BranchName: "feature/XRAY-123-cool-feature",
var GitInfoContextWithMissingFields = xscServices.XscGitInfoContext{
GitRepoHttpsCloneUrl: "https://git.jfrog.info/projects/XSC/repos/xsc-service",
BranchName: "feature/XRAY-123-cool-feature",
}

const TestMultiScanId = "3472b4e2-bddc-11ee-a9c9-acde48001122"
Expand Down
33 changes: 14 additions & 19 deletions artifactory/usage/reportusage.go → artifactory/usage/call_home.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import (
"github.com/jfrog/jfrog-client-go/utils/io/httputils"
)

const minArtifactoryVersion = "6.9.0"

type ReportUsageAttribute struct {
AttributeName string
AttributeValue string
}

type ArtifactoryCallHome struct{}

func NewArtifactoryCallHome() *ArtifactoryCallHome {
return &ArtifactoryCallHome{}
}

func (rua *ReportUsageAttribute) isEmpty() bool {
return rua.AttributeName == ""
}

func validateAndGetUsageServerInfo(serviceManager artifactory.ArtifactoryServicesManager) (url string, clientDetails httputils.HttpClientDetails, err error) {
func (ach *ArtifactoryCallHome) getUsageServerInfo(serviceManager artifactory.ArtifactoryServicesManager) (url string, clientDetails httputils.HttpClientDetails, err error) {
config := serviceManager.GetConfig()
if config == nil {
err = errorutils.CheckErrorf("expected full config, but no configuration exists.")
Expand All @@ -33,15 +37,6 @@ func validateAndGetUsageServerInfo(serviceManager artifactory.ArtifactoryService
err = errorutils.CheckErrorf("Artifactory details not configured.")
return
}
// Check Artifactory version
artifactoryVersion, err := rtDetails.GetVersion()
if err != nil {
err = errors.New("Couldn't get Artifactory version. Error: " + err.Error())
return
}
if err = clientutils.ValidateMinimumVersion(clientutils.Artifactory, artifactoryVersion, minArtifactoryVersion); err != nil {
return
}
url, err = clientutils.BuildUrl(rtDetails.GetUrl(), "api/system/usage", make(map[string]string))
if err != nil {
return
Expand All @@ -50,7 +45,7 @@ func validateAndGetUsageServerInfo(serviceManager artifactory.ArtifactoryService
return
}

func sendReport(url string, serviceManager artifactory.ArtifactoryServicesManager, clientDetails httputils.HttpClientDetails, bodyContent []byte) error {
func (ach *ArtifactoryCallHome) sendReport(url string, serviceManager artifactory.ArtifactoryServicesManager, clientDetails httputils.HttpClientDetails, bodyContent []byte) error {
clientDetails.SetContentTypeApplicationJson()
resp, body, err := serviceManager.Client().SendPost(url, bodyContent, &clientDetails)
if err != nil {
Expand All @@ -63,28 +58,28 @@ func sendReport(url string, serviceManager artifactory.ArtifactoryServicesManage
return nil
}

func ReportUsageToArtifactory(productId string, serviceManager artifactory.ArtifactoryServicesManager, features ...Feature) error {
url, clientDetails, err := validateAndGetUsageServerInfo(serviceManager)
func (ach *ArtifactoryCallHome) SendToArtifactory(productId string, serviceManager artifactory.ArtifactoryServicesManager, features ...Feature) error {
url, clientDetails, err := ach.getUsageServerInfo(serviceManager)
if err != nil || url == "" {
return err
}
bodyContent, err := usageFeaturesToJson(productId, features...)
if err != nil {
return err
}
return sendReport(url, serviceManager, clientDetails, bodyContent)
return ach.sendReport(url, serviceManager, clientDetails, bodyContent)
}

func SendReportUsage(productId, commandName string, serviceManager artifactory.ArtifactoryServicesManager, attributes ...ReportUsageAttribute) error {
url, clientDetails, err := validateAndGetUsageServerInfo(serviceManager)
func (ach *ArtifactoryCallHome) Send(productId, commandName string, serviceManager artifactory.ArtifactoryServicesManager, attributes ...ReportUsageAttribute) error {
url, clientDetails, err := ach.getUsageServerInfo(serviceManager)
if err != nil || url == "" {
return err
}
bodyContent, err := reportUsageToJson(productId, commandName, attributes...)
if err != nil {
return err
}
return sendReport(url, serviceManager, clientDetails, bodyContent)
return ach.sendReport(url, serviceManager, clientDetails, bodyContent)
}

func usageFeaturesToJson(productId string, features ...Feature) ([]byte, error) {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.1
github.com/gookit/color v1.5.4
github.com/jfrog/archiver/v3 v3.6.1
github.com/jfrog/build-info-go v1.10.5
github.com/jfrog/build-info-go v1.10.7
github.com/jfrog/gofrog v1.7.6
github.com/minio/sha256-simd v1.0.1
github.com/stretchr/testify v1.9.0
Expand Down
13 changes: 3 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jfrog/archiver/v3 v3.6.1 h1:LOxnkw9pOn45DzCbZNFV6K0+6dCsQ0L8mR3ZcujO5eI=
github.com/jfrog/archiver/v3 v3.6.1/go.mod h1:VgR+3WZS4N+i9FaDwLZbq+jeU4B4zctXL+gL4EMzfLw=
github.com/jfrog/build-info-go v1.10.5 h1:cW03JlPlKv7RMUU896uLUxyLWXAmCgR5Y5QX0fwgz0Q=
github.com/jfrog/build-info-go v1.10.5/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE=
github.com/jfrog/build-info-go v1.10.7 h1:10NVHYg0193gJpQft+S4WQfvYMtj5jlwwhJRvkFJtBE=
github.com/jfrog/build-info-go v1.10.7/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE=
github.com/jfrog/gofrog v1.7.6 h1:QmfAiRzVyaI7JYGsB7cxfAJePAZTzFz0gRWZSE27c6s=
github.com/jfrog/gofrog v1.7.6/go.mod h1:ntr1txqNOZtHplmaNd7rS4f8jpA5Apx8em70oYEe7+4=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
Expand Down Expand Up @@ -124,8 +124,6 @@ github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMx
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
Expand All @@ -146,19 +144,14 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
Expand Down
17 changes: 17 additions & 0 deletions jfconnect/auth/jfconnectdetails.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package auth

import (
"github.com/jfrog/jfrog-client-go/auth"
)

func NewJfConnectDetails() auth.ServiceDetails {
return &jfConnectDetails{}
}

type jfConnectDetails struct {
auth.CommonConfigFields
}

func (jc *jfConnectDetails) GetVersion() (string, error) {
panic("Failed: Method is not implemented")
}
45 changes: 45 additions & 0 deletions jfconnect/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package jfconnect

import (
"github.com/jfrog/jfrog-client-go/config"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/jfconnect/services"
)

type Manager interface {
PostVisibilityMetric(services.VisibilityMetric) error
}

type jfConnectManager struct {
client *jfroghttpclient.JfrogHttpClient
config config.Config
}

func NewManager(config config.Config) (Manager, error) {
details := config.GetServiceDetails()
var err error
manager := &jfConnectManager{config: config}
manager.client, err = jfroghttpclient.JfrogClientBuilder().
SetCertificatesPath(config.GetCertificatesPath()).
SetInsecureTls(config.IsInsecureTls()).
SetClientCertPath(details.GetClientCertPath()).
SetClientCertKeyPath(details.GetClientCertKeyPath()).
AppendPreRequestInterceptor(details.RunPreRequestFunctions).
SetContext(config.GetContext()).
SetDialTimeout(config.GetDialTimeout()).
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
Build()

return manager, err
}

func (jm *jfConnectManager) Client() *jfroghttpclient.JfrogHttpClient {
return jm.client
}

func (jm *jfConnectManager) PostVisibilityMetric(metric services.VisibilityMetric) error {
jfConnectService := services.NewJfConnectService(jm.config.GetServiceDetails(), jm.client)
return jfConnectService.PostVisibilityMetric(metric)
}
61 changes: 61 additions & 0 deletions jfconnect/services/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package services

import (
"encoding/json"
"net/http"

"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

const LogMetricApiEndpoint = "api/v1/backoffice/metrics/log"

type JfConnectService struct {
client *jfroghttpclient.JfrogHttpClient
serviceDetails *auth.ServiceDetails
}

func NewJfConnectService(serviceDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) *JfConnectService {
return &JfConnectService{serviceDetails: &serviceDetails, client: client}
}

func (jcs *JfConnectService) GetJfConnectDetails() auth.ServiceDetails {
return *jcs.serviceDetails
}

func (jcs *JfConnectService) PostVisibilityMetric(metric VisibilityMetric) error {
metricJson, err := json.Marshal(metric)
if err != nil {
return errorutils.CheckError(err)
}
details := jcs.GetJfConnectDetails()
httpClientDetails := details.CreateHttpClientDetails()
httpClientDetails.SetContentTypeApplicationJson()

url := clientutils.AddTrailingSlashIfNeeded(details.GetUrl())
url += LogMetricApiEndpoint
resp, body, err := jcs.client.SendPost(url, metricJson, &httpClientDetails)
if err != nil {
return err
}
return errorutils.CheckResponseStatusWithBody(resp, body, http.StatusCreated, http.StatusOK)
}

type Labels struct {
ProductID string `json:"product_id"`
ProductVersion string `json:"product_version"`
FeatureID string `json:"feature_id"`
OIDCUsed string `json:"oidc_used"`
JobID string `json:"job_id"`
RunID string `json:"run_id"`
GitRepo string `json:"git_repo"`
GhTokenForCodeScanningAlertsProvided string `json:"gh_token_for_code_scanning_alerts_provided"`
}

type VisibilityMetric struct {
Value int `json:"value"`
MetricsName string `json:"metrics_name"`
Labels Labels `json:"labels"`
}
25 changes: 19 additions & 6 deletions tests/xraypolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestXrayPolicy(t *testing.T) {
t.Run("create2Priorities", create2Priorities)
t.Run("createPolicyActions", createPolicyActions)
t.Run("createUpdatePolicy", createUpdatePolicy)
t.Run("createSkipNonApplicablePolicy", createSkipNonApplicable)
}

func deletePolicy(t *testing.T, policyName string) {
Expand All @@ -30,7 +31,7 @@ func createMinSeverity(t *testing.T) {

policyRule := utils.PolicyRule{
Name: "min-severity" + getRunId(),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Low),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Low, false),
Priority: 1,
}
createAndCheckPolicy(t, policyName, true, utils.Security, policyRule)
Expand Down Expand Up @@ -78,12 +79,12 @@ func create2Priorities(t *testing.T) {

policyRule1 := utils.PolicyRule{
Name: "priority-1" + getRunId(),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Low),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Low, false),
Priority: 1,
}
policyRule2 := utils.PolicyRule{
Name: "priority-2" + getRunId(),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Medium),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Medium, false),
Priority: 2,
}
createAndCheckPolicy(t, policyName, true, utils.Security, policyRule1, policyRule2)
Expand All @@ -95,7 +96,7 @@ func createPolicyActions(t *testing.T) {

policyRule := utils.PolicyRule{
Name: "policy-actions" + getRunId(),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.High),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.High, false),
Priority: 1,
Actions: &utils.PolicyAction{
BlockDownload: utils.PolicyBlockDownload{
Expand All @@ -118,20 +119,32 @@ func createUpdatePolicy(t *testing.T) {

policyRule := utils.PolicyRule{
Name: "low-severity" + getRunId(),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Low),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Low, false),
Priority: 1,
}
createAndCheckPolicy(t, policyName, true, utils.Security, policyRule)

policyRule = utils.PolicyRule{
Name: "medium-severity" + getRunId(),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Medium),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Medium, false),
Priority: 1,
}

createAndCheckPolicy(t, policyName, false, utils.Security, policyRule)
}

func createSkipNonApplicable(t *testing.T) {
policyName := "skip-non-applicable" + getRunId()
defer deletePolicy(t, policyName)

policyRule := utils.PolicyRule{
Name: "skip-non-applicable-rule" + getRunId(),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Low, true),
Priority: 1,
}
createAndCheckPolicy(t, policyName, true, utils.Security, policyRule)
}

func createPolicy(t *testing.T, policyName string, policyType utils.PolicyType, policyRules ...utils.PolicyRule) *utils.PolicyParams {
policyParams := utils.PolicyParams{
Name: policyName,
Expand Down
2 changes: 1 addition & 1 deletion tests/xraywatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func createDummyPolicy(policyName string) error {
Type: utils.Security,
Rules: []utils.PolicyRule{{
Name: "sec_rule",
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Medium),
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Medium, false),
Actions: &utils.PolicyAction{
Webhooks: []string{},
BlockDownload: utils.PolicyBlockDownload{
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
const (
Development = "development"
Agent = "jfrog-client-go"
Version = "1.48.1"
Version = "1.48.4"
)

const xrayDevVersion = "3.x-dev"
Expand Down
Loading

0 comments on commit e9a1a39

Please sign in to comment.