setup.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package dns
  15. import (
  16. "context"
  17. "github.com/coredns/caddy"
  18. "github.com/coredns/coredns/core/dnsserver"
  19. "github.com/coredns/coredns/plugin"
  20. "github.com/coredns/coredns/plugin/pkg/fall"
  21. clog "github.com/coredns/coredns/plugin/pkg/log"
  22. "github.com/coredns/coredns/plugin/pkg/upstream"
  23. "github.com/miekg/dns"
  24. "yunion.io/x/pkg/errors"
  25. "yunion.io/x/pkg/util/regutils"
  26. "yunion.io/x/onecloud/pkg/apis"
  27. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  28. )
  29. var log = clog.NewWithPlugin(PluginName)
  30. func init() {
  31. plugin.Register(PluginName, setup)
  32. }
  33. func setup(c *caddy.Controller) error {
  34. for c.Next() {
  35. log.Infof("setup: %s", PluginName)
  36. var fallF fall.F
  37. upStream := upstream.New()
  38. rDNS := New()
  39. rDNS.Zones = c.RemainingArgs()
  40. if len(rDNS.Zones) == 0 {
  41. rDNS.Zones = make([]string, len(c.ServerBlockKeys))
  42. copy(rDNS.Zones, c.ServerBlockKeys)
  43. }
  44. for i, str := range rDNS.Zones {
  45. rDNS.Zones[i] = plugin.Host(str).Normalize()
  46. }
  47. for c.NextBlock() {
  48. switch c.Val() {
  49. case "dns_domain":
  50. if !c.NextArg() {
  51. return plugin.Error(PluginName+".dns_domain", c.ArgErr())
  52. }
  53. rDNS.PrimaryZone = c.Val()
  54. case "sql_connection":
  55. if !c.NextArg() {
  56. return plugin.Error(PluginName+".sql_connection", c.ArgErr())
  57. }
  58. rDNS.SqlConnection = c.Val()
  59. case "auth_url":
  60. if !c.NextArg() {
  61. return plugin.Error(PluginName+".auth_url", c.ArgErr())
  62. }
  63. rDNS.AuthUrl = c.Val()
  64. case "admin_project":
  65. if !c.NextArg() {
  66. return plugin.Error(PluginName+".admin_project", c.ArgErr())
  67. }
  68. rDNS.AdminProject = c.Val()
  69. case "admin_project_domain":
  70. if !c.NextArg() {
  71. return plugin.Error(PluginName+".admin_project_domain", c.ArgErr())
  72. }
  73. rDNS.AdminProjectDomain = c.Val()
  74. case "admin_user":
  75. if !c.NextArg() {
  76. return plugin.Error(PluginName+".admin_user", c.ArgErr())
  77. }
  78. rDNS.AdminUser = c.Val()
  79. case "admin_domain":
  80. if !c.NextArg() {
  81. return plugin.Error(PluginName+".admin_domain", c.ArgErr())
  82. }
  83. rDNS.AdminDomain = c.Val()
  84. case "admin_password":
  85. if !c.NextArg() {
  86. return plugin.Error(PluginName+".admin_password", c.ArgErr())
  87. }
  88. rDNS.AdminPassword = c.Val()
  89. case "region":
  90. if !c.NextArg() {
  91. return plugin.Error(PluginName+".region", c.ArgErr())
  92. }
  93. rDNS.Region = c.Val()
  94. case "in_cloud_only":
  95. rDNS.InCloudOnly = true
  96. case "upstream":
  97. c.RemainingArgs()
  98. case "fallthrough":
  99. fallF.SetZonesFromArgs(c.RemainingArgs())
  100. default:
  101. return plugin.Error(PluginName, c.Errf("unknown property %q", c.Val()))
  102. }
  103. }
  104. ctx, cancel := context.WithCancel(context.Background())
  105. rDNS.initAuth()
  106. conf, err := identity.ServicesV3.GetConfig(rDNS.getAdminSession(ctx), apis.SERVICE_TYPE_REGION)
  107. if err != nil {
  108. cancel()
  109. return errors.Wrap(err, "GetConfig")
  110. }
  111. if conf.Contains("dns_domain") {
  112. rDNS.PrimaryZone, _ = conf.GetString("dns_domain")
  113. }
  114. log.Infof("use dns_domain %q", rDNS.PrimaryZone)
  115. if len(rDNS.PrimaryZone) > 0 && !regutils.MatchDomainName(rDNS.PrimaryZone) {
  116. cancel()
  117. return errors.Wrapf(errors.ErrInvalidFormat, "dns_domain %q invalid", rDNS.PrimaryZone)
  118. }
  119. if len(rDNS.PrimaryZone) > 0 {
  120. if r := rDNS.PrimaryZone[len(rDNS.PrimaryZone)-1]; r != '.' {
  121. rDNS.PrimaryZone += "."
  122. }
  123. } else {
  124. rDNS.PrimaryZone = ""
  125. }
  126. rDNS.primaryZoneLabelCount = dns.CountLabel(rDNS.PrimaryZone)
  127. err = rDNS.initDB(c)
  128. if err != nil {
  129. cancel()
  130. return plugin.Error(PluginName, err)
  131. }
  132. rDNS.Fall = fallF
  133. rDNS.Upstream = upStream
  134. dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
  135. rDNS.Next = next
  136. return rDNS
  137. })
  138. c.OnShutdown(func() error { cancel(); return nil })
  139. }
  140. return nil
  141. }