Skip to content

Commit

Permalink
util: remove unused cidr method
Browse files Browse the repository at this point in the history
We don't use the WrapTLS method and it is better to remove it and if we
need it in the future bring it back.

Epic: none

Release note: None
  • Loading branch information
andrewbaptist committed Sep 23, 2024
1 parent 9dbbdcc commit 611bce8
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 68 deletions.
37 changes: 0 additions & 37 deletions pkg/util/cidr/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package cidr

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
io "io"
Expand Down Expand Up @@ -407,42 +406,6 @@ func (m *NetMetrics) Wrap(dial DialContext, labels ...string) DialContext {
}
}

// WrapTLS is like Wrap, but can be used if the underlying library doesn't
// expose a way to plug in a dialer for TLS connections. This is unfortunately
// pretty ugly... Copied from tls.Dial and kgo.DialTLS because they don't expose
// a dial call with a DialContext. Ideally you don't have to use this if the
// third party API does a sensible thing and exposes the ability to replace the
// "DialContext" directly.
func (m *NetMetrics) WrapTLS(dial DialContext, tlsCfg *tls.Config, labels ...string) DialContext {
return func(ctx context.Context, network, host string) (net.Conn, error) {
c := tlsCfg.Clone()
if c.ServerName == "" {
server, _, err := net.SplitHostPort(host)
if err != nil {
return nil, fmt.Errorf("unable to split host:port for dialing: %w", err)
}
c.ServerName = server
}

rawConn, err := dial(ctx, network, host)
if err != nil {
return nil, err
}
scopedConn := rawConn
// m can be nil in tests.
if m != nil {
scopedConn = m.track(rawConn, labels...)
}

conn := tls.Client(scopedConn, c)
if err := conn.HandshakeContext(ctx); err != nil {
scopedConn.Close()
return nil, err
}
return conn, nil
}
}

type Dialer interface {
Dial(network, addr string) (c net.Conn, err error)
DialContext(ctx context.Context, network, addr string) (c net.Conn, err error)
Expand Down
31 changes: 0 additions & 31 deletions pkg/util/cidr/cidr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package cidr

import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
Expand Down Expand Up @@ -220,36 +219,6 @@ func TestWrapHTTP(t *testing.T) {
require.Greater(t, m.mu.childMetrics["foo/test"].ReadBytes.Value(), int64(1))
}

// TestWrapHTTP validates the wrapping function for HTTP connections.
func TestWrapHTTPS(t *testing.T) {
s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer s.Close()
// Create a mapping for this server's IP.
mapping := fmt.Sprintf(`[ { "Name": "test", "Ipnet": "%s/32" } ]`, s.Listener.Addr().(*net.TCPAddr).IP.String())
c := Lookup{}
require.NoError(t, c.setDestinations(context.Background(), []byte(mapping)))

// This is the standard way to wrap the transport.
m := c.MakeNetMetrics(writeBytes, readBytes, "label")
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DialTLSContext = m.WrapTLS(transport.DialContext, &tls.Config{InsecureSkipVerify: true}, "foo")

// Create a simple get request.
client := &http.Client{Transport: transport}
_, err := client.Get(s.URL)
require.NoError(t, err)

// Ideally we could check the actual value, but the header includes the date
// and could be flaky.
require.Greater(t, m.WriteBytes.Count(), int64(1))
require.Greater(t, m.ReadBytes.Count(), int64(1))
// Also check the child metrics by looking up in the map directly.
m.mu.Lock()
defer m.mu.Unlock()
require.Greater(t, m.mu.childMetrics["foo/test"].WriteBytes.Value(), int64(1))
require.Greater(t, m.mu.childMetrics["foo/test"].ReadBytes.Value(), int64(1))
}

func TestWrapDialer(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer s.Close()
Expand Down

0 comments on commit 611bce8

Please sign in to comment.