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
19 changes: 11 additions & 8 deletions internal/definitions/definition_handler_identity_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,24 @@ func (dh *definitionHandlers) handleIdentityClaimBroadcast(ctx context.Context,

}

func (dh *definitionHandlers) getExpectedSigner(identity *fftypes.Identity, parent *fftypes.Identity) *fftypes.Identity {
switch {
case identity.Type == fftypes.IdentityTypeNode && parent != nil:
// In the special case of a node, the parent signs it directly
return parent
default:
return identity
}
}

func (dh *definitionHandlers) verifyClaimSignature(ctx context.Context, msg *fftypes.Message, identity *fftypes.Identity, parent *fftypes.Identity) (valid bool) {

author := msg.Header.Author
if author == "" {
return false
}

var expectedSigner *fftypes.Identity
switch {
case identity.Type == fftypes.IdentityTypeNode:
// In the special case of a node, the parent signs it directly
expectedSigner = parent
default:
expectedSigner = identity
}
expectedSigner := dh.getExpectedSigner(identity, parent)

valid = author == expectedSigner.DID ||
(expectedSigner.Type == fftypes.IdentityTypeOrg && author == fmt.Sprintf("%s%s", fftypes.FireFlyOrgDIDPrefix, expectedSigner.ID))
Expand Down
11 changes: 10 additions & 1 deletion internal/definitions/definition_handler_identity_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,17 @@ func (dh *definitionHandlers) handleIdentityUpdateBroadcast(ctx context.Context,
return HandlerResult{Action: ActionReject}, nil
}

parent, retryable, err := dh.identity.VerifyIdentityChain(ctx, identity)
if err != nil && retryable {
return HandlerResult{Action: ActionRetry}, err
} else if err != nil {
log.L(ctx).Infof("Unable to process identity update (parked) %s: %s", msg.Header.ID, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this one parked? This would be for things like an unknown or unconfirmed parent identity. All other failures here seem to result in outright rejection of the update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, we couldn't resolve something up the parent stack. The code in creation (which this was copied from) did a wait at this point, on the basis there could be something in the parent stack still being resolved.

I believe it's technical also possible when replaying a chain for that to be true here. Or at least I've not done enough analysis of the topics used on the messages in that scenario to rule it out. So I left the logic equivalent in create and update paths.

return HandlerResult{Action: ActionWait}, nil
}

// Check the author matches
if identity.DID != msg.Header.Author {
expectedSigner := dh.getExpectedSigner(identity, parent)
if expectedSigner.DID != msg.Header.Author {
log.L(ctx).Warnf("Invalid identity update message %s - wrong author: %s", msg.Header.ID, msg.Header.Author)
return HandlerResult{Action: ActionReject}, nil
}
Expand Down
41 changes: 41 additions & 0 deletions internal/definitions/definition_handler_identity_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestHandleDefinitionIdentityUpdateOk(t *testing.T) {

mim := dh.identity.(*identitymanagermocks.Manager)
mim.On("CachedIdentityLookupByID", ctx, org1.ID).Return(org1, nil)
mim.On("VerifyIdentityChain", ctx, mock.Anything).Return(nil, false, nil)

mdi := dh.database.(*databasemocks.Plugin)
mdi.On("UpsertIdentity", ctx, mock.MatchedBy(func(identity *fftypes.Identity) bool {
Expand Down Expand Up @@ -105,6 +106,7 @@ func TestHandleDefinitionIdentityUpdateUpsertFail(t *testing.T) {

mim := dh.identity.(*identitymanagermocks.Manager)
mim.On("CachedIdentityLookupByID", ctx, org1.ID).Return(org1, nil)
mim.On("VerifyIdentityChain", ctx, mock.Anything).Return(nil, false, nil)

mdi := dh.database.(*databasemocks.Plugin)
mdi.On("UpsertIdentity", ctx, mock.Anything, database.UpsertOptimizationExisting).Return(fmt.Errorf("pop"))
Expand All @@ -127,6 +129,7 @@ func TestHandleDefinitionIdentityInvalidIdentity(t *testing.T) {

mim := dh.identity.(*identitymanagermocks.Manager)
mim.On("CachedIdentityLookupByID", ctx, org1.ID).Return(org1, nil)
mim.On("VerifyIdentityChain", ctx, mock.Anything).Return(nil, false, nil)

action, err := dh.HandleDefinitionBroadcast(ctx, bs, updateMsg, fftypes.DataArray{updateData}, fftypes.NewUUID())
assert.Equal(t, HandlerResult{Action: ActionReject}, action)
Expand All @@ -136,6 +139,44 @@ func TestHandleDefinitionIdentityInvalidIdentity(t *testing.T) {
bs.assertNoFinalizers()
}

func TestHandleDefinitionVerifyFail(t *testing.T) {
dh, bs := newTestDefinitionHandlers(t)
ctx := context.Background()

org1, updateMsg, updateData, _ := testIdentityUpdate(t)
updateMsg.Header.Author = "wrong"

mim := dh.identity.(*identitymanagermocks.Manager)
mim.On("CachedIdentityLookupByID", ctx, org1.ID).Return(org1, nil)
mim.On("VerifyIdentityChain", ctx, mock.Anything).Return(nil, true, fmt.Errorf("pop"))

action, err := dh.HandleDefinitionBroadcast(ctx, bs, updateMsg, fftypes.DataArray{updateData}, fftypes.NewUUID())
assert.Equal(t, HandlerResult{Action: ActionRetry}, action)
assert.Regexp(t, "pop", err)

mim.AssertExpectations(t)
bs.assertNoFinalizers()
}

func TestHandleDefinitionVerifyWait(t *testing.T) {
dh, bs := newTestDefinitionHandlers(t)
ctx := context.Background()

org1, updateMsg, updateData, _ := testIdentityUpdate(t)
updateMsg.Header.Author = "wrong"

mim := dh.identity.(*identitymanagermocks.Manager)
mim.On("CachedIdentityLookupByID", ctx, org1.ID).Return(org1, nil)
mim.On("VerifyIdentityChain", ctx, mock.Anything).Return(nil, false, fmt.Errorf("pop"))

action, err := dh.HandleDefinitionBroadcast(ctx, bs, updateMsg, fftypes.DataArray{updateData}, fftypes.NewUUID())
assert.Equal(t, HandlerResult{Action: ActionWait}, action)
assert.NoError(t, err)

mim.AssertExpectations(t)
bs.assertNoFinalizers()
}

func TestHandleDefinitionIdentityNotFound(t *testing.T) {
dh, bs := newTestDefinitionHandlers(t)
ctx := context.Background()
Expand Down