diff --git a/artifactory/usage/reportusage.go b/artifactory/usage/call_home.go similarity index 72% rename from artifactory/usage/reportusage.go rename to artifactory/usage/call_home.go index bda41b225..cdcb76eae 100644 --- a/artifactory/usage/reportusage.go +++ b/artifactory/usage/call_home.go @@ -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.") @@ -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 { @@ -63,8 +69,8 @@ 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 } @@ -72,11 +78,11 @@ func ReportUsageToArtifactory(productId string, serviceManager artifactory.Artif 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 } @@ -84,7 +90,7 @@ func SendReportUsage(productId, commandName string, serviceManager artifactory.A 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) { diff --git a/artifactory/usage/reportusage_test.go b/artifactory/usage/call_home_test.go similarity index 100% rename from artifactory/usage/reportusage_test.go rename to artifactory/usage/call_home_test.go diff --git a/go.mod b/go.mod index 8385a0305..25d108b10 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 87554483e..d0fc3425f 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -144,19 +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= diff --git a/jfconnect/auth/jfconnectdetails.go b/jfconnect/auth/jfconnectdetails.go new file mode 100644 index 000000000..734eb37e0 --- /dev/null +++ b/jfconnect/auth/jfconnectdetails.go @@ -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") +} diff --git a/jfconnect/manager.go b/jfconnect/manager.go new file mode 100644 index 000000000..08fffb052 --- /dev/null +++ b/jfconnect/manager.go @@ -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) +} diff --git a/jfconnect/services/metrics.go b/jfconnect/services/metrics.go new file mode 100644 index 000000000..cb3fa9ecb --- /dev/null +++ b/jfconnect/services/metrics.go @@ -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) +} diff --git a/utils/utils.go b/utils/utils.go index 17bec088e..b9c13f3c6 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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"