Skip to content

Commit

Permalink
feat(agent): add support for port 443 (#3276)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren authored Oct 17, 2023
1 parent c63c8cb commit b7615c1
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions agent/client/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package client

import (
"context"
"crypto/tls"
"fmt"
"net"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)

Expand All @@ -27,11 +30,38 @@ func connect(ctx context.Context, endpoint string) (*grpc.ClientConn, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

// TODO: don't use insecure transportation
conn, err := grpc.DialContext(ctx, endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
transportCredentials, err := getTransportCredentialsForEndpoint(endpoint)
if err != nil {
return nil, fmt.Errorf("could not get transport credentials: %w", err)
}

conn, err := grpc.DialContext(
ctx, endpoint,
grpc.WithTransportCredentials(transportCredentials),
)
if err != nil {
return nil, fmt.Errorf("could not connect to server: %w", err)
}

return conn, nil
}

func getTransportCredentialsForEndpoint(endpoint string) (credentials.TransportCredentials, error) {
_, port, err := net.SplitHostPort(endpoint)
if err != nil {
return nil, fmt.Errorf("cannot parse endpoint: %w", err)
}

switch port {
case "443":
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
transportCredentials := credentials.NewTLS(tlsConfig)
return transportCredentials, nil

default:
return insecure.NewCredentials(), nil
}

}

0 comments on commit b7615c1

Please sign in to comment.