Skip to content

Commit

Permalink
moved kms instrumentation to the meter file
Browse files Browse the repository at this point in the history
  • Loading branch information
azazeal committed Jan 23, 2024
1 parent 78a7e2d commit d40182a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
29 changes: 0 additions & 29 deletions authority/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"io"
"log"
"net/http"
"strings"
Expand Down Expand Up @@ -972,31 +971,3 @@ func (a *Authority) startCRLGenerator() error {

return nil
}

type instrumentedKeyManager struct {
kms.KeyManager
meter Meter
}

func (i *instrumentedKeyManager) CreateSigner(req *kmsapi.CreateSignerRequest) (s crypto.Signer, err error) {
if s, err = i.KeyManager.CreateSigner(req); err == nil {
s = &instrumentedKMSSigner{s, i.meter}
}

return
}

type instrumentedKMSSigner struct {
crypto.Signer
meter Meter
}

func (i *instrumentedKMSSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
s, err := i.Signer.Sign(rand, digest, opts)
defer i.meter.KMSSigned(err)

if err != nil {
return nil, err
}
return s, nil
}
38 changes: 37 additions & 1 deletion authority/meter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package authority

import "github.com/smallstep/certificates/authority/provisioner"
import (
"crypto"
"io"

"go.step.sm/crypto/kms"
kmsapi "go.step.sm/crypto/kms/apiv1"

"github.com/smallstep/certificates/authority/provisioner"
)

// Meter wraps the set of defined callbacks for metrics gatherers.
type Meter interface {
Expand Down Expand Up @@ -52,3 +60,31 @@ func (noopMeter) X509Signed(provisioner.Interface, error) {}
func (noopMeter) X509WebhookAuthorized(provisioner.Interface, error) {}
func (noopMeter) X509WebhookEnriched(provisioner.Interface, error) {}
func (noopMeter) KMSSigned(error) {}

type instrumentedKeyManager struct {
kms.KeyManager
meter Meter
}

func (i *instrumentedKeyManager) CreateSigner(req *kmsapi.CreateSignerRequest) (s crypto.Signer, err error) {
if s, err = i.KeyManager.CreateSigner(req); err == nil {
s = &instrumentedKMSSigner{s, i.meter}
}

return
}

type instrumentedKMSSigner struct {
crypto.Signer
meter Meter
}

func (i *instrumentedKMSSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
s, err := i.Signer.Sign(rand, digest, opts)
defer i.meter.KMSSigned(err)

if err != nil {
return nil, err
}
return s, nil
}

0 comments on commit d40182a

Please sign in to comment.