xds.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // - else, do TLS
  39. //
  40. // On the server, ServerHandshake always does TLS.
  41. type clusterTransportCreds struct {
  42. tls credentials.TransportCredentials
  43. alts credentials.TransportCredentials
  44. }
  45. func newClusterTransportCreds(tls, alts credentials.TransportCredentials) *clusterTransportCreds {
  46. return &clusterTransportCreds{
  47. tls: tls,
  48. alts: alts,
  49. }
  50. }
  51. // clusterName returns the xDS cluster name stored in the attributes in the
  52. // context.
  53. func clusterName(ctx context.Context) string {
  54. chi := credentials.ClientHandshakeInfoFromContext(ctx)
  55. if chi.Attributes == nil {
  56. return ""
  57. }
  58. cluster, _ := internal.GetXDSHandshakeClusterName(chi.Attributes)
  59. return cluster
  60. }
  61. // isDirectPathCluster returns true if the cluster in the context is a
  62. // directpath cluster, meaning ALTS should be used.
  63. func isDirectPathCluster(ctx context.Context) bool {
  64. cluster := clusterName(ctx)
  65. if cluster == "" {
  66. // No cluster; not xDS; use TLS.
  67. return false
  68. }
  69. if strings.HasPrefix(cluster, cfeClusterNamePrefix) {
  70. // xDS cluster prefixed by "google_cfe_"; use TLS.
  71. return false
  72. }
  73. if !strings.HasPrefix(cluster, "xdstp:") {
  74. // Other xDS cluster name; use ALTS.
  75. return true
  76. }
  77. u, err := url.Parse(cluster)
  78. if err != nil {
  79. // Shouldn't happen, but assume ALTS.
  80. return true
  81. }
  82. // If authority AND path match our CFE checks, use TLS; otherwise use ALTS.
  83. return u.Host != cfeClusterAuthorityName || !strings.HasPrefix(u.Path, cfeClusterResourceNamePrefix)
  84. }
  85. func (c *clusterTransportCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
  86. if isDirectPathCluster(ctx) {
  87. // If attributes have cluster name, and cluster name is not cfe, it's a
  88. // backend address, use ALTS.
  89. return c.alts.ClientHandshake(ctx, authority, rawConn)
  90. }
  91. return c.tls.ClientHandshake(ctx, authority, rawConn)
  92. }
  93. func (c *clusterTransportCreds) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
  94. return c.tls.ServerHandshake(conn)
  95. }
  96. func (c *clusterTransportCreds) Info() credentials.ProtocolInfo {
  97. // TODO: this always returns tls.Info now, because we don't have a cluster
  98. // name to check when this method is called. This method doesn't affect
  99. // anything important now. We may want to revisit this if it becomes more
  100. // important later.
  101. return c.tls.Info()
  102. }
  103. func (c *clusterTransportCreds) Clone() credentials.TransportCredentials {
  104. return &clusterTransportCreds{
  105. tls: c.tls.Clone(),
  106. alts: c.alts.Clone(),
  107. }
  108. }
  109. func (c *clusterTransportCreds) OverrideServerName(s string) error {
  110. if err := c.tls.OverrideServerName(s); err != nil {
  111. return err
  112. }
  113. return c.alts.OverrideServerName(s)
  114. }