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 {
die "Tried to connect to non-existent 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"},
$params->{'shadowdb'}, $params->{"shadowdbport"},
$params->{"shadowdbsock"}, $lc->{db_user}, $lc->{db_pass});
return _connect(\%connect_params);
}
sub connect_main {
my $lc = Bugzilla->localconfig;
return _connect($lc->{db_driver}, $lc->{db_host}, $lc->{db_name}, $lc->{db_port},
$lc->{db_sock}, $lc->{db_user}, $lc->{db_pass});
return _connect(Bugzilla->localconfig);
}
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};
# do the actual import
......@@ -107,7 +112,7 @@ sub _connect {
. " localconfig: " . $@);
# 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;
}
......@@ -224,13 +229,14 @@ sub bz_create_database {
sub _get_no_db_connection {
my ($sql_server) = @_;
my $dbh;
my $lc = Bugzilla->localconfig;
my %connect_params = %{ Bugzilla->localconfig };
$connect_params{db_name} = '';
my $conn_success = eval {
$dbh = _connect($lc->{db_driver}, $lc->{db_host}, '', $lc->{db_port},
$lc->{db_sock}, $lc->{db_user}, $lc->{db_pass});
$dbh = _connect(\%connect_params);
};
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.
my $error = $DBI::errstr || $@;
chomp($error);
......@@ -1060,7 +1066,9 @@ sub bz_rollback_transaction {
#####################################################################
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
# (may be overridden by DB driver implementations)
......
......@@ -61,7 +61,9 @@ use constant MAX_COMMENTS => 50;
use base qw(Bugzilla::DB);
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
my $dsn = "dbi:mysql:host=$host;database=$dbname";
......@@ -74,7 +76,8 @@ sub new {
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
# return their data correctly.
......
......@@ -54,7 +54,9 @@ use constant ISOLATION_LEVEL => 'READ COMMITTED';
use constant BLOB_TYPE => { ora_type => ORA_BLOB };
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,
# and there is no default DB.
......@@ -70,7 +72,8 @@ sub new {
LongReadLen => ( Bugzilla->params->{'maxattachmentsize'}
|| 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
$self->{private_bz_dsn} = $dsn;
......
......@@ -52,7 +52,9 @@ use base qw(Bugzilla::DB);
use constant BLOB_TYPE => { pg_type => DBD::Pg::PG_BYTEA };
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
# to connect to SOME database, even if we have
......@@ -70,7 +72,8 @@ sub new {
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
# 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