Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment thread
timflannagan marked this conversation as resolved.
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
68 changes: 67 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,71 @@ 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") != "",
Comment on lines +126 to +129
Copy link
Copy Markdown
Member

@njhale njhale Sep 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We should probably memoize these so we don't fetch them for every dial.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable but would make the values static, which likely isn't a problem given how deployments and pods work.

}
Comment thread
njhale marked this conversation as resolved.

// 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 {
Comment thread
tylerslaton marked this conversation as resolved.
for _, n := range names {
if val := os.Getenv(n); val != "" {
return val
}
}
return ""
}

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

if proxyURL != nil {
dialOptions = append(dialOptions, 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, dialOptions...)
}

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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we abstract the dial logic away? maybe take a Dailer as a field of SourceStore?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we pursue this option wouldn't the connection to the proxy need to remain open indefinitely?

if err != nil {
return nil, err
}
Expand Down
Loading