Why is the mqtt client's emqx authorization not working?

127 Views Asked by At

I am using emqx mqtt broker to create a server. I have docker compose. Which has postgresql and emqx. When starting the container, everything is fine, emqx starts up quietly, but in the future the client cannot log in in any way, given that I enter exactly the correct authorization data. So I think there is a mistake in setting up the emqx container.

version: "3.9"
services:
  postgres:
    container_name: postgres_container
    image: postgres:14.8-alpine3.18
    command:
      - "postgres"
      - "-c"
      - "max_connections=50"
      - "-c"
      - "shared_buffers=1GB"
      - "-c"
      - "effective_cache_size=4GB"
      - "-c"
      - "work_mem=16MB"
      - "-c"
      - "maintenance_work_mem=512MB"
      - "-c"
      - "random_page_cost=1.1"
      - "-c"
      - "temp_file_limit=10GB"
      - "-c"
      - "log_min_duration_statement=200ms"
      - "-c"
      - "idle_in_transaction_session_timeout=10s"
      - "-c"
      - "lock_timeout=1s"
      - "-c"
      - "statement_timeout=60s"
      - "-c"
      - "shared_preload_libraries=pg_stat_statements"
      - "-c"
      - "pg_stat_statements.max=10000"
      - "-c"
      - "pg_stat_statements.track=all"
    environment:
      POSTGRES_DB: ${DATABASE_NAME}
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
      PGDATA: "/var/lib/postgresql/data/pgdata"
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
      - ../2. Init Database:/docker-entrypoint-initdb.d
      - ./mosquitto/init-mqtt-auth-db.sh:/docker-entrypoint-initdb.d/init-mqtt-auth-db.sh
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DATABASE_USER} -d ${DATABASE_NAME}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 4G
    networks:
      - postgres

  pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4:7.2
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PG_DEFAULT_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PG_DEFAULT_ADMIN_PASSWORD}
      PGADMIN_CONFIG_SERVER_MODE: "False"
    volumes:
      - pgadmin-data:/var/lib/pgadmin
    ports:
      - "5050:80"
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 1G
    networks:
      - postgres

  emqx:
    container_name: emqx_container
    image: emqx/emqx:4.3.5
    environment:
      EMQX_NAME: emqx
      EMQX_HOST: "localhost"
      EMQX_NODE__DIST_LISTEN_MAX: 6379
      EMQX_LOAD__DIST_MAX: 64
      EMQX_CLUSTER__ACCEPT_SYS_HEARTBEAT: "true"
      EMQX_CLUSTER__NODE__NAME: "[email protected]"
      EMQX_LISTENER__TCP__EXTERNAL: 1883
      EMQX_LISTENER__WS__EXTERNAL: 8083
      EMQX_CLUSTER__DISCOVERY__STATIC__SEEDS: "[email protected]"
      EMQX_PLUGINS: "emqx_auth_pgsql"
      EMQX_AUTH__PGSQL__SERVER: "postgresql://${DATABASE_USER}:${DATABASE_PASSWORD}@postgres:5432/${DATABASE_NAME}"
      EMQX_AUTH__PGSQL__SUPERUSER: ${MQTT_USER}
      EMQX_AUTH__PGSQL__SUPERUSER_PASSWORD: ${MQTT_PASSWORD}
      EMQX_AUTH__PGSQL__STARTUP_RETRIES: 3
      EMQX_AUTH__PGSQL__POOL__SIZE: 8
      EMQX_AUTH__PGSQL__POOL__MAX_OVERFLOW: 10
      EMQX_AUTH__PGSQL__QUERY__USER: "SELECT secret FROM 'Wardrobe' WHERE mac = '$1'"
      EMQX_LOG__LEVEL: debug
      EMQX_ALLOW_ANONYMOUS: "false"
    ports:
      - "1883:1883"
      - "8083:8083"  # Для поддержки MQTT-over-Websocket
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M
    networks:
      - mqtt-net

volumes:
  postgres-data:
  pgadmin-data:
  data:
    name: "mqtt-broker-data"

networks:
  postgres:
    driver: bridge

  mqtt-net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.100.10.0/24

  postgres-data:

This is my docker container. Below is an example of a login attempt:

client = mqtt.Client()
client.username_pw_set(username="11:11:11:11:11:11", password="1")
client.on_message = on_message
client.on_connect = on_connect
client.connect(host="127.0.0.1", port=1883, keepalive=60)
client.subscribe("test")
msg = json.dumps({"from": 0,"name": "Alex", "age": 123})
client.publish(topic="test", payload=msg, qos=0)
client.loop_start()

emqx message:

2024-01-29 23:50:55 2024-01-29T20:50:55.938874+00:00 [info] [email protected]:48590 file: emqx_connection.erl, line: 520, mfa: {emqx_connection,terminate,2}, msg: terminate, pid: <0.2130.0>, reason: {shutdown,not_authorized}
2024-01-29 23:50:56 2024-01-29T20:50:56.213804+00:00 [debug] 172.100.10.1:48592 [MQTT] RECV <<16,34,0,4,77,81,84,84,4,194,0,60,0,0,0,17,49,49,58,...>>
2024-01-29 23:50:56 2024-01-29T20:50:56.213921+00:00 [debug] 172.100.10.1:48592 [MQTT] RECV CONNECT(Q0, R0, D0, ClientId=, ProtoName=MQTT, ProtoVsn=4, CleanStart=true, KeepAlive=60, Username=11:11:11:11:11:11, Password=******)
2024-01-29 23:50:56 2024-01-29T20:50:56.214042+00:00 [warning] [email protected]:48592 [Channel] Client MzE0ODA1MDI0Mjg4MzY4ODcyMjYxMDcxMDEzMDI2ODU2OTG (Username: '11:11:11:11:11:11') login failed for not_authorized
2024-01-29 23:50:56 2024-01-29T20:50:56.214104+00:00 [debug] [email protected]:48592 [MQTT] SEND CONNACK(Q0, R0, D0, AckFlags=0, ReasonCode=5)
2024-01-29 23:50:56 2024-01-29T20:50:56.214204+00:00 [info] [email protected]:48592 file: emqx_connection.erl, line: 520, mfa: {emqx_connection,terminate,2}, msg: terminate, pid: <0.2131.0>, reason: {shutdown,not_authorized}

postgres.Wardrobe:

db data

I have two options as to what this problem is about: 1: The container is not configured correctly 2: Related to the first one, I have an incorrectly configured link to the authorization attempt

Help me

0

There are 0 best solutions below