Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalb4doc committed Dec 21, 2024
2 parents eab61eb + e5ef994 commit 4658ed5
Show file tree
Hide file tree
Showing 17 changed files with 269 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ type ReportUsageAttribute struct {
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) validateAndGetUsageServerInfo(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 Down Expand Up @@ -50,7 +56,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 +69,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) SendUsageToArtifactory(productId string, serviceManager artifactory.ArtifactoryServicesManager, features ...Feature) error {
url, clientDetails, err := ach.validateAndGetUsageServerInfo(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) SendUsage(productId, commandName string, serviceManager artifactory.ArtifactoryServicesManager, attributes ...ReportUsageAttribute) error {
url, clientDetails, err := ach.validateAndGetUsageServerInfo(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.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ 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
github.com/xanzy/ssh-agent v0.3.3
golang.org/x/crypto v0.29.0
golang.org/x/crypto v0.31.0
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
golang.org/x/term v0.26.0
golang.org/x/term v0.27.0
)

require (
Expand Down Expand Up @@ -52,7 +52,7 @@ require (
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/tools v0.27.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
20 changes: 10 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,8 @@ 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=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
Expand All @@ -142,14 +142,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
4 changes: 4 additions & 0 deletions http/httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func (jc *HttpClient) Send(method, url string, content []byte, followRedirect, c
}
resp, respBody, redirectUrl, err = jc.doRequest(req, content, followRedirect, closeBody, httpClientsDetails)
if err != nil {
if strings.Contains(err.Error(), "unsupported protocol scheme") {
// Wrong URL, so no need to retry
return false, fmt.Errorf("%w\nThe recieved error indicats an invalid URL: %q, Please ensure the URL includes a valid scheme like 'http://' or 'https://'.", err, url)
}
return true, err
}
// Response must not be nil
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 {
PostMetric([]byte) 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) PostMetric(metric []byte) error {
jfConnectService := services.NewJfConnectService(jm.config.GetServiceDetails(), jm.client)
return jfConnectService.PostMetric(metric)
}
39 changes: 39 additions & 0 deletions jfconnect/services/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package services

import (
"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 = "jfconnect/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) PostMetric(metric []byte) error {
details := jcs.GetJfConnectDetails()
httpClientDetails := details.CreateHttpClientDetails()
httpClientDetails.SetContentTypeApplicationJson()

url := clientutils.AddTrailingSlashIfNeeded(details.GetUrl())
url += LogMetricApiEndpoint
resp, body, err := jcs.client.SendPost(url, metric, &httpClientDetails)
if err != nil {
return err
}
return errorutils.CheckResponseStatusWithBody(resp, body, http.StatusCreated, http.StatusOK)
}
37 changes: 27 additions & 10 deletions tests/xsc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestXscVersion(t *testing.T) {
initXscTest(t, "")
initXscTest(t, "", "")
version, err := GetXscDetails().GetVersion()
if err != nil {
t.Error(err)
Expand All @@ -16,21 +16,38 @@ func TestXscVersion(t *testing.T) {
}
}

func initXscTest(t *testing.T, minVersion string) {
func initXscTest(t *testing.T, minXscVersion string, minXrayVersion string) {
if !*TestXsc {
t.Skip("Skipping xsc test. To run xsc test add the '-test.xsc=true' option.")
}
validateXscVersion(t, minVersion)
validateXscAndXrayVersion(t, minXscVersion, minXrayVersion)
}
func validateXscVersion(t *testing.T, minVersion string) {
// Validate active XSC server.
version, err := GetXscDetails().GetVersion()

// This func validates minimal Xsc version.
// Since Xsc was migrated into Xray from version 3.107.13, we need to check minimal Xray version from this version forward instead of Xsc version.
// For features that are available before the migration we pass minXscVersion to check. If the utilized Xray version >= 3.107.13, the returned Xsc version will always suffice the check.
// For features that were introduced only after the migration we pass only minXrayVersion to check and can leave minXscVersion blank.
// In general minXscVersion should be provided only for features that were introduced before Xsc migration to Xray
func validateXscAndXrayVersion(t *testing.T, minXscVersion string, minXrayVersion string) {
// Validate active Xsc server
currentXscVersion, err := GetXscDetails().GetVersion()
if err != nil {
t.Skip(err)
}
// Validate minimum XSC version.
err = clientUtils.ValidateMinimumVersion(clientUtils.Xsc, version, minVersion)
if err != nil {
t.Skip(err)

if minXscVersion != "" {
if err = clientUtils.ValidateMinimumVersion(clientUtils.Xsc, currentXscVersion, minXscVersion); err != nil {
t.Skip(err)
}
}

if minXrayVersion != "" {
var currentXrayVersion string
if currentXrayVersion, err = GetXrayDetails().GetVersion(); err != nil {
t.Skip(err)
}
if err = clientUtils.ValidateMinimumVersion(clientUtils.Xsc, currentXrayVersion, minXrayVersion); err != nil {
t.Skip(err)
}
}
}
2 changes: 1 addition & 1 deletion tests/xscanalyticsevent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func isValidUUID(str string) bool {

func initXscEventTest(t *testing.T) (xscDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) {
var err error
initXscTest(t, services.AnalyticsMetricsMinXscVersion)
initXscTest(t, services.AnalyticsMetricsMinXscVersion, "")
xscDetails = GetXscDetails()
client, err = jfroghttpclient.JfrogClientBuilder().
SetClientCertPath(xscDetails.GetClientCertPath()).
Expand Down
Loading

0 comments on commit 4658ed5

Please sign in to comment.