Commit 68af4d3c authored by Max Kanat-Alexander's avatar Max Kanat-Alexander

Bug 561322: Make Bugzilla::DB::_connect and related functions take their

parameters as a hashref, to improve the API and to avoid exposing the database password on error. r=mkanat, a=mkanat (module owner)
parent b7fbcf61
...@@ -83,22 +83,27 @@ sub connect_shadow { ...@@ -83,22 +83,27 @@ sub connect_shadow {
die "Tried to connect to non-existent shadowdb" die "Tried to connect to non-existent shadowdb"
unless $params->{'shadowdb'}; unless $params->{'shadowdb'};
my $lc = Bugzilla->localconfig; # Instead of just passing in a new hashref, we locally modify the
# values of "localconfig", because some drivers access it while
# connecting.
my %connect_params = %{ Bugzilla->localconfig };
$connect_params{db_host} = $params->{'shadowdbhost'};
$connect_params{db_name} = $params->{'shadowdb'};
$connect_params{db_port} = $params->{'shadowdbport'};
$connect_params{db_sock} = $params->{'shadowdbsock'};
return _connect($lc->{db_driver}, $params->{"shadowdbhost"}, return _connect(\%connect_params);
$params->{'shadowdb'}, $params->{"shadowdbport"},
$params->{"shadowdbsock"}, $lc->{db_user}, $lc->{db_pass});
} }
sub connect_main { sub connect_main {
my $lc = Bugzilla->localconfig; my $lc = Bugzilla->localconfig;
return _connect($lc->{db_driver}, $lc->{db_host}, $lc->{db_name}, $lc->{db_port}, return _connect(Bugzilla->localconfig);
$lc->{db_sock}, $lc->{db_user}, $lc->{db_pass});
} }
sub _connect { sub _connect {
my ($driver, $host, $dbname, $port, $sock, $user, $pass) = @_; my ($params) = @_;
my $driver = $params->{db_driver};
my $pkg_module = DB_MODULE->{lc($driver)}->{db}; my $pkg_module = DB_MODULE->{lc($driver)}->{db};
# do the actual import # do the actual import
...@@ -107,7 +112,7 @@ sub _connect { ...@@ -107,7 +112,7 @@ sub _connect {
. " localconfig: " . $@); . " localconfig: " . $@);
# instantiate the correct DB specific module # instantiate the correct DB specific module
my $dbh = $pkg_module->new($user, $pass, $host, $dbname, $port, $sock); my $dbh = $pkg_module->new($params);
return $dbh; return $dbh;
} }
...@@ -224,13 +229,14 @@ sub bz_create_database { ...@@ -224,13 +229,14 @@ sub bz_create_database {
sub _get_no_db_connection { sub _get_no_db_connection {
my ($sql_server) = @_; my ($sql_server) = @_;
my $dbh; my $dbh;
my $lc = Bugzilla->localconfig; my %connect_params = %{ Bugzilla->localconfig };
$connect_params{db_name} = '';
my $conn_success = eval { my $conn_success = eval {
$dbh = _connect($lc->{db_driver}, $lc->{db_host}, '', $lc->{db_port}, $dbh = _connect(\%connect_params);
$lc->{db_sock}, $lc->{db_user}, $lc->{db_pass});
}; };
if (!$conn_success) { if (!$conn_success) {
my $sql_server = DB_MODULE->{lc($lc->{db_driver})}->{name}; my $driver = $connect_params{db_driver};
my $sql_server = DB_MODULE->{lc($driver)}->{name};
# Can't use $dbh->errstr because $dbh is undef. # Can't use $dbh->errstr because $dbh is undef.
my $error = $DBI::errstr || $@; my $error = $DBI::errstr || $@;
chomp($error); chomp($error);
...@@ -1060,7 +1066,9 @@ sub bz_rollback_transaction { ...@@ -1060,7 +1066,9 @@ sub bz_rollback_transaction {
##################################################################### #####################################################################
sub db_new { sub db_new {
my ($class, $dsn, $user, $pass, $override_attrs) = @_; my ($class, $params) = @_;
my ($dsn, $user, $pass, $override_attrs) =
@$params{qw(dsn user pass attrs)};
# set up default attributes used to connect to the database # set up default attributes used to connect to the database
# (may be overridden by DB driver implementations) # (may be overridden by DB driver implementations)
......
...@@ -61,7 +61,9 @@ use constant MAX_COMMENTS => 50; ...@@ -61,7 +61,9 @@ use constant MAX_COMMENTS => 50;
use base qw(Bugzilla::DB); use base qw(Bugzilla::DB);
sub new { sub new {
my ($class, $user, $pass, $host, $dbname, $port, $sock) = @_; my ($class, $params) = @_;
my ($user, $pass, $host, $dbname, $port, $sock) =
@$params{qw(db_user db_pass db_host db_name db_port db_sock)};
# construct the DSN from the parameters we got # construct the DSN from the parameters we got
my $dsn = "dbi:mysql:host=$host;database=$dbname"; my $dsn = "dbi:mysql:host=$host;database=$dbname";
...@@ -74,7 +76,8 @@ sub new { ...@@ -74,7 +76,8 @@ sub new {
mysql_auto_reconnect => 1, mysql_auto_reconnect => 1,
); );
my $self = $class->db_new($dsn, $user, $pass, \%attrs); my $self = $class->db_new({ dsn => $dsn, user => $user,
pass => $pass, attrs => \%attrs });
# This makes sure that if the tables are encoded as UTF-8, we # This makes sure that if the tables are encoded as UTF-8, we
# return their data correctly. # return their data correctly.
......
...@@ -54,7 +54,9 @@ use constant ISOLATION_LEVEL => 'READ COMMITTED'; ...@@ -54,7 +54,9 @@ use constant ISOLATION_LEVEL => 'READ COMMITTED';
use constant BLOB_TYPE => { ora_type => ORA_BLOB }; use constant BLOB_TYPE => { ora_type => ORA_BLOB };
sub new { sub new {
my ($class, $user, $pass, $host, $dbname, $port) = @_; my ($class, $params) = @_;
my ($user, $pass, $host, $dbname, $port) =
@$params{qw(db_user db_pass db_host db_name db_port)};
# You can never connect to Oracle without a DB name, # You can never connect to Oracle without a DB name,
# and there is no default DB. # and there is no default DB.
...@@ -70,7 +72,8 @@ sub new { ...@@ -70,7 +72,8 @@ sub new {
LongReadLen => ( Bugzilla->params->{'maxattachmentsize'} LongReadLen => ( Bugzilla->params->{'maxattachmentsize'}
|| 1000 ) * 1024, || 1000 ) * 1024,
}; };
my $self = $class->db_new($dsn, $user, $pass, $attrs); my $self = $class->db_new({ dsn => $dsn, user => $user,
pass => $pass, attrs => $attrs });
# Needed by TheSchwartz # Needed by TheSchwartz
$self->{private_bz_dsn} = $dsn; $self->{private_bz_dsn} = $dsn;
......
...@@ -52,7 +52,9 @@ use base qw(Bugzilla::DB); ...@@ -52,7 +52,9 @@ use base qw(Bugzilla::DB);
use constant BLOB_TYPE => { pg_type => DBD::Pg::PG_BYTEA }; use constant BLOB_TYPE => { pg_type => DBD::Pg::PG_BYTEA };
sub new { sub new {
my ($class, $user, $pass, $host, $dbname, $port) = @_; my ($class, $params) = @_;
my ($user, $pass, $host, $dbname, $port) =
@$params{qw(db_user db_pass db_host db_name db_port)};
# The default database name for PostgreSQL. We have # The default database name for PostgreSQL. We have
# to connect to SOME database, even if we have # to connect to SOME database, even if we have
...@@ -70,7 +72,8 @@ sub new { ...@@ -70,7 +72,8 @@ sub new {
my $attrs = { pg_enable_utf8 => Bugzilla->params->{'utf8'} }; my $attrs = { pg_enable_utf8 => Bugzilla->params->{'utf8'} };
my $self = $class->db_new($dsn, $user, $pass, $attrs); my $self = $class->db_new({ dsn => $dsn, user => $user,
pass => $pass, attrs => $attrs });
# all class local variables stored in DBI derived class needs to have # all class local variables stored in DBI derived class needs to have
# a prefix 'private_'. See DBI documentation. # a prefix 'private_'. See DBI documentation.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment