github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/networkmap/register_org.go (about) 1 // Copyright © 2021 Kaleido, Inc. 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package networkmap 18 19 import ( 20 "context" 21 22 "github.com/kaleido-io/firefly/internal/config" 23 "github.com/kaleido-io/firefly/internal/i18n" 24 "github.com/kaleido-io/firefly/pkg/fftypes" 25 ) 26 27 func (nm *networkMap) findOrgsToRoot(ctx context.Context, idType, identity, parent string) (err error) { 28 29 var root *fftypes.Organization 30 for parent != "" { 31 root, err = nm.database.GetOrganizationByIdentity(ctx, parent) 32 if err != nil { 33 return err 34 } 35 if root == nil { 36 return i18n.NewError(ctx, i18n.MsgParentIdentityNotFound, parent, idType, identity) 37 } 38 parent = root.Parent 39 } 40 return err 41 } 42 43 // RegisterNodeOrganization is a convenience helper to register the org configured on the node, without any extra info 44 func (nm *networkMap) RegisterNodeOrganization(ctx context.Context) (msg *fftypes.Message, err error) { 45 org := &fftypes.Organization{ 46 Name: config.GetString(config.OrgName), 47 Identity: config.GetString(config.OrgIdentity), 48 Description: config.GetString(config.OrgDescription), 49 } 50 if org.Identity == "" || org.Name == "" { 51 return nil, i18n.NewError(ctx, i18n.MsgNodeAndOrgIDMustBeSet) 52 } 53 return nm.RegisterOrganization(ctx, org) 54 } 55 56 func (nm *networkMap) RegisterOrganization(ctx context.Context, org *fftypes.Organization) (*fftypes.Message, error) { 57 58 err := org.Validate(ctx, false) 59 if err != nil { 60 return nil, err 61 } 62 org.ID = fftypes.NewUUID() 63 org.Created = fftypes.Now() 64 65 // If we're a root identity, we self-sign 66 signingIdentityString := org.Identity 67 if org.Parent != "" { 68 // Check the identity itself is ok 69 if _, err = nm.identity.Resolve(ctx, org.Identity); err != nil { 70 return nil, err 71 } 72 73 // Otherwise we must have access to the signing key of the parent, and the parents 74 // must already have been broadcast to the network 75 signingIdentityString = org.Parent 76 if err = nm.findOrgsToRoot(ctx, "organization", org.Identity, signingIdentityString); err != nil { 77 return nil, err 78 } 79 } 80 81 signingIdentity, err := nm.identity.Resolve(ctx, signingIdentityString) 82 if err != nil { 83 return nil, i18n.WrapError(ctx, err, i18n.MsgInvalidSigningIdentity) 84 } 85 86 return nm.broadcast.BroadcastDefinition(ctx, org, signingIdentity, fftypes.SystemTagDefineOrganization) 87 }