Skip to content

Commit 76b2dd3

Browse files
committed
Fix merge conflict
Signed-off-by: Doğukan Teber <[email protected]>
2 parents cacb1d3 + d2ef5ae commit 76b2dd3

15 files changed

+156
-111
lines changed

gateway/gateway_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"net/http"
77
"net/http/httptest"
8-
"os"
98
"strings"
109
"testing"
1110
"time"
@@ -48,8 +47,6 @@ func TestNewGateway(t *testing.T) {
4847
}
4948

5049
func TestStartGateway(t *testing.T) {
51-
InitLogger(os.Stdout)
52-
5350
distributorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
5451
frontendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
5552
alertmanagerServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))

gateway/load_balancer.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package gateway
22

33
import (
44
"fmt"
5-
"log"
65
"net"
76
"net/http"
87
"strings"
98
"sync"
109
"time"
10+
11+
"github.com/sirupsen/logrus"
1112
)
1213

1314
type DNSResolver interface {
@@ -29,7 +30,7 @@ type roundRobinLoadBalancer struct {
2930
sync.RWMutex
3031
}
3132

32-
func newRoundRobinLoadBalancer(hostname string, resolver func(hostname string) ([]net.IP, error)) *roundRobinLoadBalancer {
33+
func newRoundRobinLoadBalancer(hostname string, resolver func(hostname string) ([]net.IP, error)) (*roundRobinLoadBalancer, error) {
3334
lb := &roundRobinLoadBalancer{
3435
hostname: hostname,
3536
transport: http.DefaultTransport,
@@ -39,21 +40,23 @@ func newRoundRobinLoadBalancer(hostname string, resolver func(hostname string) (
3940
// Resolve IPs initially
4041
ips, err := lb.resolveIPs(hostname)
4142
if err != nil {
42-
log.Printf("Failed to resolve IPs for hostname %s: %v", lb.hostname, err)
43+
logrus.Errorf("failed to resolve IPs for hostname %s: %v", lb.hostname, err)
44+
return nil, err
4345
} else {
4446
lb.ips = ipsToStrings(ips)
4547
}
4648

47-
return lb
49+
return lb, nil
4850
}
4951

5052
func (lb *roundRobinLoadBalancer) roundTrip(req *http.Request) (*http.Response, error) {
5153
lb.Lock()
5254
defer lb.Unlock()
5355

5456
if len(lb.ips) == 0 {
55-
// TODO: replace format error with a log statement
56-
return nil, fmt.Errorf("no IP addresses available")
57+
errMsg := fmt.Sprintln("no IP addresses available")
58+
logrus.Errorf(errMsg)
59+
return nil, fmt.Errorf(errMsg)
5760
}
5861

5962
ip := lb.getNextIP()
@@ -79,8 +82,7 @@ func (lb *roundRobinLoadBalancer) refreshIPs(refreshInterval time.Duration) {
7982
for {
8083
ips, err := lb.resolveIPs(lb.hostname)
8184
if err != nil {
82-
// TODO: replace std library log package with logrus
83-
log.Printf("Failed to resolve IPs for hostname %s: %v", lb.hostname, err)
85+
logrus.Errorf("failed to resolve IPs for hostname %s: %v", lb.hostname, err)
8486
} else {
8587
lb.Lock()
8688
lb.ips = ipsToStrings(ips)

gateway/load_balancer_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ func TestDistribution(t *testing.T) {
113113
Err: nil,
114114
}
115115

116-
lb := newRoundRobinLoadBalancer(hostname, mockResolver.LookupIP)
116+
lb, err := newRoundRobinLoadBalancer(hostname, mockResolver.LookupIP)
117+
if err != nil {
118+
t.Fatal(err)
119+
}
117120
lb.transport = &customRoundTripper{}
118121

119122
go lb.refreshIPs(tc.refreshInterval)

gateway/middleware.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"net/http"
55

66
"github.com/cortexproject/auth-gateway/middleware"
7-
"github.com/go-kit/log/level"
7+
"github.com/sirupsen/logrus"
88
)
99

1010
type Authentication struct {
@@ -36,7 +36,7 @@ func (a Authentication) Wrap(next http.Handler) http.Handler {
3636
if ok {
3737
next.ServeHTTP(sr, r)
3838
} else {
39-
level.Debug(logger).Log("msg", "No valid tenant credentials are found")
39+
logrus.Debugf("No valid tenant credentials are found")
4040
sr.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
4141
http.Error(sr, "Unauthorized", http.StatusUnauthorized)
4242
}

gateway/middleware_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import (
44
"encoding/base64"
55
"net/http"
66
"net/http/httptest"
7-
"os"
87
"testing"
98
)
109

1110
func TestAuthenticate(t *testing.T) {
12-
InitLogger(os.Stdout)
1311
testCases := []struct {
1412
name string
1513
config *Config

gateway/proxy.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package gateway
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"net"
78
"net/http"
89
"net/http/httputil"
910
"net/url"
1011
"time"
12+
13+
"github.com/cortexproject/auth-gateway/utils"
1114
)
1215

1316
const (
@@ -56,13 +59,18 @@ func NewProxy(targetURL string, upstream Upstream, component string) (*Proxy, er
5659
return nil, err
5760
}
5861
if url.Scheme == "" {
59-
return nil, fmt.Errorf("invalid URL scheme: %s", targetURL)
62+
return nil, fmt.Errorf("invalid URL scheme when creating a proxy for the %s: %s", component, targetURL)
6063
}
6164

6265
reverseProxy := httputil.NewSingleHostReverseProxy(url)
63-
reverseProxy.Transport = customTransport(component, upstream)
66+
transport, err := customTransport(component, upstream)
67+
if err != nil {
68+
return nil, err
69+
}
70+
reverseProxy.Transport = transport
6471
originalDirector := reverseProxy.Director
6572
reverseProxy.Director = customDirector(url, originalDirector)
73+
reverseProxy.ErrorLog = log.New(utils.LogrusErrorWriter{}, "", 0)
6674

6775
if upstream.HTTPClientTimeout == 0 {
6876
upstream.HTTPClientTimeout = defaultTimeoutValues[component].HTTPClientTimeout
@@ -82,7 +90,7 @@ func customDirector(targetURL *url.URL, originalDirector func(*http.Request)) fu
8290
}
8391
}
8492

85-
func customTransport(component string, upstream Upstream) http.RoundTripper {
93+
func customTransport(component string, upstream Upstream) (http.RoundTripper, error) {
8694
dialerTimeout := upstream.HTTPClientDialerTimeout * time.Second
8795
if dialerTimeout == 0 {
8896
dialerTimeout = defaultTimeoutValues[component].HTTPClientDialerTimeout
@@ -102,14 +110,17 @@ func customTransport(component string, upstream Upstream) http.RoundTripper {
102110

103111
url, err := url.Parse(upstream.URL)
104112
if err != nil {
105-
// TODO: log the error with logrus
106-
fmt.Println(err)
113+
return nil, fmt.Errorf("unexpected error when parsing the upstream url: %v", err)
107114
}
108115

109116
resolver := DefaultDNSResolver{}
117+
lb, err := newRoundRobinLoadBalancer(url.Hostname(), resolver.LookupIP)
118+
if err != nil {
119+
return nil, fmt.Errorf("unexpected error when creating the load balancer: %v", err)
120+
}
110121
t := &CustomTransport{
111122
Transport: *http.DefaultTransport.(*http.Transport).Clone(),
112-
lb: newRoundRobinLoadBalancer(url.Hostname(), resolver.LookupIP),
123+
lb: lb,
113124
}
114125
go t.lb.refreshIPs(upstream.DNSRefreshInterval)
115126

@@ -120,7 +131,7 @@ func customTransport(component string, upstream Upstream) http.RoundTripper {
120131
t.TLSHandshakeTimeout = TLSHandshakeTimeout
121132
t.ResponseHeaderTimeout = responseHeaderTimeout
122133

123-
return t
134+
return t, nil
124135
}
125136

126137
func (p *Proxy) Handler(w http.ResponseWriter, r *http.Request) {

gateway/proxy_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,37 @@ func TestNewProxy(t *testing.T) {
1111
testCases := []struct {
1212
name string
1313
targetURL string
14+
upstream Upstream
1415
expectErr bool
1516
expectHost string
1617
}{
1718
{
18-
name: "invalid URL",
19-
targetURL: "invalid url",
19+
name: "invalid URL",
20+
targetURL: "invalid url",
21+
upstream: Upstream{
22+
URL: "",
23+
Paths: []string{},
24+
},
2025
expectErr: true,
2126
expectHost: "",
2227
},
2328
{
24-
name: "valid URL",
25-
targetURL: "http://localhost:8080",
29+
name: "valid URL",
30+
upstream: Upstream{
31+
URL: "http://localhost:8080",
32+
Paths: []string{
33+
"/api/v1",
34+
"/api/v1/push",
35+
},
36+
},
2637
expectErr: false,
2738
expectHost: "localhost:8080",
2839
},
2940
}
3041

3142
for _, tc := range testCases {
3243
t.Run(tc.name, func(t *testing.T) {
33-
p, err := NewProxy(tc.targetURL, Upstream{}, "")
44+
p, err := NewProxy(tc.upstream.URL, tc.upstream, DISTRIBUTOR)
3445
if (err != nil) != tc.expectErr {
3546
t.Errorf("unexpected error: %v", err)
3647
return

gateway/utils.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

gateway/utils_test.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/cortexproject/auth-gateway
33
go 1.20
44

55
require (
6-
github.com/go-kit/log v0.2.1
76
github.com/prometheus/client_golang v1.14.0
87
github.com/sirupsen/logrus v1.6.0
98
github.com/stretchr/testify v1.8.2
@@ -14,7 +13,6 @@ require (
1413
github.com/beorn7/perks v1.0.1 // indirect
1514
github.com/cespare/xxhash/v2 v2.1.2 // indirect
1615
github.com/davecgh/go-spew v1.1.1 // indirect
17-
github.com/go-logfmt/logfmt v0.5.1 // indirect
1816
github.com/golang/protobuf v1.5.2 // indirect
1917
github.com/google/go-cmp v0.5.9 // indirect
2018
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
@@ -23,7 +21,7 @@ require (
2321
github.com/prometheus/client_model v0.3.0 // indirect
2422
github.com/prometheus/common v0.37.0 // indirect
2523
github.com/prometheus/procfs v0.8.0 // indirect
26-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
24+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
2725
google.golang.org/protobuf v1.28.1 // indirect
2826
gopkg.in/yaml.v3 v3.0.1 // indirect
2927
)

0 commit comments

Comments
 (0)