Просмотр исходного кода

Add method to add the provisioner name

This commit adds the RequestBodyOption WithProvisionerName that adds the
provisioner name if the provisioner is not nil
Mariano Cano 8 месяцев назад
Родитель
Сommit
b289e6fdc4
4 измененных файлов с 29 добавлено и 12 удалено
  1. 2 6
      authority/ssh.go
  2. 2 6
      authority/tls.go
  3. 16 0
      webhook/options.go
  4. 9 0
      webhook/options_test.go

+ 2 - 6
authority/ssh.go

@@ -699,10 +699,8 @@ func (a *Authority) callEnrichingWebhooksSSH(ctx context.Context, prov provision
 	var whEnrichReq *webhook.RequestBody
 	if whEnrichReq, err = webhook.NewRequestBody(
 		webhook.WithSSHCertificateRequest(cr),
+		webhook.WithProvisionerName(prov),
 	); err == nil {
-		if prov != nil {
-			whEnrichReq.ProvisionerName = prov.GetName()
-		}
 		err = webhookCtl.Enrich(ctx, whEnrichReq)
 	}
 
@@ -718,10 +716,8 @@ func (a *Authority) callAuthorizingWebhooksSSH(ctx context.Context, prov provisi
 	var whAuthBody *webhook.RequestBody
 	if whAuthBody, err = webhook.NewRequestBody(
 		webhook.WithSSHCertificate(cert, certTpl),
+		webhook.WithProvisionerName(prov),
 	); err == nil {
-		if prov != nil {
-			whAuthBody.ProvisionerName = prov.GetName()
-		}
 		err = webhookCtl.Authorize(ctx, whAuthBody)
 	}
 

+ 2 - 6
authority/tls.go

@@ -1052,10 +1052,8 @@ func (a *Authority) callEnrichingWebhooksX509(ctx context.Context, prov provisio
 	if whEnrichReq, err = webhook.NewRequestBody(
 		webhook.WithX509CertificateRequest(csr),
 		webhook.WithAttestationData(attested),
+		webhook.WithProvisionerName(prov),
 	); err == nil {
-		if prov != nil {
-			whEnrichReq.ProvisionerName = prov.GetName()
-		}
 		err = webhookCtl.Enrich(ctx, whEnrichReq)
 	}
 
@@ -1079,10 +1077,8 @@ func (a *Authority) callAuthorizingWebhooksX509(ctx context.Context, prov provis
 	if whAuthBody, err = webhook.NewRequestBody(
 		webhook.WithX509Certificate(cert, leaf),
 		webhook.WithAttestationData(attested),
+		webhook.WithProvisionerName(prov),
 	); err == nil {
-		if prov != nil {
-			whAuthBody.ProvisionerName = prov.GetName()
-		}
 		err = webhookCtl.Authorize(ctx, whAuthBody)
 	}
 

+ 16 - 0
webhook/options.go

@@ -10,6 +10,10 @@ import (
 
 type RequestBodyOption func(*RequestBody) error
 
+type Provisioner interface {
+	GetName() string
+}
+
 func NewRequestBody(options ...RequestBodyOption) (*RequestBody, error) {
 	rb := &RequestBody{}
 
@@ -22,6 +26,18 @@ func NewRequestBody(options ...RequestBodyOption) (*RequestBody, error) {
 	return rb, nil
 }
 
+// WithProvisionerName sets the provisioner name in the webhook request body
+// using the name from the given provisioner. If p is nil, the provisioner name
+// is left unchanged.
+func WithProvisionerName(p Provisioner) RequestBodyOption {
+	return func(rb *RequestBody) error {
+		if p != nil {
+			rb.ProvisionerName = p.GetName()
+		}
+		return nil
+	}
+}
+
 func WithX509CertificateRequest(cr *x509.CertificateRequest) RequestBodyOption {
 	return func(rb *RequestBody) error {
 		rb.X509CertificateRequest = &X509CertificateRequest{

+ 9 - 0
webhook/options_test.go

@@ -13,6 +13,12 @@ import (
 	"golang.org/x/crypto/ssh"
 )
 
+type fakeProvisioner string
+
+func (f fakeProvisioner) GetName() string {
+	return string(f)
+}
+
 func TestNewRequestBody(t *testing.T) {
 	t1 := time.Now()
 	t2 := t1.Add(time.Hour)
@@ -43,6 +49,7 @@ func TestNewRequestBody(t *testing.T) {
 		},
 		"X509 Certificate Request": {
 			options: []RequestBodyOption{
+				WithProvisionerName(fakeProvisioner("test@example.com")),
 				WithX509CertificateRequest(&x509.CertificateRequest{
 					PublicKeyAlgorithm: x509.ECDSA,
 					Subject:            pkix.Name{CommonName: "foo"},
@@ -50,6 +57,7 @@ func TestNewRequestBody(t *testing.T) {
 				}),
 			},
 			want: &RequestBody{
+				ProvisionerName: "test@example.com",
 				X509CertificateRequest: &X509CertificateRequest{
 					CertificateRequest: &x509util.CertificateRequest{
 						PublicKeyAlgorithm: x509.ECDSA,
@@ -63,6 +71,7 @@ func TestNewRequestBody(t *testing.T) {
 		},
 		"X509 Certificate": {
 			options: []RequestBodyOption{
+				WithProvisionerName(nil),
 				WithX509Certificate(&x509util.Certificate{}, &x509.Certificate{
 					NotBefore:          t1,
 					NotAfter:           t2,