package dns import ( "fmt" "net" "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" ) const ( providerName = "sthome" shell = "/bin/bash" acmeAuthCmd = "/acme/acmeauth.sh" dnsserver_net = "10.0.0.15" dnsserver_lan = "192.168.2.1" hostserver_net = "truenas.sthome.net" hostserver_lan = "truenas.sthome.lan" ) // 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 (p *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) cfg, err := loadConfig(ch.Config) if err != nil { return err } klog.InfoS("CZ: Presenting challenge", "dnsName", ch.DNSName, "resolvedZone", ch.ResolvedZone, "resolvedFQDN", ch.ResolvedFQDN) /* provider, cfg, err := loc.init(ch.Config, ch.ResourceNamespace) if err != nil { return fmt.Errorf("failed initializing sthome provider: %v", err) } */ if !cfg.IsAllowedZone(ch.ResolvedZone) { return fmt.Errorf("zone %s may not be edited per config (allowed zones are %v)", ch.ResolvedZone, cfg.AllowedZones) } /* ctx := context.Background() records, err := loc.getExistingRecords(ctx, provider, ch.ResolvedZone, ch.ResolvedFQDN) if err != nil { return fmt.Errorf("failed loading existing records for %s in domain %s: %v", ch.ResolvedFQDN, ch.ResolvedZone, err) } // Add the record, only if it doesn't exist already content := quote(ch.Key) if _, ok := findRecord(records, content); !ok { disabled := false records = append(records, sthome.Record{Disabled: &disabled, Content: &content}) } */ // TODO: do something more useful with the decoded configuration klog.Infof("Decoded configuration %v\n", cfg) klog.Infof("Presenting record for %s, type: %s, uid: %s, key: %s, ns: %s, fqdn: %s, zone: %s, allowambcred: %t, cfg: %s", ch.DNSName, ch.UID, ch.Type, ch.Key, ch.ResourceNamespace, ch.ResolvedFQDN, ch.ResolvedZone, ch.AllowAmbientCredentials, ch.Config, ) // TODO: convert shell script to golang localip := getOutboundIP(dnsserver_net) success, _ := Execute( shell, acmeAuthCmd, "set", ch.DNSName, ch.ResolvedFQDN, ch.Key, "-l", localip, "-v", ) klog.Infof("Execute set TXT returned success: %t", success) return nil } // 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) // TODO: add code that deletes a record from the DNS provider's console localip := getOutboundIP(dnsserver_net) success, _ := Execute( shell, acmeAuthCmd, "unset", ch.DNSName, ch.ResolvedFQDN, ch.Key, "-l", localip, "-v", ) klog.Infof("Execute unset TXT returned success: %t", success) return nil } // 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 } 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) } // Get preferred outbound ip of this machine func getOutboundIP(dest string) string { conn, err := net.Dial("udp", dest+":80") if err != nil { klog.Errorf("net.Dial error: %s", err) return "0.0.0.0" } defer conn.Close() localAddr := conn.LocalAddr().(*net.UDPAddr) return localAddr.IP.String() }