From 8ee1cff7fb3010c0dc65a5c9971b885fe4ab842e Mon Sep 17 00:00:00 2001 From: Guillaume Rousse <guillaume.rousse@renater.fr> Date: Thu, 2 Nov 2017 17:46:06 +0100 Subject: [PATCH] don't hardcode database parameters in code --- bin/account-manager-client.pl | 25 ++++++++++++++++++++----- conf/Conf.pm | 4 ++-- lib/IdPAccountManager/DB.pm | 27 ++++++++++++++++++++------- lib/IdPAccountManager/WebRequest.pm | 17 +++++++++++++++++ 4 files changed, 59 insertions(+), 14 deletions(-) diff --git a/bin/account-manager-client.pl b/bin/account-manager-client.pl index 346069f..1379207 100755 --- a/bin/account-manager-client.pl +++ b/bin/account-manager-client.pl @@ -48,6 +48,15 @@ pod2usage( -verbose => 0 ) unless $action; +IdPAccountManager::DB->register_db( + driver => $Conf::global{database_type}, + database => $Conf::global{database_name}, + host => $Conf::global{database_host}, + password => $Conf::global{database_password}, + username => $Conf::global{database_user} +); + +my $db = IdPAccountManager::DB->new(); if ($action eq 'add_test_account') { @@ -62,6 +71,7 @@ if ($action eq 'add_test_account') { ) unless $options{'sp_entityid'}; my $test_account = IdPAccountManager::Data::TestAccount->new( + db => $db, account_profile => $options{'account_profile'}, sp_entityid => $options{'sp_entityid'} ); @@ -94,7 +104,7 @@ if ($action eq 'add_test_account') { } my $accounts = - IdPAccountManager::Data::TestAccount::Manager->get_testaccounts(%args); + IdPAccountManager::Data::TestAccount::Manager->get_testaccounts(db => $db, %args); if (! @$accounts) { printf "No matching test account in DB\n"; @@ -160,7 +170,9 @@ if ($action eq 'add_test_account') { ## Check if entry already exists in DB first my $service_provider = IdPAccountManager::Data::ServiceProvider->new( - entityid => $options{'sp_entityid'}); + db => $db, + entityid => $options{'sp_entityid'} + ); if ($service_provider->load(speculative => 1)) { printf "Entry for %s already in DB; update it with new data\n", $options{'sp_entityid'}; @@ -171,6 +183,7 @@ if ($action eq 'add_test_account') { } else { $service_provider = IdPAccountManager::Data::ServiceProvider->new( + db => $db, entityid => $options{'sp_entityid'}, contacts => $options{'contacts'}, displayname => $options{'displayname'} @@ -188,7 +201,7 @@ if ($action eq 'add_test_account') { my %args; - my $providers = IdPAccountManager::Data::ServiceProvider::Manager->get_serviceproviders(%args); + my $providers = IdPAccountManager::Data::ServiceProvider::Manager->get_serviceproviders(db => $db, %args); if (@$providers) { printf "No service provider in DB\n"; @@ -213,7 +226,7 @@ if ($action eq 'add_test_account') { } my $tokens = - IdPAccountManager::Data::AuthenticationToken::Manager->get_authenticationtokens(%args); + IdPAccountManager::Data::AuthenticationToken::Manager->get_authenticationtokens(db => $db, %args); if (!@$tokens) { printf "No corresponding token found in DB\n"; @@ -239,7 +252,7 @@ if ($action eq 'add_test_account') { } my $authentication_token = - IdPAccountManager::AuthenticationToken->new(%args); + IdPAccountManager::AuthenticationToken->new(db => $db, %args); die "No corresponding token found in DB\n" unless $authentication_token->load(); @@ -265,6 +278,7 @@ if ($action eq 'add_test_account') { ) unless $options{'sp_entityid'}; my $authentication_token = IdPAccountManager::Data::AuthenticationToken->new( + db => $db, 'email_address' => $options{'email_address'}, 'sp_entityid' => $options{'sp_entityid'} ); @@ -278,6 +292,7 @@ if ($action eq 'add_test_account') { unless $authentication_token->delete(); $authentication_token = IdPAccountManager::Data::AuthenticationToken->new( + db => $db, 'email_address' => $options{'email_address'}, 'sp_entityid' => $options{'sp_entityid'} ); diff --git a/conf/Conf.pm b/conf/Conf.pm index 37021d2..18ae1e0 100644 --- a/conf/Conf.pm +++ b/conf/Conf.pm @@ -44,10 +44,10 @@ our %global = ( 'database_name' => 'idp_account_manager', ## Database username - 'database_user' => 'idpadmin', + 'database_user' => 'root', ## Database user password - 'database_password' => 'secret', + 'database_password' => 'root', ## Log file for the manager 'log_file' => '/opt/testidp/IdPAccountManager/log/manager.log', diff --git a/lib/IdPAccountManager/DB.pm b/lib/IdPAccountManager/DB.pm index b8206d5..eb7f5bb 100644 --- a/lib/IdPAccountManager/DB.pm +++ b/lib/IdPAccountManager/DB.pm @@ -7,13 +7,26 @@ use base 'Rose::DB'; __PACKAGE__->use_private_registry; -__PACKAGE__->register_db -( - connect_options => { AutoCommit => 1, ChopBlanks => 1 }, - driver => 'mysql', - dsn => 'dbi:mysql:dbname=idp_account_manager;host=localhost', - password => 'root', - username => 'root', +__PACKAGE__->SUPER::register_db ( + connect_options => { AutoCommit => 1, ChopBlanks => 1 }, + driver => 'mysql', + host => 'localhost', + database => 'fake', + password => 'fake', + username => 'fake', ); +sub register_db { + my ($pkg, %args) = @_; + + __PACKAGE__->SUPER::register_db ( + connect_options => { AutoCommit => 1, ChopBlanks => 1 }, + driver => $args{driver}, + host => $args{host}, + database => $args{database}, + password => $args{password}, + username => $args{username} + ); +} + 1; diff --git a/lib/IdPAccountManager/WebRequest.pm b/lib/IdPAccountManager/WebRequest.pm index 97ebc20..e1c5bf1 100755 --- a/lib/IdPAccountManager/WebRequest.pm +++ b/lib/IdPAccountManager/WebRequest.pm @@ -28,6 +28,16 @@ sub new { message => '' ); + IdPAccountManager::DB->register_db( + driver => $Conf::global{database_type}, + database => $Conf::global{database_name}, + host => $Conf::global{database_host}, + password => $Conf::global{database_password}, + username => $Conf::global{database_user} + ); + + $self->{db} = IdPAccountManager::DB->new(); + my $http_query = CGI->new(); ## Input parameters @@ -298,6 +308,7 @@ sub req_select_sp { ## Create a serviceprovider object to store major parameters for this SP in DB my $service_provider = IdPAccountManager::Data::ServiceProvider->new( + db => $self->{db}, entityid => $self->{'param_in'}{'sp_entityid'}, dev_sp_contact => $Conf::global{'dev_sp_contact'} ); @@ -338,6 +349,7 @@ sub req_select_sp { } else { $service_provider = IdPAccountManager::Data::ServiceProvider->new( + db => $self->{db}, entityid => $self->{'param_in'}{'sp_entityid'}, contacts => join(',', @contacts), displayname => $display_name, @@ -393,6 +405,7 @@ sub req_generate_token { ## Create a serviceprovider object to load parameters for this SP from DB my $service_provider = IdPAccountManager::Data::ServiceProvider->new( + db => $self->{db}, entityid => $self->{'param_in'}{'sp_entityid'}, dev_sp_contact => $Conf::global{'dev_sp_contact'} ); @@ -423,6 +436,7 @@ sub req_generate_token { } my $authentication_token = IdPAccountManager::Data::AuthenticationToken->new( + db => $self->{db}, 'email_address' => $self->{'param_in'}{'email_address'}, 'sp_entityid' => $self->{'param_in'}{'sp_entityid'} ); @@ -450,6 +464,7 @@ sub req_generate_token { } $authentication_token = IdPAccountManager::Data::AuthenticationToken->new( + db => $self->{db}, 'email_address' => $self->{'param_in'}{'email_address'}, 'sp_entityid' => $self->{'param_in'}{'sp_entityid'} ); @@ -523,6 +538,7 @@ sub req_validate_token { } my $authentication_token = IdPAccountManager::Data::AuthenticationToken->new( + db => $self->{db}, token => $self->{'param_in'}{'authentication_token'}); unless ($authentication_token->load()) { @@ -565,6 +581,7 @@ sub req_validate_token { foreach my $profile ($Conf::global{'account_profiles'}) { my $test_account = IdPAccountManager::Data::TestAccount->new( + db => $self->{db}, account_profile => $profile, sp_entityid => $self->{'param_in'}{'sp_entityid'} ); -- GitLab