dns_resolver.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. // Package dns implements a dns resolver to be installed as the default resolver
  19. // in grpc.
  20. package dns
  21. import (
  22. "context"
  23. "encoding/json"
  24. "errors"
  25. "fmt"
  26. "net"
  27. "os"
  28. "strconv"
  29. "strings"
  30. "sync"
  31. "time"
  32. grpclbstate "google.golang.org/grpc/balancer/grpclb/state"
  33. "google.golang.org/grpc/grpclog"
  34. "google.golang.org/grpc/internal/backoff"
  35. "google.golang.org/grpc/internal/envconfig"
  36. "google.golang.org/grpc/internal/grpcrand"
  37. "google.golang.org/grpc/resolver"
  38. "google.golang.org/grpc/serviceconfig"
  39. )
  40. // EnableSRVLookups controls whether the DNS resolver attempts to fetch gRPCLB
  41. // addresses from SRV records. Must not be changed after init time.
  42. var EnableSRVLookups = false
  43. var logger = grpclog.Component("dns")
  44. // Globals to stub out in tests. TODO: Perhaps these two can be combined into a
  45. // single variable for testing the resolver?
  46. var (
  47. newTimer = time.NewTimer
  48. newTimerDNSResRate = time.NewTimer
  49. )
  50. func init() {
  51. resolver.Register(NewBuilder())
  52. }
  53. const (
  54. defaultPort = "443"
  55. defaultDNSSvrPort = "53"
  56. golang = "GO"
  57. // txtPrefix is the prefix string to be prepended to the host name for txt
  58. // record lookup.
  59. txtPrefix = "_grpc_config."
  60. // In DNS, service config is encoded in a TXT record via the mechanism
  61. // described in RFC-1464 using the attribute name grpc_config.
  62. txtAttribute = "grpc_config="
  63. )
  64. var (
  65. errMissingAddr = errors.New("dns resolver: missing address")
  66. // Addresses ending with a colon that is supposed to be the separator
  67. // between host and port is not allowed. E.g. "::" is a valid address as
  68. // it is an IPv6 address (host only) and "[::]:" is invalid as it ends with
  69. // a colon as the host and port separator
  70. errEndsWithColon = errors.New("dns resolver: missing port after port-separator colon")
  71. )
  72. var (
  73. defaultResolver netResolver = net.DefaultResolver
  74. // To prevent excessive re-resolution, we enforce a rate limit on DNS
  75. // resolution requests.
  76. minDNSResRate = 30 * time.Second
  77. )
  78. var addressDialer = func(address string) func(context.Context, string, string) (net.Conn, error) {
  79. return func(ctx context.Context, network, _ string) (net.Conn, error) {
  80. var dialer net.Dialer
  81. return dialer.DialContext(ctx, network, address)
  82. }
  83. }
  84. var newNetResolver = func(authority string) (netResolver, error) {
  85. host, port, err := parseTarget(authority, defaultDNSSvrPort)
  86. if err != nil {
  87. return nil, err
  88. }
  89. authorityWithPort := net.JoinHostPort(host, port)
  90. return &net.Resolver{
  91. PreferGo: true,
  92. Dial: addressDialer(authorityWithPort),
  93. }, nil
  94. }
  95. // NewBuilder creates a dnsBuilder which is used to factory DNS resolvers.
  96. func NewBuilder() resolver.Builder {
  97. return &dnsBuilder{}
  98. }
  99. type dnsBuilder struct{}
  100. // Build creates and starts a DNS resolver that watches the name resolution of
  101. // the target.
  102. func (b *dnsBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
  103. host, port, err := parseTarget(target.Endpoint(), defaultPort)
  104. if err != nil {
  105. return nil, err
  106. }
  107. // IP address.
  108. if ipAddr, ok := formatIP(host); ok {
  109. addr := []resolver.Address{{Addr: ipAddr + ":" + port}}
  110. cc.UpdateState(resolver.State{Addresses: addr})
  111. return deadResolver{}, nil
  112. }
  113. // DNS address (non-IP).
  114. ctx, cancel := context.WithCancel(context.Background())
  115. d := &dnsResolver{
  116. host: host,
  117. port: port,
  118. ctx: ctx,
  119. cancel: cancel,
  120. cc: cc,
  121. rn: make(chan struct{}, 1),
  122. disableServiceConfig: opts.DisableServiceConfig,
  123. }
  124. if target.URL.Host == "" {
  125. d.resolver = defaultResolver
  126. } else {
  127. d.resolver, err = newNetResolver(target.URL.Host)
  128. if err != nil {
  129. return nil, err
  130. }
  131. }
  132. d.wg.Add(1)
  133. go d.watcher()
  134. return d, nil
  135. }
  136. // Scheme returns the naming scheme of this resolver builder, which is "dns".
  137. func (b *dnsBuilder) Scheme() string {
  138. return "dns"
  139. }
  140. type netResolver interface {
  141. LookupHost(ctx context.Context, host string) (addrs []string, err error)
  142. LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*net.SRV, err error)
  143. LookupTXT(ctx context.Context, name string) (txts []string, err error)
  144. }
  145. // deadResolver is a resolver that does nothing.
  146. type deadResolver struct{}
  147. func (deadResolver) ResolveNow(resolver.ResolveNowOptions) {}
  148. func (deadResolver) Close() {}
  149. // dnsResolver watches for the name resolution update for a non-IP target.
  150. type dnsResolver struct {
  151. host string
  152. port string
  153. resolver netResolver
  154. ctx context.Context
  155. cancel context.CancelFunc
  156. cc resolver.ClientConn
  157. // rn channel is used by ResolveNow() to force an immediate resolution of the
  158. // target.
  159. rn chan struct{}
  160. // wg is used to enforce Close() to return after the watcher() goroutine has
  161. // finished. Otherwise, data race will be possible. [Race Example] in
  162. // dns_resolver_test we replace the real lookup functions with mocked ones to
  163. // facilitate testing. If Close() doesn't wait for watcher() goroutine
  164. // finishes, race detector sometimes will warns lookup (READ the lookup
  165. // function pointers) inside watcher() goroutine has data race with
  166. // replaceNetFunc (WRITE the lookup function pointers).
  167. wg sync.WaitGroup
  168. disableServiceConfig bool
  169. }
  170. // ResolveNow invoke an immediate resolution of the target that this
  171. // dnsResolver watches.
  172. func (d *dnsResolver) ResolveNow(resolver.ResolveNowOptions) {
  173. select {
  174. case d.rn <- struct{}{}:
  175. default:
  176. }
  177. }
  178. // Close closes the dnsResolver.
  179. func (d *dnsResolver) Close() {
  180. d.cancel()
  181. d.wg.Wait()
  182. }
  183. func (d *dnsResolver) watcher() {
  184. defer d.wg.Done()
  185. backoffIndex := 1
  186. for {
  187. state, err := d.lookup()
  188. if err != nil {
  189. // Report error to the underlying grpc.ClientConn.
  190. d.cc.ReportError(err)
  191. } else {
  192. err = d.cc.UpdateState(*state)
  193. }
  194. var timer *time.Timer
  195. if err == nil {
  196. // Success resolving, wait for the next ResolveNow. However, also wait 30
  197. // seconds at the very least to prevent constantly re-resolving.
  198. backoffIndex = 1
  199. timer = newTimerDNSResRate(minDNSResRate)
  200. select {
  201. case <-d.ctx.Done():
  202. timer.Stop()
  203. return
  204. case <-d.rn:
  205. }
  206. } else {
  207. // Poll on an error found in DNS Resolver or an error received from
  208. // ClientConn.
  209. timer = newTimer(backoff.DefaultExponential.Backoff(backoffIndex))
  210. backoffIndex++
  211. }
  212. select {
  213. case <-d.ctx.Done():
  214. timer.Stop()
  215. return
  216. case <-timer.C:
  217. }
  218. }
  219. }
  220. func (d *dnsResolver) lookupSRV() ([]resolver.Address, error) {
  221. if !EnableSRVLookups {
  222. return nil, nil
  223. }
  224. var newAddrs []resolver.Address
  225. _, srvs, err := d.resolver.LookupSRV(d.ctx, "grpclb", "tcp", d.host)
  226. if err != nil {
  227. err = handleDNSError(err, "SRV") // may become nil
  228. return nil, err
  229. }
  230. for _, s := range srvs {
  231. lbAddrs, err := d.resolver.LookupHost(d.ctx, s.Target)
  232. if err != nil {
  233. err = handleDNSError(err, "A") // may become nil
  234. if err == nil {
  235. // If there are other SRV records, look them up and ignore this
  236. // one that does not exist.
  237. continue
  238. }
  239. return nil, err
  240. }
  241. for _, a := range lbAddrs {
  242. ip, ok := formatIP(a)
  243. if !ok {
  244. return nil, fmt.Errorf("dns: error parsing A record IP address %v", a)
  245. }
  246. addr := ip + ":" + strconv.Itoa(int(s.Port))
  247. newAddrs = append(newAddrs, resolver.Address{Addr: addr, ServerName: s.Target})
  248. }
  249. }
  250. return newAddrs, nil
  251. }
  252. func handleDNSError(err error, lookupType string) error {
  253. dnsErr, ok := err.(*net.DNSError)
  254. if ok && !dnsErr.IsTimeout && !dnsErr.IsTemporary {
  255. // Timeouts and temporary errors should be communicated to gRPC to
  256. // attempt another DNS query (with backoff). Other errors should be
  257. // suppressed (they may represent the absence of a TXT record).
  258. return nil
  259. }
  260. if err != nil {
  261. err = fmt.Errorf("dns: %v record lookup error: %v", lookupType, err)
  262. logger.Info(err)
  263. }
  264. return err
  265. }
  266. func (d *dnsResolver) lookupTXT() *serviceconfig.ParseResult {
  267. ss, err := d.resolver.LookupTXT(d.ctx, txtPrefix+d.host)
  268. if err != nil {
  269. if envconfig.TXTErrIgnore {
  270. return nil
  271. }
  272. if err = handleDNSError(err, "TXT"); err != nil {
  273. return &serviceconfig.ParseResult{Err: err}
  274. }
  275. return nil
  276. }
  277. var res string
  278. for _, s := range ss {
  279. res += s
  280. }
  281. // TXT record must have "grpc_config=" attribute in order to be used as
  282. // service config.
  283. if !strings.HasPrefix(res, txtAttribute) {
  284. logger.Warningf("dns: TXT record %v missing %v attribute", res, txtAttribute)
  285. // This is not an error; it is the equivalent of not having a service
  286. // config.
  287. return nil
  288. }
  289. sc := canaryingSC(strings.TrimPrefix(res, txtAttribute))
  290. return d.cc.ParseServiceConfig(sc)
  291. }
  292. func (d *dnsResolver) lookupHost() ([]resolver.Address, error) {
  293. addrs, err := d.resolver.LookupHost(d.ctx, d.host)
  294. if err != nil {
  295. err = handleDNSError(err, "A")
  296. return nil, err
  297. }
  298. newAddrs := make([]resolver.Address, 0, len(addrs))
  299. for _, a := range addrs {
  300. ip, ok := formatIP(a)
  301. if !ok {
  302. return nil, fmt.Errorf("dns: error parsing A record IP address %v", a)
  303. }
  304. addr := ip + ":" + d.port
  305. newAddrs = append(newAddrs, resolver.Address{Addr: addr})
  306. }
  307. return newAddrs, nil
  308. }
  309. func (d *dnsResolver) lookup() (*resolver.State, error) {
  310. srv, srvErr := d.lookupSRV()
  311. addrs, hostErr := d.lookupHost()
  312. if hostErr != nil && (srvErr != nil || len(srv) == 0) {
  313. return nil, hostErr
  314. }
  315. state := resolver.State{Addresses: addrs}
  316. if len(srv) > 0 {
  317. state = grpclbstate.Set(state, &grpclbstate.State{BalancerAddresses: srv})
  318. }
  319. if !d.disableServiceConfig {
  320. state.ServiceConfig = d.lookupTXT()
  321. }
  322. return &state, nil
  323. }
  324. // formatIP returns ok = false if addr is not a valid textual representation of
  325. // an IP address. If addr is an IPv4 address, return the addr and ok = true.
  326. // If addr is an IPv6 address, return the addr enclosed in square brackets and
  327. // ok = true.
  328. func formatIP(addr string) (addrIP string, ok bool) {
  329. ip := net.ParseIP(addr)
  330. if ip == nil {
  331. return "", false
  332. }
  333. if ip.To4() != nil {
  334. return addr, true
  335. }
  336. return "[" + addr + "]", true
  337. }
  338. // parseTarget takes the user input target string and default port, returns
  339. // formatted host and port info. If target doesn't specify a port, set the port
  340. // to be the defaultPort. If target is in IPv6 format and host-name is enclosed
  341. // in square brackets, brackets are stripped when setting the host.
  342. // examples:
  343. // target: "www.google.com" defaultPort: "443" returns host: "www.google.com", port: "443"
  344. // target: "ipv4-host:80" defaultPort: "443" returns host: "ipv4-host", port: "80"
  345. // target: "[ipv6-host]" defaultPort: "443" returns host: "ipv6-host", port: "443"
  346. // target: ":80" defaultPort: "443" returns host: "localhost", port: "80"
  347. func parseTarget(target, defaultPort string) (host, port string, err error) {
  348. if target == "" {
  349. return "", "", errMissingAddr
  350. }
  351. if ip := net.ParseIP(target); ip != nil {
  352. // target is an IPv4 or IPv6(without brackets) address
  353. return target, defaultPort, nil
  354. }
  355. if host, port, err = net.SplitHostPort(target); err == nil {
  356. if port == "" {
  357. // If the port field is empty (target ends with colon), e.g. "[::1]:",
  358. // this is an error.
  359. return "", "", errEndsWithColon
  360. }
  361. // target has port, i.e ipv4-host:port, [ipv6-host]:port, host-name:port
  362. if host == "" {
  363. // Keep consistent with net.Dial(): If the host is empty, as in ":80",
  364. // the local system is assumed.
  365. host = "localhost"
  366. }
  367. return host, port, nil
  368. }
  369. if host, port, err = net.SplitHostPort(target + ":" + defaultPort); err == nil {
  370. // target doesn't have port
  371. return host, port, nil
  372. }
  373. return "", "", fmt.Errorf("invalid target address %v, error info: %v", target, err)
  374. }
  375. type rawChoice struct {
  376. ClientLanguage *[]string `json:"clientLanguage,omitempty"`
  377. Percentage *int `json:"percentage,omitempty"`
  378. ClientHostName *[]string `json:"clientHostName,omitempty"`
  379. ServiceConfig *json.RawMessage `json:"serviceConfig,omitempty"`
  380. }
  381. func containsString(a *[]string, b string) bool {
  382. if a == nil {
  383. return true
  384. }
  385. for _, c := range *a {
  386. if c == b {
  387. return true
  388. }
  389. }
  390. return false
  391. }
  392. func chosenByPercentage(a *int) bool {
  393. if a == nil {
  394. return true
  395. }
  396. return grpcrand.Intn(100)+1 <= *a
  397. }
  398. func canaryingSC(js string) string {
  399. if js == "" {
  400. return ""
  401. }
  402. var rcs []rawChoice
  403. err := json.Unmarshal([]byte(js), &rcs)
  404. if err != nil {
  405. logger.Warningf("dns: error parsing service config json: %v", err)
  406. return ""
  407. }
  408. cliHostname, err := os.Hostname()
  409. if err != nil {
  410. logger.Warningf("dns: error getting client hostname: %v", err)
  411. return ""
  412. }
  413. var sc string
  414. for _, c := range rcs {
  415. if !containsString(c.ClientLanguage, golang) ||
  416. !chosenByPercentage(c.Percentage) ||
  417. !containsString(c.ClientHostName, cliHostname) ||
  418. c.ServiceConfig == nil {
  419. continue
  420. }
  421. sc = string(*c.ServiceConfig)
  422. break
  423. }
  424. return sc
  425. }