messenger

package
v0.0.0-...-162ef27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2020 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package messenger - Dummy in-memory messenger for testing

Package messenger - Interface of messengers for publishers and subscribers

Package messenger - Publish and Subscribe to message using the MQTT message bus

Package messenger with methods for JWE encryption of messages

Package messenger for handling encryption and signing keys

Package messenger with methods for signing and verifying a message

Index

Constants

View Source
const ConnectionTimeoutSec = 20

ConnectionTimeoutSec constant with connection and reconnection timeouts

View Source
const TLSPort = 8883

TLSPort is the default secure port to connect to mqtt

Variables

This section is empty.

Functions

func CreateAsymKeys

func CreateAsymKeys() *ecdsa.PrivateKey

CreateAsymKeys creates a asymmetric key set Returns a private key that contains its associated public key

func CreateEcdsaSignature

func CreateEcdsaSignature(payload string, privateKey *ecdsa.PrivateKey) string

CreateEcdsaSignature creates a ECDSA256 signature from the payload using the provided private key This returns a base64url encoded signature

func CreateJWSSignature

func CreateJWSSignature(payload string, privateKey *ecdsa.PrivateKey) (string, error)

CreateJWSSignature signs the payload using JSE ES256 and return the JSE compact serialized message

func DecryptMessage

func DecryptMessage(serialized string, privateKey *ecdsa.PrivateKey) (isEncrypted bool, message string, err error)

DecryptMessage deserializes and decrypts the message using JWE This returns the decrypted message, or the input message if the message was not encrypted

func EncryptMessage

func EncryptMessage(message string, publicKey *ecdsa.PublicKey) (serialized string, err error)

EncryptMessage encrypts and serializes the message using JWE

func PrivateKeyFromPem

func PrivateKeyFromPem(pemEncodedPriv string) *ecdsa.PrivateKey

PrivateKeyFromPem converts PEM encoded private keys into a ECDSA object for use in the application See also PrivateKeyToPem for the opposite. Returns nil if the encoded pem source isn't a pem format

func PrivateKeyToPem

func PrivateKeyToPem(privateKey *ecdsa.PrivateKey) string

PrivateKeyToPem converts a private key into their PEM encoded ascii format see also https://stackoverflow.com/questions/21322182/how-to-store-ecdsa-private-key-in-go

func PublicKeyFromPem

func PublicKeyFromPem(pemEncodedPub string) *ecdsa.PublicKey

PublicKeyFromPem converts a ascii encoded public key into a ECDSA public key

func PublicKeyToPem

func PublicKeyToPem(publicKey *ecdsa.PublicKey) string

PublicKeyToPem converts a public key into PEM encoded ascii format See also PublicKeyFromPem for its counterpart

func SignEncodeIdentity

func SignEncodeIdentity(ident *iotc.PublisherPublicIdentity, privKey *ecdsa.PrivateKey) string

SignEncodeIdentity returns a base64URL encoded ECDSA256 signature of the publisher identity. Used in creating or updating a publisher's identity.

func VerifyEcdsaSignature

func VerifyEcdsaSignature(payload string, signatureB64urlEncoded string, publicKey *ecdsa.PublicKey) bool

VerifyEcdsaSignature the payload using the base64url encoded signature and public key payload is a text or base64 encoded raw data signatureB64urlEncoded is the ecdsa 256 URL encoded signature

func VerifyJWSMessage

func VerifyJWSMessage(message string, publicKey *ecdsa.PublicKey) (payload string, err error)

VerifyJWSMessage verifies a signed message and returns its payload message is the message to verify publicKey from the signer. This must be known to verify the message.

func VerifySender

func VerifySender(message string, object interface{}, getPublicKey func(address string) *ecdsa.PublicKey) (isSigned bool, err error)

VerifySender verifies if a message is JWS signed. If signed then the signature is verified using the 'Sender' Attribute to determine the public key to verify with. To verify correctly, the sender has to be a known publisher and verified with the DSS.

getPublicKey is a lookup function for providing the public key from the given sender address.

it should only provide a public key if the publisher is known and verified by the DSS, or
if this zone does not use a DSS (publisher are protected through message bus ACLs)

The message is json unmarshalled into the given object.

This returns a flag if the message was signed and if so, an error if the verification failed

Types

type DummyMessenger

type DummyMessenger struct {
	// contains filtered or unexported fields
}

DummyMessenger that implements IMessenger

func NewDummyMessenger

func NewDummyMessenger(config *MessengerConfig, logger *log.Logger) *DummyMessenger

NewDummyMessenger provides a messenger for messages that go no.where... logger to use for debug messages

func (*DummyMessenger) Connect

func (messenger *DummyMessenger) Connect(lastWillAddress string, lastWillValue string) error

Connect the messenger

func (*DummyMessenger) Disconnect

func (messenger *DummyMessenger) Disconnect()

Disconnect gracefully disconnects the messenger

func (*DummyMessenger) FindLastPublication

func (messenger *DummyMessenger) FindLastPublication(addr string) (message string)

FindLastPublication with the given address

func (*DummyMessenger) GetDomain

func (messenger *DummyMessenger) GetDomain() string

GetDomain returns the domain in which this messenger operates This is provided via the messenger config file or defaults to iotc.LocalDomainID

func (*DummyMessenger) NrPublications

func (messenger *DummyMessenger) NrPublications() int

NrPublications returns the number of received publications

func (*DummyMessenger) OnReceive

func (messenger *DummyMessenger) OnReceive(address string, message string)

OnReceive function to simulate a received message

func (*DummyMessenger) Publish

func (messenger *DummyMessenger) Publish(address string, retained bool, message string) error

Publish a message address is the MQTT address to send to retained (ignored) message JSON text or raw message base64 encoded text

func (*DummyMessenger) Subscribe

func (messenger *DummyMessenger) Subscribe(
	address string, onMessage func(address string, message string))

Subscribe to a message by address

type ECDSASignature

type ECDSASignature struct {
	R, S *big.Int
}

ECDSASignature ...

type IMessenger

type IMessenger interface {

	// Connect the messenger.
	// This contains the last-will & testament information which is useful to inform subscribers
	//  when a publisher is unintentionally disconnected. Non MQTT busses can replace this with
	// their equivalent if available. Subscribers-only leave this empty.
	//
	// lastWillAddress optional last will & testament address for publishing device state
	//                 on accidental disconnect. Subscribers use "" to ignore.
	// lastWillValue payload to use with the last will publication
	Connect(lastWillAddress string, lastWillValue string) error

	// Gracefully disconnect the messenger and unsubscribe to all subscribed messages.
	// This will prevent the LWT publication so publishers must publish a graceful disconnect
	// message.
	Disconnect()

	// Publish a message. The publisher must sign and optionally encrypt the message before
	// publishing, using the Signing method specified in the config.
	//  address to subscribe to as per IotConnect standard
	//  retained to have MQTT persists the last message
	//  message is a serialized message to send
	Publish(address string, retained bool, message string) error

	// Subscribe to a message. The subscriber must handle message decryption and signing verification.
	//  address to subscribe to with support for wildcards '+' and '#'. Non MQTT busses must conver to equivalent
	//  onMessage callback is invoked when a message on this address is received
	Subscribe(address string, onMessage func(address string, message string))
}

IMessenger interface for messenger implementations

func NewMessenger

func NewMessenger(messengerConfig *MessengerConfig, logger *logrus.Logger) IMessenger

NewMessenger creates a new messenger instance Create a messenger instance using configuration setting:

"DummyMessenger" (default)
MQTTMessenger, requires server, login and credentials properties set

config holds the messenger configuration. If no server is given, 'localhost' will be used. logger is optional in case you want to use a predefined logging. Default is a default logger.

type MessengerConfig

type MessengerConfig struct {
	ClientID  string `yaml:"clientid,omitempty"`  // optional connect ID, must be unique. Default is generated.
	Domain    string `yaml:"domain,omitempty"`    // Domain to be used by all publishers
	Login     string `yaml:"login"`               // messenger login name
	Port      uint16 `yaml:"port,omitempty"`      // optional port, default is 8883 for TLS
	Password  string `yaml:"credentials"`         // messenger login credentials
	PubQos    byte   `yaml:"pubqos,omitempty"`    // publishing QOS 0-2. Default=0
	Server    string `yaml:"server"`              // Message bus server/broker hostname or ip address, required
	Signing   string `yaml:"signing,omitempty"`   // Message signing method to be used by all publishers. Default is "jws"
	SubQos    byte   `yaml:"subqos,omitempty"`    // Subscription QOS 0-2. Default=0
	Messenger string `yaml:"messenger,omitempty"` // Messenger client type: "DummyMessenger" (default) or "MQTTMessenger"
}

MessengerConfig with configuration of a messenger

type MqttMessenger

type MqttMessenger struct {
	// contains filtered or unexported fields
}

MqttMessenger that implements IMessenger

func NewMqttMessenger

func NewMqttMessenger(config *MessengerConfig, logger *log.Logger) *MqttMessenger

NewMqttMessenger creates a new MQTT messenger instance

func (*MqttMessenger) Connect

func (messenger *MqttMessenger) Connect(lastWillAddress string, lastWillValue string) error

Connect to the MQTT broker and set the LWT If a previous connection exists then it is disconnected first. This publishes the LWT on the address baseTopic/deviceID/$state. @param lastWillTopic optional last will and testament address for publishing device state on accidental disconnect.

Use "" to ignore LWT feature.

@param lastWillValue to use as the last will

func (*MqttMessenger) Disconnect

func (messenger *MqttMessenger) Disconnect()

Disconnect from the MQTT broker and unsubscribe from all addresss and set device state to disconnected

func (*MqttMessenger) Publish

func (messenger *MqttMessenger) Publish(address string, retained bool, message string) error

Publish value using the device address as base address to publish on. retained to have the broker retain the address value payload is converted to string if it isn't a byte array, as Paho doesn't handle int and bool

func (*MqttMessenger) PublishRaw

func (messenger *MqttMessenger) PublishRaw(address string, retained bool, message string) error

PublishRaw message

func (*MqttMessenger) Subscribe

func (messenger *MqttMessenger) Subscribe(
	address string, onMessage func(address string, message string))

Subscribe to a address Subscribers are automatically resubscribed after the connection is restored If no connection exists, then subscriptions are stored until a connection is established. address: address to subscribe to. This can contain wildcards. qos: Quality of service for subscription: 0, 1, 2 handler: callback handler.

type Subscription

type Subscription struct {
	// contains filtered or unexported fields
}

Subscription to messages

type TopicSubscription

type TopicSubscription struct {
	// contains filtered or unexported fields
}

TopicSubscription holds subscriptions to restore after disconnect

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL