cert-manager-webhook-sthome/sthome/solver_local.go
2024-03-25 17:40:38 +02:00

143 lines
4.9 KiB
Go

package sthome
import (
"fmt"
"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"
dnsUpdaterScript = "/mnt/stpool1/scripts/acme/updatedns.sh"
)
// 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.Clientset
//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("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.InfoS("Decoded configuration %v", cfg)
klog.InfoS("presenting record for %s (%s)\n", ch.ResolvedFQDN, domainName)
// TODO: add code that sets a record in the DNS provider's console
// shell command
command := []string{
dnsUpdaterScript,
"arg1=-set",
"arg2=.net",
fmt.Sprintf("arg3=%s", ch.DNSName),
"arg4=TXT",
fmt.Sprintf("arg5=%s", ch.Key),
}
Execute(dnsUpdaterScript, command)
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 {
// TODO: add code that deletes a record from the DNS provider's console
// shell command
command := []string{
dnsUpdaterScript,
"arg1=-unset",
"arg2=.net",
fmt.Sprintf("arg3=%s", ch.DNSName),
"arg4=TXT",
fmt.Sprintf("arg5=%s", ch.Key),
}
Execute(dnsUpdaterScript, command)
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("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)
}