xds.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. *
  3. * Copyright 2021 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package google
  19. import (
  20. "context"
  21. "net"
  22. "net/url"
  23. "strings"
  24. "google.golang.org/grpc/credentials"
  25. "google.golang.org/grpc/internal"
  26. )
  27. const cfeClusterNamePrefix = "google_cfe_"
  28. const cfeClusterResourceNamePrefix = "/envoy.config.cluster.v3.Cluster/google_cfe_"
  29. const cfeClusterAuthorityName = "traffic-director-c2p.xds.googleapis.com"
  30. // clusterTransportCreds is a combo of TLS + ALTS.
  31. //
  32. // On the client, ClientHandshake picks TLS or ALTS based on address attributes.
  33. // - if attributes has cluster name
  34. // - if cluster name has prefix "google_cfe_", or
  35. // "xdstp://traffic-director-c2p.xds.googleapis.com/envoy.config.cluster.v3.Cluster/google_cfe_",
  36. // use TLS
  37. // - otherwise, use ALTS
  38. //
  39. // - else, do TLS
  40. //
  41. // On the server, ServerHandshake always does TLS.
  42. type clusterTransportCreds struct {
  43. tls credentials.TransportCredentials
  44. alts credentials.TransportCredentials
  45. }
  46. func newClusterTransportCreds(tls, alts credentials.TransportCredentials) *clusterTransportCreds {
  47. return &clusterTransportCreds{
  48. tls: tls,
  49. alts: alts,
  50. }
  51. }
  52. // clusterName returns the xDS cluster name stored in the attributes in the
  53. // context.
  54. func clusterName(ctx context.Context) string {
  55. chi := credentials.ClientHandshakeInfoFromContext(ctx)
  56. if chi.Attributes == nil {
  57. return ""
  58. }
  59. cluster, _ := internal.GetXDSHandshakeClusterName(chi.Attributes)
  60. return cluster
  61. }
  62. // isDirectPathCluster returns true if the cluster in the context is a
  63. // directpath cluster, meaning ALTS should be used.
  64. func isDirectPathCluster(ctx context.Context) bool {
  65. cluster := clusterName(ctx)
  66. if cluster == "" {
  67. // No cluster; not xDS; use TLS.
  68. return false
  69. }
  70. if strings.HasPrefix(cluster, cfeClusterNamePrefix) {
  71. // xDS cluster prefixed by "google_cfe_"; use TLS.
  72. return false
  73. }
  74. if !strings.HasPrefix(cluster, "xdstp:") {
  75. // Other xDS cluster name; use ALTS.
  76. return true
  77. }
  78. u, err := url.Parse(cluster)
  79. if err != nil {
  80. // Shouldn't happen, but assume ALTS.
  81. return true
  82. }
  83. // If authority AND path match our CFE checks, use TLS; otherwise use ALTS.
  84. return u.Host != cfeClusterAuthorityName || !strings.HasPrefix(u.Path, cfeClusterResourceNamePrefix)
  85. }
  86. func (c *clusterTransportCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
  87. if isDirectPathCluster(ctx) {
  88. // If attributes have cluster name, and cluster name is not cfe, it's a
  89. // backend address, use ALTS.
  90. return c.alts.ClientHandshake(ctx, authority, rawConn)
  91. }
  92. return c.tls.ClientHandshake(ctx, authority, rawConn)
  93. }
  94. func (c *clusterTransportCreds) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
  95. return c.tls.ServerHandshake(conn)
  96. }
  97. func (c *clusterTransportCreds) Info() credentials.ProtocolInfo {
  98. // TODO: this always returns tls.Info now, because we don't have a cluster
  99. // name to check when this method is called. This method doesn't affect
  100. // anything important now. We may want to revisit this if it becomes more
  101. // important later.
  102. return c.tls.Info()
  103. }
  104. func (c *clusterTransportCreds) Clone() credentials.TransportCredentials {
  105. return &clusterTransportCreds{
  106. tls: c.tls.Clone(),
  107. alts: c.alts.Clone(),
  108. }
  109. }
  110. func (c *clusterTransportCreds) OverrideServerName(s string) error {
  111. if err := c.tls.OverrideServerName(s); err != nil {
  112. return err
  113. }
  114. return c.alts.OverrideServerName(s)
  115. }