158 lines
5.0 KiB
Go
158 lines
5.0 KiB
Go
package dns
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
"k8s.io/client-go/rest"
|
|
"k8s.io/klog/v2"
|
|
|
|
"github.com/cert-manager/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1"
|
|
//"github.com/cert-manager/cert-manager/pkg/issuer/acme/dns/util"
|
|
"github.com/stuurmcp/cert-manager-webhook-sthome/pkg/util"
|
|
)
|
|
|
|
var (
|
|
DNS01Nameservers = []string{"10.0.0.15:53", "192.168.2.1:53", "1.1.1.1:53", "1.0.0.1:53"}
|
|
)
|
|
|
|
const (
|
|
DNS01CheckAuthoritative = true
|
|
)
|
|
|
|
// LocalDNSProviderSolver implements the provider-specific logic needed to
|
|
// 'present' an ACME challenge TXT record for your own DNS provider.
|
|
// To do so, it must implement the `github.com/cert-manager/cert-manager/pkg/acme/webhook.Solver`
|
|
// interface.
|
|
type LocalDNSProviderSolver struct {
|
|
client kubernetes.Interface
|
|
}
|
|
|
|
// Name is used as the name for this DNS solver when referencing it on the ACME
|
|
// Issuer resource.
|
|
// This should be unique **within the group name**, i.e. you can have two
|
|
// solvers configured with the same Name() **so long as they do not co-exist
|
|
// within a single webhook deployment**.
|
|
// For example, `cloudflare` may be used as the name of a solver.
|
|
func (loc *LocalDNSProviderSolver) Name() string {
|
|
return ProviderName
|
|
}
|
|
|
|
// Present is responsible for actually presenting the DNS record with the
|
|
// DNS provider.
|
|
// This method should tolerate being called multiple times with the same value.
|
|
// cert-manager itself will later perform a self check to ensure that the
|
|
// solver has correctly configured the DNS provider.
|
|
func (loc *LocalDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error {
|
|
//domainName := extractDomainName(ch.ResolvedZone)
|
|
_, err := LoadConfig(ch.Config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// TODO: convert shell script to golang
|
|
//localip := GetOutboundIP(Dnsserver_net)
|
|
success, err := Execute2(
|
|
Shell,
|
|
// "-c",
|
|
AcmeAuthCmd,
|
|
"set",
|
|
ch.DNSName,
|
|
ch.ResolvedFQDN,
|
|
ch.Key,
|
|
"-l",
|
|
"\"\"", //localip,
|
|
//"-v",
|
|
)
|
|
klog.Infof("Present: Execute set TXT returned success: %t\n", success)
|
|
err2 := loc.Check(ch.DNSName, ch.Key)
|
|
if err2 != nil {
|
|
klog.Infof("Present: Check prop failed: %s", err2)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// CleanUp should delete the relevant TXT record from the DNS provider console.
|
|
// If multiple TXT records exist with the same record name (e.g.
|
|
// _acme-challenge.example.com) then **only** the record with the same `key`
|
|
// value provided on the ChallengeRequest should be cleaned up.
|
|
// This is in order to facilitate multiple DNS validations for the same domain
|
|
// concurrently.
|
|
func (loc *LocalDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
|
|
//domainName := extractDomainName(ch.ResolvedZone)
|
|
//localip := GetOutboundIP(Dnsserver_net)
|
|
success, err := Execute2(
|
|
Shell,
|
|
// "-c",
|
|
AcmeAuthCmd,
|
|
"unset",
|
|
ch.DNSName,
|
|
ch.ResolvedFQDN,
|
|
ch.Key,
|
|
"-l",
|
|
"\"\"", //localip,
|
|
//"-v",
|
|
)
|
|
klog.Infof("Execute unset TXT returned success: %t\n", success)
|
|
return err
|
|
}
|
|
|
|
// Initialize will be called when the webhook first starts.
|
|
// This method can be used to instantiate the webhook, i.e. initialising
|
|
// connections or warming up caches.
|
|
// Typically, the kubeClientConfig parameter is used to build a Kubernetes
|
|
// client that can be used to fetch resources from the Kubernetes API, e.g.
|
|
// Secret resources containing credentials used to authenticate with DNS
|
|
// provider accounts.
|
|
// The stopCh can be used to handle early termination of the webhook, in cases
|
|
// where a SIGTERM or similar signal is sent to the webhook process.
|
|
func (loc *LocalDNSProviderSolver) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
|
|
cl, err := kubernetes.NewForConfig(kubeClientConfig)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get kubernetes client: %w", err)
|
|
}
|
|
loc.client = cl
|
|
klog.InfoS("CZ: Successfully initialised kubernetes client!")
|
|
return nil
|
|
}
|
|
|
|
// Check verifies that the DNS records for the ACME challenge have propagated.
|
|
func (s *LocalDNSProviderSolver) Check(DNSName string, Key string) error {
|
|
ttl := 20
|
|
|
|
fqdn, err := util.DNS01LookupFQDN(DNSName, false, DNS01Nameservers...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
klog.Info("waiting DNS record TTL to allow the DNS01 record to propagate for domain")
|
|
time.Sleep(time.Second * time.Duration(ttl))
|
|
|
|
//klog.Info("checking DNS propagation: ", "dns: ", DNSName, ", fqdn: ", fqdn, ", key: ", Key, ", nameservers: ", DNS01Nameservers)
|
|
klog.Info("checking DNS prop: fqdn: ", fqdn)
|
|
|
|
ok, err := util.PreCheckDNS(fqdn, Key, DNS01Nameservers, DNS01CheckAuthoritative)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return fmt.Errorf("DNS record for %q not yet propagated", DNSName)
|
|
}
|
|
|
|
// klog.Info("waiting DNS record TTL to allow the DNS01 record to propagate for domain")
|
|
// time.Sleep(time.Second * time.Duration(ttl))
|
|
klog.Info("ACME DNS01 validation record propagated: ", "fqdn", fqdn)
|
|
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
func extractDomainName(zone string) string {
|
|
authZone, err := util.FindZoneByFqdn(zone, util.RecursiveNameservers)
|
|
if err != nil {
|
|
klog.Errorf("could not get zone by fqdn %v", err)
|
|
return zone
|
|
}
|
|
return util.UnFqdn(authZone)
|
|
}
|
|
*/
|