View | Details | Raw Unified | Return to bug 36026
Collapse All | Expand All

(-)a/Koha/Database.pm (-17 / +50 lines)
Lines 61-73 sub dbh { Link Here
61
    my $driver = db_scheme2dbi($config->get('db_scheme'));
61
    my $driver = db_scheme2dbi($config->get('db_scheme'));
62
    my $user = $config->get("user"),
62
    my $user = $config->get("user"),
63
    my $pass = $config->get("pass"),
63
    my $pass = $config->get("pass"),
64
    my $dsn = sprintf(
64
    my $dsn = generate_dsn($config);
65
        'dbi:%s:database=%s;host=%s;port=%s',
66
        $driver,
67
        $config->get("database_test") || $config->get("database"),
68
        $config->get("hostname"),
69
        $config->get("port") || '',
70
    );
71
65
72
    my $attr = {
66
    my $attr = {
73
        RaiseError => 1,
67
        RaiseError => 1,
Lines 75-90 sub dbh { Link Here
75
    };
69
    };
76
70
77
    if ($driver eq 'mysql') {
71
    if ($driver eq 'mysql') {
78
        my $tls = $config->get("tls");
79
        if ($tls && $tls eq 'yes') {
80
            $dsn .= sprintf(
81
                ';mysql_ssl=1;mysql_ssl_client_key=%s;mysql_ssl_client_cert=%s;mysql_ssl_ca_file=%s',
82
                $config->get('key'),
83
                $config->get('cert'),
84
                $config->get('ca'),
85
            );
86
        }
87
88
        $attr->{mysql_enable_utf8} = 1;
72
        $attr->{mysql_enable_utf8} = 1;
89
    }
73
    }
90
74
Lines 197-202 sub db_scheme2dbi { Link Here
197
    return $scheme eq 'Pg' ? $scheme : 'mysql';
181
    return $scheme eq 'Pg' ? $scheme : 'mysql';
198
}
182
}
199
183
184
=head2 generate_dsn
185
186
    my $dsn = Koha::Database::generate_dsn($config);
187
188
Returns a data source name (DSN) for a database connection
189
from the config instance.
190
191
=cut
192
193
sub generate_dsn {
194
    my ($config) = @_;
195
    my $driver = db_scheme2dbi( $config->get('db_scheme') );
196
197
    my $dsn = sprintf(
198
        'dbi:%s:database=%s;host=%s;port=%s',
199
        $driver,
200
        $config->get("database_test") || $config->get("database"),
201
        $config->get("hostname"),
202
        $config->get("port") || '',
203
    );
204
205
    if ( $driver eq 'mysql' ) {
206
        my $tls = $config->get("tls");
207
        if ( $tls && $tls eq 'yes' ) {
208
209
            $dsn .= ';mysql_ssl=1';
210
211
            my $mysql_ssl_client_key  = $config->get('key');
212
            my $mysql_ssl_client_cert = $config->get('cert');
213
            my $mysql_ssl_ca_file     = $config->get('ca');
214
215
            if ( $mysql_ssl_client_key && $mysql_ssl_client_key ne '__DB_TLS_CLIENT_KEY__' ) {
216
                $dsn .= sprintf( ';mysql_ssl_client_key=%s', $mysql_ssl_client_key );
217
            }
218
219
            if ( $mysql_ssl_client_cert && $mysql_ssl_client_cert ne '__DB_TLS_CLIENT_CERTIFICATE__' ) {
220
                $dsn .= sprintf( ';mysql_ssl_client_cert=%s', $mysql_ssl_client_cert );
221
            }
222
223
            if ( $mysql_ssl_ca_file && $mysql_ssl_ca_file ne '__DB_TLS_CA_CERTIFICATE__' ) {
224
                $dsn .= sprintf( ';mysql_ssl_ca_file=%s', $mysql_ssl_ca_file );
225
            }
226
        }
227
228
    }
229
230
    return $dsn;
231
}
232
200
=head2 EXPORT
233
=head2 EXPORT
201
234
202
None by default.
235
None by default.
(-)a/t/db_dependent/Koha/Database.t (-2 / +57 lines)
Lines 16-23 Link Here
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
use Test::More tests => 3;
19
use Test::More tests => 4;
20
use C4::Context;
20
use C4::Context;
21
use t::lib::Mocks;
21
22
22
my $dbh = C4::Context->dbh;
23
my $dbh = C4::Context->dbh;
23
my $sql_mode = $dbh->selectrow_array(q|SELECT @@SQL_MODE|);
24
my $sql_mode = $dbh->selectrow_array(q|SELECT @@SQL_MODE|);
Lines 33-35 subtest 'db_scheme2dbi' => sub { Link Here
33
    is(Koha::Database::db_scheme2dbi('xxx'),   'mysql', 'ask for unsupported DBMS, get mysql');
34
    is(Koha::Database::db_scheme2dbi('xxx'),   'mysql', 'ask for unsupported DBMS, get mysql');
34
    is(Koha::Database::db_scheme2dbi(),        'mysql', 'ask for nothing, get mysql');
35
    is(Koha::Database::db_scheme2dbi(),        'mysql', 'ask for nothing, get mysql');
35
};
36
};
36
- 
37
38
subtest 'generate_dsn' => sub {
39
    plan tests => 6;
40
41
    my $config = Koha::Config->get_instance;
42
43
    t::lib::Mocks::mock_config( 'database',  'koha' );
44
    t::lib::Mocks::mock_config( 'hostname',  'localhost' );
45
    t::lib::Mocks::mock_config( 'port',      '3306' );
46
    t::lib::Mocks::mock_config( 'db_scheme', 'mysql' );
47
    t::lib::Mocks::mock_config( 'tls',       'no' );
48
49
    is(
50
        Koha::Database::generate_dsn($config),
51
        'dbi:mysql:database=koha;host=localhost;port=3306',
52
        'DSN string for MySQL configuration without TLS.'
53
    );
54
55
    t::lib::Mocks::mock_config( 'tls', 'yes' );
56
    is(
57
        Koha::Database::generate_dsn($config),
58
        'dbi:mysql:database=koha;host=localhost;port=3306;mysql_ssl=1',
59
        'DSN string for MySQL configuration with TLS without client key, client cert and ca file.'
60
    );
61
62
    t::lib::Mocks::mock_config( 'key', '/path/to/client-key.pem' );
63
    is(
64
        Koha::Database::generate_dsn($config),
65
        'dbi:mysql:database=koha;host=localhost;port=3306;mysql_ssl=1;mysql_ssl_client_key=/path/to/client-key.pem',
66
        'DSN string for MySQL configuration with TLS and client key.'
67
    );
68
69
    t::lib::Mocks::mock_config( 'cert', '/path/to/client-cert.pem' );
70
    is(
71
        Koha::Database::generate_dsn($config),
72
        'dbi:mysql:database=koha;host=localhost;port=3306;mysql_ssl=1;mysql_ssl_client_key=/path/to/client-key.pem;mysql_ssl_client_cert=/path/to/client-cert.pem',
73
        'DSN string for MySQL configuration with TLS, client key and client cert.'
74
    );
75
76
    t::lib::Mocks::mock_config( 'ca', '/path/to/ca.pem' );
77
    is(
78
        Koha::Database::generate_dsn($config),
79
        'dbi:mysql:database=koha;host=localhost;port=3306;mysql_ssl=1;mysql_ssl_client_key=/path/to/client-key.pem;mysql_ssl_client_cert=/path/to/client-cert.pem;mysql_ssl_ca_file=/path/to/ca.pem',
80
        'DSN string for MySQL configuration with TLS with client key, client cert and ca file.'
81
    );
82
83
    t::lib::Mocks::mock_config( 'key',  '__DB_TLS_CLIENT_KEY__' );
84
    t::lib::Mocks::mock_config( 'cert', '__DB_TLS_CLIENT_CERTIFICATE__' );
85
86
    is(
87
        Koha::Database::generate_dsn($config),
88
        'dbi:mysql:database=koha;host=localhost;port=3306;mysql_ssl=1;mysql_ssl_ca_file=/path/to/ca.pem',
89
        'DSN string for MySQL configuration with TLS with ca file.'
90
    );
91
};

Return to bug 36026