Skip to content

Commit

Permalink
chore: ban http.Transport & http.Client; add cleanhttp (#8991)
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinaecalderon authored Mar 13, 2024
1 parent 52572d4 commit 4730d76
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 2 additions & 0 deletions master/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ linters-settings:
- 'fmt\.Print.*'
- 'metaV1.NamespaceAll' # Will error if someone has namespace restricted permissions.
- 'bundebug.WithVerbose'
- 'http.Client' # Use cleanhttp instead.
- 'http.Transport' # Use cleanhttp instead.
staticcheck:
go: "1.20"

Expand Down
9 changes: 5 additions & 4 deletions master/internal/elastic/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"time"

"github.com/hashicorp/go-cleanhttp"
log "github.com/sirupsen/logrus"

"github.com/elastic/go-elasticsearch/v7"
Expand Down Expand Up @@ -37,11 +37,12 @@ func Setup(conf model.ElasticLoggingConfig) (*Elastic, error) {
addr := fmt.Sprintf("%s%s:%d", scheme, conf.Host, conf.Port)
log.Infof("connecting to elasticsearch %s", addr)

transport := cleanhttp.DefaultTransport()
transport.TLSClientConfig = tlsCfg

cfg := elasticsearch.Config{
Addresses: []string{addr},
Transport: &http.Transport{
TLSClientConfig: tlsCfg,
},
Transport: transport,
}

if conf.Security.Username != nil && conf.Security.Password != nil {
Expand Down
8 changes: 4 additions & 4 deletions master/internal/rm/kubernetesrm/mock_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

"github.com/hashicorp/go-cleanhttp"
"github.com/pkg/errors"

k8sV1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -222,10 +223,9 @@ func (m *mockPodInterface) Evict(ctx context.Context, eviction *v1beta1.Eviction
}

func (m *mockPodInterface) GetLogs(name string, opts *k8sV1.PodLogOptions) *rest.Request {
return rest.NewRequestWithClient(&url.URL{}, "", rest.ClientContentConfig{},
&http.Client{
Transport: &mockRoundTripInterface{message: m.logMessage},
})
client := cleanhttp.DefaultClient()
client.Transport = &mockRoundTripInterface{message: m.logMessage}
return rest.NewRequestWithClient(&url.URL{}, "", rest.ClientContentConfig{}, client)
}

func (m *mockPodInterface) ProxyGet(
Expand Down
7 changes: 3 additions & 4 deletions master/internal/webhooks/api_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import (
"net/url"
"time"

log "github.com/sirupsen/logrus"

"github.com/google/uuid"
"github.com/hashicorp/go-cleanhttp"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/determined-ai/determined/master/internal/grpcutil"
"github.com/determined-ai/determined/master/pkg/ptrs"

"github.com/determined-ai/determined/proto/pkg/apiv1"
"github.com/determined-ai/determined/proto/pkg/webhookv1"
)
Expand Down Expand Up @@ -189,7 +188,7 @@ func (a *WebhooksAPIServer) TestWebhook(
}

log.Infof("creating webhook request for event %v", eventID)
c := http.Client{}
c := cleanhttp.DefaultClient()
resp, err := c.Do(tReq)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument,
Expand Down
2 changes: 1 addition & 1 deletion master/internal/webhooks/shipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func newWorker(id int) *worker {
type worker struct {
// System dependencies.
log *log.Entry
cl *http.Client
cl *http.Client //nolint:forbidigo
}

func (w *worker) work(ctx context.Context, wake <-chan struct{}) {
Expand Down

0 comments on commit 4730d76

Please sign in to comment.