4
0

meter.go 3.9 KB

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