2

I am new to emqtt. I am trying to use emq_auth_http but it is not working.

I have these 3 requests to console some data and send data back with status 200.

app.post('/mqtt/auth', function(req, res) {
console.log('This is body ', req.body);
res.status(200).send(req.body);
});
app.post('/mqtt/superuser', function(req, res) {
console.log('This is body in superuser ', req.body);
res.status(200).send(req.body);
});
app.get('/mqtt/acl', function(req, res) {
console.log('This is params in acl ', req.params);
res.status(200).send(req.body);
});

Requests are working fine on postman.

I have configured my emqtt on windows with docker. I have placed my config file in /etc/plugins/emq_auth_http.conf.

This is my config file

## Variables: %u = username, %c = clientid, %a = ipaddress, %P = password, %t = topic

auth.http.auth_req = http://127.0.0.1:3000/mqtt/auth
auth.http.auth_req.method = post
auth.http.auth_req.params = clientid=%c,username=%u,password=%P

auth.http.super_req = http://127.0.0.1:3000/mqtt/superuser
auth.http.super_req.method = post
auth.http.super_req.params = clientid=%c,username=%u

## 'access' parameter: sub = 1, pub = 2
auth.http.acl_req = http://127.0.0.1:3000/mqtt/acl
auth.http.acl_req.method = get
auth.http.acl_req.params = 
access=%A,username=%u,clientid=%c,ipaddr=%a,topic=%t

Then I enabled emq_auth_http from dashboard

Now when I tried to connect my mqtt client to my server it is not calling the api. It logs

09:28:29.642 [error] Unexpected HTTP Request: POST /mqtt/auth
09:28:29.644 [error] Client(19645050-9d1b-4c50-acf9- 
c1fe7e69eea8@172.17.0.1:60968): Username 'username' login failed for 404

Is there anything I missed? Why it is not working?

Thanks

Unknown
  • 373
  • 1
  • 4
  • 15
  • Where is the node.js code running? Given that emq is running in docker are you sure `127.0.0.1` points to where you think it does? Also does the node.js code log anything when called by emq? – hardillb Aug 15 '18 at 10:11
  • 1
    Node js code is running on my localhost. I have also tested it by deploying my code on server. That's the issue nodejs code ddoes not log anything. I think request does not reach on nodejs server – Unknown Aug 15 '18 at 11:10
  • How are you using this? Is it through docker or kubernetes – Christian Matthew Sep 18 '22 at 03:50

1 Answers1

1

127.0.0.1 in a container refers to the container itself and not the host machine. you should set the host machine ip,you can obtain the host machine ip from a container by issuing the command /sbin/ip route|awk '/default/ { print $3 }' which could be found here

ps: this way you can get the ip of docker machine and not the host ,if your service is served by windows you can reach the ip of host machine from the container which is 10.0.75.1. you can find it in How to connect to docker host from container on Windows 10 (Docker for Windows)

  • 2
    Explain more on why this is an answer. – 0xInfection Feb 20 '19 at 07:43
  • in a config file the ip is set to 127.0.0.1 , but in a container this ip refers to to container itself , the correct ip address in config file must be the host machine ip, using the command above in a container would give the host machine ip which in most cases for docker for windows is 10.0.75.1.make sure the service allows connections from outside your host. As far as that service is concerned, your docker container is a different machine. Also make sure Windows Firewall allows communication to and from the service – Majid Mohsenifar Feb 20 '19 at 20:45
  • Simply put, use your internal IP within your network instead of referring to the localhost. Because the localhost inside of a container is different from the localhost at your actual machine. – Alan Sereb Mar 15 '20 at 22:21
  • I don't get the purpose of this at all. the service is outside of the host machine. can this not go over 80 or 443? Does this service have to be inside of my container service? – Christian Matthew Sep 16 '22 at 22:09