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