Skip to content

Commit

Permalink
refactor(config): remove GetConfig and its last uses
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Nov 23, 2023
1 parent 23359d1 commit e4be0c0
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 25 deletions.
8 changes: 0 additions & 8 deletions config/config.go
Expand Up @@ -538,14 +538,6 @@ func (cfg *Config) migrate(logger *logrus.Entry) bool {
return usesDepredOpts
}

// GetConfig returns the current config
func GetConfig() *Config {
cfgLock.RLock()
defer cfgLock.RUnlock()

return config
}

// ConvertPort converts string representation into a valid port (0 - 65535)
func ConvertPort(in string) (uint16, error) {
const (
Expand Down
2 changes: 0 additions & 2 deletions config/config_test.go
Expand Up @@ -799,8 +799,6 @@ func defaultTestFileConfig() {
Expect(config.Caching.MinCachingTime).Should(BeZero())

Expect(config.MinTLSServeVer).Should(Equal("1.3"))

Expect(GetConfig()).Should(Not(BeNil()))
}

func writeConfigYml(tmpDir *helpertest.TmpFolder) *helpertest.TmpFile {
Expand Down
2 changes: 1 addition & 1 deletion resolver/bootstrap.go
Expand Up @@ -124,7 +124,7 @@ func (b *Bootstrap) resolveUpstream(ctx context.Context, r Resolver, host string
ctx, cancel := context.WithTimeout(ctx, b.timeout)
defer cancel()

return b.systemResolver.LookupIP(ctx, config.GetConfig().ConnectIPVersion.Net(), host)
return b.systemResolver.LookupIP(ctx, b.connectIPVersion.Net(), host)
}

if ips, ok := b.bootstraped[r]; ok {
Expand Down
10 changes: 5 additions & 5 deletions resolver/strict_resolver_test.go
Expand Up @@ -61,8 +61,6 @@ var _ = Describe("StrictResolver", Label("strictResolver"), func() {
sut, err = NewStrictResolver(ctx, sutConfig, bootstrap)
})

config.GetConfig().Upstreams.Timeout = config.Duration(time.Second)

Describe("IsEnabled", func() {
It("is true", func() {
Expect(sut.IsEnabled()).Should(BeTrue())
Expand Down Expand Up @@ -167,9 +165,10 @@ var _ = Describe("StrictResolver", Label("strictResolver"), func() {
})
When("first upstream exceeds upstreamTimeout", func() {
BeforeEach(func() {
timeout := sut.cfg.Timeout.ToDuration()
testUpstream1 := NewMockUDPUpstreamServer().WithAnswerFn(func(request *dns.Msg) (response *dns.Msg) {
response, err := util.NewMsgWithAnswer("example.com", 123, A, "123.124.122.1")
time.Sleep(time.Duration(config.GetConfig().Upstreams.Timeout) + 2*time.Second)
time.Sleep(2 * timeout)

Expect(err).To(Succeed())

Expand All @@ -195,9 +194,10 @@ var _ = Describe("StrictResolver", Label("strictResolver"), func() {
})
When("all upstreams exceed upsteamTimeout", func() {
BeforeEach(func() {
timeout := sut.cfg.Timeout.ToDuration()
testUpstream1 := NewMockUDPUpstreamServer().WithAnswerFn(func(request *dns.Msg) (response *dns.Msg) {
response, err := util.NewMsgWithAnswer("example.com", 123, A, "123.124.122.1")
time.Sleep(config.GetConfig().Upstreams.Timeout.ToDuration() + 2*time.Second)
time.Sleep(2 * timeout)

Expect(err).To(Succeed())

Expand All @@ -207,7 +207,7 @@ var _ = Describe("StrictResolver", Label("strictResolver"), func() {

testUpstream2 := NewMockUDPUpstreamServer().WithAnswerFn(func(request *dns.Msg) (response *dns.Msg) {
response, err := util.NewMsgWithAnswer("example.com", 123, A, "123.124.122.2")
time.Sleep(config.GetConfig().Upstreams.Timeout.ToDuration() + 2*time.Second)
time.Sleep(2 * timeout)

Expect(err).To(Succeed())

Expand Down
12 changes: 6 additions & 6 deletions server/server.go
Expand Up @@ -56,8 +56,8 @@ func logger() *logrus.Entry {
return log.PrefixedLog("server")
}

func minTLSVersion() uint16 {
minTLSVer := config.GetConfig().MinTLSServeVer
func minTLSVersion(cfg *config.Config) uint16 {
minTLSVer := cfg.MinTLSServeVer
switch minTLSVer {
case "1.2":
return tls.VersionTLS12
Expand Down Expand Up @@ -210,7 +210,7 @@ func createServers(cfg *config.Config, cert tls.Certificate) ([]*dns.Server, err
addServers(createUDPServer, cfg.Ports.DNS),
addServers(createTCPServer, cfg.Ports.DNS),
addServers(func(address string) (*dns.Server, error) {
return createTLSServer(address, cert)
return createTLSServer(cfg, address, cert)
}, cfg.Ports.TLS))

return dnsServers, err.ErrorOrNil()
Expand Down Expand Up @@ -245,14 +245,14 @@ func newListeners(proto string, addresses config.ListenConfig) ([]net.Listener,
return listeners, nil
}

func createTLSServer(address string, cert tls.Certificate) (*dns.Server, error) {
func createTLSServer(cfg *config.Config, address string, cert tls.Certificate) (*dns.Server, error) {
return &dns.Server{
Addr: address,
Net: "tcp-tls",
//nolint:gosec
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: minTLSVersion(),
MinVersion: minTLSVersion(cfg),
CipherSuites: tlsCipherSuites(),
},
Handler: dns.NewServeMux(),
Expand Down Expand Up @@ -524,7 +524,7 @@ func (s *Server) Start(ctx context.Context, errCh chan<- error) {
WriteTimeout: writeTimeout,
//nolint:gosec
TLSConfig: &tls.Config{
MinVersion: minTLSVersion(),
MinVersion: minTLSVersion(s.cfg),
CipherSuites: tlsCipherSuites(),
Certificates: []tls.Certificate{s.cert},
},
Expand Down
3 changes: 0 additions & 3 deletions server/server_test.go
Expand Up @@ -179,9 +179,6 @@ var _ = BeforeSuite(func() {
},
}

// Hacky but needed to update the global since we still have code that reads it
*config.GetConfig() = *cfg

// create server
sut, err = NewServer(ctx, cfg)
Expect(err).Should(Succeed())
Expand Down

0 comments on commit e4be0c0

Please sign in to comment.