Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 auth-ledger issue with Paho #134

Closed
bkupidura opened this issue Jan 8, 2023 · 2 comments
Closed

v2 auth-ledger issue with Paho #134

bkupidura opened this issue Jan 8, 2023 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@bkupidura
Copy link
Collaborator

Im investigeting new (v2) auth-ledger hook, but i believe that there is some issue there.

Looks like golang-paho is not receiving ErrBadUsernameOrPassword packet when auth is failing on broker side.

Simple code to reproduce:

package t

import (
	"log"

	"github.com/mochi-co/mqtt/v2"
	"github.com/mochi-co/mqtt/v2/hooks/auth"
	"github.com/mochi-co/mqtt/v2/listeners"
)

func New() (*mqtt.Server, error) {
	mqttServer := mqtt.New(&mqtt.Options{})

	if err := mqttServer.AddHook(new(auth.Hook), &auth.Options{
		Ledger: &auth.Ledger{
			Auth: auth.AuthRules{
				{Username: "test", Password: "test"},
			},
		},
	}); err != nil {
		return nil, err
	}

	listener := listeners.NewTCP("tcp", ":1883", nil)

	if err := mqttServer.AddListener(listener); err != nil {
		return nil, err
	}

	go func() {
		if err := mqttServer.Serve(); err != nil {
			log.Fatalf("mqttServer.Serve worker died: %s", err)
		}
	}()

	return mqttServer, nil
}
package t

import (
	"errors"
	"testing"

	paho "github.com/eclipse/paho.mqtt.golang"
	"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
	b, err := New()
	require.Nil(t, err)

	mqttClient := paho.NewClient(paho.NewClientOptions().
		AddBroker("127.0.0.1:1883").
		SetUsername("wrong-username").
		SetPassword("wrong-password").
		SetAutoReconnect(false).
		SetConnectRetry(false))

	token := mqttClient.Connect()
	token.Wait()

	require.Equal(t, errors.New("bad user name or password"), token.Error())

	b.Close()
}

I would expect that paho ConnectToken will return error, but it looks like from paho perspective connection was successfull.

4:28PM INF added hook hook=auth-ledger
4:28PM INF loaded auth rules acl=0 authentication=1 hook=auth-ledger
4:28PM INF attached listener address=:1883 id=tcp protocol=tcp
4:28PM INF mochi mqtt starting version=2.1.1
4:28PM INF mochi mqtt server started
4:28PM INF client failed authentication check hook=auth-ledger remote=127.0.0.1:60562 username=wrong-username
4:28PM WRN error="bad username or password" listener=tcp
4:28PM INF client failed authentication check hook=auth-ledger remote=127.0.0.1:60563 username=wrong-username
4:28PM WRN error="bad username or password" listener=tcp
--- FAIL: TestNew (0.01s)
    main_test.go:25:
        	Error Trace:	main_test.go:25
        	Error:      	Not equal:
        	            	expected: *errors.errorString(&errors.errorString{s:"bad user name or password"})
        	            	actual  : <nil>(<nil>)
        	Test:       	TestNew
FAIL
exit status 1
FAIL	t	0.539s
@mochi-co mochi-co self-assigned this Jan 9, 2023
@mochi-co
Copy link
Collaborator

Hi @bkupidura - Thank you for finding and raising this issue!
I looked into this and it's because MQTT v3 has a different specification for the (very few) Connack return codes it provides. In this case the broker was sending the error correctly, but the byte value was incorrect for v3.

I have put together a v5 to v3 code map which seems to fix this issue, and is released in v2.1.4. Give it a try and see if it works as you would expect.

@mochi-co mochi-co added the bug Something isn't working label Jan 10, 2023
@bkupidura
Copy link
Collaborator Author

@mochi-co can confirm, new version works as expected! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants