bigpresh / Dancer-Plugin-Database

Dancer::Plugin::Database - easy database support for Dancer applications
http://search.cpan.org/dist/Dancer-Plugin-Database
37 stars 36 forks source link

No DB settings for database #84

Open BTrey opened 8 years ago

BTrey commented 8 years ago

Using YARBAC with Dancer2 and Database. This has been working but abruptly stopped working yesterday. Immediately prior to it stopping working, I installed Dancer2::Serializer::JSON. Not sure that was the cause of the issue but I thought I'd mention it in case it's relevant. I used git to restore my app to a previous version which was working fine, and it continues to fail. The error I'm getting on the web page is a 500: Can't call method "quick_select" on an undefined value at /usr/local/share/perl/5.14.2/Dancer2/Plugin/Auth/YARBAC/Provider/Database.pm line 108.

The call stack is:

main in -e l. 0
main in -e l. 0
Plack::Handler::Apache2 in /usr/local/share/perl/5.14.2/Plack/Handler/Apache2.pm l. 126
Plack::Handler::Apache2 in /usr/local/share/perl/5.14.2/Plack/Handler/Apache2.pm l. 87
Plack::Component in /usr/local/share/perl/5.14.2/Plack/Component.pm l. 50
Plack::Middleware::Head in /usr/local/share/perl/5.14.2/Plack/Middleware/Head.pm l. 9
Plack::Component in /usr/local/share/perl/5.14.2/Plack/Component.pm l. 50
Plack::Middleware::Static in /usr/local/share/perl/5.14.2/Plack/Middleware/Static.pm l. 18
Plack::Component in /usr/local/share/perl/5.14.2/Plack/Component.pm l. 50
Plack::Middleware::FixMissingBodyInRedirect in /usr/local/share/perl/5.14.2/Plack/Middleware/FixMissingBodyInRedirect.pm l. 50
Dancer2::Core::App in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 1300
Dancer2::Core::App in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 1298
Dancer2::Core::App in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 1379
Return::MultiLevel in /usr/local/share/perl/5.14.2/Return/MultiLevel.pm l. 97
Return::MultiLevel in /usr/local/share/perl/5.14.2/Return/MultiLevel.pm l. 61
Dancer2::Core::App in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 1378
Dancer2::Core::App in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 1489
Dancer2::Core::App in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 1522
Dancer2::Core::Error in /usr/local/share/perl/5.14.2/Dancer2/Core/Error.pm l. 303
Method::Generate::Accessor in (eval 652) l. 18
Dancer2::Core::Error in /usr/local/share/perl/5.14.2/Dancer2/Core/Error.pm l. 290
Dancer2::Core::Error in /usr/local/share/perl/5.14.2/Dancer2/Core/Error.pm l. 111
Dancer2::Core::Error in /usr/local/share/perl/5.14.2/Dancer2/Core/Error.pm l. 160
Dancer2::Core::Error in /usr/local/share/perl/5.14.2/Dancer2/Core/Error.pm l. 386

Checking the Apache log, however, I find:

[Dancer2::Plugin::Auth::YARBAC::Provider::Database:31005] error @2016-06-06 08:21:31> No DB settings for sonic in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 836

That error actually appears to come from Dancer/Plugin/Database/Core.pm, sub database. In that sub, _get_settings($arg, $settings, $logger) is failing. I added a couple of quick logging lines to _get_settings and I'm seeing:

[Dancer2::Plugin::Auth::YARBAC::Provider::Database:31005] error @2016-06-06 08:21:31> $settings->{connections} connections does not exist in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 836 [Dancer2::Plugin::Auth::YARBAC::Provider::Database:31005] error @2016-06-06 08:21:31> $settings is $VAR1 = { 'charset' => 'utf-8' }; in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 836

However, from the 500 error web page, I see this:

Settings
appdir => '/var/www/sonic/',
apphandler => 'PSGI',
appname => 'SONIC',
behind_proxy => 0,
charset => 'utf-8',
content_type => 'text/html',
engines => {
  logger => {
    File => {
      file_name => 'sonic.log',
      log_dir => '/var/log/sonic'
    }
  },
  session => {
    Cookie => {
      default_duration => 10800,
      secret_key => 'Hidden (looks potentially sensitive)'
    }
  },
  template => {
    template_toolkit => {
      end_tag => '%>',
      start_tag => '<%'
    }
  }
},
environment => 'development',
host => '0.0.0.0',
layout => 'main',
log => 'debug',
logger => 'file',
no_server_tokens => 0,
plugins => {
  Ajax => {
    content_type => 'application/json'
  },
  'Auth::YARBAC' => {
    after_login => '/',
    after_logout => '/',
    login_denied => '/',
    no_login_required => '^/login|^/about|^/adduser|^/$',
    realms => {
      dexter => {
        db_connection_name => 'sonic',
        provider => 'Database'
      /
    }
  },
  Database => {
    connections => {
      dexter => {
        database => 'dexter',
        dbi_params => {
          AutoCommit => 1,
          RaiseError => 1
        },
        driver => 'mysql',
        host => 'localhost',
        log_queries => 1,
        on_connect_do => [
          'SET CHARACTER SET \'utf8\'',
          'SET SQL_MODE=\'TRADITIONAL\''
        ],
        password => 'Hidden (looks potentially sensitive)',
        port => 3306,
        username => 'dexter'
      },
      sonic => {
        database => 'sonic',
        dbi_params => {
          AutoCommit => 1,
          RaiseError => 1
        },
        driver => 'mysql',
        host => 'localhost',
        log_queries => 1,
        on_connect_do => [
          'SET CHARACTER SET \'utf8\'',
          'SET SQL_MODE=\'TRADITIONAL\''
        ],
        password => 'Hidden (looks potentially sensitive)',
        port => 3306,
        username => 'dexter'
      }
    }
  }
},
port => '3000',
public_dir => '/var/www/sonic/public',
route_handlers => [
  [
    'AutoPage',
    1
  ]
],
session => 'Cookie',
show_errors => 1,
startup_info => 1,
static_handler => 1,
template => 'template_toolkit',
traces => 0,
views => '/var/www/sonic/views',
warnings => 1

Note: Values of 3 sensitive-looking keys hidden

So it appears as though the settings are being correctly read from my .yml file. I'm far from an expert Perl developer, so perhaps I just have something screwed up.

BTrey commented 8 years ago

It appears the installation of Dancer2::Serializer::JSON updated Dancer2 from version .166001 to .200000 and the Database plugin from 2.15 to 2.16. I'm not sure exactly where it lies but there appears to be an incompatibility between this version of Dancer2, the Database plugin and YARBAC. YARBAC is attempting to access $self->db and that key doesn't appear to exist. I'm guessing YARBAC needs to be updated to work with the newer versions and am opening a bug on YARBAC.

BTrey commented 8 years ago

Per here there is a known issue with YARBAC. YARBAC appears to be abandoned. There are three bugs filed against it on CPAN, one of which is a year old, and none of them have been so much as acknowledged. There was a pull request on the incompatibility with the new version of Dancer in January and it doesn't appear to have had any response either. So I switched to Auth::Extensible with the Database provider and I'm still having issues with the Database credentials. Getting a 500 error: Can't get a database connection without settings supplied! Please check you've supplied settings in config as per the Dancer::Plugin::Database documentation at /usr/local/share/perl/5.14.2/Dancer/Plugin/Database/Core.pm line 213.

Throwing a few debug Dumpers into the complaining function like so:

sub _get_connection {
    my ($settings, $logger, $hook_exec) = @_;

    $logger->(debug => "settings are " . Dumper($settings));
    $logger->(debug => "dsn is " . Dumper($settings->{dsn}));
    $logger->(debug => "driver is " . Dumper($settings->{driver}));

    if (!$settings->{dsn} && !$settings->{driver}) {`

gets me:

[dexter:17741] debug @2016-06-12 19:14:37> settings are $VAR1 = {
        'connection_check_threshold' => 30,
        'connections' => { 
                            'port' => 3306,
                            'username' => 'dexter',
                            'host' => 'localhost',
                            'database' => 'sonic',
                            'on_connect_do' => [ 
                                                'SET CHARACTER SET \'utf8\'',
                                                'SET SQL_MODE=\'TRADITIONAL\''
                                                ],
                            'password' => 'blah',
                            'log_queries' => 0,
                            'dbi_params' => { 
                                            'AutoCommit' => 1,
                                            'RaiseError' => 1
                                            },
                            'driver' => 'mysql'
                        },
        'charset' => 'utf-8'
        }; in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 836
[dexter:17741] debug @2016-06-12 19:14:37> dsn is $VAR1 = undef; in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 836
[dexter:17741] debug @2016-06-12 19:14:37> driver is $VAR1 = undef; in /usr/local/share/perl/5.14.2/Dancer2/Core/App.pm l. 836

So the DB settings are there under settings->connections (including settings->connections->driver) but not under either settings->dsn or settings->drvier. The user code works fine with an older version of Dancer2. I verified by copying the Dancer and Dancer2 pm's and all subdirectories from another machine which has not been updated to the new version of Dancer2.

BTrey commented 8 years ago

In file Core.pm, sub _get_settings, making this change:

    if (!defined $name) {
    #$return_settings = { %$settings };
    $return_settings = $settings->{connections};

gets past the error. Obviously, you'd need to make a similar change under the else clause if you have multiple connections. I'm not at all certain this is anything more than a band-aid for another issue, so I'm not submitting a pull request.