如何在 PostgreSQL 中使用 Mosquitto 身份验证插件?

问题描述 投票:0回答:1

我正在 PostgreSQL 和 Mosquitto MQTT 上编写一个服务器。使用 PostgreSQL 数据库的客户端需要使用 MQTT 授权。

我找不到任何说明,甚至找不到如何使用该插件或配置它的解释。我的任何尝试都会导致错误或只是缺乏结果。

那是我的

docker-compose.yml

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

  mqtt-broker:
    image: eclipse-mosquitto:latest
    user: mosquitto
    volumes:
      - ./mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
      - ./mosquitto/config/password.txt:/mosquitto/password.txt
      - ./mosquitto/log:/mosquitto/log
      - data:/mosquitto/data
    ports:
      - target: 1883
        published: 1883
        protocol: tcp
        mode: host
      - target: 9001
        published: 9001
        protocol: tcp
        mode: host
    networks:
      mqtt-net:
        ipv4_address: 172.100.10.10

  mqtt-pub:
    image: eclipse-mosquitto:latest
    command: sh -c "mosquitto_pub -h mqtt-broker -t test -m 'Hello World' -u Frog -P 1234"
    depends_on:
      - mqtt-broker
    networks:
      mqtt-net:
        ipv4_address: 172.100.10.11

  mqtt-sub:
    image: eclipse-mosquitto:latest
    command: sh -c "mosquitto_sub -h mqtt-broker -t test -u Frog -P 1234"
    depends_on:
      - mqtt-broker
    networks:
      mqtt-net:
        ipv4_address: 172.100.10.12

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:

请帮我设置 PostgreSQL 和 Mosquitto。

我尝试在Linux虚拟机上运行它,但出现此错误:

user@user-virtual-machine:~/mosquitto-auth-plug$ sudo nano config.mk
user@user-virtual-machine:~/mosquitto-auth-plug$ make
Selected backends:         PostgreSQL
Using mosquitto source dir: mosquitto-2.0.18/
OpenSSL install dir:        /usr

If you changed the backend selection, you might need to 'make clean' first

CFLAGS:  -Imosquitto-2.0.18//src/ -Imosquitto-2.0.18//lib/ -fPIC -Wall -Werror -DBE_POSTGRES -I/usr/include/postgresql -I/src -DDEBUG=1 -I/usr/include
LDFLAGS:  -Lmosquitto-2.0.18//lib/
LDADD:   -L/usr/lib/x86_64-linux-gnu -lpq -L/usr/lib -lcrypto -lmosquitto

cc -Imosquitto-2.0.18//src/ -Imosquitto-2.0.18//lib/ -fPIC -Wall -Werror -DBE_POSTGRES -I`pg_config --includedir` -I/src -DDEBUG=1 -I/usr/include   -c -o auth-plug.o auth-plug.c
auth-plug.c:502:5: error: conflicting types for ‘mosquitto_auth_unpwd_check’; have ‘int(void *, const struct mosquitto *, const char *, const char *)’
  502 | int mosquitto_auth_unpwd_check(void *userdata, const struct mosquitto *client, const char *username, const char *password)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from auth-plug.c:36:
/usr/include/mosquitto_plugin.h:356:24: note: previous declaration of ‘mosquitto_auth_unpwd_check’ with type ‘int(void *, struct mosquitto *, const char *, const char *)’
  356 | mosq_plugin_EXPORT int mosquitto_auth_unpwd_check(void *user_data, struct mosquitto *client, const char *username, const char *password);
      |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~
auth-plug.c:601:5: error: conflicting types for ‘mosquitto_auth_acl_check’; have ‘int(void *, int,  const struct mosquitto *, const struct mosquitto_acl_msg *)’
  601 | int mosquitto_auth_acl_check(void *userdata, int access, const struct mosquitto *client, const struct mosquitto_acl_msg *msg)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from auth-plug.c:36:
/usr/include/mosquitto_plugin.h:339:24: note: previous declaration of ‘mosquitto_auth_acl_check’ with type ‘int(void *, int,  struct mosquitto *, const struct mosquitto_acl_msg *)’
  339 | mosq_plugin_EXPORT int mosquitto_auth_acl_check(void *user_data, int access, struct mosquitto *client, const struct mosquitto_acl_msg *msg);
      |                        ^~~~~~~~~~~~~~~~~~~~~~~~
auth-plug.c:752:5: error: conflicting types for ‘mosquitto_auth_psk_key_get’; have ‘int(void *, const struct mosquitto *, const char *, const char *, char *, int)’
  752 | int mosquitto_auth_psk_key_get(void *userdata, const struct mosquitto *client, const char *hint, const char *identity, char *key, int max_key_len)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from auth-plug.c:36:
/usr/include/mosquitto_plugin.h:384:24: note: previous declaration of ‘mosquitto_auth_psk_key_get’ with type ‘int(void *, struct mosquitto *, const char *, const char *, char *, int)’
  384 | mosq_plugin_EXPORT int mosquitto_auth_psk_key_get(void *user_data, struct mosquitto *client, const char *hint, const char *identity, char *key, int max_key_len);
      |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [<встроенное>: auth-plug.o] Ошибка 1

我也在Docker上尝试过,但没有得到任何结果。

postgresql docker docker-compose mqtt mosquitto
1个回答
0
投票

经过多次尝试和问题的提示:

mosquitto-go-auth 出现 postgres 授权错误

我设法定制 docker-compose 来满足我的需求:

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

  auth-plugin:
    image: iegomez/mosquitto-go-auth
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - ./mosquitto/config/mosquitto.conf:/etc/mosquitto/mosquitto.conf
      - ./mosquitto/log/mosquitto.log:/mosquitto/log/mosquitto.log
      - data:/mosquitto/data
    ports:
      - target: 1883
        published: 1883
        protocol: tcp
        mode: host
      - target: 9001
        published: 9001
        protocol: tcp
        mode: host
    networks:
      - postgres


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

networks:
  postgres:
    driver: bridge

  postgres-data:
© www.soinside.com 2019 - 2024. All rights reserved.