2023-04-28 13:47:22 +00:00
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-04-28 15:15:05 +00:00
|
|
|
"go.step.sm/linkedca"
|
|
|
|
|
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
|
|
|
"github.com/smallstep/certificates/webhook"
|
2023-04-28 13:47:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Controller struct {
|
2023-04-28 15:15:05 +00:00
|
|
|
client *http.Client
|
|
|
|
webhooks []*provisioner.Webhook
|
2023-04-28 13:47:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 15:15:05 +00:00
|
|
|
func New(webhooks []*provisioner.Webhook) (*Controller, error) {
|
|
|
|
return &Controller{
|
|
|
|
client: http.DefaultClient,
|
|
|
|
webhooks: webhooks,
|
|
|
|
}, nil
|
2023-04-28 13:47:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 15:15:05 +00:00
|
|
|
func (c *Controller) Validate(challenge string) error {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
2023-04-28 13:47:22 +00:00
|
|
|
}
|
2023-04-28 15:15:05 +00:00
|
|
|
for _, wh := range c.webhooks {
|
|
|
|
if wh.Kind != linkedca.Webhook_SCEPCHALLENGE.String() {
|
|
|
|
continue
|
2023-04-28 13:47:22 +00:00
|
|
|
}
|
2023-04-28 15:15:05 +00:00
|
|
|
if !c.isCertTypeOK(wh) {
|
|
|
|
continue
|
2023-04-28 13:47:22 +00:00
|
|
|
}
|
2023-04-28 15:15:05 +00:00
|
|
|
req := &webhook.RequestBody{
|
|
|
|
SCEPChallenge: challenge,
|
2023-04-28 13:47:22 +00:00
|
|
|
}
|
2023-04-28 15:15:05 +00:00
|
|
|
resp, err := wh.Do(c.client, req, nil) // TODO(hs): support templated URL?
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-04-28 13:47:22 +00:00
|
|
|
}
|
2023-04-28 15:15:05 +00:00
|
|
|
if !resp.Allow {
|
|
|
|
return provisioner.ErrWebhookDenied
|
2023-04-28 13:47:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-28 15:15:05 +00:00
|
|
|
return nil
|
2023-04-28 13:47:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 15:15:05 +00:00
|
|
|
func (c *Controller) isCertTypeOK(wh *provisioner.Webhook) bool {
|
|
|
|
return linkedca.Webhook_X509.String() == wh.CertType
|
2023-04-28 13:47:22 +00:00
|
|
|
}
|