| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package log | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2019-07-16 15:00:22 -04:00
										 |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 	"log" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest" | 
					
						
							| 
									
										
										
										
											2018-07-19 16:23:06 +01:00
										 |  |  | 	clog "github.com/coredns/coredns/plugin/pkg/log" | 
					
						
							| 
									
										
										
										
											2019-02-12 07:38:49 +00:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/replacer" | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/response" | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/test" | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-19 16:23:06 +01:00
										 |  |  | func init() { clog.Discard() } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | func TestLoggedStatus(t *testing.T) { | 
					
						
							|  |  |  | 	rule := Rule{ | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 		NameScope: ".", | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Format:    DefaultLogFormat, | 
					
						
							| 
									
										
										
										
											2019-01-30 19:12:26 +08:00
										 |  |  | 		Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 21:32:23 +00:00
										 |  |  | 	var f bytes.Buffer | 
					
						
							|  |  |  | 	log.SetOutput(&f) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	logger := Logger{ | 
					
						
							|  |  |  | 		Rules: []Rule{rule}, | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		Next:  test.ErrorHandler(), | 
					
						
							| 
									
										
										
										
											2019-02-12 07:38:49 +00:00
										 |  |  | 		repl:  replacer.New(), | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 	ctx := context.TODO() | 
					
						
							|  |  |  | 	r := new(dns.Msg) | 
					
						
							|  |  |  | 	r.SetQuestion("example.org.", dns.TypeA) | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 	rec := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 	rcode, _ := logger.ServeDNS(ctx, rec, r) | 
					
						
							| 
									
										
										
										
											2019-03-25 03:36:46 +00:00
										 |  |  | 	if rcode != 2 { | 
					
						
							|  |  |  | 		t.Errorf("Expected rcode to be 2 - was: %d", rcode) | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logged := f.String() | 
					
						
							| 
									
										
										
										
											2016-11-30 20:44:00 +00:00
										 |  |  | 	if !strings.Contains(logged, "A IN example.org. udp 29 false 512") { | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		t.Errorf("Expected it to be logged. Logged string: %s", logged) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestLoggedClassDenial(t *testing.T) { | 
					
						
							|  |  |  | 	rule := Rule{ | 
					
						
							|  |  |  | 		NameScope: ".", | 
					
						
							|  |  |  | 		Format:    DefaultLogFormat, | 
					
						
							| 
									
										
										
										
											2019-01-30 19:12:26 +08:00
										 |  |  | 		Class:     map[response.Class]struct{}{response.Denial: {}}, | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 21:32:23 +00:00
										 |  |  | 	var f bytes.Buffer | 
					
						
							|  |  |  | 	log.SetOutput(&f) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 	logger := Logger{ | 
					
						
							|  |  |  | 		Rules: []Rule{rule}, | 
					
						
							|  |  |  | 		Next:  test.ErrorHandler(), | 
					
						
							| 
									
										
										
										
											2019-02-12 07:38:49 +00:00
										 |  |  | 		repl:  replacer.New(), | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ctx := context.TODO() | 
					
						
							|  |  |  | 	r := new(dns.Msg) | 
					
						
							|  |  |  | 	r.SetQuestion("example.org.", dns.TypeA) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 	rec := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	logger.ServeDNS(ctx, rec, r) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logged := f.String() | 
					
						
							|  |  |  | 	if len(logged) != 0 { | 
					
						
							|  |  |  | 		t.Errorf("Expected it not to be logged, but got string: %s", logged) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestLoggedClassError(t *testing.T) { | 
					
						
							|  |  |  | 	rule := Rule{ | 
					
						
							|  |  |  | 		NameScope: ".", | 
					
						
							|  |  |  | 		Format:    DefaultLogFormat, | 
					
						
							| 
									
										
										
										
											2019-01-30 19:12:26 +08:00
										 |  |  | 		Class:     map[response.Class]struct{}{response.Error: {}}, | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 21:32:23 +00:00
										 |  |  | 	var f bytes.Buffer | 
					
						
							|  |  |  | 	log.SetOutput(&f) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 	logger := Logger{ | 
					
						
							|  |  |  | 		Rules: []Rule{rule}, | 
					
						
							|  |  |  | 		Next:  test.ErrorHandler(), | 
					
						
							| 
									
										
										
										
											2019-02-12 07:38:49 +00:00
										 |  |  | 		repl:  replacer.New(), | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ctx := context.TODO() | 
					
						
							|  |  |  | 	r := new(dns.Msg) | 
					
						
							|  |  |  | 	r.SetQuestion("example.org.", dns.TypeA) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 	rec := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	logger.ServeDNS(ctx, rec, r) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logged := f.String() | 
					
						
							|  |  |  | 	if !strings.Contains(logged, "SERVFAIL") { | 
					
						
							|  |  |  | 		t.Errorf("Expected it to be logged. Logged string: %s", logged) | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-01-30 19:12:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestLogged(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		Rules           []Rule | 
					
						
							|  |  |  | 		Domain          string | 
					
						
							|  |  |  | 		ShouldLog       bool | 
					
						
							|  |  |  | 		ShouldString    string | 
					
						
							|  |  |  | 		ShouldNOTString string // for test format | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// case for NameScope | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Rules: []Rule{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					NameScope: "example.org.", | 
					
						
							|  |  |  | 					Format:    DefaultLogFormat, | 
					
						
							|  |  |  | 					Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			Domain:       "example.org.", | 
					
						
							|  |  |  | 			ShouldLog:    true, | 
					
						
							|  |  |  | 			ShouldString: "A IN example.org.", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Rules: []Rule{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					NameScope: "example.org.", | 
					
						
							|  |  |  | 					Format:    DefaultLogFormat, | 
					
						
							|  |  |  | 					Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			Domain:       "example.net.", | 
					
						
							|  |  |  | 			ShouldLog:    false, | 
					
						
							|  |  |  | 			ShouldString: "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Rules: []Rule{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					NameScope: "example.org.", | 
					
						
							|  |  |  | 					Format:    DefaultLogFormat, | 
					
						
							|  |  |  | 					Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					NameScope: "example.net.", | 
					
						
							|  |  |  | 					Format:    DefaultLogFormat, | 
					
						
							|  |  |  | 					Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			Domain:       "example.net.", | 
					
						
							|  |  |  | 			ShouldLog:    true, | 
					
						
							|  |  |  | 			ShouldString: "A IN example.net.", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// case for format | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Rules: []Rule{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					NameScope: ".", | 
					
						
							|  |  |  | 					Format:    "{type} {class}", | 
					
						
							|  |  |  | 					Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			Domain:          "example.org.", | 
					
						
							|  |  |  | 			ShouldLog:       true, | 
					
						
							|  |  |  | 			ShouldString:    "A IN", | 
					
						
							|  |  |  | 			ShouldNOTString: "example.org", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Rules: []Rule{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					NameScope: ".", | 
					
						
							|  |  |  | 					Format:    "{remote}:{port}", | 
					
						
							|  |  |  | 					Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			Domain:          "example.org.", | 
					
						
							|  |  |  | 			ShouldLog:       true, | 
					
						
							|  |  |  | 			ShouldString:    "10.240.0.1:40212", | 
					
						
							|  |  |  | 			ShouldNOTString: "A IN example.org", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Rules: []Rule{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					NameScope: ".", | 
					
						
							|  |  |  | 					Format:    CombinedLogFormat, | 
					
						
							|  |  |  | 					Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			Domain:       "example.org.", | 
					
						
							|  |  |  | 			ShouldLog:    true, | 
					
						
							|  |  |  | 			ShouldString: "\"0\"", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tc := range tests { | 
					
						
							|  |  |  | 		var f bytes.Buffer | 
					
						
							|  |  |  | 		log.SetOutput(&f) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		logger := Logger{ | 
					
						
							|  |  |  | 			Rules: tc.Rules, | 
					
						
							|  |  |  | 			Next:  test.ErrorHandler(), | 
					
						
							| 
									
										
										
										
											2019-02-12 07:38:49 +00:00
										 |  |  | 			repl:  replacer.New(), | 
					
						
							| 
									
										
										
										
											2019-01-30 19:12:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ctx := context.TODO() | 
					
						
							|  |  |  | 		r := new(dns.Msg) | 
					
						
							|  |  |  | 		r.SetQuestion(tc.Domain, dns.TypeA) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_, err := logger.ServeDNS(ctx, rec, r) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		logged := f.String() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !tc.ShouldLog && len(logged) != 0 { | 
					
						
							|  |  |  | 			t.Errorf("Expected it not to be logged, but got string: %s", logged) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if tc.ShouldLog && !strings.Contains(logged, tc.ShouldString) { | 
					
						
							|  |  |  | 			t.Errorf("Expected it to contains: %s. Logged string: %s", tc.ShouldString, logged) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if tc.ShouldLog && tc.ShouldNOTString != "" && strings.Contains(logged, tc.ShouldNOTString) { | 
					
						
							|  |  |  | 			t.Errorf("Expected it to NOT contains: %s. Logged string: %s", tc.ShouldNOTString, logged) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkLogged(b *testing.B) { | 
					
						
							| 
									
										
										
										
											2019-07-16 15:00:22 -04:00
										 |  |  | 	log.SetOutput(ioutil.Discard) | 
					
						
							| 
									
										
										
										
											2019-01-30 19:12:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	rule := Rule{ | 
					
						
							|  |  |  | 		NameScope: ".", | 
					
						
							|  |  |  | 		Format:    DefaultLogFormat, | 
					
						
							|  |  |  | 		Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logger := Logger{ | 
					
						
							|  |  |  | 		Rules: []Rule{rule}, | 
					
						
							|  |  |  | 		Next:  test.ErrorHandler(), | 
					
						
							| 
									
										
										
										
											2019-02-12 07:38:49 +00:00
										 |  |  | 		repl:  replacer.New(), | 
					
						
							| 
									
										
										
										
											2019-01-30 19:12:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ctx := context.TODO() | 
					
						
							|  |  |  | 	r := new(dns.Msg) | 
					
						
							|  |  |  | 	r.SetQuestion("example.org.", dns.TypeA) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rec := dnstest.NewRecorder(&test.ResponseWriter{}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b.StartTimer() | 
					
						
							|  |  |  | 	for i := 0; i < b.N; i++ { | 
					
						
							|  |  |  | 		logger.ServeDNS(ctx, rec, r) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |