Andrea Campanella | 3614a92 | 2021-02-25 12:40:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2018 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 | |
| 19 | // Package dns implements a dns resolver to be installed as the default resolver |
| 20 | // in grpc. |
| 21 | package dns |
| 22 | |
| 23 | import ( |
| 24 | "context" |
| 25 | "encoding/json" |
| 26 | "errors" |
| 27 | "fmt" |
| 28 | "net" |
| 29 | "os" |
| 30 | "strconv" |
| 31 | "strings" |
| 32 | "sync" |
| 33 | "time" |
| 34 | |
| 35 | "google.golang.org/grpc/backoff" |
| 36 | "google.golang.org/grpc/grpclog" |
| 37 | internalbackoff "google.golang.org/grpc/internal/backoff" |
| 38 | "google.golang.org/grpc/internal/grpcrand" |
| 39 | "google.golang.org/grpc/resolver" |
| 40 | ) |
| 41 | |
| 42 | func init() { |
| 43 | resolver.Register(NewBuilder()) |
| 44 | } |
| 45 | |
| 46 | const ( |
| 47 | defaultPort = "443" |
| 48 | defaultFreq = time.Minute * 30 |
| 49 | defaultDNSSvrPort = "53" |
| 50 | golang = "GO" |
| 51 | // txtPrefix is the prefix string to be prepended to the host name for txt record lookup. |
| 52 | txtPrefix = "_grpc_config." |
| 53 | // In DNS, service config is encoded in a TXT record via the mechanism |
| 54 | // described in RFC-1464 using the attribute name grpc_config. |
| 55 | txtAttribute = "grpc_config=" |
| 56 | ) |
| 57 | |
| 58 | var ( |
| 59 | errMissingAddr = errors.New("dns resolver: missing address") |
| 60 | |
| 61 | // Addresses ending with a colon that is supposed to be the separator |
| 62 | // between host and port is not allowed. E.g. "::" is a valid address as |
| 63 | // it is an IPv6 address (host only) and "[::]:" is invalid as it ends with |
| 64 | // a colon as the host and port separator |
| 65 | errEndsWithColon = errors.New("dns resolver: missing port after port-separator colon") |
| 66 | ) |
| 67 | |
| 68 | var ( |
| 69 | defaultResolver netResolver = net.DefaultResolver |
| 70 | // To prevent excessive re-resolution, we enforce a rate limit on DNS |
| 71 | // resolution requests. |
| 72 | minDNSResRate = 30 * time.Second |
| 73 | ) |
| 74 | |
| 75 | var customAuthorityDialler = func(authority string) func(ctx context.Context, network, address string) (net.Conn, error) { |
| 76 | return func(ctx context.Context, network, address string) (net.Conn, error) { |
| 77 | var dialer net.Dialer |
| 78 | return dialer.DialContext(ctx, network, authority) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | var customAuthorityResolver = func(authority string) (netResolver, error) { |
| 83 | host, port, err := parseTarget(authority, defaultDNSSvrPort) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | |
| 88 | authorityWithPort := net.JoinHostPort(host, port) |
| 89 | |
| 90 | return &net.Resolver{ |
| 91 | PreferGo: true, |
| 92 | Dial: customAuthorityDialler(authorityWithPort), |
| 93 | }, nil |
| 94 | } |
| 95 | |
| 96 | // NewBuilder creates a dnsBuilder which is used to factory DNS resolvers. |
| 97 | func NewBuilder() resolver.Builder { |
| 98 | return &dnsBuilder{minFreq: defaultFreq} |
| 99 | } |
| 100 | |
| 101 | type dnsBuilder struct { |
| 102 | // minimum frequency of polling the DNS server. |
| 103 | minFreq time.Duration |
| 104 | } |
| 105 | |
| 106 | // Build creates and starts a DNS resolver that watches the name resolution of the target. |
| 107 | func (b *dnsBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) { |
| 108 | host, port, err := parseTarget(target.Endpoint, defaultPort) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | |
| 113 | // IP address. |
| 114 | if net.ParseIP(host) != nil { |
| 115 | host, _ = formatIP(host) |
| 116 | addr := []resolver.Address{{Addr: host + ":" + port}} |
| 117 | i := &ipResolver{ |
| 118 | cc: cc, |
| 119 | ip: addr, |
| 120 | rn: make(chan struct{}, 1), |
| 121 | q: make(chan struct{}), |
| 122 | } |
| 123 | cc.NewAddress(addr) |
| 124 | go i.watcher() |
| 125 | return i, nil |
| 126 | } |
| 127 | |
| 128 | // DNS address (non-IP). |
| 129 | ctx, cancel := context.WithCancel(context.Background()) |
| 130 | bc := backoff.DefaultConfig |
| 131 | bc.MaxDelay = b.minFreq |
| 132 | d := &dnsResolver{ |
| 133 | freq: b.minFreq, |
| 134 | backoff: internalbackoff.Exponential{Config: bc}, |
| 135 | host: host, |
| 136 | port: port, |
| 137 | ctx: ctx, |
| 138 | cancel: cancel, |
| 139 | cc: cc, |
| 140 | t: time.NewTimer(0), |
| 141 | rn: make(chan struct{}, 1), |
| 142 | disableServiceConfig: opts.DisableServiceConfig, |
| 143 | } |
| 144 | |
| 145 | if target.Authority == "" { |
| 146 | d.resolver = defaultResolver |
| 147 | } else { |
| 148 | d.resolver, err = customAuthorityResolver(target.Authority) |
| 149 | if err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | d.wg.Add(1) |
| 155 | go d.watcher() |
| 156 | return d, nil |
| 157 | } |
| 158 | |
| 159 | // Scheme returns the naming scheme of this resolver builder, which is "dns". |
| 160 | func (b *dnsBuilder) Scheme() string { |
| 161 | return "dns" |
| 162 | } |
| 163 | |
| 164 | type netResolver interface { |
| 165 | LookupHost(ctx context.Context, host string) (addrs []string, err error) |
| 166 | LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*net.SRV, err error) |
| 167 | LookupTXT(ctx context.Context, name string) (txts []string, err error) |
| 168 | } |
| 169 | |
| 170 | // ipResolver watches for the name resolution update for an IP address. |
| 171 | type ipResolver struct { |
| 172 | cc resolver.ClientConn |
| 173 | ip []resolver.Address |
| 174 | // rn channel is used by ResolveNow() to force an immediate resolution of the target. |
| 175 | rn chan struct{} |
| 176 | q chan struct{} |
| 177 | } |
| 178 | |
| 179 | // ResolveNow resend the address it stores, no resolution is needed. |
| 180 | func (i *ipResolver) ResolveNow(opt resolver.ResolveNowOption) { |
| 181 | select { |
| 182 | case i.rn <- struct{}{}: |
| 183 | default: |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // Close closes the ipResolver. |
| 188 | func (i *ipResolver) Close() { |
| 189 | close(i.q) |
| 190 | } |
| 191 | |
| 192 | func (i *ipResolver) watcher() { |
| 193 | for { |
| 194 | select { |
| 195 | case <-i.rn: |
| 196 | i.cc.NewAddress(i.ip) |
| 197 | case <-i.q: |
| 198 | return |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // dnsResolver watches for the name resolution update for a non-IP target. |
| 204 | type dnsResolver struct { |
| 205 | freq time.Duration |
| 206 | backoff internalbackoff.Exponential |
| 207 | retryCount int |
| 208 | host string |
| 209 | port string |
| 210 | resolver netResolver |
| 211 | ctx context.Context |
| 212 | cancel context.CancelFunc |
| 213 | cc resolver.ClientConn |
| 214 | // rn channel is used by ResolveNow() to force an immediate resolution of the target. |
| 215 | rn chan struct{} |
| 216 | t *time.Timer |
| 217 | // wg is used to enforce Close() to return after the watcher() goroutine has finished. |
| 218 | // Otherwise, data race will be possible. [Race Example] in dns_resolver_test we |
| 219 | // replace the real lookup functions with mocked ones to facilitate testing. |
| 220 | // If Close() doesn't wait for watcher() goroutine finishes, race detector sometimes |
| 221 | // will warns lookup (READ the lookup function pointers) inside watcher() goroutine |
| 222 | // has data race with replaceNetFunc (WRITE the lookup function pointers). |
| 223 | wg sync.WaitGroup |
| 224 | disableServiceConfig bool |
| 225 | } |
| 226 | |
| 227 | // ResolveNow invoke an immediate resolution of the target that this dnsResolver watches. |
| 228 | func (d *dnsResolver) ResolveNow(opt resolver.ResolveNowOption) { |
| 229 | select { |
| 230 | case d.rn <- struct{}{}: |
| 231 | default: |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Close closes the dnsResolver. |
| 236 | func (d *dnsResolver) Close() { |
| 237 | d.cancel() |
| 238 | d.wg.Wait() |
| 239 | d.t.Stop() |
| 240 | } |
| 241 | |
| 242 | func (d *dnsResolver) watcher() { |
| 243 | defer d.wg.Done() |
| 244 | for { |
| 245 | select { |
| 246 | case <-d.ctx.Done(): |
| 247 | return |
| 248 | case <-d.t.C: |
| 249 | case <-d.rn: |
| 250 | if !d.t.Stop() { |
| 251 | // Before resetting a timer, it should be stopped to prevent racing with |
| 252 | // reads on it's channel. |
| 253 | <-d.t.C |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | result, sc := d.lookup() |
| 258 | // Next lookup should happen within an interval defined by d.freq. It may be |
| 259 | // more often due to exponential retry on empty address list. |
| 260 | if len(result) == 0 { |
| 261 | d.retryCount++ |
| 262 | d.t.Reset(d.backoff.Backoff(d.retryCount)) |
| 263 | } else { |
| 264 | d.retryCount = 0 |
| 265 | d.t.Reset(d.freq) |
| 266 | } |
| 267 | d.cc.NewServiceConfig(sc) |
| 268 | d.cc.NewAddress(result) |
| 269 | |
| 270 | // Sleep to prevent excessive re-resolutions. Incoming resolution requests |
| 271 | // will be queued in d.rn. |
| 272 | t := time.NewTimer(minDNSResRate) |
| 273 | select { |
| 274 | case <-t.C: |
| 275 | case <-d.ctx.Done(): |
| 276 | t.Stop() |
| 277 | return |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | func (d *dnsResolver) lookupSRV() []resolver.Address { |
| 283 | var newAddrs []resolver.Address |
| 284 | _, srvs, err := d.resolver.LookupSRV(d.ctx, "grpclb", "tcp", d.host) |
| 285 | if err != nil { |
| 286 | grpclog.Infof("grpc: failed dns SRV record lookup due to %v.\n", err) |
| 287 | return nil |
| 288 | } |
| 289 | for _, s := range srvs { |
| 290 | lbAddrs, err := d.resolver.LookupHost(d.ctx, s.Target) |
| 291 | if err != nil { |
| 292 | grpclog.Infof("grpc: failed load balancer address dns lookup due to %v.\n", err) |
| 293 | continue |
| 294 | } |
| 295 | for _, a := range lbAddrs { |
| 296 | a, ok := formatIP(a) |
| 297 | if !ok { |
| 298 | grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err) |
| 299 | continue |
| 300 | } |
| 301 | addr := a + ":" + strconv.Itoa(int(s.Port)) |
| 302 | newAddrs = append(newAddrs, resolver.Address{Addr: addr, Type: resolver.GRPCLB, ServerName: s.Target}) |
| 303 | } |
| 304 | } |
| 305 | return newAddrs |
| 306 | } |
| 307 | |
| 308 | func (d *dnsResolver) lookupTXT() string { |
| 309 | ss, err := d.resolver.LookupTXT(d.ctx, txtPrefix+d.host) |
| 310 | if err != nil { |
| 311 | grpclog.Infof("grpc: failed dns TXT record lookup due to %v.\n", err) |
| 312 | return "" |
| 313 | } |
| 314 | var res string |
| 315 | for _, s := range ss { |
| 316 | res += s |
| 317 | } |
| 318 | |
| 319 | // TXT record must have "grpc_config=" attribute in order to be used as service config. |
| 320 | if !strings.HasPrefix(res, txtAttribute) { |
| 321 | grpclog.Warningf("grpc: TXT record %v missing %v attribute", res, txtAttribute) |
| 322 | return "" |
| 323 | } |
| 324 | return strings.TrimPrefix(res, txtAttribute) |
| 325 | } |
| 326 | |
| 327 | func (d *dnsResolver) lookupHost() []resolver.Address { |
| 328 | var newAddrs []resolver.Address |
| 329 | addrs, err := d.resolver.LookupHost(d.ctx, d.host) |
| 330 | if err != nil { |
| 331 | grpclog.Warningf("grpc: failed dns A record lookup due to %v.\n", err) |
| 332 | return nil |
| 333 | } |
| 334 | for _, a := range addrs { |
| 335 | a, ok := formatIP(a) |
| 336 | if !ok { |
| 337 | grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err) |
| 338 | continue |
| 339 | } |
| 340 | addr := a + ":" + d.port |
| 341 | newAddrs = append(newAddrs, resolver.Address{Addr: addr}) |
| 342 | } |
| 343 | return newAddrs |
| 344 | } |
| 345 | |
| 346 | func (d *dnsResolver) lookup() ([]resolver.Address, string) { |
| 347 | newAddrs := d.lookupSRV() |
| 348 | // Support fallback to non-balancer address. |
| 349 | newAddrs = append(newAddrs, d.lookupHost()...) |
| 350 | if d.disableServiceConfig { |
| 351 | return newAddrs, "" |
| 352 | } |
| 353 | sc := d.lookupTXT() |
| 354 | return newAddrs, canaryingSC(sc) |
| 355 | } |
| 356 | |
| 357 | // formatIP returns ok = false if addr is not a valid textual representation of an IP address. |
| 358 | // If addr is an IPv4 address, return the addr and ok = true. |
| 359 | // If addr is an IPv6 address, return the addr enclosed in square brackets and ok = true. |
| 360 | func formatIP(addr string) (addrIP string, ok bool) { |
| 361 | ip := net.ParseIP(addr) |
| 362 | if ip == nil { |
| 363 | return "", false |
| 364 | } |
| 365 | if ip.To4() != nil { |
| 366 | return addr, true |
| 367 | } |
| 368 | return "[" + addr + "]", true |
| 369 | } |
| 370 | |
| 371 | // parseTarget takes the user input target string and default port, returns formatted host and port info. |
| 372 | // If target doesn't specify a port, set the port to be the defaultPort. |
| 373 | // If target is in IPv6 format and host-name is enclosed in square brackets, brackets |
| 374 | // are stripped when setting the host. |
| 375 | // examples: |
| 376 | // target: "www.google.com" defaultPort: "443" returns host: "www.google.com", port: "443" |
| 377 | // target: "ipv4-host:80" defaultPort: "443" returns host: "ipv4-host", port: "80" |
| 378 | // target: "[ipv6-host]" defaultPort: "443" returns host: "ipv6-host", port: "443" |
| 379 | // target: ":80" defaultPort: "443" returns host: "localhost", port: "80" |
| 380 | func parseTarget(target, defaultPort string) (host, port string, err error) { |
| 381 | if target == "" { |
| 382 | return "", "", errMissingAddr |
| 383 | } |
| 384 | if ip := net.ParseIP(target); ip != nil { |
| 385 | // target is an IPv4 or IPv6(without brackets) address |
| 386 | return target, defaultPort, nil |
| 387 | } |
| 388 | if host, port, err = net.SplitHostPort(target); err == nil { |
| 389 | if port == "" { |
| 390 | // If the port field is empty (target ends with colon), e.g. "[::1]:", this is an error. |
| 391 | return "", "", errEndsWithColon |
| 392 | } |
| 393 | // target has port, i.e ipv4-host:port, [ipv6-host]:port, host-name:port |
| 394 | if host == "" { |
| 395 | // Keep consistent with net.Dial(): If the host is empty, as in ":80", the local system is assumed. |
| 396 | host = "localhost" |
| 397 | } |
| 398 | return host, port, nil |
| 399 | } |
| 400 | if host, port, err = net.SplitHostPort(target + ":" + defaultPort); err == nil { |
| 401 | // target doesn't have port |
| 402 | return host, port, nil |
| 403 | } |
| 404 | return "", "", fmt.Errorf("invalid target address %v, error info: %v", target, err) |
| 405 | } |
| 406 | |
| 407 | type rawChoice struct { |
| 408 | ClientLanguage *[]string `json:"clientLanguage,omitempty"` |
| 409 | Percentage *int `json:"percentage,omitempty"` |
| 410 | ClientHostName *[]string `json:"clientHostName,omitempty"` |
| 411 | ServiceConfig *json.RawMessage `json:"serviceConfig,omitempty"` |
| 412 | } |
| 413 | |
| 414 | func containsString(a *[]string, b string) bool { |
| 415 | if a == nil { |
| 416 | return true |
| 417 | } |
| 418 | for _, c := range *a { |
| 419 | if c == b { |
| 420 | return true |
| 421 | } |
| 422 | } |
| 423 | return false |
| 424 | } |
| 425 | |
| 426 | func chosenByPercentage(a *int) bool { |
| 427 | if a == nil { |
| 428 | return true |
| 429 | } |
| 430 | return grpcrand.Intn(100)+1 <= *a |
| 431 | } |
| 432 | |
| 433 | func canaryingSC(js string) string { |
| 434 | if js == "" { |
| 435 | return "" |
| 436 | } |
| 437 | var rcs []rawChoice |
| 438 | err := json.Unmarshal([]byte(js), &rcs) |
| 439 | if err != nil { |
| 440 | grpclog.Warningf("grpc: failed to parse service config json string due to %v.\n", err) |
| 441 | return "" |
| 442 | } |
| 443 | cliHostname, err := os.Hostname() |
| 444 | if err != nil { |
| 445 | grpclog.Warningf("grpc: failed to get client hostname due to %v.\n", err) |
| 446 | return "" |
| 447 | } |
| 448 | var sc string |
| 449 | for _, c := range rcs { |
| 450 | if !containsString(c.ClientLanguage, golang) || |
| 451 | !chosenByPercentage(c.Percentage) || |
| 452 | !containsString(c.ClientHostName, cliHostname) || |
| 453 | c.ServiceConfig == nil { |
| 454 | continue |
| 455 | } |
| 456 | sc = string(*c.ServiceConfig) |
| 457 | break |
| 458 | } |
| 459 | return sc |
| 460 | } |