mirror of
https://github.com/cert-manager/webhook-example.git
synced 2025-07-02 23:05:48 +02:00
initial commit
This commit is contained in:
parent
5131c1f665
commit
d4706e7e3f
15 changed files with 201 additions and 34 deletions
|
@ -6,7 +6,7 @@
|
||||||
# solve the DNS01 challenge.
|
# solve the DNS01 challenge.
|
||||||
# This group name should be **unique**, hence using your own company's domain
|
# This group name should be **unique**, hence using your own company's domain
|
||||||
# here is recommended.
|
# here is recommended.
|
||||||
groupName: acme.mycompany.com
|
groupName: dns.abion.core
|
||||||
|
|
||||||
certManager:
|
certManager:
|
||||||
namespace: cert-manager
|
namespace: cert-manager
|
216
main.go
216
main.go
|
@ -4,6 +4,10 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
"net/http"
|
||||||
|
"io"
|
||||||
|
"bytes"
|
||||||
|
|
||||||
extapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
extapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
|
@ -25,22 +29,16 @@ func main() {
|
||||||
// webhook, where the Name() method will be used to disambiguate between
|
// webhook, where the Name() method will be used to disambiguate between
|
||||||
// the different implementations.
|
// the different implementations.
|
||||||
cmd.RunWebhookServer(GroupName,
|
cmd.RunWebhookServer(GroupName,
|
||||||
&customDNSProviderSolver{},
|
&abioncoreDNSProviderSolver{},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// customDNSProviderSolver implements the provider-specific logic needed to
|
// abioncoreDNSProviderSolver implements the provider-specific logic needed to
|
||||||
// 'present' an ACME challenge TXT record for your own DNS provider.
|
// '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`
|
// To do so, it must implement the `github.com/cert-manager/cert-manager/pkg/acme/webhook.Solver`
|
||||||
// interface.
|
// interface.
|
||||||
type customDNSProviderSolver struct {
|
type abioncoreDNSProviderSolver struct {
|
||||||
// If a Kubernetes 'clientset' is needed, you must:
|
|
||||||
// 1. uncomment the additional `client` field in this structure below
|
|
||||||
// 2. uncomment the "k8s.io/client-go/kubernetes" import at the top of the file
|
|
||||||
// 3. uncomment the relevant code in the Initialize method below
|
|
||||||
// 4. ensure your webhook's service account has the required RBAC role
|
|
||||||
// assigned to it for interacting with the Kubernetes APIs you need.
|
|
||||||
//client kubernetes.Clientset
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// customDNSProviderConfig is a structure that is used to decode into when
|
// customDNSProviderConfig is a structure that is used to decode into when
|
||||||
|
@ -57,24 +55,44 @@ type customDNSProviderSolver struct {
|
||||||
// You should not include sensitive information here. If credentials need to
|
// You should not include sensitive information here. If credentials need to
|
||||||
// be used by your provider here, you should reference a Kubernetes Secret
|
// be used by your provider here, you should reference a Kubernetes Secret
|
||||||
// resource and fetch these credentials using a Kubernetes clientset.
|
// resource and fetch these credentials using a Kubernetes clientset.
|
||||||
type customDNSProviderConfig struct {
|
type abioncoreDNSProviderConfig struct {
|
||||||
// Change the two fields below according to the format of the configuration
|
// Change the two fields below according to the format of the configuration
|
||||||
// to be decoded.
|
// to be decoded.
|
||||||
// These fields will be set by users in the
|
// These fields will be set by users in the
|
||||||
// `issuer.spec.acme.dns01.providers.webhook.config` field.
|
// `issuer.spec.acme.dns01.providers.webhook.config` field.
|
||||||
|
APIKey string `json:"x-api-key"`
|
||||||
//Email string `json:"email"`
|
|
||||||
//APIKeySecretRef v1alpha1.SecretKeySelector `json:"apiKeySecretRef"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name is used as the name for this DNS solver when referencing it on the ACME
|
// Name is used as the name for this DNS solver when referencing it on the ACME
|
||||||
// Issuer resource.
|
// Issuer resource.
|
||||||
// This should be unique **within the group name**, i.e. you can have two
|
// 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
|
// solvers configured with the same Name() **so long as they do not co-exist
|
||||||
// within a single webhook deployment**.
|
// within a single webhook deployment**.
|
||||||
// For example, `cloudflare` may be used as the name of a solver.
|
// For example, `cloudflare` may be used as the name of a solver.
|
||||||
func (c *customDNSProviderSolver) Name() string {
|
func (c *abioncoreDNSProviderSolver) Name() string {
|
||||||
return "my-custom-solver"
|
return "abion-core"
|
||||||
|
}
|
||||||
|
|
||||||
|
type Entry struct {
|
||||||
|
Data Data `json:"data"`
|
||||||
|
}
|
||||||
|
type Txt struct {
|
||||||
|
TTL int `json:"ttl"`
|
||||||
|
Rdata string `json:"rdata"`
|
||||||
|
Comments string `json:"comments"`
|
||||||
|
}
|
||||||
|
type AcmeChallenge struct {
|
||||||
|
Txt []Txt `json:"TXT"`
|
||||||
|
}
|
||||||
|
type Records struct {
|
||||||
|
AcmeChallenge AcmeChallenge `json:"_acme-challenge"`
|
||||||
|
}
|
||||||
|
type Attributes struct {
|
||||||
|
Records Records `json:"records"`
|
||||||
|
}
|
||||||
|
type Data struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
Attributes Attributes `json:"attributes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Present is responsible for actually presenting the DNS record with the
|
// Present is responsible for actually presenting the DNS record with the
|
||||||
|
@ -82,16 +100,91 @@ func (c *customDNSProviderSolver) Name() string {
|
||||||
// This method should tolerate being called multiple times with the same value.
|
// 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
|
// cert-manager itself will later perform a self check to ensure that the
|
||||||
// solver has correctly configured the DNS provider.
|
// solver has correctly configured the DNS provider.
|
||||||
func (c *customDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error {
|
func (c *abioncoreDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error {
|
||||||
|
|
||||||
|
// klog.V(2).Infof("Present: namespace=%s, zone=%s, fqdn=%s",
|
||||||
|
// ch.ResourceNamespace, ch.ResolvedZone, ch.ResolvedFQDN)
|
||||||
|
|
||||||
cfg, err := loadConfig(ch.Config)
|
cfg, err := loadConfig(ch.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get name and zone from challenge request
|
||||||
|
// name := strings.TrimSuffix(strings.TrimSuffix(ch.ResolvedFQDN, ch.ResolvedZone), ".")
|
||||||
|
zone := strings.TrimSuffix(ch.ResolvedZone, ".")
|
||||||
|
|
||||||
// TODO: do something more useful with the decoded configuration
|
// TODO: do something more useful with the decoded configuration
|
||||||
fmt.Printf("Decoded configuration %v", cfg)
|
fmt.Printf("Decoded configuration %v", cfg)
|
||||||
|
|
||||||
// TODO: add code that sets a record in the DNS provider's console
|
// https://demo.abion.com/pmapi/ ################### Main Api
|
||||||
|
|
||||||
|
// Get all zones (GET https://demo.abion.com/pmapi/v1/zones)
|
||||||
|
// Create client
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
// Create request
|
||||||
|
request, err := http.NewRequest("GET", "https://demo.abion.com/pmapi/v1/zones/"+zone, nil)
|
||||||
|
// Headers
|
||||||
|
request.Header.Add("X-API-KEY", cfg.APIKey)
|
||||||
|
|
||||||
|
// Fetch Request
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failure : ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if response.StatusCode != 200 {
|
||||||
|
return fmt.Errorf("did not get expected HTTP 200 but %s", response.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display Results
|
||||||
|
fmt.Println("response Status : ", response.Status)
|
||||||
|
fmt.Println("response Headers : ", response.Header)
|
||||||
|
|
||||||
|
// Create DNS
|
||||||
|
entry, err := json.Marshal(
|
||||||
|
Entry{
|
||||||
|
Data{"zone", zone,
|
||||||
|
Attributes{
|
||||||
|
Records{
|
||||||
|
AcmeChallenge{
|
||||||
|
[]Txt{
|
||||||
|
{
|
||||||
|
300,
|
||||||
|
ch.Key,
|
||||||
|
"Let's Encrypt _acme-challenge",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
body := bytes.NewBuffer(entry)
|
||||||
|
fmt.Println("entry json struct : ", body)
|
||||||
|
|
||||||
|
// Create request
|
||||||
|
request, err = http.NewRequest("PATCH", "https://demo.abion.com/pmapi/v1/zones/"+zone, body)
|
||||||
|
// Headers
|
||||||
|
request.Header.Add("Content-Type", "application/json")
|
||||||
|
request.Header.Add("X-API-KEY", cfg.APIKey)
|
||||||
|
|
||||||
|
// Fetch Request
|
||||||
|
response, err = client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failure : ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read Response Body
|
||||||
|
responseBody, _ := io.ReadAll(response.Body)
|
||||||
|
|
||||||
|
// Display Results
|
||||||
|
fmt.Println("response Status : ", response.Status)
|
||||||
|
fmt.Println("response Headers : ", response.Header)
|
||||||
|
fmt.Println("response Body : ", string(responseBody))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,8 +194,79 @@ func (c *customDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error {
|
||||||
// value provided on the ChallengeRequest should be cleaned up.
|
// value provided on the ChallengeRequest should be cleaned up.
|
||||||
// This is in order to facilitate multiple DNS validations for the same domain
|
// This is in order to facilitate multiple DNS validations for the same domain
|
||||||
// concurrently.
|
// concurrently.
|
||||||
func (c *customDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
|
func (c *abioncoreDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
|
||||||
// TODO: add code that deletes a record from the DNS provider's console
|
|
||||||
|
cfg, err := loadConfig(ch.Config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get name and zone from challenge request
|
||||||
|
// name := strings.TrimSuffix(strings.TrimSuffix(ch.ResolvedFQDN, ch.ResolvedZone), ".")
|
||||||
|
zone := strings.TrimSuffix(ch.ResolvedZone, ".")
|
||||||
|
|
||||||
|
// https://demo.abion.com/pmapi/ ################### Main Api
|
||||||
|
|
||||||
|
// Get all zones (GET https://demo.abion.com/pmapi/v1/zones)
|
||||||
|
// Create client
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
// Create request
|
||||||
|
request, err := http.NewRequest("GET", "https://demo.abion.com/pmapi/v1/zones/"+zone, nil)
|
||||||
|
// Headers
|
||||||
|
request.Header.Add("X-API-KEY", cfg.APIKey)
|
||||||
|
|
||||||
|
// Fetch Request
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failure : ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if response.StatusCode != 200 {
|
||||||
|
return fmt.Errorf("did not get expected HTTP 200 but %s", response.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display Results
|
||||||
|
fmt.Println("response Status : ", response.Status)
|
||||||
|
fmt.Println("response Headers : ", response.Header)
|
||||||
|
//fmt.Println("response Body : ", respBody.Zones[0].ZoneID)
|
||||||
|
|
||||||
|
// Clear txt
|
||||||
|
entry, err := json.Marshal(
|
||||||
|
Entry{
|
||||||
|
Data{"zone", zone,
|
||||||
|
Attributes{
|
||||||
|
Records{
|
||||||
|
AcmeChallenge{
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
body := bytes.NewBuffer(entry)
|
||||||
|
|
||||||
|
// Create request
|
||||||
|
request, err = http.NewRequest("PATCH", "https://demo.abion.com/pmapi/v1/zones/"+zone, body)
|
||||||
|
// Headers
|
||||||
|
request.Header.Add("Content-Type", "application/json")
|
||||||
|
request.Header.Add("X-API-KEY", cfg.APIKey)
|
||||||
|
|
||||||
|
// Fetch Request
|
||||||
|
response, err = client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failure : ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read Response Body
|
||||||
|
responseBody, _ := io.ReadAll(response.Body)
|
||||||
|
|
||||||
|
// Display Results
|
||||||
|
fmt.Println("response Status : ", response.Status)
|
||||||
|
fmt.Println("response Headers : ", response.Header)
|
||||||
|
fmt.Println("response Body : ", string(responseBody))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +279,7 @@ func (c *customDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
|
||||||
// provider accounts.
|
// provider accounts.
|
||||||
// The stopCh can be used to handle early termination of the webhook, in cases
|
// 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.
|
// where a SIGTERM or similar signal is sent to the webhook process.
|
||||||
func (c *customDNSProviderSolver) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
|
func (c *abioncoreDNSProviderSolver) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
|
||||||
///// UNCOMMENT THE BELOW CODE TO MAKE A KUBERNETES CLIENTSET AVAILABLE TO
|
///// UNCOMMENT THE BELOW CODE TO MAKE A KUBERNETES CLIENTSET AVAILABLE TO
|
||||||
///// YOUR CUSTOM DNS PROVIDER
|
///// YOUR CUSTOM DNS PROVIDER
|
||||||
|
|
||||||
|
@ -132,14 +296,16 @@ func (c *customDNSProviderSolver) Initialize(kubeClientConfig *rest.Config, stop
|
||||||
|
|
||||||
// loadConfig is a small helper function that decodes JSON configuration into
|
// loadConfig is a small helper function that decodes JSON configuration into
|
||||||
// the typed config struct.
|
// the typed config struct.
|
||||||
func loadConfig(cfgJSON *extapi.JSON) (customDNSProviderConfig, error) {
|
func loadConfig(cfgJSON *extapi.JSON) (abioncoreDNSProviderConfig, error) {
|
||||||
cfg := customDNSProviderConfig{}
|
|
||||||
|
cfg := abioncoreDNSProviderConfig{}
|
||||||
// handle the 'base case' where no configuration has been provided
|
// handle the 'base case' where no configuration has been provided
|
||||||
if cfgJSON == nil {
|
if cfgJSON == nil {
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal(cfgJSON.Raw, &cfg); err != nil {
|
if err := json.Unmarshal(cfgJSON.Raw, &cfg); err != nil {
|
||||||
return cfg, fmt.Errorf("error decoding solver config: %v", err)
|
return cfg, fmt.Errorf("error decoding config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
|
|
13
main_test.go
13
main_test.go
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
acmetest "github.com/cert-manager/cert-manager/test/acme"
|
acmetest "github.com/cert-manager/cert-manager/test/acme"
|
||||||
|
|
||||||
"github.com/cert-manager/webhook-example/example"
|
// "github.com/cert-manager/webhook-example/example"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -26,16 +26,15 @@ func TestRunsSuite(t *testing.T) {
|
||||||
// acmetest.SetManifestPath("testdata/my-custom-solver"),
|
// acmetest.SetManifestPath("testdata/my-custom-solver"),
|
||||||
// acmetest.SetBinariesPath("_test/kubebuilder/bin"),
|
// acmetest.SetBinariesPath("_test/kubebuilder/bin"),
|
||||||
//)
|
//)
|
||||||
solver := example.New("59351")
|
solver := abioncoreDNSProviderSolver{}
|
||||||
fixture := acmetest.NewFixture(solver,
|
fixture := acmetest.NewFixture(&solver,
|
||||||
acmetest.SetResolvedZone("example.com."),
|
acmetest.SetResolvedZone(zone),
|
||||||
acmetest.SetManifestPath("testdata/my-custom-solver"),
|
acmetest.SetAllowAmbientCredentials(false),
|
||||||
acmetest.SetDNSServer("127.0.0.1:59351"),
|
acmetest.SetManifestPath("testdata/cert-manager-webhook-abion-core"),
|
||||||
acmetest.SetUseAuthoritative(false),
|
acmetest.SetUseAuthoritative(false),
|
||||||
)
|
)
|
||||||
//need to uncomment and RunConformance delete runBasic and runExtended once https://github.com/cert-manager/cert-manager/pull/4835 is merged
|
//need to uncomment and RunConformance delete runBasic and runExtended once https://github.com/cert-manager/cert-manager/pull/4835 is merged
|
||||||
//fixture.RunConformance(t)
|
//fixture.RunConformance(t)
|
||||||
fixture.RunBasic(t)
|
fixture.RunBasic(t)
|
||||||
fixture.RunExtended(t)
|
fixture.RunExtended(t)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
3
testdata/cert-manager-webhook-abion-core/config.json
vendored
Normal file
3
testdata/cert-manager-webhook-abion-core/config.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"x-api-key": "key"
|
||||||
|
}
|
1
testdata/my-custom-solver/config.json
vendored
1
testdata/my-custom-solver/config.json
vendored
|
@ -1 +0,0 @@
|
||||||
{}
|
|
Loading…
Reference in a new issue