|
| 1 | +package authority |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto" |
| 5 | + "io" |
| 6 | + |
| 7 | + "go.step.sm/crypto/kms" |
| 8 | + kmsapi "go.step.sm/crypto/kms/apiv1" |
| 9 | + |
| 10 | + "github.com/smallstep/certificates/authority/provisioner" |
| 11 | +) |
| 12 | + |
| 13 | +// Meter wraps the set of defined callbacks for metrics gatherers. |
| 14 | +type Meter interface { |
| 15 | + // X509Signed is called whenever an X509 certificate is signed. |
| 16 | + X509Signed(provisioner.Interface, error) |
| 17 | + |
| 18 | + // X509Renewed is called whenever an X509 certificate is renewed. |
| 19 | + X509Renewed(provisioner.Interface, error) |
| 20 | + |
| 21 | + // X509Rekeyed is called whenever an X509 certificate is rekeyed. |
| 22 | + X509Rekeyed(provisioner.Interface, error) |
| 23 | + |
| 24 | + // X509WebhookAuthorized is called whenever an X509 authoring webhook is called. |
| 25 | + X509WebhookAuthorized(provisioner.Interface, error) |
| 26 | + |
| 27 | + // X509WebhookEnriched is called whenever an X509 enriching webhook is called. |
| 28 | + X509WebhookEnriched(provisioner.Interface, error) |
| 29 | + |
| 30 | + // SSHSigned is called whenever an SSH certificate is signed. |
| 31 | + SSHSigned(provisioner.Interface, error) |
| 32 | + |
| 33 | + // SSHRenewed is called whenever an SSH certificate is renewed. |
| 34 | + SSHRenewed(provisioner.Interface, error) |
| 35 | + |
| 36 | + // SSHRekeyed is called whenever an SSH certificate is rekeyed. |
| 37 | + SSHRekeyed(provisioner.Interface, error) |
| 38 | + |
| 39 | + // SSHWebhookAuthorized is called whenever an SSH authoring webhook is called. |
| 40 | + SSHWebhookAuthorized(provisioner.Interface, error) |
| 41 | + |
| 42 | + // SSHWebhookEnriched is called whenever an SSH enriching webhook is called. |
| 43 | + SSHWebhookEnriched(provisioner.Interface, error) |
| 44 | + |
| 45 | + // KMSSigned is called per KMS signer signature. |
| 46 | + KMSSigned(error) |
| 47 | +} |
| 48 | + |
| 49 | +// noopMeter implements a noop [Meter]. |
| 50 | +type noopMeter struct{} |
| 51 | + |
| 52 | +func (noopMeter) SSHRekeyed(provisioner.Interface, error) {} |
| 53 | +func (noopMeter) SSHRenewed(provisioner.Interface, error) {} |
| 54 | +func (noopMeter) SSHSigned(provisioner.Interface, error) {} |
| 55 | +func (noopMeter) SSHWebhookAuthorized(provisioner.Interface, error) {} |
| 56 | +func (noopMeter) SSHWebhookEnriched(provisioner.Interface, error) {} |
| 57 | +func (noopMeter) X509Rekeyed(provisioner.Interface, error) {} |
| 58 | +func (noopMeter) X509Renewed(provisioner.Interface, error) {} |
| 59 | +func (noopMeter) X509Signed(provisioner.Interface, error) {} |
| 60 | +func (noopMeter) X509WebhookAuthorized(provisioner.Interface, error) {} |
| 61 | +func (noopMeter) X509WebhookEnriched(provisioner.Interface, error) {} |
| 62 | +func (noopMeter) KMSSigned(error) {} |
| 63 | + |
| 64 | +type instrumentedKeyManager struct { |
| 65 | + kms.KeyManager |
| 66 | + meter Meter |
| 67 | +} |
| 68 | + |
| 69 | +func (i *instrumentedKeyManager) CreateSigner(req *kmsapi.CreateSignerRequest) (s crypto.Signer, err error) { |
| 70 | + if s, err = i.KeyManager.CreateSigner(req); err == nil { |
| 71 | + s = &instrumentedKMSSigner{s, i.meter} |
| 72 | + } |
| 73 | + |
| 74 | + return |
| 75 | +} |
| 76 | + |
| 77 | +type instrumentedKMSSigner struct { |
| 78 | + crypto.Signer |
| 79 | + meter Meter |
| 80 | +} |
| 81 | + |
| 82 | +func (i *instrumentedKMSSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) { |
| 83 | + signature, err = i.Signer.Sign(rand, digest, opts) |
| 84 | + i.meter.KMSSigned(err) |
| 85 | + |
| 86 | + return |
| 87 | +} |
0 commit comments