Skip to content

[BUG] Mongodb Authentication failed on clean setup #172

@rkone

Description

@rkone

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

With mongodb 8.0 and unifi-network-application 10.0.162, using the template init-mongo.sh script sets the $MONGO_USER user incorrectly, it is created for the admin database, then the user can't authenticate to the $MONGO_DBNAME database.

After the script, I used mongosh and checked the following:
mongosh (server)/($MONGO_DBNAME) -u ($MONGO_USER) - p ($MONGO_PASS)
Result: authentication failure

however this works:
mongosh (server)/($MONGO_DBNAME) -u ($MONGO_USER) - p ($MONGO_PASS) --authenticationDatabase admin

I don't see a way to configure this auth database parameter in docker compose, so I instead modified the init-mongo.sh script as follows:

#!/bin/bash

if which mongosh > /dev/null 2>&1; then
  mongo_init_bin='mongosh'
else
  mongo_init_bin='mongo'
fi
"${mongo_init_bin}" <<EOF
use ${MONGO_AUTHSOURCE}
db.auth("${MONGO_INITDB_ROOT_USERNAME}", "${MONGO_INITDB_ROOT_PASSWORD}")
use ${MONGO_DBNAME}
db.createUser({
  user: "${MONGO_USER}",
  pwd: "${MONGO_PASS}",
  roles: [
    { db: "${MONGO_DBNAME}", role: "dbOwner" },
    { db: "${MONGO_DBNAME}_stat", role: "dbOwner" },
    { db: "${MONGO_DBNAME}_audit", role: "dbOwner" }
  ]
})
EOF

Note the extra use ${MONGO_DBNAME} . This creates the $MONGO_USER in the $MONGO_DBNAME database instead of the admin database.
After this mongosh (server)/($MONGO_DBNAME) -u ($MONGO_USER) - p ($MONGO_PASS) succeeds, and my unifi-network-application container loaded successfully.

Expected Behavior

No response

Steps To Reproduce

Docker compose with given yaml, modifying paths to suit the local environment, and adding the default init-mongo.sh script

Environment

- OS: Ubuntu 22.04
- How docker service was installed: snap

CPU architecture

x86-64

Docker creation

services:
  unifi-db:
    image: docker.io/mongo:8.0
    container_name: unifi-db
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=America/Toronto
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=yyyyyyyyyyyy
      - MONGO_USER=unifi
      - MONGO_PASS=xxxxxxxxxxxx
      - MONGO_DBNAME=unifi
      - MONGO_AUTHSOURCE=admin
    volumes:
      - /var/snap/docker/common/unifi-mongodb-data:/data/db
      - /var/snap/docker/common/unifi-mongodb-initdb.d/init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh:ro
    ports:
      - 27017:27017
    networks:
      unifi_bridge:
        aliases:
          - unifi-db
    restart: unless-stopped
  unifi-network-application:
    image: lscr.io/linuxserver/unifi-network-application:latest
    container_name: unifi-network-application
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=America/Toronto
      - MONGO_USER=unifi
      - MONGO_PASS=xxxxxxxxxxxx
      - MONGO_HOST=unifi-db
      - MONGO_PORT=27017
      - MONGO_DBNAME=unifi
      - MEM_LIMIT=1024 #optional
      - MEM_STARTUP=1024 #optional
      - MONGO_TLS= #optional
    depends_on:
      - unifi-db
    volumes:
      - /var/snap/docker/common/unifi-network-application:/config
    ports:
      - 8443:8443
      - 3478:3478/udp
      - 10001:10001/udp
      - 8080:8080
      - 1900:1900/udp #optional
      - 8843:8843 #optional
      - 8880:8880 #optional
      - 6789:6789 #optional
      - 5514:5514/udp #optional
    networks:
      unifi_bridge:
        aliases:
          - unifi-network-application
    restart: unless-stopped
    
networks:
  unifi_bridge:
    driver: bridge

Container logs

Did not capture, but it was a java crash with authentication error as the root cause

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions