Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Username and password not used or should we use only auth_database #2448

Closed
dave2309 opened this issue Sep 15, 2022 · 2 comments
Closed

Username and password not used or should we use only auth_database #2448

dave2309 opened this issue Sep 15, 2022 · 2 comments

Comments

@dave2309
Copy link

  • Laravel-mongodb Version: 3.9.2
  • PHP Version: 8.0.22
  • Database Driver & Version: mongodb 5.0.12

Description:

In mongodb/src/Connection.php
There is those lines

    protected function createConnection($dsn, array $config, array $options)
    {
        // By default driver options is an empty array.
        $driverOptions = [];

        if (isset($config['driver_options']) && is_array($config['driver_options'])) {
            $driverOptions = $config['driver_options'];
        }

        // Check if the credentials are not already set in the options
        if (! isset($options['username']) && ! empty($config['username'])) {
            $options['username'] = $config['username'];
        }
        if (! isset($options['password']) && ! empty($config['password'])) {
            $options['password'] = $config['password'];
        }

        return new Client($dsn, $options, $driverOptions);
    }

Where username and password are considered as options if they exist in the database configuration.
But later in

    protected function getHostDsn(array $config)
    {
        // Treat host option as array of hosts
        $hosts = is_array($config['host']) ? $config['host'] : [$config['host']];

        foreach ($hosts as &$host) {
            // Check if we need to add a port to the host
            if (strpos($host, ':') === false && ! empty($config['port'])) {
                $host = $host.':'.$config['port'];
            }
        }

        // Check if we want to authenticate against a specific database.
        $auth_database = isset($config['options']) && ! empty($config['options']['database']) ? $config['options']['database'] : null;

        return 'mongodb://'.implode(',', $hosts).($auth_database ? '/'.$auth_database : '');
    }

When composing the DSN to be used, then username and password are not inserted or used at all.
Actually I did not find anywhere where those values are actually used.

Steps to reproduce

Add the username and password configuration entries.
2.
Try a query
3.
Authentication fails (as the credentials are not passed on the DSN

Expected behaviour

username and `password should be used when resolving the DSN.

Actual behaviour

Authentication fails so fails the query

Logs: [2022-09-15 06:47:47] local.ERROR: Authentication failed. {"exception":"[object] (MongoDB\\Driver\\Exception\\AuthenticationException(code: 11): Authentication failed. at /srv/lrvl.dev.timesofmalta.com/source/1/vendor/mongodb/mongodb/src/Operation/Find.php:317) [stacktrace] #0 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/mongodb/mongodb/src/Operation/Find.php(317): MongoDB\\Driver\\Server->executeQuery() #1 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/mongodb/mongodb/src/Collection.php(666): MongoDB\\Operation\\Find->execute() #2 [internal function]: MongoDB\\Collection->find() #3 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/jenssegers/mongodb/src/Collection.php(45): call_user_func_array() #4 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/jenssegers/mongodb/src/Query/Builder.php(410): Jenssegers\\Mongodb\\Collection->__call() #5 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/jenssegers/mongodb/src/Query/Builder.php(201): Jenssegers\\Mongodb\\Query\\Builder->getFresh() #6 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(699): Jenssegers\\Mongodb\\Query\\Builder->get() #7 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(683): Illuminate\\Database\\Eloquent\\Builder->getModels() #8 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php(296): Illuminate\\Database\\Eloquent\\Builder->get() #9 /srv/lrvl.dev.timesofmalta.com/source/1/app/Http/Controllers/ArticleController.php(17): Illuminate\\Database\\Eloquent\\Builder->first() #10 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\\Http\\Controllers\\ArticleController->index() #11 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction() #12 /srv/lrvl.dev.timesofmalta.com/source/1/vendor/laravel/f
@dave2309
Copy link
Author

Ok, I think I figured it out.
DB_AUTHENTICATION_DATABASE needs to be the database on which the user has been created.
I will close this issue.
Sorry for the disturbance.

@alcaeus
Copy link
Member

alcaeus commented Sep 15, 2022

Noted that you resolved the issue while I was typing this, but posting it anyways for the benefit of others:

While you can add username and password to the connection string for MongoDB, these can also be passed via the $uriOptions argument to MongoDB\Driver\Manager::__construct, which is invoked in the constructor for MongoDB\Client (created in createConnection). This options argument takes any connection string option.

The AuthenticationException indicates that the driver attempted authentication, which then failed. This can be caused due to attempting to authenticate against the wrong authentication database. As is currently happening, this library expects the configured database (where data is stored) to also be used as the default authentication database (as indicated by it being passed as default value for the authentication database). Setting the authSource connection string option to the correct database will resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants