Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/biz/usecase/tuples/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ type ReadTuplesCommand struct {
// AcquireLockCommand - domain command for acquiring locks (DEPRECATED).
// This command exists only for RBAC backward compatibility and will be removed.
type AcquireLockCommand struct {
LockId string
LockId model.LockId
}

// FencingCheck represents distributed locking parameters.
type FencingCheck struct {
LockId string
LockToken string
LockId model.LockId
LockToken model.LockToken
}

// CreateTuplesResult - result for CreateTuples operation.
Expand All @@ -51,5 +51,5 @@ type DeleteTuplesResult struct {

// AcquireLockResult - result for AcquireLock operation.
type AcquireLockResult struct {
LockToken string
LockToken model.LockToken
}
9 changes: 4 additions & 5 deletions internal/biz/usecase/tuples/tuple_crud_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (uc *TupleCrudUseCase) CreateTuples(ctx context.Context, cmd CreateTuplesCo

var fencing *model.FencingCheck
if cmd.FencingCheck != nil {
fc := model.NewFencingCheck(model.DeserializeLockId(cmd.FencingCheck.LockId), model.DeserializeLockToken(cmd.FencingCheck.LockToken))
fc := model.NewFencingCheck(cmd.FencingCheck.LockId, cmd.FencingCheck.LockToken)
fencing = &fc
}

Expand All @@ -61,7 +61,7 @@ func (uc *TupleCrudUseCase) DeleteTuples(ctx context.Context, cmd DeleteTuplesCo

var fencing *model.FencingCheck
if cmd.FencingCheck != nil {
fc := model.NewFencingCheck(model.DeserializeLockId(cmd.FencingCheck.LockId), model.DeserializeLockToken(cmd.FencingCheck.LockToken))
fc := model.NewFencingCheck(cmd.FencingCheck.LockId, cmd.FencingCheck.LockToken)
fencing = &fc
}

Expand Down Expand Up @@ -96,13 +96,12 @@ func (uc *TupleCrudUseCase) AcquireLock(ctx context.Context, cmd AcquireLockComm
return nil, err
}

lockId := model.DeserializeLockId(cmd.LockId)
result, err := uc.Authz.AcquireLock(ctx, lockId)
result, err := uc.Authz.AcquireLock(ctx, cmd.LockId)
if err != nil {
return nil, err
}

return &AcquireLockResult{
LockToken: result.LockToken().String(),
LockToken: result.LockToken(),
}, nil
}
10 changes: 5 additions & 5 deletions internal/biz/usecase/tuples/tuple_crud_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func TestAcquireLock_Success(t *testing.T) {
uc := New(&data.AllowAllRelationsRepository{}, meta, log.DefaultLogger)

cmd := AcquireLockCommand{
LockId: "lock-123",
LockId: model.DeserializeLockId("lock-123"),
}

result, err := uc.AcquireLock(ctx, cmd)
Expand All @@ -283,7 +283,7 @@ func TestAcquireLock_UsesAcquireLockRelation(t *testing.T) {
uc := New(&data.AllowAllRelationsRepository{}, meta, log.DefaultLogger)

cmd := AcquireLockCommand{
LockId: "lock-123",
LockId: model.DeserializeLockId("lock-123"),
}

_, _ = uc.AcquireLock(ctx, cmd)
Expand All @@ -298,7 +298,7 @@ func TestAcquireLock_MetaAuthzDenied(t *testing.T) {
uc := New(&data.AllowAllRelationsRepository{}, meta, log.DefaultLogger)

cmd := AcquireLockCommand{
LockId: "lock-123",
LockId: model.DeserializeLockId("lock-123"),
}

_, err := uc.AcquireLock(ctx, cmd)
Expand All @@ -312,7 +312,7 @@ func TestAcquireLock_MetaAuthzContextMissing(t *testing.T) {
uc := New(&data.AllowAllRelationsRepository{}, meta, log.DefaultLogger)

cmd := AcquireLockCommand{
LockId: "lock-123",
LockId: model.DeserializeLockId("lock-123"),
}

_, err := uc.AcquireLock(context.Background(), cmd)
Expand Down Expand Up @@ -528,7 +528,7 @@ func TestWhitelistMetaAuthorizer_Integration_AllOperations(t *testing.T) {
})

t.Run("AcquireLock", func(t *testing.T) {
cmd := AcquireLockCommand{LockId: "lock-123"}
cmd := AcquireLockCommand{LockId: model.DeserializeLockId("lock-123")}
_, err := uc.AcquireLock(ctx, cmd)
require.NoError(t, err)
})
Expand Down
51 changes: 36 additions & 15 deletions internal/service/tuples/tuples.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,18 @@ func (s *TupleService) ReadTuples(req *pb.ReadTuplesRequest, stream pb.KesselTup
// AcquireLock acquires a distributed lock (DEPRECATED).
// This endpoint exists only for RBAC backward compatibility.
func (s *TupleService) AcquireLock(ctx context.Context, req *pb.AcquireLockRequest) (*pb.AcquireLockResponse, error) {
cmd := toAcquireLockCommand(req)
cmd, err := toAcquireLockCommand(req)
if err != nil {
return nil, err
}

result, err := s.Ctl.AcquireLock(ctx, cmd)
if err != nil {
return nil, err
}

return &pb.AcquireLockResponse{
LockToken: result.LockToken,
LockToken: result.LockToken.String(),
}, nil
}

Expand All @@ -115,12 +118,11 @@ func toCreateTuplesCommand(req *pb.CreateTuplesRequest) (tuplesctl.CreateTuplesC
Upsert: req.GetUpsert(),
}

if req.GetFencingCheck() != nil {
cmd.FencingCheck = &tuplesctl.FencingCheck{
LockId: req.GetFencingCheck().GetLockId(),
LockToken: req.GetFencingCheck().GetLockToken(),
}
fc, err := fencingCheckFromProto(req.GetFencingCheck())
if err != nil {
return tuplesctl.CreateTuplesCommand{}, err
}
cmd.FencingCheck = fc

return cmd, nil
}
Expand All @@ -130,12 +132,11 @@ func toDeleteTuplesCommand(req *pb.DeleteTuplesRequest) (tuplesctl.DeleteTuplesC
Filter: tupleFilterFromProto(req.GetFilter()),
}

if req.GetFencingCheck() != nil {
cmd.FencingCheck = &tuplesctl.FencingCheck{
LockId: req.GetFencingCheck().GetLockId(),
LockToken: req.GetFencingCheck().GetLockToken(),
}
fc, err := fencingCheckFromProto(req.GetFencingCheck())
if err != nil {
return tuplesctl.DeleteTuplesCommand{}, err
}
cmd.FencingCheck = fc

return cmd, nil
}
Expand All @@ -148,14 +149,34 @@ func toReadTuplesCommand(req *pb.ReadTuplesRequest) (tuplesctl.ReadTuplesCommand
}, nil
}

func toAcquireLockCommand(req *pb.AcquireLockRequest) tuplesctl.AcquireLockCommand {
return tuplesctl.AcquireLockCommand{
LockId: req.GetLockId(),
func toAcquireLockCommand(req *pb.AcquireLockRequest) (tuplesctl.AcquireLockCommand, error) {
lockId, err := model.NewLockId(req.GetLockId())
if err != nil {
return tuplesctl.AcquireLockCommand{}, fmt.Errorf("invalid lock id: %w", err)
}
return tuplesctl.AcquireLockCommand{LockId: lockId}, nil
}

// --- proto → domain helpers ---

func fencingCheckFromProto(fc *pb.RelationFencingCheck) (*tuplesctl.FencingCheck, error) {
if fc == nil {
return nil, nil
}
lockId, err := model.NewLockId(fc.GetLockId())
if err != nil {
return nil, fmt.Errorf("invalid fencing lock id: %w", err)
}
lockToken, err := model.NewLockToken(fc.GetLockToken())
if err != nil {
return nil, fmt.Errorf("invalid fencing lock token: %w", err)
}
return &tuplesctl.FencingCheck{
LockId: lockId,
LockToken: lockToken,
}, nil
}

func relationshipsToRelationsTuples(rels []*pb.Relationship) ([]model.RelationsTuple, error) {
tuples := make([]model.RelationsTuple, len(rels))
for i, rel := range rels {
Expand Down
27 changes: 17 additions & 10 deletions internal/service/tuples/tuples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ func TestToCreateTuplesCommand(t *testing.T) {

require.NoError(t, err)
require.NotNil(t, cmd.FencingCheck)
assert.Equal(t, "lock-1", cmd.FencingCheck.LockId)
assert.Equal(t, "token-1", cmd.FencingCheck.LockToken)
expectedLockId := model.DeserializeLockId("lock-1")
expectedLockToken := model.DeserializeLockToken("token-1")
assert.Equal(t, expectedLockId, cmd.FencingCheck.LockId)
assert.Equal(t, expectedLockToken, cmd.FencingCheck.LockToken)
})

t.Run("invalid relationship - bad resource ID", func(t *testing.T) {
Expand Down Expand Up @@ -127,7 +129,10 @@ func TestToDeleteTuplesCommand(t *testing.T) {

require.NoError(t, err)
require.NotNil(t, cmd.FencingCheck)
assert.Equal(t, "lock-2", cmd.FencingCheck.LockId)
expectedLockId := model.DeserializeLockId("lock-2")
expectedLockToken := model.DeserializeLockToken("token-2")
assert.Equal(t, expectedLockId, cmd.FencingCheck.LockId)
assert.Equal(t, expectedLockToken, cmd.FencingCheck.LockToken)
})
}

Expand Down Expand Up @@ -171,9 +176,11 @@ func TestToAcquireLockCommand(t *testing.T) {
LockId: "lock-123",
}

cmd := toAcquireLockCommand(req)
cmd, err := toAcquireLockCommand(req)

assert.Equal(t, "lock-123", cmd.LockId)
require.NoError(t, err)
expectedLockId := model.DeserializeLockId("lock-123")
assert.Equal(t, expectedLockId, cmd.LockId)
}

func TestRelationshipToRelationsTuple(t *testing.T) {
Expand Down Expand Up @@ -462,8 +469,8 @@ func TestReadTuplesItemToProto(t *testing.T) {
object,
model.DeserializeRelation("member"),
model.NewSubjectReferenceWithoutRelation(subRes),
"",
"",
model.DeserializeContinuationToken(""),
model.DeserializeConsistencyToken(""),
)

result := readTuplesItemToProto(item)
Expand Down Expand Up @@ -497,8 +504,8 @@ func TestReadTuplesItemToProto(t *testing.T) {
object,
model.DeserializeRelation("member"),
model.NewSubjectReference(subRes, &r),
"",
"",
model.DeserializeContinuationToken(""),
model.DeserializeConsistencyToken(""),
)

result := readTuplesItemToProto(item)
Expand All @@ -524,7 +531,7 @@ func TestReadTuplesItemToProto(t *testing.T) {
object,
model.DeserializeRelation("member"),
model.NewSubjectReferenceWithoutRelation(subRes),
"page-token-abc",
model.DeserializeContinuationToken("page-token-abc"),
model.DeserializeConsistencyToken("ct-123"),
)

Expand Down
Loading