2021-02-22 15:11:18 +00:00
|
|
|
// package example contains a self-contained example of a webhook that passes the cert-manager
|
|
|
|
// DNS conformance tests
|
2021-02-22 15:10:52 +00:00
|
|
|
package example
|
2021-02-22 15:11:18 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-22 16:02:41 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-02-22 15:11:18 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/jetstack/cert-manager/pkg/acme/webhook"
|
|
|
|
acme "github.com/jetstack/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type exampleSolver struct {
|
|
|
|
name string
|
|
|
|
server *dns.Server
|
|
|
|
txtRecords map[string]string
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:02:41 +00:00
|
|
|
func (e *exampleSolver) Name() string {
|
2021-02-22 15:11:18 +00:00
|
|
|
return e.name
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:02:41 +00:00
|
|
|
func (e *exampleSolver) Present(ch *acme.ChallengeRequest) error {
|
2021-02-22 15:11:18 +00:00
|
|
|
e.Lock()
|
|
|
|
e.txtRecords[ch.ResolvedFQDN] = ch.Key
|
|
|
|
e.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:02:41 +00:00
|
|
|
func (e *exampleSolver) CleanUp(ch *acme.ChallengeRequest) error {
|
2021-02-22 15:11:18 +00:00
|
|
|
e.Lock()
|
|
|
|
delete(e.txtRecords, ch.ResolvedFQDN)
|
|
|
|
e.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:02:41 +00:00
|
|
|
func (e *exampleSolver) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
|
2021-02-22 15:11:18 +00:00
|
|
|
go func(done <-chan struct{}) {
|
|
|
|
<-done
|
2021-02-22 16:02:41 +00:00
|
|
|
if err := e.server.Shutdown(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
}
|
2021-02-22 15:11:18 +00:00
|
|
|
}(stopCh)
|
|
|
|
go func() {
|
2021-02-22 16:02:41 +00:00
|
|
|
if err := e.server.ListenAndServe(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-02-22 15:11:18 +00:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(port string) webhook.Solver {
|
|
|
|
e := &exampleSolver{
|
|
|
|
name: "example",
|
|
|
|
txtRecords: make(map[string]string),
|
|
|
|
}
|
|
|
|
e.server = &dns.Server{
|
|
|
|
Addr: ":" + port,
|
|
|
|
Net: "udp",
|
|
|
|
Handler: dns.HandlerFunc(e.handleDNSRequest),
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|