cert-manager-webhook-sthome/pkg/dns/solver_local.go

149 lines
4.9 KiB
Go

package dns
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"
)
// 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)
_, err := LoadConfig(ch.Config)
if err != nil {
return err
}
/*
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("CZ: Presenting record for %s, type: %s, uid: %s, key: %s, ns: %s, fqdn: %s, zone: %s, allowambcred: %t, cfg.secret: %s, cfg.email: %s, cfg.allowz: %s",
ch.DNSName,
ch.UID,
ch.Type,
ch.Key,
ch.ResourceNamespace,
ch.ResolvedFQDN,
ch.ResolvedZone,
ch.AllowAmbientCredentials,
cfg.APIKeySecretRef.Name,
cfg.Email,
strings.Join(cfg.AllowedZones, ","),
)
*/
// 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\n", 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)
//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\n", 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)
}
*/