2021-12-06 16:57:35 +01:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
2022-09-12 17:18:08 +01:00
|
|
|
"fmt"
|
2021-12-06 16:57:35 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-11-22 12:56:43 +02:00
|
|
|
"go.uber.org/mock/gomock"
|
2021-12-06 16:57:35 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-10-20 13:36:52 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/keypair"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-12-06 16:57:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestKeyProjection_reduces(t *testing.T) {
|
|
|
|
type args struct {
|
2022-01-03 09:19:07 +01:00
|
|
|
event func(t *testing.T) eventstore.Event
|
2021-12-06 16:57:35 +01:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2022-01-03 09:19:07 +01:00
|
|
|
reduce func(event eventstore.Event) (*handler.Statement, error)
|
2021-12-06 16:57:35 +01:00
|
|
|
want wantReduce
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reduceKeyPairAdded",
|
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
keypair.AddedEventType,
|
|
|
|
keypair.AggregateType,
|
2024-08-14 17:18:14 +03:00
|
|
|
keypairAddedEventData(crypto.KeyUsageSigning, time.Now().Add(time.Hour)),
|
2023-10-19 12:19:10 +02:00
|
|
|
), keypair.AddedEventMapper),
|
2021-12-06 16:57:35 +01:00
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&keyProjection{encryptionAlgorithm: crypto.CreateMockEncryptionAlg(gomock.NewController(t))}).reduceKeyPairAdded,
|
2021-12-06 16:57:35 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: eventstore.AggregateType("key_pair"),
|
|
|
|
sequence: 15,
|
2021-12-06 16:57:35 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "INSERT INTO projections.keys4 (id, creation_date, change_date, resource_owner, instance_id, sequence, algorithm, use) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
|
2021-12-06 16:57:35 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
anyArg{},
|
|
|
|
anyArg{},
|
|
|
|
"ro-id",
|
2022-03-23 09:02:39 +01:00
|
|
|
"instance-id",
|
2021-12-06 16:57:35 +01:00
|
|
|
uint64(15),
|
|
|
|
"algorithm",
|
2024-08-14 17:18:14 +03:00
|
|
|
crypto.KeyUsageSigning,
|
2021-12-06 16:57:35 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "INSERT INTO projections.keys4_private (id, instance_id, expiry, key) VALUES ($1, $2, $3, $4)",
|
2021-12-06 16:57:35 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
2022-05-25 14:15:13 +02:00
|
|
|
"instance-id",
|
2021-12-06 16:57:35 +01:00
|
|
|
anyArg{},
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("privateKey"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "INSERT INTO projections.keys4_public (id, instance_id, expiry, key) VALUES ($1, $2, $3, $4)",
|
2021-12-06 16:57:35 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
2022-05-25 14:15:13 +02:00
|
|
|
"instance-id",
|
2021-12-06 16:57:35 +01:00
|
|
|
anyArg{},
|
|
|
|
[]byte("publicKey"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceKeyPairAdded expired",
|
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
keypair.AddedEventType,
|
|
|
|
keypair.AggregateType,
|
2024-08-14 17:18:14 +03:00
|
|
|
keypairAddedEventData(crypto.KeyUsageSigning, time.Now().Add(-time.Hour)),
|
2023-10-19 12:19:10 +02:00
|
|
|
), keypair.AddedEventMapper),
|
2021-12-06 16:57:35 +01:00
|
|
|
},
|
2022-06-14 07:51:00 +02:00
|
|
|
reduce: (&keyProjection{}).reduceKeyPairAdded,
|
2021-12-06 16:57:35 +01:00
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: eventstore.AggregateType("key_pair"),
|
|
|
|
sequence: 15,
|
|
|
|
executer: &testExecuter{},
|
2021-12-06 16:57:35 +01:00
|
|
|
},
|
|
|
|
},
|
2022-10-20 13:36:52 +01:00
|
|
|
{
|
2022-11-10 11:59:33 +01:00
|
|
|
name: "instance reduceInstanceRemoved",
|
2022-10-20 13:36:52 +01:00
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
instance.InstanceRemovedEventType,
|
|
|
|
instance.AggregateType,
|
|
|
|
nil,
|
|
|
|
), instance.InstanceRemovedEventMapper),
|
2022-10-20 13:36:52 +01:00
|
|
|
},
|
|
|
|
reduce: reduceInstanceRemovedHelper(KeyColumnInstanceID),
|
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
2022-10-20 13:36:52 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "DELETE FROM projections.keys4 WHERE (instance_id = $1)",
|
2022-10-20 13:36:52 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-09-12 17:18:08 +01:00
|
|
|
{
|
|
|
|
name: "reduceCertificateAdded",
|
|
|
|
args: args{
|
2023-10-19 12:19:10 +02:00
|
|
|
event: getEvent(
|
|
|
|
testEvent(
|
|
|
|
keypair.AddedCertificateEventType,
|
|
|
|
keypair.AggregateType,
|
2024-08-14 17:18:14 +03:00
|
|
|
certificateAddedEventData(crypto.KeyUsageSAMLMetadataSigning, time.Now().Add(time.Hour)),
|
2023-10-19 12:19:10 +02:00
|
|
|
), keypair.AddedCertificateEventMapper),
|
2022-09-12 17:18:08 +01:00
|
|
|
},
|
|
|
|
reduce: (&keyProjection{certEncryptionAlgorithm: crypto.CreateMockEncryptionAlg(gomock.NewController(t))}).reduceCertificateAdded,
|
|
|
|
want: wantReduce{
|
2023-10-19 12:19:10 +02:00
|
|
|
aggregateType: eventstore.AggregateType("key_pair"),
|
|
|
|
sequence: 15,
|
2022-09-12 17:18:08 +01:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-11-30 17:01:17 +01:00
|
|
|
expectedStmt: "INSERT INTO projections.keys4_certificate (id, instance_id, expiry, certificate) VALUES ($1, $2, $3, $4)",
|
2022-09-12 17:18:08 +01:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
"instance-id",
|
|
|
|
anyArg{},
|
|
|
|
[]byte("privateKey"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-06 16:57:35 +01:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
event := baseEvent(t)
|
|
|
|
got, err := tt.reduce(event)
|
2023-12-08 16:30:55 +02:00
|
|
|
if !zerrors.IsErrorInvalidArgument(err) {
|
2021-12-06 16:57:35 +01:00
|
|
|
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
event = tt.args.event(t)
|
|
|
|
got, err = tt.reduce(event)
|
2022-10-20 13:36:52 +01:00
|
|
|
assertReduce(t, got, err, KeyProjectionTable, tt.want)
|
2021-12-06 16:57:35 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 17:18:14 +03:00
|
|
|
func keypairAddedEventData(usage crypto.KeyUsage, t time.Time) []byte {
|
2022-09-12 17:18:08 +01:00
|
|
|
return []byte(`{"algorithm": "algorithm", "usage": ` + fmt.Sprintf("%d", usage) + `, "privateKey": {"key": {"cryptoType": 0, "algorithm": "enc", "keyID": "id", "crypted": "cHJpdmF0ZUtleQ=="}, "expiry": "` + t.Format(time.RFC3339) + `"}, "publicKey": {"key": {"cryptoType": 0, "algorithm": "enc", "keyID": "id", "crypted": "cHVibGljS2V5"}, "expiry": "` + t.Format(time.RFC3339) + `"}}`)
|
|
|
|
}
|
|
|
|
|
2024-08-14 17:18:14 +03:00
|
|
|
func certificateAddedEventData(usage crypto.KeyUsage, t time.Time) []byte {
|
2022-09-12 17:18:08 +01:00
|
|
|
return []byte(`{"algorithm": "algorithm", "usage": ` + fmt.Sprintf("%d", usage) + `, "certificate": {"key": {"cryptoType": 0, "algorithm": "enc", "keyID": "id", "crypted": "cHJpdmF0ZUtleQ=="}, "expiry": "` + t.Format(time.RFC3339) + `"}}`)
|
2021-12-06 16:57:35 +01:00
|
|
|
}
|