endpoint.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. func extractHostFromPath(pathStr string) string {
  39. return extractHostFromHostPort(path.Base(pathStr))
  40. }
  41. //mustSplit2 returns the values from strings.SplitN(s, sep, 2).
  42. //If sep is not found, it returns ("", "", false) instead.
  43. func mustSplit2(s, sep string) (string, string) {
  44. spl := strings.SplitN(s, sep, 2)
  45. if len(spl) < 2 {
  46. panic(fmt.Errorf("token '%v' expected to have separator sep: `%v`", s, sep))
  47. }
  48. return spl[0], spl[1]
  49. }
  50. func schemeToCredsRequirement(schema string) CredsRequirement {
  51. switch schema {
  52. case "https", "unixs":
  53. return CREDS_REQUIRE
  54. case "http":
  55. return CREDS_DROP
  56. case "unix":
  57. // Preserving previous behavior from:
  58. // https://github.com/etcd-io/etcd/blob/dae29bb719dd69dc119146fc297a0628fcc1ccf8/client/v3/client.go#L212
  59. // that likely was a bug due to missing 'fallthrough'.
  60. // At the same time it seems legit to let the users decide whether they
  61. // want credential control or not (and 'unixs' schema is not a standard thing).
  62. return CREDS_OPTIONAL
  63. case "":
  64. return CREDS_OPTIONAL
  65. default:
  66. return CREDS_OPTIONAL
  67. }
  68. }
  69. // This function translates endpoints names supported by etcd server into
  70. // endpoints as supported by grpc with additional information
  71. // (server_name for cert validation, requireCreds - whether certs are needed).
  72. // The main differences:
  73. // - etcd supports unixs & https names as opposed to unix & http to
  74. // distinguish need to configure certificates.
  75. // - etcd support http(s) names as opposed to tcp supported by grpc/dial method.
  76. // - etcd supports unix(s)://local-file naming schema
  77. // (as opposed to unix:local-file canonical name used by grpc for current dir files).
  78. // - Within the unix(s) schemas, the last segment (filename) without 'port' (content after colon)
  79. // is considered serverName - to allow local testing of cert-protected communication.
  80. // See more:
  81. // - https://github.com/grpc/grpc-go/blob/26c143bd5f59344a4b8a1e491e0f5e18aa97abc7/internal/grpcutil/target.go#L47
  82. // - https://golang.org/pkg/net/#Dial
  83. // - https://github.com/grpc/grpc/blob/master/doc/naming.md
  84. func translateEndpoint(ep string) (addr string, serverName string, requireCreds CredsRequirement) {
  85. if strings.HasPrefix(ep, "unix:") || strings.HasPrefix(ep, "unixs:") {
  86. if strings.HasPrefix(ep, "unix:///") || strings.HasPrefix(ep, "unixs:///") {
  87. // absolute path case
  88. schema, absolutePath := mustSplit2(ep, "://")
  89. return "unix://" + absolutePath, extractHostFromPath(absolutePath), schemeToCredsRequirement(schema)
  90. }
  91. if strings.HasPrefix(ep, "unix://") || strings.HasPrefix(ep, "unixs://") {
  92. // legacy etcd local path
  93. schema, localPath := mustSplit2(ep, "://")
  94. return "unix:" + localPath, extractHostFromPath(localPath), schemeToCredsRequirement(schema)
  95. }
  96. schema, localPath := mustSplit2(ep, ":")
  97. return "unix:" + localPath, extractHostFromPath(localPath), schemeToCredsRequirement(schema)
  98. }
  99. if strings.Contains(ep, "://") {
  100. url, err := url.Parse(ep)
  101. if err != nil {
  102. return ep, extractHostFromHostPort(ep), CREDS_OPTIONAL
  103. }
  104. if url.Scheme == "http" || url.Scheme == "https" {
  105. return url.Host, url.Hostname(), schemeToCredsRequirement(url.Scheme)
  106. }
  107. return ep, url.Hostname(), schemeToCredsRequirement(url.Scheme)
  108. }
  109. // Handles plain addresses like 10.0.0.44:437.
  110. return ep, extractHostFromHostPort(ep), CREDS_OPTIONAL
  111. }
  112. // RequiresCredentials returns whether given endpoint requires
  113. // credentials/certificates for connection.
  114. func RequiresCredentials(ep string) CredsRequirement {
  115. _, _, requireCreds := translateEndpoint(ep)
  116. return requireCreds
  117. }
  118. // Interpret endpoint parses an endpoint of the form
  119. // (http|https)://<host>*|(unix|unixs)://<path>)
  120. // and returns low-level address (supported by 'net') to connect to,
  121. // and a server name used for x509 certificate matching.
  122. func Interpret(ep string) (address string, serverName string) {
  123. addr, serverName, _ := translateEndpoint(ep)
  124. return addr, serverName
  125. }