|
1 | 1 | package authority |
2 | 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 | + |
3 | 13 | // Meter wraps the set of defined callbacks for metrics gatherers. |
4 | 14 | type Meter interface { |
5 | 15 | // X509Signed is called whenever an X509 certificate is signed. |
6 | | - X509Signed(provisioner string, success bool) |
| 16 | + X509Signed(provisioner.Interface, error) |
7 | 17 |
|
8 | 18 | // X509Renewed is called whenever an X509 certificate is renewed. |
9 | | - X509Renewed(provisioner string, success bool) |
| 19 | + X509Renewed(provisioner.Interface, error) |
10 | 20 |
|
11 | 21 | // X509Rekeyed is called whenever an X509 certificate is rekeyed. |
12 | | - X509Rekeyed(provisioner string, success bool) |
| 22 | + X509Rekeyed(provisioner.Interface, error) |
13 | 23 |
|
14 | 24 | // X509WebhookAuthorized is called whenever an X509 authoring webhook is called. |
15 | | - X509WebhookAuthorized(provisioner string, success bool) |
| 25 | + X509WebhookAuthorized(provisioner.Interface, error) |
16 | 26 |
|
17 | 27 | // X509WebhookEnriched is called whenever an X509 enriching webhook is called. |
18 | | - X509WebhookEnriched(provisioner string, success bool) |
| 28 | + X509WebhookEnriched(provisioner.Interface, error) |
19 | 29 |
|
20 | 30 | // SSHSigned is called whenever an SSH certificate is signed. |
21 | | - SSHSigned(provisioner string, success bool) |
| 31 | + SSHSigned(provisioner.Interface, error) |
22 | 32 |
|
23 | 33 | // SSHRenewed is called whenever an SSH certificate is renewed. |
24 | | - SSHRenewed(provisioner string, success bool) |
| 34 | + SSHRenewed(provisioner.Interface, error) |
25 | 35 |
|
26 | 36 | // SSHRekeyed is called whenever an SSH certificate is rekeyed. |
27 | | - SSHRekeyed(provisioner string, success bool) |
| 37 | + SSHRekeyed(provisioner.Interface, error) |
28 | 38 |
|
29 | 39 | // SSHWebhookAuthorized is called whenever an SSH authoring webhook is called. |
30 | | - SSHWebhookAuthorized(provisioner string, success bool) |
| 40 | + SSHWebhookAuthorized(provisioner.Interface, error) |
31 | 41 |
|
32 | 42 | // SSHWebhookEnriched is called whenever an SSH enriching webhook is called. |
33 | | - SSHWebhookEnriched(provisioner string, success bool) |
| 43 | + SSHWebhookEnriched(provisioner.Interface, error) |
34 | 44 |
|
35 | 45 | // KMSSigned is called per KMS signer signature. |
36 | | - KMSSigned() |
37 | | - |
38 | | - // KMSSigned is called per KMS signer signature error. |
39 | | - KMSError() |
| 46 | + KMSSigned(error) |
40 | 47 | } |
41 | 48 |
|
42 | 49 | // noopMeter implements a noop [Meter]. |
43 | 50 | type noopMeter struct{} |
44 | 51 |
|
45 | | -func (noopMeter) SSHRekeyed(string, bool) {} |
46 | | -func (noopMeter) SSHRenewed(string, bool) {} |
47 | | -func (noopMeter) SSHSigned(string, bool) {} |
48 | | -func (noopMeter) SSHWebhookAuthorized(string, bool) {} |
49 | | -func (noopMeter) SSHWebhookEnriched(string, bool) {} |
50 | | -func (noopMeter) X509Rekeyed(string, bool) {} |
51 | | -func (noopMeter) X509Renewed(string, bool) {} |
52 | | -func (noopMeter) X509Signed(string, bool) {} |
53 | | -func (noopMeter) X509WebhookAuthorized(string, bool) {} |
54 | | -func (noopMeter) X509WebhookEnriched(string, bool) {} |
55 | | -func (noopMeter) KMSSigned() {} |
56 | | -func (noopMeter) KMSError() {} |
| 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