fix(events): save token and none sources with an empty secret instead of NULL (#2112)
This commit is contained in:
2 files changed
+60
-1
No files matched your search
@@ -109,7 +109,12 @@ func (m *Module) putSource(ctx context.Context, in *putSourceInput) (*httpapi.Re
|
|||||||
// if there is one. It returns the new token when one was issued.
|
// if there is one. It returns the new token when one was issued.
|
||||||
func (m *Module) sourceToSave(in *putSourceInput, stored *store.Source) (store.Source, string, error) {
|
func (m *Module) sourceToSave(in *putSourceInput, stored *store.Source) (store.Source, string, error) {
|
||||||
explicit := in.Body.Verification != ""
|
explicit := in.Body.Verification != ""
|
||||||
src := store.Source{OrgID: m.orgID, Name: in.Name, Connector: in.Body.Connector, Verification: in.Body.Verification}
|
// The secret column is NOT NULL, so a source without a secret carries an
|
||||||
|
// empty value: the driver writes a nil slice as NULL.
|
||||||
|
src := store.Source{
|
||||||
|
OrgID: m.orgID, Name: in.Name, Connector: in.Body.Connector,
|
||||||
|
Verification: in.Body.Verification, HMACSecretSealed: []byte{},
|
||||||
|
}
|
||||||
switch {
|
switch {
|
||||||
case !explicit && stored != nil:
|
case !explicit && stored != nil:
|
||||||
// Omitting the scheme keeps the stored one with its token or secret, so
|
// Omitting the scheme keeps the stored one with its token or secret, so
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package events
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"filippo.io/age"
|
||||||
|
|
||||||
|
"github.com/samcharles93/archie-core/internal/platform/seal"
|
||||||
|
"github.com/samcharles93/archie-core/modules/events/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestSavedSourceSecretIsSetOnlyForHMAC guards the NOT NULL hmac_secret_sealed
|
||||||
|
// column: a nil secret is written as NULL and fails the save, so token and none
|
||||||
|
// sources must carry an empty, non-nil secret and only HMAC a sealed one.
|
||||||
|
func TestSavedSourceSecretIsSetOnlyForHMAC(t *testing.T) {
|
||||||
|
id, err := age.GenerateX25519Identity()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
sealer, err := seal.New(id.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
m := &Module{orgID: "org", sealer: sealer}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
verify string
|
||||||
|
wantSecret bool
|
||||||
|
}{
|
||||||
|
{"none", store.None, false},
|
||||||
|
{"token", store.Token, false},
|
||||||
|
{"hmac", store.HMAC, true},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
in := &putSourceInput{Name: "hook"}
|
||||||
|
in.Body.Connector = "c"
|
||||||
|
in.Body.Verification = tt.verify
|
||||||
|
in.Body.HMACHeader = "X-Signature"
|
||||||
|
in.Body.HMACSecret = "not-a-real-secret" //nolint:gosec // fixture secret to be sealed
|
||||||
|
src, _, err := m.sourceToSave(in, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if src.HMACSecretSealed == nil {
|
||||||
|
t.Fatal("secret is nil, which the store writes as NULL")
|
||||||
|
}
|
||||||
|
if got := len(src.HMACSecretSealed) > 0; got != tt.wantSecret {
|
||||||
|
t.Fatalf("secret set = %v, want %v", got, tt.wantSecret)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user