func delService(t *testing.T, s *server, k string) { path, _ := msg.PathWithWildcard(k) _, err := s.client.Delete(path, false) if err != nil { t.Fatal(err) } }
func delService(t *testing.T, s *server, k string) { path, _ := msg.PathWithWildcard(k) _, err := s.backend.(*backendetcd.Backend).Client().Delete(ctx, path, &etcd.DeleteOptions{Recursive: false}) if err != nil { t.Fatal(err) } }
func delService(t *testing.T, s *server, k string) { path, _ := msg.PathWithWildcard(k) _, err := s.backend.(*backendetcd.Backend).Client().Delete(path, false) if err != nil { t.Fatal(err) } }
func (s *server) PTRRecords(q dns.Question) (records []dns.RR, err error) { name := strings.ToLower(q.Name) path, star := msg.PathWithWildcard(name) if star { return nil, fmt.Errorf("reverse can not contain wildcards") } r, err := get(s.client, path, false) if err != nil { // if server has a forward, forward the query return nil, err } if r.Node.Dir { return nil, fmt.Errorf("reverse should not be a directory") } serv := new(msg.Service) if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil { s.config.log.Infof("failed to parse json: %s", err.Error()) return nil, err } ttl := uint32(r.Node.TTL) if ttl == 0 { ttl = s.config.Ttl } serv.Key = r.Node.Key // If serv.Host is parseble as a IP address we should not return anything. // TODO(miek). records = append(records, serv.NewPTR(q.Name, ttl)) return records, nil }
func (g *Backendv3) Records(name string, exact bool) ([]msg.Service, error) { path, star := msg.PathWithWildcard(name) r, err := g.get(path, true) if err != nil { return nil, err } segments := strings.Split(msg.Path(name), "/") return g.loopNodes(r.Kvs, segments, star, nil) }
func (g *Backend) Records(name string, exact bool) ([]msg.Service, error) { path, star := msg.PathWithWildcard(name) bits := strings.Split(path, "/") fmt.Println(bits, path, star) srv := msg.Service{} srv.Host = "192.168.5.1" l := make([]msg.Service, 0) l = append(l, srv) return l, nil // errors.New("FAIL") }
func (d *Domain) Records(name string, exact bool) ([]msg.Service, error) { path, _ := msg.PathWithWildcard(name) logger.Debug("fetch record %s", path) val, ok := d.entries[path] if ok { l := make([]msg.Service, 1) l[0] = *val return l, nil } return nil, errors.New("FAIL") }
// NSRecords returns NS records from etcd. func (s *server) NSRecords(q dns.Question, name string) (records []dns.RR, extra []dns.RR, err error) { path, star := msg.PathWithWildcard(name) r, err := get(s.client, path, true) if err != nil { return nil, nil, err } if !r.Node.Dir { // single element serv := new(msg.Service) if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil { s.config.log.Infof("failed to parse json: %s", err.Error()) return nil, nil, err } ip := net.ParseIP(serv.Host) ttl := s.calculateTtl(r.Node, serv) serv.Key = r.Node.Key serv.Ttl = ttl switch { case ip == nil: return nil, nil, fmt.Errorf("NS record must be an IP address") case ip.To4() != nil: serv.Host = msg.Domain(serv.Key) records = append(records, serv.NewNS(q.Name, serv.Host)) extra = append(extra, serv.NewA(serv.Host, ip.To4())) case ip.To4() == nil: serv.Host = msg.Domain(serv.Key) records = append(records, serv.NewNS(q.Name, serv.Host)) extra = append(extra, serv.NewAAAA(serv.Host, ip.To16())) } return records, extra, nil } sx, err := s.loopNodes(&r.Node.Nodes, strings.Split(msg.Path(name), "/"), star, nil) if err != nil || len(sx) == 0 { return nil, nil, err } for _, serv := range sx { ip := net.ParseIP(serv.Host) switch { case ip == nil: return nil, nil, fmt.Errorf("NS record must be an IP address") case ip.To4() != nil: serv.Host = msg.Domain(serv.Key) records = append(records, serv.NewNS(q.Name, serv.Host)) extra = append(extra, serv.NewA(serv.Host, ip.To4())) case ip.To4() == nil: serv.Host = msg.Domain(serv.Key) records = append(records, serv.NewNS(q.Name, serv.Host)) extra = append(extra, serv.NewAAAA(serv.Host, ip.To16())) } } return records, extra, nil }
func addService(t *testing.T, s *server, k string, ttl uint64, m *msg.Service) { b, err := json.Marshal(m) if err != nil { t.Fatal(err) } path, _ := msg.PathWithWildcard(k) _, err = s.backend.(*backendetcd.Backend).Client().Create(path, string(b), ttl) if err != nil { // TODO(miek): allow for existing keys... t.Fatal(err) } }
func addService(t *testing.T, s *server, k string, ttl time.Duration, m *msg.Service) { b, err := json.Marshal(m) if err != nil { t.Fatal(err) } path, _ := msg.PathWithWildcard(k) _, err = s.backend.(*backendetcd.Backend).Client().Set(ctx, path, string(b), &etcd.SetOptions{TTL: ttl}) if err != nil { // TODO(miek): allow for existing keys... t.Fatal(err) } }
func addService(t *testing.T, s *server, k string, ttl uint64, m *msg.Service) { b, err := json.Marshal(m) if err != nil { t.Fatal(err) } path, _ := msg.PathWithWildcard(k) t.Logf("Adding path %s:", path) _, err = s.client.Create(path, string(b), ttl) if err != nil { // TODO(miek): allow for existing keys... t.Fatal(err) } }
func (g *Backend) Records(name string, exact bool) ([]msg.Service, error) { path, star := msg.PathWithWildcard(name) r, err := g.get(path, true) if err != nil { return nil, err } segments := strings.Split(msg.Path(name), "/") switch { case exact && r.Node.Dir: return nil, nil case r.Node.Dir: return g.loopNodes(r.Node.Nodes, segments, star, nil) default: return g.loopNodes([]*etcd.Node{r.Node}, segments, false, nil) } }
func (g *Backendv3) ReverseRecord(name string) (*msg.Service, error) { path, star := msg.PathWithWildcard(name) if star { return nil, fmt.Errorf("reverse can not contain wildcards") } r, err := g.get(path, true) if err != nil { return nil, err } segments := strings.Split(msg.Path(name), "/") records, err := g.loopNodes(r.Kvs, segments, false, nil) if err != nil { return nil, err } if len(records) != 1 { return nil, fmt.Errorf("must be only one service record") } return &records[0], nil }
func (g *Backend) ReverseRecord(name string, rmtIP net.IP) (*msg.Service, error) { path, star := msg.PathWithWildcard(name) if star { return nil, fmt.Errorf("reverse can not contain wildcards") } r, err := g.get(path, true) if err != nil { return nil, err } if r.Node.Dir { return nil, fmt.Errorf("reverse must not be a directory") } segments := strings.Split(msg.Path(name), "/") records, err := g.loopNodes(&etcd.Nodes{r.Node}, segments, false, nil, rmtIP) if err != nil { return nil, err } if len(records) != 1 { return nil, fmt.Errorf("must be only one service record") } return &records[0], nil }
func (s *server) CNAMERecords(q dns.Question, name string) (records []dns.RR, err error) { path, _ := msg.PathWithWildcard(name) // no wildcards here r, err := get(s.client, path, true) if err != nil { return nil, err } if !r.Node.Dir { serv := new(msg.Service) if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil { s.config.log.Infof("failed to parse json: %s", err.Error()) return nil, err } ip := net.ParseIP(serv.Host) ttl := s.calculateTtl(r.Node, serv) serv.Key = r.Node.Key serv.Ttl = ttl if ip == nil { records = append(records, serv.NewCNAME(q.Name, dns.Fqdn(serv.Host))) } } return records, nil }
func (g *Backend) Records(name string, exact bool, rmtIP net.IP) ([]msg.Service, error) { path, star := msg.PathWithWildcard(name) r, err := g.get(path, true) if err != nil { return nil, err } segments := strings.Split(msg.Path(name), "/") switch { case exact && r.Node.Dir: return nil, nil case r.Node.Dir: if _, err := g.get(fmt.Sprintf("%s/.wildcards", path), true); err != nil { return nil, err } if r2, err := g.get(fmt.Sprintf("%s/.self", path), true); err != nil { return g.loopNodes(&r.Node.Nodes, segments, star, nil, rmtIP) } else { return g.loopNodes(&etcd.Nodes{r2.Node}, segments, false, nil, rmtIP) } default: return g.loopNodes(&etcd.Nodes{r.Node}, segments, false, nil, rmtIP) } }
func (s *server) AddressRecords(q dns.Question, name string, previousRecords []dns.RR) (records []dns.RR, err error) { path, star := msg.PathWithWildcard(name) r, err := get(s.client, path, true) if err != nil { return nil, err } if !r.Node.Dir { // single element serv := new(msg.Service) if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil { s.config.log.Infof("failed to parse json: %s", err.Error()) return nil, err } ip := net.ParseIP(serv.Host) ttl := s.calculateTtl(r.Node, serv) serv.Ttl = ttl serv.Key = r.Node.Key switch { case ip == nil: // Try to resolve as CNAME if it's not an IP. newRecord := serv.NewCNAME(q.Name, dns.Fqdn(serv.Host)) if len(previousRecords) > 7 { s.config.log.Errorf("CNAME lookup limit of 8 exceeded for %s", newRecord) return nil, fmt.Errorf("exceeded CNAME lookup limit") } if s.isDuplicateCNAME(newRecord, previousRecords) { s.config.log.Errorf("CNAME loop detected for record %s", newRecord) return nil, fmt.Errorf("detected CNAME loop") } records = append(records, newRecord) nextRecords, err := s.AddressRecords(dns.Question{Name: dns.Fqdn(serv.Host), Qtype: q.Qtype, Qclass: q.Qclass}, strings.ToLower(dns.Fqdn(serv.Host)), append(previousRecords, newRecord)) if err != nil { // This means we can not complete the CNAME, this is OK, but // if we return an error this will trigger an NXDOMAIN. // We also don't want to return the CNAME, because of the // no other data rule. So return nothing and let NODATA // kick in (via a hack). return records, fmt.Errorf("incomplete CNAME chain") } records = append(records, nextRecords...) case ip.To4() != nil && q.Qtype == dns.TypeA: records = append(records, serv.NewA(q.Name, ip.To4())) case ip.To4() == nil && q.Qtype == dns.TypeAAAA: records = append(records, serv.NewAAAA(q.Name, ip.To16())) } return records, nil } nodes, err := s.loopNodes(&r.Node.Nodes, strings.Split(msg.Path(name), "/"), star, nil) if err != nil { s.config.log.Infof("failed to parse json: %s", err.Error()) return nil, err } for _, serv := range nodes { ip := net.ParseIP(serv.Host) switch { case ip == nil: // TODO: deduplicate with above code // Try to resolve as CNAME if it's not an IP. newRecord := serv.NewCNAME(q.Name, dns.Fqdn(serv.Host)) if len(previousRecords) > 7 { s.config.log.Errorf("CNAME lookup limit of 8 exceeded for %s", newRecord) return nil, fmt.Errorf("exceeded CNAME lookup limit") } if s.isDuplicateCNAME(newRecord, previousRecords) { s.config.log.Errorf("CNAME loop detected for record %s", newRecord) return nil, fmt.Errorf("detected CNAME loop") } records = append(records, newRecord) nextRecords, err := s.AddressRecords(dns.Question{Name: dns.Fqdn(serv.Host), Qtype: q.Qtype, Qclass: q.Qclass}, strings.ToLower(dns.Fqdn(serv.Host)), append(previousRecords, newRecord)) if err != nil { // This means we can not complete the CNAME, this is OK, but // if we return an error this will trigger an NXDOMAIN. // We also don't want to return the CNAME, because of the // no other data rule. So return nothing and let NODATA // kick in (via a hack). return records, fmt.Errorf("incomplete CNAME chain") } records = append(records, nextRecords...) case ip.To4() != nil && q.Qtype == dns.TypeA: records = append(records, serv.NewA(q.Name, ip.To4())) case ip.To4() == nil && q.Qtype == dns.TypeAAAA: records = append(records, serv.NewAAAA(q.Name, ip.To16())) } } if s.config.RoundRobin { switch l := len(records); l { case 2: if dns.Id()%2 == 0 { records[0], records[1] = records[1], records[0] } default: // Do a minimum of l swap, maximum of 4l swaps for j := 0; j < l*(int(dns.Id())%4+1); j++ { q := int(dns.Id()) % l p := int(dns.Id()) % l if q == p { p = (p + 1) % l } records[q], records[p] = records[p], records[q] } } } return records, nil }
func (g *Backend) ReverseRecord(name string) (*msg.Service, error) { path, star := msg.PathWithWildcard(name) fmt.Println(path, star) return nil, errors.New("FAIL REVERSE") }
// SRVRecords returns SRV records from etcd. // If the Target is not an name but an IP address, an name is created . func (s *server) SRVRecords(q dns.Question, name string, bufsize uint16, dnssec bool) (records []dns.RR, extra []dns.RR, err error) { path, star := msg.PathWithWildcard(name) r, err := get(s.client, path, true) if err != nil { return nil, nil, err } if !r.Node.Dir { // single element serv := new(msg.Service) if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil { s.config.log.Infof("failed to parse json: %s", err.Error()) return nil, nil, err } ip := net.ParseIP(serv.Host) ttl := s.calculateTtl(r.Node, serv) if serv.Priority == 0 { serv.Priority = int(s.config.Priority) } serv.Key = r.Node.Key serv.Ttl = ttl switch { case ip == nil: srv := serv.NewSRV(q.Name, uint16(100)) records = append(records, srv) if !dns.IsSubDomain(s.config.Domain, srv.Target) { m1, e1 := s.Lookup(srv.Target, dns.TypeA, bufsize, dnssec) if e1 == nil { extra = append(extra, m1.Answer...) } m1, e1 = s.Lookup(srv.Target, dns.TypeAAAA, bufsize, dnssec) if e1 == nil { // If we have seen CNAME's we *assume* that they already added. for _, a := range m1.Answer { if _, ok := a.(*dns.CNAME); !ok { extra = append(extra, a) } } } } case ip.To4() != nil: serv.Host = msg.Domain(serv.Key) records = append(records, serv.NewSRV(q.Name, uint16(100))) extra = append(extra, serv.NewA(serv.Host, ip.To4())) case ip.To4() == nil: serv.Host = msg.Domain(serv.Key) records = append(records, serv.NewSRV(q.Name, uint16(100))) extra = append(extra, serv.NewAAAA(serv.Host, ip.To16())) } return records, extra, nil } sx, err := s.loopNodes(&r.Node.Nodes, strings.Split(msg.Path(name), "/"), star, nil) if err != nil || len(sx) == 0 { return nil, nil, err } // Looping twice to get the right weight vs priority w := make(map[int]int) for _, serv := range sx { weight := 100 if serv.Weight != 0 { weight = serv.Weight } if _, ok := w[serv.Priority]; !ok { w[serv.Priority] = weight continue } w[serv.Priority] += weight } lookup := make(map[string]bool) for _, serv := range sx { w1 := 100.0 / float64(w[serv.Priority]) if serv.Weight == 0 { w1 *= 100 } else { w1 *= float64(serv.Weight) } weight := uint16(math.Floor(w1)) ip := net.ParseIP(serv.Host) switch { case ip == nil: srv := serv.NewSRV(q.Name, weight) records = append(records, srv) if _, ok := lookup[srv.Target]; !ok { if !dns.IsSubDomain(s.config.Domain, srv.Target) { m1, e1 := s.Lookup(srv.Target, dns.TypeA, bufsize, dnssec) if e1 == nil { extra = append(extra, m1.Answer...) } m1, e1 = s.Lookup(srv.Target, dns.TypeAAAA, bufsize, dnssec) if e1 == nil { // If we have seen CNAME's we *assume* that they are already added. for _, a := range m1.Answer { if _, ok := a.(*dns.CNAME); !ok { extra = append(extra, a) } } } } } lookup[srv.Target] = true case ip.To4() != nil: serv.Host = msg.Domain(serv.Key) records = append(records, serv.NewSRV(q.Name, weight)) extra = append(extra, serv.NewA(serv.Host, ip.To4())) case ip.To4() == nil: serv.Host = msg.Domain(serv.Key) records = append(records, serv.NewSRV(q.Name, weight)) extra = append(extra, serv.NewAAAA(serv.Host, ip.To16())) } } return records, extra, nil }