mosquittoctrl

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

Go Mosquitto Ctrl

Manage Mosquitto users backed by Dynamic Security Plugin over SSH.

Official eclipse-mosquitto Docker image does not have SSH daemon set up. I recommend using my eclipse-mosquitto-ssh Docker image which has SSH and Dynsec initialization ready-to-go.

Usage

// Connect to Mosquitto container via SSH
sshClient, err := ssh.Dial("tcp", "localhost:1882", &ssh.ClientConfig{
  User: "admin",
  Auth: []ssh.AuthMethod{
    ssh.Password("123"),
  },
  // Example only, use safer option in production
  HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
  return fmt.Errorf("dialing ssh: %w", err)
}
defer sshClient.Close()

// Initialize client
ds := mosquittoctrl.NewDynsec(sshClient, "admin", "123")

roleName := "time"
clientUsername := "time_publisher"
clientPassword := "123"

// Manage users and permissions
err = ds.CreateRole(roleName)
if err != nil {
  return fmt.Errorf("creating role: %w", err)
}
err = ds.AddRoleACL(roleName, "publishClientSend", "time_current", "allow", 1)
if err != nil {
  return fmt.Errorf("adding role ACL (publish): %w", err)
}
err = ds.CreateClient(clientUsername, clientPassword)
if err != nil {
  return fmt.Errorf("creating client: %w", err)
}
err = ds.AddClientRole(clientUsername, roleName)
if err != nil {
  return fmt.Errorf("adding client role: %w", err)
}

Documentation

Index

Constants

View Source
const DefaultDynsecConfigFile = "/mosquitto/config/dynamic-security.json"

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnectionError

type ConnectionError struct {
	Reason string
}

ConnectionError represents MQTT errors printed to stderr See https://github.com/eclipse/mosquitto/blob/master/lib/strings_mosq.c for possible error messages Example: Connection error: Not authorized

func (*ConnectionError) Error

func (ce *ConnectionError) Error() string

type Dynsec

type Dynsec struct {
	Logger Logger
	// contains filtered or unexported fields
}

func NewDynsec

func NewDynsec(client *ssh.Client, adminUsername, adminPassword string) *Dynsec

func (*Dynsec) AddClientRole

func (d *Dynsec) AddClientRole(client string, role string) error

func (*Dynsec) AddRoleACL

func (d *Dynsec) AddRoleACL(role, aclType, topicFilter, allowOrDeny string, priority int) error

func (*Dynsec) CreateClient

func (d *Dynsec) CreateClient(name, password string) error

func (*Dynsec) CreateRole

func (d *Dynsec) CreateRole(name string) error

func (*Dynsec) DeleteClient

func (d *Dynsec) DeleteClient(name string) error

func (*Dynsec) DeleteRole

func (d *Dynsec) DeleteRole(name string) error

type Logger

type Logger interface {
	Cmd(cmd string, stdin, stdout, stderr *bytes.Buffer)
}

type LoggerStd

type LoggerStd struct {
	Logger *log.Logger
}

func (*LoggerStd) Cmd

func (ls *LoggerStd) Cmd(cmd string, stdin, stdout, stderr *bytes.Buffer)

Jump to

Keyboard shortcuts

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