2019-04-04 15:05:35 +00:00
|
|
|
// Copyright 2019 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-11-03 12:15:51 +00:00
|
|
|
"errors"
|
2024-09-10 01:41:53 +00:00
|
|
|
"log/slog"
|
2019-04-04 15:05:35 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2023-10-23 13:55:36 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-10-22 09:00:08 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-29 09:43:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-27 08:38:08 +00:00
|
|
|
"go.uber.org/goleak"
|
2019-09-23 11:26:12 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2019-04-04 15:05:35 +00:00
|
|
|
|
2024-01-23 15:53:55 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2019-04-04 15:05:35 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
|
|
)
|
|
|
|
|
2020-07-27 08:38:08 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2019-04-04 15:05:35 +00:00
|
|
|
func TestDNS(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
config SDConfig
|
2024-09-10 01:41:53 +00:00
|
|
|
lookup func(name string, qtype uint16, logger *slog.Logger) (*dns.Msg, error)
|
2019-04-04 15:05:35 +00:00
|
|
|
|
|
|
|
expected []*targetgroup.Group
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "A record query with error",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"web.example.com."},
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
Port: 80,
|
|
|
|
Type: "A",
|
|
|
|
},
|
2024-09-10 01:41:53 +00:00
|
|
|
lookup: func(name string, qtype uint16, logger *slog.Logger) (*dns.Msg, error) {
|
2024-11-03 12:15:51 +00:00
|
|
|
return nil, errors.New("some error")
|
2019-04-04 15:05:35 +00:00
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "A record query",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"web.example.com."},
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
Port: 80,
|
|
|
|
Type: "A",
|
|
|
|
},
|
2024-09-10 01:41:53 +00:00
|
|
|
lookup: func(name string, qtype uint16, logger *slog.Logger) (*dns.Msg, error) {
|
2019-04-04 15:05:35 +00:00
|
|
|
return &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.A{A: net.IPv4(192, 0, 2, 2)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
2019-08-13 08:34:14 +00:00
|
|
|
{
|
2019-04-04 15:05:35 +00:00
|
|
|
Source: "web.example.com.",
|
|
|
|
Targets: []model.LabelSet{
|
2020-07-28 20:09:01 +00:00
|
|
|
{
|
|
|
|
"__address__": "192.0.2.2:80",
|
|
|
|
"__meta_dns_name": "web.example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "",
|
|
|
|
"__meta_dns_srv_record_port": "",
|
2022-08-03 09:19:26 +00:00
|
|
|
"__meta_dns_mx_record_target": "",
|
2023-11-29 17:19:02 +00:00
|
|
|
"__meta_dns_ns_record_target": "",
|
2020-07-28 20:09:01 +00:00
|
|
|
},
|
2019-04-04 15:05:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "AAAA record query",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"web.example.com."},
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
Port: 80,
|
|
|
|
Type: "AAAA",
|
|
|
|
},
|
2024-09-10 01:41:53 +00:00
|
|
|
lookup: func(name string, qtype uint16, logger *slog.Logger) (*dns.Msg, error) {
|
2019-04-04 15:05:35 +00:00
|
|
|
return &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.AAAA{AAAA: net.IPv6loopback},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
2019-08-13 08:34:14 +00:00
|
|
|
{
|
2019-04-04 15:05:35 +00:00
|
|
|
Source: "web.example.com.",
|
|
|
|
Targets: []model.LabelSet{
|
2020-07-28 20:09:01 +00:00
|
|
|
{
|
|
|
|
"__address__": "[::1]:80",
|
|
|
|
"__meta_dns_name": "web.example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "",
|
|
|
|
"__meta_dns_srv_record_port": "",
|
2022-08-03 09:19:26 +00:00
|
|
|
"__meta_dns_mx_record_target": "",
|
2023-11-29 17:19:02 +00:00
|
|
|
"__meta_dns_ns_record_target": "",
|
2020-07-28 20:09:01 +00:00
|
|
|
},
|
2019-04-04 15:05:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SRV record query",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"_mysql._tcp.db.example.com."},
|
2019-09-23 11:26:12 +00:00
|
|
|
Type: "SRV",
|
2019-04-04 15:05:35 +00:00
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
},
|
2024-09-10 01:41:53 +00:00
|
|
|
lookup: func(name string, qtype uint16, logger *slog.Logger) (*dns.Msg, error) {
|
2019-04-04 15:05:35 +00:00
|
|
|
return &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.SRV{Port: 3306, Target: "db1.example.com."},
|
|
|
|
&dns.SRV{Port: 3306, Target: "db2.example.com."},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
2019-08-13 08:34:14 +00:00
|
|
|
{
|
2019-04-04 15:05:35 +00:00
|
|
|
Source: "_mysql._tcp.db.example.com.",
|
|
|
|
Targets: []model.LabelSet{
|
2020-07-28 20:09:01 +00:00
|
|
|
{
|
|
|
|
"__address__": "db1.example.com:3306",
|
|
|
|
"__meta_dns_name": "_mysql._tcp.db.example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "db1.example.com.",
|
|
|
|
"__meta_dns_srv_record_port": "3306",
|
2022-08-03 09:19:26 +00:00
|
|
|
"__meta_dns_mx_record_target": "",
|
2023-11-29 17:19:02 +00:00
|
|
|
"__meta_dns_ns_record_target": "",
|
2020-07-28 20:09:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": "db2.example.com:3306",
|
|
|
|
"__meta_dns_name": "_mysql._tcp.db.example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "db2.example.com.",
|
|
|
|
"__meta_dns_srv_record_port": "3306",
|
2022-08-03 09:19:26 +00:00
|
|
|
"__meta_dns_mx_record_target": "",
|
2023-11-29 17:19:02 +00:00
|
|
|
"__meta_dns_ns_record_target": "",
|
2020-07-28 20:09:01 +00:00
|
|
|
},
|
2019-04-04 15:05:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SRV record query with unsupported resource records",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"_mysql._tcp.db.example.com."},
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
},
|
2024-09-10 01:41:53 +00:00
|
|
|
lookup: func(name string, qtype uint16, logger *slog.Logger) (*dns.Msg, error) {
|
2019-04-04 15:05:35 +00:00
|
|
|
return &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.SRV{Port: 3306, Target: "db1.example.com."},
|
|
|
|
&dns.TXT{Txt: []string{"this should be discarded"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
2019-08-13 08:34:14 +00:00
|
|
|
{
|
2019-04-04 15:05:35 +00:00
|
|
|
Source: "_mysql._tcp.db.example.com.",
|
|
|
|
Targets: []model.LabelSet{
|
2020-07-28 20:09:01 +00:00
|
|
|
{
|
|
|
|
"__address__": "db1.example.com:3306",
|
|
|
|
"__meta_dns_name": "_mysql._tcp.db.example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "db1.example.com.",
|
|
|
|
"__meta_dns_srv_record_port": "3306",
|
2022-08-03 09:19:26 +00:00
|
|
|
"__meta_dns_mx_record_target": "",
|
2023-11-29 17:19:02 +00:00
|
|
|
"__meta_dns_ns_record_target": "",
|
2020-07-28 20:09:01 +00:00
|
|
|
},
|
2019-04-04 15:05:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SRV record query with empty answer (NXDOMAIN)",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"_mysql._tcp.db.example.com."},
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
},
|
2024-09-10 01:41:53 +00:00
|
|
|
lookup: func(name string, qtype uint16, logger *slog.Logger) (*dns.Msg, error) {
|
2019-04-04 15:05:35 +00:00
|
|
|
return &dns.Msg{}, nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
2019-08-13 08:34:14 +00:00
|
|
|
{
|
2019-04-04 15:05:35 +00:00
|
|
|
Source: "_mysql._tcp.db.example.com.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-03 09:19:26 +00:00
|
|
|
{
|
|
|
|
name: "MX record query",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"example.com."},
|
|
|
|
Type: "MX",
|
|
|
|
Port: 25,
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
},
|
2024-09-10 01:41:53 +00:00
|
|
|
lookup: func(name string, qtype uint16, logger *slog.Logger) (*dns.Msg, error) {
|
2022-08-03 09:19:26 +00:00
|
|
|
return &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.MX{Preference: 0, Mx: "smtp1.example.com."},
|
|
|
|
&dns.MX{Preference: 10, Mx: "smtp2.example.com."},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
|
|
|
{
|
|
|
|
Source: "example.com.",
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
"__address__": "smtp1.example.com:25",
|
|
|
|
"__meta_dns_name": "example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "",
|
|
|
|
"__meta_dns_srv_record_port": "",
|
|
|
|
"__meta_dns_mx_record_target": "smtp1.example.com.",
|
2023-11-29 17:19:02 +00:00
|
|
|
"__meta_dns_ns_record_target": "",
|
2022-08-03 09:19:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": "smtp2.example.com:25",
|
|
|
|
"__meta_dns_name": "example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "",
|
|
|
|
"__meta_dns_srv_record_port": "",
|
|
|
|
"__meta_dns_mx_record_target": "smtp2.example.com.",
|
2023-11-29 17:19:02 +00:00
|
|
|
"__meta_dns_ns_record_target": "",
|
2022-08-03 09:19:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-04 15:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2024-01-23 15:53:55 +00:00
|
|
|
|
|
|
|
reg := prometheus.NewRegistry()
|
|
|
|
refreshMetrics := discovery.NewRefreshMetrics(reg)
|
|
|
|
metrics := tc.config.NewDiscovererMetrics(reg, refreshMetrics)
|
|
|
|
require.NoError(t, metrics.Register())
|
|
|
|
|
|
|
|
sd, err := NewDiscovery(tc.config, nil, metrics)
|
2023-10-23 13:55:36 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-04 15:05:35 +00:00
|
|
|
sd.lookupFn = tc.lookup
|
|
|
|
|
|
|
|
tgs, err := sd.refresh(context.Background())
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, tgs)
|
2024-01-23 15:53:55 +00:00
|
|
|
|
|
|
|
metrics.Unregister()
|
2019-04-04 15:05:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-09-23 11:26:12 +00:00
|
|
|
|
|
|
|
func TestSDConfigUnmarshalYAML(t *testing.T) {
|
|
|
|
marshal := func(c SDConfig) []byte {
|
|
|
|
d, err := yaml.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
unmarshal := func(d []byte) func(interface{}) error {
|
|
|
|
return func(o interface{}) error {
|
|
|
|
return yaml.Unmarshal(d, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
input SDConfig
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid srv",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "SRV",
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid a",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "A",
|
|
|
|
Port: 5300,
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid aaaa",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "AAAA",
|
|
|
|
Port: 5300,
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid a without port",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "A",
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid aaaa without port",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "AAAA",
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid empty names",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{},
|
|
|
|
Type: "AAAA",
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2019-09-25 07:51:43 +00:00
|
|
|
name: "invalid unknown dns type",
|
2019-09-23 11:26:12 +00:00
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "PTR",
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
var config SDConfig
|
|
|
|
d := marshal(c.input)
|
|
|
|
err := config.UnmarshalYAML(unmarshal(d))
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, c.expectErr, err != nil)
|
2019-09-23 11:26:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|