diff --git a/controllers/node_grpc.go b/controllers/node_grpc.go index d20cb759..bd2bb7f2 100644 --- a/controllers/node_grpc.go +++ b/controllers/node_grpc.go @@ -77,7 +77,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object) } // TODO consolidate functionality around files node.NetworkSettings.DefaultServerAddrs = serverAddrs - key, keyErr := logic.RetrieveTrafficKey() + key, keyErr := logic.RetrievePublicTrafficKey() if keyErr != nil { logger.Log(0, "error retrieving key: ", keyErr.Error()) return nil, keyErr @@ -85,7 +85,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object) node.TrafficKeys = models.TrafficKeys{ Mine: node.TrafficKeys.Mine, - Server: key.PublicKey, + Server: key, } err = logic.CreateNode(&node) diff --git a/database/database.go b/database/database.go index 7aa26858..c3ba3a25 100644 --- a/database/database.go +++ b/database/database.go @@ -210,8 +210,9 @@ func initializeUUID() error { if keyErr != nil { return keyErr } + var rsaPublicKey = &rsaPrivKey.PublicKey - telemetry := models.Telemetry{UUID: uuid.NewString(), TrafficKey: *rsaPrivKey} + telemetry := models.Telemetry{UUID: uuid.NewString(), TrafficKeyPriv: *rsaPrivKey, TrafficKeyPub: *rsaPublicKey} telJSON, err := json.Marshal(&telemetry) if err != nil { return err diff --git a/logic/telemetry.go b/logic/telemetry.go index 1dae4808..e2643ea9 100644 --- a/logic/telemetry.go +++ b/logic/telemetry.go @@ -77,9 +77,10 @@ func fetchTelemetryData() (telemetryData, error) { func setTelemetryTimestamp(telRecord *models.Telemetry) error { lastsend := time.Now().Unix() var serverTelData = models.Telemetry{ - UUID: telRecord.UUID, - LastSend: lastsend, - TrafficKey: telRecord.TrafficKey, + UUID: telRecord.UUID, + LastSend: lastsend, + TrafficKeyPriv: telRecord.TrafficKeyPriv, + TrafficKeyPub: telRecord.TrafficKeyPub, } jsonObj, err := json.Marshal(&serverTelData) if err != nil { diff --git a/logic/traffic.go b/logic/traffic.go index 262b06fd..4b9ae686 100644 --- a/logic/traffic.go +++ b/logic/traffic.go @@ -5,13 +5,24 @@ import ( "fmt" ) -// RetrieveTrafficKey - retrieves public key based on node -func RetrieveTrafficKey() (rsa.PrivateKey, error) { +// RetrievePrivateTrafficKey - retrieves private key of server +func RetrievePrivateTrafficKey() (rsa.PrivateKey, error) { var telRecord, err = fetchTelemetryRecord() if err != nil { return rsa.PrivateKey{}, err } - fmt.Printf("fetched key %v \n", telRecord.TrafficKey) + fmt.Printf("fetched priv key %v \n", telRecord.TrafficKeyPriv) - return telRecord.TrafficKey, nil + return telRecord.TrafficKeyPriv, nil +} + +// RetrievePublicTrafficKey - retrieves public key of server +func RetrievePublicTrafficKey() (rsa.PublicKey, error) { + var telRecord, err = fetchTelemetryRecord() + if err != nil { + return rsa.PublicKey{}, err + } + fmt.Printf("fetched pub key %v \n", telRecord.TrafficKeyPub) + + return telRecord.TrafficKeyPub, nil } diff --git a/models/structs.go b/models/structs.go index b8a18d15..739d81d5 100644 --- a/models/structs.go +++ b/models/structs.go @@ -170,9 +170,10 @@ type ServerUpdateData struct { // Telemetry - contains UUID of the server and timestamp of last send to posthog type Telemetry struct { - UUID string `json:"uuid" bson:"uuid"` - LastSend int64 `json:"lastsend" bson:"lastsend"` - TrafficKey rsa.PrivateKey `json:"traffickey" bson:"traffickey"` + UUID string `json:"uuid" bson:"uuid"` + LastSend int64 `json:"lastsend" bson:"lastsend"` + TrafficKeyPriv rsa.PrivateKey `json:"traffickeypriv" bson:"traffickeypriv"` + TrafficKeyPub rsa.PublicKey `json:"traffickeypub" bson:"traffickeypub"` } // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not diff --git a/mq/util.go b/mq/util.go index 0066fb9a..4614b603 100644 --- a/mq/util.go +++ b/mq/util.go @@ -3,15 +3,13 @@ package mq import ( "fmt" - "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/logic" "github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/netclient/ncutils" ) func decryptMsg(msg []byte) ([]byte, error) { - logger.Log(0, "found message for decryption: %s \n", string(msg)) - trafficKey, trafficErr := logic.RetrieveTrafficKey() + trafficKey, trafficErr := logic.RetrievePrivateTrafficKey() if trafficErr != nil { return nil, trafficErr } diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index 63998c8b..8bf335fc 100644 --- a/netclient/ncutils/netclientutils.go +++ b/netclient/ncutils/netclientutils.go @@ -566,7 +566,7 @@ func DestructMessage(builtMsg string, priv *rsa.PrivateKey) []byte { // BuildMessage Build a message for publishing func BuildMessage(originalMessage []byte, pub *rsa.PublicKey) string { - chunks := getSliceChunks(originalMessage, 245) + chunks := getSliceChunks(originalMessage, 240) var message = "" for i := 0; i < len(chunks); i++ { var encryptedText, encryptErr = encryptWithPublicKey(chunks[i], pub)