Skip to content

Commit

Permalink
xds/google-c2p: validate url for no authorities (#5756)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindbr8 authored Nov 2, 2022
1 parent 040b795 commit fcb8bdf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions xds/googledirectpath/googlec2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ type c2pResolverBuilder struct {
}

func (c2pResolverBuilder) Build(t resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
if t.URL.Host != "" {
return nil, fmt.Errorf("google-c2p URI scheme does not support authorities")
}

if !runDirectPath() {
// If not xDS, fallback to DNS.
t.Scheme = dnsName
Expand Down
19 changes: 19 additions & 0 deletions xds/googledirectpath/googlec2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ package googledirectpath

import (
"strconv"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/envconfig"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/xds/internal/xdsclient"
Expand Down Expand Up @@ -251,3 +253,20 @@ func TestBuildXDS(t *testing.T) {
})
}
}

// TestDialFailsWhenTargetContainsAuthority attempts to Dial a target URI of
// google-c2p scheme with a non-empty authority and verifies that it fails with
// an expected error.
func TestBuildFailsWhenCalledWithAuthority(t *testing.T) {
uri := "google-c2p://an-authority/resource"
cc, err := grpc.Dial(uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
defer func() {
if cc != nil {
cc.Close()
}
}()
wantErr := "google-c2p URI scheme does not support authorities"
if err == nil || !strings.Contains(err.Error(), wantErr) {
t.Fatalf("grpc.Dial(%s) returned error: %v, want: %v", uri, err, wantErr)
}
}

0 comments on commit fcb8bdf

Please sign in to comment.