...
1
18
19 package google
20
21 import (
22 "context"
23 "net"
24 "net/url"
25 "strings"
26
27 "google.golang.org/grpc/credentials"
28 "google.golang.org/grpc/internal/xds"
29 )
30
31 const cfeClusterNamePrefix = "google_cfe_"
32 const cfeClusterResourceNamePrefix = "/envoy.config.cluster.v3.Cluster/google_cfe_"
33 const cfeClusterAuthorityName = "traffic-director-c2p.xds.googleapis.com"
34
35
36
37
38
39
40
41
42
43
44
45
46
47 type clusterTransportCreds struct {
48 tls credentials.TransportCredentials
49 alts credentials.TransportCredentials
50 }
51
52 func newClusterTransportCreds(tls, alts credentials.TransportCredentials) *clusterTransportCreds {
53 return &clusterTransportCreds{
54 tls: tls,
55 alts: alts,
56 }
57 }
58
59
60
61 func clusterName(ctx context.Context) string {
62 chi := credentials.ClientHandshakeInfoFromContext(ctx)
63 if chi.Attributes == nil {
64 return ""
65 }
66 cluster, _ := xds.GetXDSHandshakeClusterName(chi.Attributes)
67 return cluster
68 }
69
70
71
72 func isDirectPathCluster(ctx context.Context) bool {
73 cluster := clusterName(ctx)
74 if cluster == "" {
75
76 return false
77 }
78 if strings.HasPrefix(cluster, cfeClusterNamePrefix) {
79
80 return false
81 }
82 if !strings.HasPrefix(cluster, "xdstp:") {
83
84 return true
85 }
86 u, err := url.Parse(cluster)
87 if err != nil {
88
89 return true
90 }
91
92 return u.Host != cfeClusterAuthorityName || !strings.HasPrefix(u.Path, cfeClusterResourceNamePrefix)
93 }
94
95 func (c *clusterTransportCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
96 if isDirectPathCluster(ctx) {
97
98
99 return c.alts.ClientHandshake(ctx, authority, rawConn)
100 }
101 return c.tls.ClientHandshake(ctx, authority, rawConn)
102 }
103
104 func (c *clusterTransportCreds) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
105 return c.tls.ServerHandshake(conn)
106 }
107
108 func (c *clusterTransportCreds) Info() credentials.ProtocolInfo {
109
110
111
112
113 return c.tls.Info()
114 }
115
116 func (c *clusterTransportCreds) Clone() credentials.TransportCredentials {
117 return &clusterTransportCreds{
118 tls: c.tls.Clone(),
119 alts: c.alts.Clone(),
120 }
121 }
122
123 func (c *clusterTransportCreds) OverrideServerName(s string) error {
124 if err := c.tls.OverrideServerName(s); err != nil {
125 return err
126 }
127 return c.alts.OverrideServerName(s)
128 }
129
View as plain text