Skip to content

Commit b037c55

Browse files
committed
Introduce GRPC_PROXY EnvVar Support
Introduce the ability to specify a dial context for GRPC connections.
1 parent 26c36d7 commit b037c55

File tree

4 files changed

+436
-1
lines changed

4 files changed

+436
-1
lines changed

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require (
3636
github.com/spf13/cobra v1.1.3
3737
github.com/spf13/pflag v1.0.5
3838
github.com/stretchr/testify v1.7.0
39+
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
3940
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
4041
google.golang.org/grpc v1.38.0
4142
gopkg.in/yaml.v2 v2.4.0

Diff for: pkg/controller/registry/grpc/source.go

+65-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package grpc
22

33
import (
44
"context"
5+
"net"
6+
"net/url"
7+
"os"
58
"sync"
69
"time"
710

811
"github.com/operator-framework/operator-registry/pkg/client"
912

1013
"github.com/sirupsen/logrus"
14+
"golang.org/x/net/http/httpproxy"
15+
"golang.org/x/net/proxy"
1116
"google.golang.org/grpc"
1217
"google.golang.org/grpc/connectivity"
1318

@@ -100,10 +105,69 @@ func (s *SourceStore) Get(key registry.CatalogKey) *SourceConn {
100105
return &source
101106
}
102107

108+
func grpcProxyURL(addr string) (*url.URL, error) {
109+
// Handle ip addresses
110+
host, _, err := net.SplitHostPort(addr)
111+
if err != nil {
112+
return nil, err
113+
}
114+
115+
url, err := url.Parse(host)
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
// Hardcode fields required for proxy resolution
121+
url.Host = addr
122+
url.Scheme = "http"
123+
124+
proxyConfig := httpproxy.FromEnvironment()
125+
126+
// Override HTTPS_PROXY and HTTP_PROXY with GRPC_PROXY
127+
proxyConfig.HTTPProxy = getGRPCProxyEnv()
128+
proxyConfig.HTTPSProxy = getGRPCProxyEnv()
129+
130+
// Check if a proxy should be used based on Environment variables
131+
return proxyConfig.ProxyFunc()(url)
132+
}
133+
134+
func getGRPCProxyEnv() string {
135+
return getEnvAny("GRPC_PROXY", "grpc_proxy")
136+
}
137+
138+
func getEnvAny(names ...string) string {
139+
for _, n := range names {
140+
if val := os.Getenv(n); val != "" {
141+
return val
142+
}
143+
}
144+
return ""
145+
}
146+
147+
func grpcConnection(address string) (*grpc.ClientConn, error) {
148+
// Check if a proxy should be used based on Environment variables
149+
proxyURL, err := grpcProxyURL(address)
150+
if err != nil {
151+
return nil, err
152+
}
153+
154+
if proxyURL != nil {
155+
return grpc.Dial(address, grpc.WithInsecure(), grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
156+
dialer, err := proxy.FromURL(proxyURL, &net.Dialer{})
157+
if err != nil {
158+
return nil, err
159+
}
160+
return dialer.Dial("tcp", addr)
161+
}))
162+
}
163+
164+
return grpc.Dial(address, grpc.WithInsecure())
165+
}
166+
103167
func (s *SourceStore) Add(key registry.CatalogKey, address string) (*SourceConn, error) {
104168
_ = s.Remove(key)
105169

106-
conn, err := grpc.Dial(address, grpc.WithInsecure())
170+
conn, err := grpcConnection(address)
107171
if err != nil {
108172
return nil, err
109173
}

0 commit comments

Comments
 (0)