3

I have had an elaborated check picked here and there on my ansible playbook to check whether authentication is enabled on mongo and it seems it doesn't work any longer with 4.0. Up to now it always works as expected.

/usr/bin/mongo --eval 'db.getUsers()' $1 | grep 'not auth' | grep -P "usersInfo:\s+\d\.\d" -o

Off late this returns nothing. So I wanted to cross check the facts. I found this stackoverflow post with similar commands.

I have run it as well and it shows that auth is not enabled meanwhile is it.

Below is the output of that experiment. enter image description here

I have not found anything specific to how to do this differently on 4.0 so I am wondering if anybody knows how to get this done in 4.0.

black sensei
  • 6,528
  • 22
  • 109
  • 188
  • It appears you are using Percona's MongoDB instead of the official MongoDB server, so some things may differ between them. – kevinadi May 20 '19 at 23:37

1 Answers1

1

In the newer versions the output format has changed. A script from the linked post relies on not auth substring to determine if the command was successful. So it's easy to adopt it:

#!/bin/bash

# Connect to MongoDB address (host:port/dbname) specified as first parameter
# If no address specified, `mongo` default will be localhost:27017/test
isAuth=`mongo --eval "db.getUsers()" $1 | grep "requires authentication"`

if [ -z "$isAuth" ] ;
then
   echo "mongod auth is NOT enabled"
   exit 1
else
   echo "mongod auth is ENABLED"
   exit 0
fi

P.S. I'll update the linked post accordingly

Oleh Rybalchenko
  • 6,998
  • 3
  • 22
  • 36