From 637ea1454143079910e9c1a0104769bef472b84f Mon Sep 17 00:00:00 2001 From: Andreas Hippler Date: Tue, 24 Sep 2024 00:23:57 +0200 Subject: [PATCH] fix: pass http.Client to all managers --- access/manager.go | 1 + artifactory/manager.go | 9 ++++---- distribution/manager.go | 1 + evidence/manager.go | 1 + lifecycle/manager.go | 1 + metadata/manager.go | 2 ++ pipelines/manager.go | 1 + tests/manager_test.go | 51 +++++++++++++++++++++++++++++++++++++++++ xray/manager.go | 7 +++--- xsc/manager.go | 7 +++--- 10 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 tests/manager_test.go diff --git a/access/manager.go b/access/manager.go index 3435973e7..9d38016cb 100644 --- a/access/manager.go +++ b/access/manager.go @@ -27,6 +27,7 @@ func New(config config.Config) (*AccessServicesManager, error) { SetOverallRequestTimeout(config.GetOverallRequestTimeout()). SetRetries(config.GetHttpRetries()). SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). + SetHttpClient(config.GetHttpClient()). Build() return manager, err diff --git a/artifactory/manager.go b/artifactory/manager.go index 3b1172b90..a43257744 100644 --- a/artifactory/manager.go +++ b/artifactory/manager.go @@ -604,17 +604,16 @@ func (sm *ArtifactoryServicesManagerImp) ImportReleaseBundle(filePath string) er return releaseService.ImportReleaseBundle(filePath) } -func buildJFrogHttpClient(config config.Config, authDetails auth.ServiceDetails) (*jfroghttpclient.JfrogHttpClient, error) { +func buildJFrogHttpClient(config config.Config, details auth.ServiceDetails) (*jfroghttpclient.JfrogHttpClient, error) { return 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()). - SetClientCertPath(authDetails.GetClientCertPath()). - SetClientCertKeyPath(authDetails.GetClientCertKeyPath()). - AppendPreRequestInterceptor(authDetails.RunPreRequestFunctions). - SetContext(config.GetContext()). SetRetries(config.GetHttpRetries()). SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). SetHttpClient(config.GetHttpClient()). diff --git a/distribution/manager.go b/distribution/manager.go index 257d407c7..b2565b2f9 100644 --- a/distribution/manager.go +++ b/distribution/manager.go @@ -29,6 +29,7 @@ func New(config config.Config) (*DistributionServicesManager, error) { SetContext(config.GetContext()). SetRetries(config.GetHttpRetries()). SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). + SetHttpClient(config.GetHttpClient()). Build() return manager, err } diff --git a/evidence/manager.go b/evidence/manager.go index e4a8ce45c..c0f87cebe 100644 --- a/evidence/manager.go +++ b/evidence/manager.go @@ -26,6 +26,7 @@ func New(config config.Config) (*EvidenceServicesManager, error) { SetOverallRequestTimeout(config.GetOverallRequestTimeout()). SetRetries(config.GetHttpRetries()). SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). + SetHttpClient(config.GetHttpClient()). Build() return manager, err diff --git a/lifecycle/manager.go b/lifecycle/manager.go index 72d76dd74..dcb176cbe 100644 --- a/lifecycle/manager.go +++ b/lifecycle/manager.go @@ -28,6 +28,7 @@ func New(config config.Config) (*LifecycleServicesManager, error) { SetOverallRequestTimeout(config.GetOverallRequestTimeout()). SetRetries(config.GetHttpRetries()). SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). + SetHttpClient(config.GetHttpClient()). Build() return manager, err diff --git a/metadata/manager.go b/metadata/manager.go index 9b6e3b071..08ece8f46 100644 --- a/metadata/manager.go +++ b/metadata/manager.go @@ -8,6 +8,7 @@ import ( type Manager interface { GraphqlQuery(query []byte) ([]byte, error) + Client() *jfroghttpclient.JfrogHttpClient } type metadataManager struct { @@ -30,6 +31,7 @@ func NewManager(config config.Config) (Manager, error) { SetOverallRequestTimeout(config.GetOverallRequestTimeout()). SetRetries(config.GetHttpRetries()). SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). + SetHttpClient(config.GetHttpClient()). Build() return manager, err diff --git a/pipelines/manager.go b/pipelines/manager.go index bda816e76..2b4c0eb9a 100644 --- a/pipelines/manager.go +++ b/pipelines/manager.go @@ -26,6 +26,7 @@ func New(config config.Config) (*PipelinesServicesManager, error) { SetOverallRequestTimeout(config.GetOverallRequestTimeout()). SetRetries(config.GetHttpRetries()). SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). + SetHttpClient(config.GetHttpClient()). Build() return manager, err } diff --git a/tests/manager_test.go b/tests/manager_test.go new file mode 100644 index 000000000..7f6558700 --- /dev/null +++ b/tests/manager_test.go @@ -0,0 +1,51 @@ +package tests + +import ( + "net/http" + "testing" + + "github.com/jfrog/jfrog-client-go/access" + "github.com/jfrog/jfrog-client-go/config" + "github.com/jfrog/jfrog-client-go/distribution" + "github.com/jfrog/jfrog-client-go/evidence" + "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" + "github.com/jfrog/jfrog-client-go/lifecycle" + "github.com/jfrog/jfrog-client-go/metadata" + "github.com/jfrog/jfrog-client-go/pipelines" + "github.com/jfrog/jfrog-client-go/xray" + "github.com/jfrog/jfrog-client-go/xsc" + "github.com/stretchr/testify/assert" +) + +func TestUsesCustomHttpClient(t *testing.T) { + t.Run("artifactory", func(t *testing.T) { usesCustomHttpClient(t, access.New) }) + t.Run("access", func(t *testing.T) { usesCustomHttpClient(t, access.New) }) + t.Run("distribution", func(t *testing.T) { usesCustomHttpClient(t, distribution.New) }) + t.Run("evidence", func(t *testing.T) { usesCustomHttpClient(t, evidence.New) }) + t.Run("lifecycle", func(t *testing.T) { usesCustomHttpClient(t, lifecycle.New) }) + t.Run("metadata", func(t *testing.T) { usesCustomHttpClient(t, metadata.NewManager) }) + t.Run("pipelines", func(t *testing.T) { usesCustomHttpClient(t, pipelines.New) }) + t.Run("xray", func(t *testing.T) { usesCustomHttpClient(t, xray.New) }) + t.Run("xsc", func(t *testing.T) { usesCustomHttpClient(t, xsc.New) }) +} + +type Manager interface { + Client() *jfroghttpclient.JfrogHttpClient +} + +func usesCustomHttpClient[K Manager](t *testing.T, createManager func(config config.Config) (K, error)) { + client := http.DefaultClient + config, err := config.NewConfigBuilder(). + SetServiceDetails(GetRtDetails()). + SetHttpClient(client). + Build() + if err != nil { + t.Error(err) + } + m, err := createManager(config) + if err != nil { + t.Error(err) + } + actualClient := m.Client().GetHttpClient().GetClient() + assert.Equal(t, client, actualClient, "Expected the client to be the same") +} diff --git a/xray/manager.go b/xray/manager.go index 9667e681a..aee1a7b3f 100644 --- a/xray/manager.go +++ b/xray/manager.go @@ -21,14 +21,15 @@ func New(config config.Config) (*XrayServicesManager, error) { manager.client, err = jfroghttpclient.JfrogClientBuilder(). SetCertificatesPath(config.GetCertificatesPath()). SetInsecureTls(config.IsInsecureTls()). - SetContext(config.GetContext()). - SetDialTimeout(config.GetDialTimeout()). - SetOverallRequestTimeout(config.GetOverallRequestTimeout()). SetClientCertPath(details.GetClientCertPath()). SetClientCertKeyPath(details.GetClientCertKeyPath()). AppendPreRequestInterceptor(details.RunPreRequestFunctions). + SetContext(config.GetContext()). + SetDialTimeout(config.GetDialTimeout()). + SetOverallRequestTimeout(config.GetOverallRequestTimeout()). SetRetries(config.GetHttpRetries()). SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). + SetHttpClient(config.GetHttpClient()). Build() return manager, err } diff --git a/xsc/manager.go b/xsc/manager.go index ff07b47b5..dcf006c37 100644 --- a/xsc/manager.go +++ b/xsc/manager.go @@ -20,14 +20,15 @@ func New(config config.Config) (*XscServicesManager, error) { manager.client, err = jfroghttpclient.JfrogClientBuilder(). SetCertificatesPath(config.GetCertificatesPath()). SetInsecureTls(config.IsInsecureTls()). - SetContext(config.GetContext()). - SetDialTimeout(config.GetDialTimeout()). - SetOverallRequestTimeout(config.GetOverallRequestTimeout()). SetClientCertPath(details.GetClientCertPath()). SetClientCertKeyPath(details.GetClientCertKeyPath()). AppendPreRequestInterceptor(details.RunPreRequestFunctions). + SetContext(config.GetContext()). + SetDialTimeout(config.GetDialTimeout()). + SetOverallRequestTimeout(config.GetOverallRequestTimeout()). SetRetries(config.GetHttpRetries()). SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). + SetHttpClient(config.GetHttpClient()). Build() return manager, err }