From 8c4d11ae4b47d9ae32177baf02f730355bd61afc Mon Sep 17 00:00:00 2001 From: Christophe de Carvalho Date: Tue, 31 Mar 2020 13:19:22 +0200 Subject: [PATCH] ensure public key is always send during node registration fixes #649 --- cmds/identityd/main.go | 8 +++++++- tools/explorer/pkg/directory/types/node.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cmds/identityd/main.go b/cmds/identityd/main.go index 1653c5fd9..c06174b65 100644 --- a/cmds/identityd/main.go +++ b/cmds/identityd/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/hex" "fmt" "os" "os/signal" @@ -9,6 +10,8 @@ import ( "syscall" "time" + "github.com/jbenet/go-base58" + "github.com/pkg/errors" "github.com/threefoldtech/zos/pkg/app" "github.com/threefoldtech/zos/pkg/flist" @@ -16,8 +19,8 @@ import ( "github.com/threefoldtech/zos/pkg/network" "github.com/threefoldtech/zos/pkg/stubs" "github.com/threefoldtech/zos/pkg/upgrade" - "github.com/threefoldtech/zos/tools/explorer/models/generated/directory" "github.com/threefoldtech/zos/tools/client" + "github.com/threefoldtech/zos/tools/explorer/models/generated/directory" "github.com/cenkalti/backoff/v3" "github.com/threefoldtech/zos/pkg" @@ -453,6 +456,8 @@ func registerNode(nodeID pkg.Identifier, farmID pkg.FarmID, version string, stor v1ID, _ := network.NodeIDv1() + publicKeyHex := hex.EncodeToString(base58.Decode(nodeID.Identity())) + err := store.NodeRegister(directory.Node{ NodeId: nodeID.Identity(), NodeIdV1: v1ID, @@ -465,6 +470,7 @@ func registerNode(nodeID pkg.Identifier, farmID pkg.FarmID, version string, stor Longitude: loc.Longitute, Latitude: loc.Latitude, }, + PublicKeyHex: publicKeyHex, }) if err != nil { diff --git a/tools/explorer/pkg/directory/types/node.go b/tools/explorer/pkg/directory/types/node.go index d189688bc..6088346a3 100644 --- a/tools/explorer/pkg/directory/types/node.go +++ b/tools/explorer/pkg/directory/types/node.go @@ -2,9 +2,11 @@ package types import ( "context" + "encoding/hex" "fmt" "time" + "github.com/jbenet/go-base58" "github.com/pkg/errors" "github.com/threefoldtech/zos/pkg/schema" "github.com/threefoldtech/zos/tools/explorer/models" @@ -36,6 +38,19 @@ func (n *Node) Validate() error { return fmt.Errorf("os_version is required") } + if len(n.PublicKeyHex) == 0 { + return fmt.Errorf("public_key_hex is required") + } + + pk, err := hex.DecodeString(n.PublicKeyHex) + if err != nil { + return errors.Wrap(err, "fail to decode public key") + } + + if n.NodeId != base58.Encode(pk) { + return fmt.Errorf("nodeID and public key does not match") + } + // Unfortunately, jsx schema does not support nil types // so this is the only way to check if values are not set empty := generated.Location{}