|
| 1 | +/* |
| 2 | +Copyright 2023 The Dapr Authors |
| 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 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package internal |
| 15 | + |
| 16 | +import ( |
| 17 | + "errors" |
| 18 | + "fmt" |
| 19 | + "net" |
| 20 | + "net/url" |
| 21 | + "strings" |
| 22 | +) |
| 23 | + |
| 24 | +// Parsed represents a parsed gRPC endpoint. |
| 25 | +type Parsed struct { |
| 26 | + Target string |
| 27 | + TLS bool |
| 28 | +} |
| 29 | + |
| 30 | +//nolint:revive |
| 31 | +func ParseGRPCEndpoint(endpoint string) (Parsed, error) { |
| 32 | + target := endpoint |
| 33 | + if len(target) == 0 { |
| 34 | + return Parsed{}, errors.New("target is required") |
| 35 | + } |
| 36 | + |
| 37 | + var dnsAuthority string |
| 38 | + var hostname string |
| 39 | + var tls bool |
| 40 | + |
| 41 | + urlSplit := strings.Split(target, ":") |
| 42 | + if len(urlSplit) == 3 && !strings.Contains(target, "://") { |
| 43 | + target = strings.Replace(target, ":", "://", 1) |
| 44 | + } else if len(urlSplit) >= 2 && !strings.Contains(target, "://") && schemeKnown(urlSplit[0]) { |
| 45 | + target = strings.Replace(target, ":", "://", 1) |
| 46 | + } else { |
| 47 | + urlSplit = strings.Split(target, "://") |
| 48 | + if len(urlSplit) == 1 { |
| 49 | + target = "dns://" + target |
| 50 | + } else { |
| 51 | + scheme := urlSplit[0] |
| 52 | + if !schemeKnown(scheme) { |
| 53 | + return Parsed{}, fmt.Errorf("unknown scheme: %q", scheme) |
| 54 | + } |
| 55 | + |
| 56 | + if scheme == "dns" { |
| 57 | + urlSplit = strings.Split(target, "/") |
| 58 | + if len(urlSplit) < 4 { |
| 59 | + return Parsed{}, fmt.Errorf("invalid dns scheme: %q", target) |
| 60 | + } |
| 61 | + dnsAuthority = urlSplit[2] |
| 62 | + target = "dns://" + urlSplit[3] |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + ptarget, err := url.Parse(target) |
| 68 | + if err != nil { |
| 69 | + return Parsed{}, err |
| 70 | + } |
| 71 | + |
| 72 | + var errs []string |
| 73 | + for k := range ptarget.Query() { |
| 74 | + if k != "tls" { |
| 75 | + errs = append(errs, fmt.Sprintf("unrecognized query parameter: %q", k)) |
| 76 | + } |
| 77 | + } |
| 78 | + if len(errs) > 0 { |
| 79 | + return Parsed{}, fmt.Errorf("failed to parse target %q: %s", target, strings.Join(errs, "; ")) |
| 80 | + } |
| 81 | + |
| 82 | + if ptarget.Query().Has("tls") { |
| 83 | + if ptarget.Scheme == "http" || ptarget.Scheme == "https" { |
| 84 | + return Parsed{}, errors.New("cannot use tls query parameter with http(s) scheme") |
| 85 | + } |
| 86 | + |
| 87 | + qtls := ptarget.Query().Get("tls") |
| 88 | + if qtls != "true" && qtls != "false" { |
| 89 | + return Parsed{}, fmt.Errorf("invalid value for tls query parameter: %q", qtls) |
| 90 | + } |
| 91 | + |
| 92 | + tls = qtls == "true" |
| 93 | + } |
| 94 | + |
| 95 | + scheme := ptarget.Scheme |
| 96 | + if scheme == "https" { |
| 97 | + tls = true |
| 98 | + } |
| 99 | + if scheme == "http" || scheme == "https" { |
| 100 | + scheme = "dns" |
| 101 | + } |
| 102 | + |
| 103 | + hostname = ptarget.Host |
| 104 | + |
| 105 | + host, port, err := net.SplitHostPort(hostname) |
| 106 | + aerr, ok := err.(*net.AddrError) |
| 107 | + if ok && aerr.Err == "missing port in address" { |
| 108 | + port = "443" |
| 109 | + } else if err != nil { |
| 110 | + return Parsed{}, err |
| 111 | + } else { |
| 112 | + hostname = host |
| 113 | + } |
| 114 | + |
| 115 | + if len(hostname) == 0 { |
| 116 | + if scheme == "dns" { |
| 117 | + hostname = "localhost" |
| 118 | + } else { |
| 119 | + hostname = ptarget.Path |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + switch scheme { |
| 124 | + case "unix": |
| 125 | + separator := ":" |
| 126 | + if strings.HasPrefix(endpoint, "unix://") { |
| 127 | + separator = "://" |
| 128 | + } |
| 129 | + target = scheme + separator + hostname |
| 130 | + |
| 131 | + case "vsock": |
| 132 | + target = scheme + ":" + hostname + ":" + port |
| 133 | + |
| 134 | + case "unix-abstract": |
| 135 | + target = scheme + ":" + hostname |
| 136 | + |
| 137 | + case "dns": |
| 138 | + if len(ptarget.Path) > 0 { |
| 139 | + return Parsed{}, fmt.Errorf("path is not allowed: %q", ptarget.Path) |
| 140 | + } |
| 141 | + |
| 142 | + if strings.Count(hostname, ":") == 7 && !strings.HasPrefix(hostname, "[") && !strings.HasSuffix(hostname, "]") { |
| 143 | + hostname = "[" + hostname + "]" |
| 144 | + } |
| 145 | + if len(dnsAuthority) > 0 { |
| 146 | + dnsAuthority = "//" + dnsAuthority + "/" |
| 147 | + } |
| 148 | + target = scheme + ":" + dnsAuthority + hostname + ":" + port |
| 149 | + |
| 150 | + default: |
| 151 | + return Parsed{}, fmt.Errorf("unsupported scheme: %q", scheme) |
| 152 | + } |
| 153 | + |
| 154 | + return Parsed{ |
| 155 | + Target: target, |
| 156 | + TLS: tls, |
| 157 | + }, nil |
| 158 | +} |
| 159 | + |
| 160 | +func schemeKnown(scheme string) bool { |
| 161 | + for _, s := range []string{ |
| 162 | + "dns", |
| 163 | + "unix", |
| 164 | + "unix-abstract", |
| 165 | + "vsock", |
| 166 | + "http", |
| 167 | + "https", |
| 168 | + } { |
| 169 | + if scheme == s { |
| 170 | + return true |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return false |
| 175 | +} |
0 commit comments