endpoint.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2021 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package endpoint
  15. import (
  16. "fmt"
  17. "net"
  18. "net/url"
  19. "path"
  20. "strings"
  21. )
  22. type CredsRequirement int
  23. const (
  24. // CREDS_REQUIRE - Credentials/certificate required for thi type of connection.
  25. CREDS_REQUIRE CredsRequirement = iota
  26. // CREDS_DROP - Credentials/certificate not needed and should get ignored.
  27. CREDS_DROP
  28. // CREDS_OPTIONAL - Credentials/certificate might be used if supplied
  29. CREDS_OPTIONAL
  30. )
  31. func extractHostFromHostPort(ep string) string {
  32. host, _, err := net.SplitHostPort(ep)
  33. if err != nil {
  34. return ep
  35. }
  36. return host
  37. }
  38. // mustSplit2 returns the values from strings.SplitN(s, sep, 2).
  39. // If sep is not found, it returns ("", "", false) instead.
  40. func mustSplit2(s, sep string) (string, string) {
  41. spl := strings.SplitN(s, sep, 2)
  42. if len(spl) < 2 {
  43. panic(fmt.Errorf("token '%v' expected to have separator sep: `%v`", s, sep))
  44. }
  45. return spl[0], spl[1]
  46. }
  47. func schemeToCredsRequirement(schema string) CredsRequirement {
  48. switch schema {
  49. case "https", "unixs":
  50. return CREDS_REQUIRE
  51. case "http":
  52. return CREDS_DROP
  53. case "unix":
  54. // Preserving previous behavior from:
  55. // https://github.com/etcd-io/etcd/blob/dae29bb719dd69dc119146fc297a0628fcc1ccf8/client/v3/client.go#L212
  56. // that likely was a bug due to missing 'fallthrough'.
  57. // At the same time it seems legit to let the users decide whether they
  58. // want credential control or not (and 'unixs' schema is not a standard thing).
  59. return CREDS_OPTIONAL
  60. case "":
  61. return CREDS_OPTIONAL
  62. default:
  63. return CREDS_OPTIONAL
  64. }
  65. }
  66. // This function translates endpoints names supported by etcd server into
  67. // endpoints as supported by grpc with additional information
  68. // (server_name for cert validation, requireCreds - whether certs are needed).
  69. // The main differences:
  70. // - etcd supports unixs & https names as opposed to unix & http to
  71. // distinguish need to configure certificates.
  72. // - etcd support http(s) names as opposed to tcp supported by grpc/dial method.
  73. // - etcd supports unix(s)://local-file naming schema
  74. // (as opposed to unix:local-file canonical name used by grpc for current dir files).
  75. // - Within the unix(s) schemas, the last segment (filename) without 'port' (content after colon)
  76. // is considered serverName - to allow local testing of cert-protected communication.
  77. //
  78. // See more:
  79. // - https://github.com/grpc/grpc-go/blob/26c143bd5f59344a4b8a1e491e0f5e18aa97abc7/internal/grpcutil/target.go#L47
  80. // - https://golang.org/pkg/net/#Dial
  81. // - https://github.com/grpc/grpc/blob/master/doc/naming.md
  82. func translateEndpoint(ep string) (addr string, serverName string, requireCreds CredsRequirement) {
  83. if strings.HasPrefix(ep, "unix:") || strings.HasPrefix(ep, "unixs:") {
  84. if strings.HasPrefix(ep, "unix:///") || strings.HasPrefix(ep, "unixs:///") {
  85. // absolute path case
  86. schema, absolutePath := mustSplit2(ep, "://")
  87. return "unix://" + absolutePath, path.Base(absolutePath), schemeToCredsRequirement(schema)
  88. }
  89. if strings.HasPrefix(ep, "unix://") || strings.HasPrefix(ep, "unixs://") {
  90. // legacy etcd local path
  91. schema, localPath := mustSplit2(ep, "://")
  92. return "unix:" + localPath, path.Base(localPath), schemeToCredsRequirement(schema)
  93. }
  94. schema, localPath := mustSplit2(ep, ":")
  95. return "unix:" + localPath, path.Base(localPath), schemeToCredsRequirement(schema)
  96. }
  97. if strings.Contains(ep, "://") {
  98. url, err := url.Parse(ep)
  99. if err != nil {
  100. return ep, ep, CREDS_OPTIONAL
  101. }
  102. if url.Scheme == "http" || url.Scheme == "https" {
  103. return url.Host, url.Host, schemeToCredsRequirement(url.Scheme)
  104. }
  105. return ep, url.Host, schemeToCredsRequirement(url.Scheme)
  106. }
  107. // Handles plain addresses like 10.0.0.44:437.
  108. return ep, ep, CREDS_OPTIONAL
  109. }
  110. // RequiresCredentials returns whether given endpoint requires
  111. // credentials/certificates for connection.
  112. func RequiresCredentials(ep string) CredsRequirement {
  113. _, _, requireCreds := translateEndpoint(ep)
  114. return requireCreds
  115. }
  116. // Interpret endpoint parses an endpoint of the form
  117. // (http|https)://<host>*|(unix|unixs)://<path>)
  118. // and returns low-level address (supported by 'net') to connect to,
  119. // and a server name used for x509 certificate matching.
  120. func Interpret(ep string) (address string, serverName string) {
  121. addr, serverName, _ := translateEndpoint(ep)
  122. return addr, serverName
  123. }