Skip to content

Commit

Permalink
Introduce GRPC_PROXY EnvVar Support
Browse files Browse the repository at this point in the history
Introduce the ability to specify a dial context for GRPC connections.

Signed-off-by: Alexander Greene <greene.al1991@gmail.com>
  • Loading branch information
awgreene committed Sep 28, 2021
1 parent 26c36d7 commit 99ee72c
Show file tree
Hide file tree
Showing 4 changed files with 437 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
google.golang.org/grpc v1.38.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
67 changes: 66 additions & 1 deletion pkg/controller/registry/grpc/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package grpc

import (
"context"
"net"
"net/url"
"os"
"sync"
"time"

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

"github.com/sirupsen/logrus"
"golang.org/x/net/http/httpproxy"
"golang.org/x/net/proxy"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"

Expand Down Expand Up @@ -100,10 +105,70 @@ func (s *SourceStore) Get(key registry.CatalogKey) *SourceConn {
return &source
}

func grpcProxyURL(addr string) (*url.URL, error) {
// Handle ip addresses
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}

url, err := url.Parse(host)
if err != nil {
return nil, err
}

// Hardcode fields required for proxy resolution
url.Host = addr
url.Scheme = "http"

// Override HTTPS_PROXY and HTTP_PROXY with GRPC_PROXY
proxyConfig := &httpproxy.Config{
HTTPProxy: getGRPCProxyEnv(),
HTTPSProxy: getGRPCProxyEnv(),
NoProxy: getEnvAny("NO_PROXY", "no_proxy"),
CGI: os.Getenv("REQUEST_METHOD") != "",
}

// Check if a proxy should be used based on environment variables
return proxyConfig.ProxyFunc()(url)
}

func getGRPCProxyEnv() string {
return getEnvAny("GRPC_PROXY", "grpc_proxy")
}

func getEnvAny(names ...string) string {
for _, n := range names {
if val := os.Getenv(n); val != "" {
return val
}
}
return ""
}

func grpcConnection(address string) (*grpc.ClientConn, error) {
proxyURL, err := grpcProxyURL(address)
if err != nil {
return nil, err
}

if proxyURL != nil {
return grpc.Dial(address, grpc.WithInsecure(), grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
dialer, err := proxy.FromURL(proxyURL, &net.Dialer{})
if err != nil {
return nil, err
}
return dialer.Dial("tcp", addr)
}))
}

return grpc.Dial(address, grpc.WithInsecure())
}

func (s *SourceStore) Add(key registry.CatalogKey, address string) (*SourceConn, error) {
_ = s.Remove(key)

conn, err := grpc.Dial(address, grpc.WithInsecure())
conn, err := grpcConnection(address)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 99ee72c

Please sign in to comment.