Skip to content

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

Closed
@dave2309

Description

@dave2309
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions