diff --git a/.vscode/settings.json b/.vscode/settings.json index ddae25f..1a9d4a1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { "go.useLanguageServer": true, + "go.inferGopath": false, } \ No newline at end of file diff --git a/go.mod b/go.mod index 8666587..345318e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,9 @@ module github.com/gstore/cert-manager-webhook-dynu go 1.13 require ( + github.com/go-logr/logr v0.2.1 github.com/jetstack/cert-manager v1.0.4 + github.com/miekg/dns v1.1.29 github.com/stretchr/testify v1.6.1 gitlab.com/smueller18/cert-manager-webhook-inwx v0.3.0 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect diff --git a/main_test.go b/main_test.go index 2bfc94e..a84e008 100644 --- a/main_test.go +++ b/main_test.go @@ -11,11 +11,12 @@ import ( "time" - "gitlab.com/smueller18/cert-manager-webhook-inwx/test" + //"gitlab.com/smueller18/cert-manager-webhook-inwx/test" extapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" "github.com/gstore/cert-manager-webhook-dynu/dynuclient" guntest "github.com/gstore/cert-manager-webhook-dynu/test" + test "github.com/gstore/cert-manager-webhook-dynu/test" "github.com/stretchr/testify/assert" logf "github.com/jetstack/cert-manager/pkg/logs" @@ -33,7 +34,7 @@ func TestRunsSuite(t *testing.T) { // The manifest path should contain a file named config.json that is a // snippet of valid configuration that should be included on the // ChallengeRequest passed as part of the test cases. - + // t.Skip() dnsResp := dynuclient.DNSResponse{ StatusCode: 200, ID: 98765, @@ -83,19 +84,19 @@ func TestRunsSuite(t *testing.T) { fqdn = "cert-manager-dns01-tests." + zone ctx := logf.NewContext(nil, nil, t.Name()) - + txtRecs := map[string][][]string{ + fqdn: { + {}, + {}, + {"123d=="}, + {"123d=="}, + }, + } srv := &server.BasicServer{ - Handler: &test.Handler{ - Log: logf.FromContext(ctx, "dnsBasicServerSecret"), - TxtRecords: map[string][][]string{ - fqdn: { - {}, - {}, - {"123d=="}, - {"123d=="}, - }, - }, - Zones: []string{zone}, + Handler: &test.DNSHandler{ + Log: logf.FromContext(ctx, "dnsBasicServerSecret"), + TxtRecords: txtRecs, + Zones: []string{zone}, }, } @@ -124,7 +125,7 @@ func TestRunsSuite(t *testing.T) { fixture.RunConformance(t) } func TestRunSuiteWithSecret(t *testing.T) { - t.Skip() + //t.Skip() dnsResp := dynuclient.DNSResponse{ StatusCode: 200, ID: 98765, @@ -178,18 +179,20 @@ func TestRunSuiteWithSecret(t *testing.T) { fqdn = "cert-manager-dns01-tests." + zone ctx := logf.NewContext(nil, nil, t.Name()) + txtRecs := map[string][][]string{ + fqdn: { + {}, + {}, + {"123d=="}, + {"123d=="}, + }, + } + srv := &server.BasicServer{ - Handler: &test.Handler{ - Log: logf.FromContext(ctx, "dnsBasicServerSecret"), - TxtRecords: map[string][][]string{ - fqdn: { - {}, - {}, - {"123d=="}, - {"123d=="}, - }, - }, - Zones: []string{zone}, + Handler: &test.DNSHandler{ + Log: logf.FromContext(ctx, "dnsBasicServerSecret"), + TxtRecords: txtRecs, + Zones: []string{zone}, }, } @@ -210,7 +213,7 @@ func TestRunSuiteWithSecret(t *testing.T) { dns.SetDNSServer(srv.ListenAddr()), dns.SetManifestPath("testdata/secret-dynu-credentials.yaml"), dns.SetBinariesPath(kubeBuilderBinPath), - dns.SetPropagationLimit(time.Duration(60)*time.Second), + dns.SetPropagationLimit(time.Duration(90)*time.Second), dns.SetUseAuthoritative(false), dns.SetConfig(&extapi.JSON{ Raw: d, diff --git a/test/dnshandler.go b/test/dnshandler.go new file mode 100644 index 0000000..9c83544 --- /dev/null +++ b/test/dnshandler.go @@ -0,0 +1,75 @@ +/* +Copyright 2019 The Jetstack cert-manager contributors. + +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 test + +import ( + "fmt" + "sync" + + "github.com/go-logr/logr" + "github.com/miekg/dns" +) + +const ( + defaultTTL = 1 +) + +var requestCount = map[string]int{} +var count int = 1 + +// DNSHandler ... +type DNSHandler struct { + Log logr.Logger + + TxtRecords map[string][][]string + Zones []string + tsigZone string + lock sync.Mutex +} + +// ServeDNS ... implements github.com/miekg/dns.Handler +// Imitates a DNS server +func (b *DNSHandler) ServeDNS(w dns.ResponseWriter, req *dns.Msg) { + b.lock.Lock() + defer b.lock.Unlock() + log := b.Log.WithName("serveDNS") + //fmt.Printf("req: %v\n", req) + m := new(dns.Msg) + m.SetReply(req) + defer w.WriteMsg(m) + + log.Info(m.String()) + fmt.Printf("\n\nreq count: %v\nlen: %v\n\n", requestCount[req.Question[0].Name], len(b.TxtRecords[req.Question[0].Name])) + if requestCount[req.Question[0].Name] < len(b.TxtRecords[req.Question[0].Name]) { + if requestCount[req.Question[0].Name] == 3 { + requestCount[req.Question[0].Name] = 0 + } + //fmt.Println("requestcount") + for _, record := range b.TxtRecords[req.Question[0].Name][requestCount[req.Question[0].Name]] { + fmt.Println("for loop") + txtRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN TXT %s", req.Question[0].Name, defaultTTL, record)) + m.Answer = append(m.Answer, txtRR) + } + requestCount[req.Question[0].Name]++ + } + + for _, rr := range m.Answer { + //fmt.Printf("responding %v", rr.String()) + log.Info("responding", "response", rr.String()) + } + count++ +} diff --git a/test/testclient.go b/test/testclient.go index 53728b2..43f63b9 100644 --- a/test/testclient.go +++ b/test/testclient.go @@ -28,3 +28,10 @@ func (c Testclient) TestingHTTPClient(handler http.Handler) (*http.Client, func( return cli, s.Close } + +// NewTestingHTTPClient - Create a new TestingHTTPClient +func (c *Testclient) NewTestingHTTPClient(handler http.Handler) (*http.Client, func()) { + client, fn := c.TestingHTTPClient(handler) + + return client, fn +}