dns_resolver.go 13 KB

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