dns_resolver.go 12 KB

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