forked from mtusi2026/mtusi2026lab3
Реализовать lab3 в общем Helm-релизе с Traefik, TLS и HPA
This commit is contained in:
@@ -106,3 +106,14 @@ func decodeCursorPosition(payload []byte, resource string) (Position, error) {
|
||||
}
|
||||
return Position{CreatedAt: cursor.Time, ID: cursor.ID}, nil
|
||||
}
|
||||
|
||||
// NewShared creates a codec from the common 256-bit key provisioned to every replica.
|
||||
func NewShared(encoded string) (*Codec, error) {
|
||||
key, err := base64.StdEncoding.DecodeString(encoded)
|
||||
if err != nil || len(key) != cursorSigningKeyBytes {
|
||||
return nil, fmt.Errorf("CURSOR_SIGNING_KEY must encode exactly 32 bytes as base64")
|
||||
}
|
||||
codec := &Codec{}
|
||||
copy(codec.key[:], key)
|
||||
return codec, nil
|
||||
}
|
||||
|
||||
@@ -177,3 +177,36 @@ func encodeFixture(t *testing.T, codec *Codec, resource string) string {
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
// TestSharedCodec verifies a second process can verify the first process's cursor.
|
||||
func TestSharedCodec(t *testing.T) {
|
||||
key := base64.StdEncoding.EncodeToString(make([]byte, 32))
|
||||
first, err := NewShared(key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := NewShared(key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
position := Position{CreatedAt: time.Now().UTC(), ID: uuid.New()}
|
||||
encoded, err := first.Encode("chat", position)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := second.Decode(encoded, "chat")
|
||||
if err != nil || result != position {
|
||||
t.Fatalf("cross-replica cursor: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSharedCodecRejectsInvalidKey requires a full 256-bit decoded secret.
|
||||
func TestSharedCodecRejectsInvalidKey(t *testing.T) {
|
||||
for _, key := range []string{"", "broken!", base64.StdEncoding.EncodeToString([]byte("short"))} {
|
||||
t.Run(key, func(t *testing.T) {
|
||||
if _, err := NewShared(key); err == nil {
|
||||
t.Fatal("invalid key accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user