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
58 changes: 57 additions & 1 deletion docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9069,6 +9069,61 @@ paths:
description: Success
default:
description: ""
/network/identities/{did}:
get:
description: 'TODO: Description'
operationId: getIdentityByDID
parameters:
- description: 'TODO: Description'
in: path
name: did
required: true
schema:
type: string
- description: Server-side request timeout (millseconds, or set a custom suffix
like 10s)
in: header
name: Request-Timeout
schema:
default: 120s
type: string
responses:
"200":
content:
application/json:
schema:
properties:
created: {}
description:
type: string
did:
type: string
id: {}
messages:
properties:
claim: {}
update: {}
verification: {}
type: object
name:
type: string
namespace:
type: string
parent: {}
profile:
additionalProperties: {}
type: object
type:
enum:
- org
- node
- custom
type: string
updated: {}
type: object
description: Success
default:
description: ""
/network/nodes:
get:
description: 'TODO: Description'
Expand Down Expand Up @@ -9542,7 +9597,8 @@ paths:
type: string
name:
type: string
parent: {}
parent:
type: string
profile:
additionalProperties: {}
type: object
Expand Down
43 changes: 43 additions & 0 deletions internal/apiserver/route_get_net_did.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apiserver

import (
"net/http"

"github.com/hyperledger/firefly/internal/i18n"
"github.com/hyperledger/firefly/internal/oapispec"
"github.com/hyperledger/firefly/pkg/fftypes"
)

var getIdentityByDID = &oapispec.Route{
Name: "getIdentityByDID",
Path: "network/identities/{did:.+}",
Method: http.MethodGet,
PathParams: []*oapispec.PathParam{
{Name: "did", Description: i18n.MsgTBD},
},
FilterFactory: nil,
Description: i18n.MsgTBD,
JSONInputValue: nil,
JSONOutputValue: func() interface{} { return &fftypes.Identity{} },
JSONOutputCodes: []int{http.StatusOK},
JSONHandler: func(r *oapispec.APIRequest) (output interface{}, err error) {
output, err = getOr(r.Ctx).NetworkMap().GetIdentityByDID(r.Ctx, r.PP["did"])
return output, err
},
}
42 changes: 42 additions & 0 deletions internal/apiserver/route_get_net_did_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright © 2021 Kaleido, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm surprised the linter didn't complain about the year here

//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apiserver

import (
"net/http/httptest"
"testing"

"github.com/hyperledger/firefly/mocks/networkmapmocks"
"github.com/hyperledger/firefly/pkg/fftypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestGetIdentityByDID(t *testing.T) {
o, r := newTestAPIServer()
nmn := &networkmapmocks.Manager{}
o.On("NetworkMap").Return(nmn)
req := httptest.NewRequest("GET", "/api/v1/network/identities/did:firefly:org/org_1", nil)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

nmn.On("GetIdentityByDID", mock.Anything, "did:firefly:org/org_1").
Return(&fftypes.Identity{}, nil)
r.ServeHTTP(res, req)

assert.Equal(t, 200, res.Result().StatusCode)
}
1 change: 1 addition & 0 deletions internal/apiserver/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var routes = []*oapispec.Route{
getGroups,
getIdentities,
getIdentityByID,
getIdentityByDID,
getIdentityDID,
getIdentityVerifiers,
getMsgByID,
Expand Down
11 changes: 11 additions & 0 deletions internal/networkmap/data_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ func (nm *networkMap) GetIdentityByID(ctx context.Context, ns, id string) (*ffty
return identity, nil
}

func (nm *networkMap) GetIdentityByDID(ctx context.Context, did string) (*fftypes.Identity, error) {
identity, _, err := nm.identity.CachedIdentityLookup(ctx, did)
if err != nil {
return nil, err
}
if identity == nil {
return nil, i18n.NewError(ctx, i18n.Msg404NotFound)
}
return identity, nil
}

func (nm *networkMap) GetIdentities(ctx context.Context, ns string, filter database.AndFilter) ([]*fftypes.Identity, *database.FilterResult, error) {
filter.Condition(filter.Builder().Eq("namespace", ns))
return nm.database.GetIdentities(ctx, filter)
Expand Down
31 changes: 31 additions & 0 deletions internal/networkmap/data_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/hyperledger/firefly/mocks/databasemocks"
"github.com/hyperledger/firefly/mocks/identitymanagermocks"
"github.com/hyperledger/firefly/pkg/database"
"github.com/hyperledger/firefly/pkg/fftypes"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -286,3 +287,33 @@ func TestGetVerifierByHashBadUUID(t *testing.T) {
_, err := nm.GetVerifierByHash(nm.ctx, "ns1", "bad")
assert.Regexp(t, "FF10232", err)
}

func TestGetVerifierByDIDOk(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
nm.identity.(*identitymanagermocks.Manager).On("CachedIdentityLookup", nm.ctx, "did:firefly:org/abc").
Return(testOrg("abc"), true, nil)
id, err := nm.GetIdentityByDID(nm.ctx, "did:firefly:org/abc")
assert.NoError(t, err)
assert.Equal(t, "did:firefly:org/abc", id.DID)
}

func TestGetVerifierByDIDNotFound(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
nm.identity.(*identitymanagermocks.Manager).On("CachedIdentityLookup", nm.ctx, "did:firefly:org/abc").
Return(nil, true, nil)
id, err := nm.GetIdentityByDID(nm.ctx, "did:firefly:org/abc")
assert.Regexp(t, "FF10109", err)
assert.Nil(t, id)
}

func TestGetVerifierByDIDNotErr(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
nm.identity.(*identitymanagermocks.Manager).On("CachedIdentityLookup", nm.ctx, "did:firefly:org/abc").
Return(nil, true, fmt.Errorf("pop"))
id, err := nm.GetIdentityByDID(nm.ctx, "did:firefly:org/abc")
assert.Regexp(t, "pop", err)
assert.Nil(t, id)
}
1 change: 1 addition & 0 deletions internal/networkmap/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Manager interface {
GetNodeByNameOrID(ctx context.Context, nameOrID string) (*fftypes.Identity, error)
GetNodes(ctx context.Context, filter database.AndFilter) ([]*fftypes.Identity, *database.FilterResult, error)
GetIdentityByID(ctx context.Context, ns string, id string) (*fftypes.Identity, error)
GetIdentityByDID(ctx context.Context, did string) (*fftypes.Identity, error)
GetIdentities(ctx context.Context, ns string, filter database.AndFilter) ([]*fftypes.Identity, *database.FilterResult, error)
GetIdentityVerifiers(ctx context.Context, ns, id string, filter database.AndFilter) ([]*fftypes.Verifier, *database.FilterResult, error)
GetVerifiers(ctx context.Context, ns string, filter database.AndFilter) ([]*fftypes.Verifier, *database.FilterResult, error)
Expand Down
16 changes: 15 additions & 1 deletion internal/networkmap/register_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,28 @@ import (

func (nm *networkMap) RegisterIdentity(ctx context.Context, ns string, dto *fftypes.IdentityCreateDTO, waitConfirm bool) (identity *fftypes.Identity, err error) {

// The parent can be a UUID directly
var parent *fftypes.UUID
if dto.Parent != "" {
parent, err = fftypes.ParseUUID(ctx, dto.Parent)
if err != nil {
// Or a DID
parentIdentity, _, err := nm.identity.CachedIdentityLookup(ctx, dto.Parent)
if err != nil {
return nil, err
}
parent = parentIdentity.ID
}
}

// Parse the input DTO
identity = &fftypes.Identity{
IdentityBase: fftypes.IdentityBase{
ID: fftypes.NewUUID(),
Namespace: ns,
Name: dto.Name,
Type: dto.Type,
Parent: dto.Parent,
Parent: parent,
},
IdentityProfile: fftypes.IdentityProfile{
Description: dto.Description,
Expand Down
42 changes: 34 additions & 8 deletions internal/networkmap/register_identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestRegisterIdentityOrgWithParentOk(t *testing.T) {
org, err := nm.RegisterIdentity(nm.ctx, fftypes.SystemNamespace, &fftypes.IdentityCreateDTO{
Name: "child1",
Key: "0x12345",
Parent: fftypes.NewUUID(),
Parent: fftypes.NewUUID().String(),
}, false)
assert.NoError(t, err)
assert.Equal(t, *mockMsg1.Header.ID, *org.Messages.Claim)
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestRegisterIdentityOrgWithParentWaitConfirmOk(t *testing.T) {
_, err := nm.RegisterIdentity(nm.ctx, fftypes.SystemNamespace, &fftypes.IdentityCreateDTO{
Name: "child1",
Key: "0x12345",
Parent: fftypes.NewUUID(),
Parent: fftypes.NewUUID().String(),
}, true)
assert.NoError(t, err)

Expand All @@ -142,6 +142,12 @@ func TestRegisterIdentityCustomWithParentFail(t *testing.T) {

mim := nm.identity.(*identitymanagermocks.Manager)
mim.On("VerifyIdentityChain", nm.ctx, mock.AnythingOfType("*fftypes.Identity")).Return(parentIdentity, false, nil)
mim.On("CachedIdentityLookup", nm.ctx, "did:firefly:org/parent1").Return(&fftypes.Identity{
IdentityBase: fftypes.IdentityBase{
ID: fftypes.NewUUID(),
DID: "did:firefly:org/parent1",
},
}, false, nil)
mim.On("ResolveIdentitySigner", nm.ctx, parentIdentity).Return(&fftypes.SignerRef{
Key: "0x23456",
}, nil)
Expand All @@ -168,7 +174,7 @@ func TestRegisterIdentityCustomWithParentFail(t *testing.T) {
_, err := nm.RegisterIdentity(nm.ctx, "ns1", &fftypes.IdentityCreateDTO{
Name: "custom1",
Key: "0x12345",
Parent: fftypes.NewUUID(),
Parent: "did:firefly:org/parent1",
}, false)
assert.Regexp(t, "pop", err)

Expand All @@ -190,7 +196,7 @@ func TestRegisterIdentityGetParentMsgFail(t *testing.T) {
_, err := nm.RegisterIdentity(nm.ctx, "ns1", &fftypes.IdentityCreateDTO{
Name: "custom1",
Key: "0x12345",
Parent: fftypes.NewUUID(),
Parent: fftypes.NewUUID().String(),
}, false)
assert.Regexp(t, "pop", err)

Expand All @@ -215,8 +221,9 @@ func TestRegisterIdentityRootBroadcastFail(t *testing.T) {
fftypes.SystemTagIdentityClaim, false).Return(nil, fmt.Errorf("pop"))

_, err := nm.RegisterIdentity(nm.ctx, "ns1", &fftypes.IdentityCreateDTO{
Name: "custom1",
Key: "0x12345",
Name: "custom1",
Key: "0x12345",
Parent: fftypes.NewUUID().String(),
}, false)
assert.Regexp(t, "pop", err)

Expand All @@ -233,7 +240,8 @@ func TestRegisterIdentityMissingKey(t *testing.T) {
mim.On("VerifyIdentityChain", nm.ctx, mock.AnythingOfType("*fftypes.Identity")).Return(nil, false, nil)

_, err := nm.RegisterIdentity(nm.ctx, "ns1", &fftypes.IdentityCreateDTO{
Name: "custom1",
Name: "custom1",
Parent: fftypes.NewUUID().String(),
}, false)
assert.Regexp(t, "FF10352", err)

Expand All @@ -249,7 +257,25 @@ func TestRegisterIdentityVerifyFail(t *testing.T) {
mim.On("VerifyIdentityChain", nm.ctx, mock.AnythingOfType("*fftypes.Identity")).Return(nil, false, fmt.Errorf("pop"))

_, err := nm.RegisterIdentity(nm.ctx, "ns1", &fftypes.IdentityCreateDTO{
Name: "custom1",
Name: "custom1",
Parent: fftypes.NewUUID().String(),
}, false)
assert.Regexp(t, "pop", err)

mim.AssertExpectations(t)
}

func TestRegisterIdentityBadParent(t *testing.T) {

nm, cancel := newTestNetworkmap(t)
defer cancel()

mim := nm.identity.(*identitymanagermocks.Manager)
mim.On("CachedIdentityLookup", nm.ctx, "did:firefly:org/1").Return(nil, false, fmt.Errorf("pop"))

_, err := nm.RegisterIdentity(nm.ctx, "ns1", &fftypes.IdentityCreateDTO{
Name: "custom1",
Parent: "did:firefly:org/1",
}, false)
assert.Regexp(t, "pop", err)

Expand Down
2 changes: 1 addition & 1 deletion internal/networkmap/register_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (nm *networkMap) RegisterNode(ctx context.Context, waitConfirm bool) (ident
}

nodeRequest := &fftypes.IdentityCreateDTO{
Parent: nodeOwningOrg.ID,
Parent: nodeOwningOrg.ID.String(),
Name: config.GetString(config.NodeName),
Type: fftypes.IdentityTypeNode,
IdentityProfile: fftypes.IdentityProfile{
Expand Down
4 changes: 4 additions & 0 deletions internal/oapispec/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"log"
"net/http"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
Expand All @@ -41,6 +42,8 @@ type SwaggerGenConfig struct {
Description string
}

var customRegexRemoval = regexp.MustCompile(`{(\w+)\:[^}]+}`)

func SwaggerGen(ctx context.Context, routes []*Route, conf *SwaggerGenConfig) *openapi3.T {

doc := &openapi3.T{
Expand Down Expand Up @@ -72,6 +75,7 @@ func getPathItem(doc *openapi3.T, path string) *openapi3.PathItem {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
path = customRegexRemoval.ReplaceAllString(path, `{$1}`)
if doc.Paths == nil {
doc.Paths = openapi3.Paths{}
}
Expand Down
Loading