Add cache validation

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2026-02-13 12:43:25 +01:00
parent a1e726ec49
commit 9cc6f2afc2

View File

@@ -48,6 +48,11 @@ type (
timeout time.Duration timeout time.Duration
} }
cachedSecret struct {
encryptedSecret []byte
plaintext string
}
Config struct { Config struct {
Interval time.Duration Interval time.Duration
Timeout time.Duration Timeout time.Duration
@@ -201,7 +206,10 @@ func (s *Sender) deliverToConfiguration(
func (s *Sender) getSigningSecret(webhookConfigurationID string, encryptedSigningSecret []byte) (string, error) { func (s *Sender) getSigningSecret(webhookConfigurationID string, encryptedSigningSecret []byte) (string, error) {
if cached, ok := s.cache.Load(webhookConfigurationID); ok { if cached, ok := s.cache.Load(webhookConfigurationID); ok {
return cached.(string), nil entry := cached.(*cachedSecret)
if bytes.Equal(entry.encryptedSecret, encryptedSigningSecret) {
return entry.plaintext, nil
}
} }
plaintext, err := cipher.Decrypt(encryptedSigningSecret, s.encryptionKey) plaintext, err := cipher.Decrypt(encryptedSigningSecret, s.encryptionKey)
@@ -210,7 +218,10 @@ func (s *Sender) getSigningSecret(webhookConfigurationID string, encryptedSignin
} }
signingSecret := string(plaintext) signingSecret := string(plaintext)
s.cache.Store(webhookConfigurationID, signingSecret) s.cache.Store(webhookConfigurationID, &cachedSecret{
encryptedSecret: encryptedSigningSecret,
plaintext: signingSecret,
})
return signingSecret, nil return signingSecret, nil
} }
@@ -231,7 +242,6 @@ func (s *Sender) doHTTPCall(
"createdAt": webhookData.CreatedAt, "createdAt": webhookData.CreatedAt,
"data": webhookData.Data, "data": webhookData.Data,
} }
body, err := json.Marshal(payload) body, err := json.Marshal(payload)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot marshal webhook payload: %w", err) return nil, fmt.Errorf("cannot marshal webhook payload: %w", err)