netmaker/mq/util.go

107 lines
2.6 KiB
Go
Raw Normal View History

2022-01-29 04:33:30 +08:00
package mq
import (
2022-05-16 21:38:47 +08:00
"errors"
"fmt"
"strings"
2022-05-16 21:38:47 +08:00
"time"
2022-01-29 04:33:30 +08:00
"github.com/gravitl/netmaker/logic"
2022-01-29 22:14:18 +08:00
"github.com/gravitl/netmaker/models"
2022-01-29 04:33:30 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
)
func decryptMsgWithHost(host *models.Host, msg []byte) ([]byte, error) {
2023-03-14 22:46:12 +08:00
if host.OS == models.OS_Types.IoT { // just pass along IoT messages
return msg, nil
}
2022-01-30 04:02:37 +08:00
trafficKey, trafficErr := logic.RetrievePrivateTrafficKey() // get server private key
2022-01-29 04:33:30 +08:00
if trafficErr != nil {
return nil, trafficErr
}
2022-01-30 04:02:37 +08:00
serverPrivTKey, err := ncutils.ConvertBytesToKey(trafficKey)
if err != nil {
return nil, err
}
nodePubTKey, err := ncutils.ConvertBytesToKey(host.TrafficKeyPublic)
2022-01-30 04:02:37 +08:00
if err != nil {
return nil, err
}
return ncutils.DeChunk(msg, nodePubTKey, serverPrivTKey)
}
func DecryptMsg(node *models.Node, msg []byte) ([]byte, error) {
if len(msg) <= 24 { // make sure message is of appropriate length
2023-10-25 00:52:40 +08:00
return nil, fmt.Errorf("received invalid message from broker %v", msg)
}
host, err := logic.GetHost(node.HostID.String())
if err != nil {
return nil, err
}
return decryptMsgWithHost(host, msg)
2022-01-29 04:33:30 +08:00
}
2023-01-04 12:35:05 +08:00
func encryptMsg(host *models.Host, msg []byte) ([]byte, error) {
2023-03-14 22:46:12 +08:00
if host.OS == models.OS_Types.IoT {
return msg, nil
}
2022-01-30 04:02:37 +08:00
// fetch server public key to be certain hasn't changed in transit
trafficKey, trafficErr := logic.RetrievePrivateTrafficKey()
if trafficErr != nil {
return nil, trafficErr
}
serverPrivKey, err := ncutils.ConvertBytesToKey(trafficKey)
if err != nil {
return nil, err
2022-01-29 04:33:30 +08:00
}
2022-01-30 04:02:37 +08:00
nodePubKey, err := ncutils.ConvertBytesToKey(host.TrafficKeyPublic)
2022-01-30 04:02:37 +08:00
if err != nil {
return nil, err
}
if strings.Contains(host.Version, "0.10.0") {
return ncutils.BoxEncrypt(msg, nodePubKey, serverPrivKey)
}
2022-02-15 03:21:56 +08:00
return ncutils.Chunk(msg, nodePubKey, serverPrivKey)
2022-01-29 04:33:30 +08:00
}
2023-01-04 12:35:05 +08:00
func publish(host *models.Host, dest string, msg []byte) error {
2023-01-04 12:35:05 +08:00
encrypted, encryptErr := encryptMsg(host, msg)
2022-01-29 04:33:30 +08:00
if encryptErr != nil {
return encryptErr
}
if mqclient == nil || !mqclient.IsConnectionOpen() {
2022-08-30 02:08:01 +08:00
return errors.New("cannot publish ... mqclient not connected")
}
2022-08-30 02:08:01 +08:00
if token := mqclient.Publish(dest, 0, true, encrypted); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
2022-05-16 21:38:47 +08:00
var err error
if token.Error() == nil {
err = errors.New("connection timeout")
} else {
err = token.Error()
}
return err
2022-01-29 04:33:30 +08:00
}
return nil
}
2022-08-30 02:08:01 +08:00
// decodes a message queue topic and returns the embedded node.ID
func GetID(topic string) (string, error) {
parts := strings.Split(topic, "/")
count := len(parts)
if count == 1 {
return "", fmt.Errorf("invalid topic")
}
//the last part of the topic will be the node.ID
return parts[count-1], nil
}