Skip to content

Commit 30d8ce6

Browse files
fix(appset): prevent idle connection buildup by cloning http.DefaultTransport in Bitbucket SCM/PR generator (#24264)
Signed-off-by: portly-halicore-76 <170707699+portly-halicore-76@users.noreply.github.com> Signed-off-by: anandf <anjoseph@redhat.com> Co-authored-by: portly-halicore-76 <170707699+portly-halicore-76@users.noreply.github.com>
1 parent fa342d1 commit 30d8ce6

4 files changed

Lines changed: 64 additions & 17 deletions

File tree

applicationset/services/pull_request/bitbucket_server.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package pull_request
33
import (
44
"context"
55
"fmt"
6-
"net/http"
76

87
bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
98
log "github.com/sirupsen/logrus"
109

11-
"github.com/argoproj/argo-cd/v3/applicationset/utils"
10+
"github.com/argoproj/argo-cd/v3/applicationset/services"
1211
)
1312

1413
type BitbucketService struct {
@@ -49,15 +48,10 @@ func NewBitbucketServiceNoAuth(ctx context.Context, url, projectKey, repositoryS
4948
}
5049

5150
func newBitbucketService(ctx context.Context, bitbucketConfig *bitbucketv1.Configuration, projectKey, repositorySlug string, scmRootCAPath string, insecure bool, caCerts []byte) (PullRequestService, error) {
52-
bitbucketConfig.BasePath = utils.NormalizeBitbucketBasePath(bitbucketConfig.BasePath)
53-
tlsConfig := utils.GetTlsConfig(scmRootCAPath, insecure, caCerts)
54-
bitbucketConfig.HTTPClient = &http.Client{Transport: &http.Transport{
55-
TLSClientConfig: tlsConfig,
56-
}}
57-
bitbucketClient := bitbucketv1.NewAPIClient(ctx, bitbucketConfig)
51+
bbClient := services.SetupBitbucketClient(ctx, bitbucketConfig, scmRootCAPath, insecure, caCerts)
5852

5953
return &BitbucketService{
60-
client: bitbucketClient,
54+
client: bbClient,
6155
projectKey: projectKey,
6256
repositorySlug: repositorySlug,
6357
}, nil

applicationset/services/scm_provider/bitbucket_server.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
1111
log "github.com/sirupsen/logrus"
1212

13-
"github.com/argoproj/argo-cd/v3/applicationset/utils"
13+
"github.com/argoproj/argo-cd/v3/applicationset/services"
1414
)
1515

1616
type BitbucketServerProvider struct {
@@ -49,15 +49,10 @@ func NewBitbucketServerProviderNoAuth(ctx context.Context, url, projectKey strin
4949
}
5050

5151
func newBitbucketServerProvider(ctx context.Context, bitbucketConfig *bitbucketv1.Configuration, projectKey string, allBranches bool, scmRootCAPath string, insecure bool, caCerts []byte) (*BitbucketServerProvider, error) {
52-
bitbucketConfig.BasePath = utils.NormalizeBitbucketBasePath(bitbucketConfig.BasePath)
53-
tlsConfig := utils.GetTlsConfig(scmRootCAPath, insecure, caCerts)
54-
bitbucketConfig.HTTPClient = &http.Client{Transport: &http.Transport{
55-
TLSClientConfig: tlsConfig,
56-
}}
57-
bitbucketClient := bitbucketv1.NewAPIClient(ctx, bitbucketConfig)
52+
bbClient := services.SetupBitbucketClient(ctx, bitbucketConfig, scmRootCAPath, insecure, caCerts)
5853

5954
return &BitbucketServerProvider{
60-
client: bitbucketClient,
55+
client: bbClient,
6156
projectKey: projectKey,
6257
allBranches: allBranches,
6358
}, nil

applicationset/services/util.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package services
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
8+
9+
"github.com/argoproj/argo-cd/v3/applicationset/utils"
10+
)
11+
12+
// SetupBitbucketClient configures and creates a Bitbucket API client with TLS settings
13+
func SetupBitbucketClient(ctx context.Context, config *bitbucketv1.Configuration, scmRootCAPath string, insecure bool, caCerts []byte) *bitbucketv1.APIClient {
14+
config.BasePath = utils.NormalizeBitbucketBasePath(config.BasePath)
15+
tlsConfig := utils.GetTlsConfig(scmRootCAPath, insecure, caCerts)
16+
17+
transport := http.DefaultTransport.(*http.Transport).Clone()
18+
transport.TLSClientConfig = tlsConfig
19+
config.HTTPClient = &http.Client{Transport: transport}
20+
21+
return bitbucketv1.NewAPIClient(ctx, config)
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package services
2+
3+
import (
4+
"crypto/tls"
5+
"net/http"
6+
"testing"
7+
"time"
8+
9+
bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestSetupBitbucketClient(t *testing.T) {
14+
ctx := t.Context()
15+
cfg := &bitbucketv1.Configuration{}
16+
17+
// Act
18+
client := SetupBitbucketClient(ctx, cfg, "", false, nil)
19+
20+
// Assert
21+
require.NotNil(t, client, "expected client to be created")
22+
require.NotNil(t, cfg.HTTPClient, "expected HTTPClient to be set")
23+
24+
// The transport should be a clone of DefaultTransport
25+
tr, ok := cfg.HTTPClient.Transport.(*http.Transport)
26+
require.True(t, ok, "expected HTTPClient.Transport to be *http.Transport")
27+
require.NotSame(t, http.DefaultTransport, tr, "transport should be a clone, not the global DefaultTransport")
28+
29+
// Ensure TLSClientConfig is set
30+
require.IsType(t, &tls.Config{}, tr.TLSClientConfig)
31+
32+
// Defaults from http.DefaultTransport.Clone() should be preserved
33+
require.Greater(t, tr.IdleConnTimeout, time.Duration(0), "IdleConnTimeout should be non-zero")
34+
require.Positive(t, tr.MaxIdleConns, "MaxIdleConns should be non-zero")
35+
require.Greater(t, tr.TLSHandshakeTimeout, time.Duration(0), "TLSHandshakeTimeout should be non-zero")
36+
}

0 commit comments

Comments
 (0)