Skip to content

Commit 2a6daf9

Browse files
authored
feat: add SQLSRV trustServerCertificate connection option (#10007)
* feat: add SQLSRV trustServerCertificate connection option * fix docs * fix rector
1 parent 35a1ab4 commit 2a6daf9

File tree

4 files changed

+92
-81
lines changed

4 files changed

+92
-81
lines changed

app/Config/Database.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,23 @@ class Database extends Config
108108
// * @var array<string, mixed>
109109
// */
110110
// public array $default = [
111-
// 'DSN' => '',
112-
// 'hostname' => 'localhost',
113-
// 'username' => 'root',
114-
// 'password' => 'root',
115-
// 'database' => 'ci4',
116-
// 'schema' => 'dbo',
117-
// 'DBDriver' => 'SQLSRV',
118-
// 'DBPrefix' => '',
119-
// 'pConnect' => false,
120-
// 'DBDebug' => true,
121-
// 'charset' => 'utf8',
122-
// 'swapPre' => '',
123-
// 'encrypt' => false,
124-
// 'failover' => [],
125-
// 'port' => 1433,
126-
// 'dateFormat' => [
111+
// 'DSN' => '',
112+
// 'hostname' => 'localhost',
113+
// 'username' => 'root',
114+
// 'password' => 'root',
115+
// 'database' => 'ci4',
116+
// 'schema' => 'dbo',
117+
// 'DBDriver' => 'SQLSRV',
118+
// 'DBPrefix' => '',
119+
// 'pConnect' => false,
120+
// 'DBDebug' => true,
121+
// 'charset' => 'utf8',
122+
// 'swapPre' => '',
123+
// 'encrypt' => false,
124+
// 'trustServerCertificate' => false,
125+
// 'failover' => [],
126+
// 'port' => 1433,
127+
// 'dateFormat' => [
127128
// 'date' => 'Y-m-d',
128129
// 'datetime' => 'Y-m-d H:i:s',
129130
// 'time' => 'H:i:s',

system/Database/SQLSRV/Connection.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class Connection extends BaseConnection
6666
*/
6767
public $schema = 'dbo';
6868

69+
/**
70+
* Trust server certificate.
71+
*/
72+
public bool $trustServerCertificate = false;
73+
6974
/**
7075
* Quoted identifier flag
7176
*
@@ -110,13 +115,14 @@ public function connect(bool $persistent = false)
110115
$charset = in_array(strtolower($this->charset), ['utf-8', 'utf8'], true) ? 'UTF-8' : SQLSRV_ENC_CHAR;
111116

112117
$connection = [
113-
'UID' => empty($this->username) ? '' : $this->username,
114-
'PWD' => empty($this->password) ? '' : $this->password,
115-
'Database' => $this->database,
116-
'ConnectionPooling' => $persistent ? 1 : 0,
117-
'CharacterSet' => $charset,
118-
'Encrypt' => $this->encrypt === true ? 1 : 0,
119-
'ReturnDatesAsStrings' => 1,
118+
'UID' => empty($this->username) ? '' : $this->username,
119+
'PWD' => empty($this->password) ? '' : $this->password,
120+
'Database' => $this->database,
121+
'ConnectionPooling' => $persistent ? 1 : 0,
122+
'CharacterSet' => $charset,
123+
'Encrypt' => $this->encrypt === true ? 1 : 0,
124+
'TrustServerCertificate' => $this->trustServerCertificate ? 1 : 0,
125+
'ReturnDatesAsStrings' => 1,
120126
];
121127

122128
// If the username and password are both empty, assume this is a

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ Testing
132132
Database
133133
========
134134

135+
- Added ``trustServerCertificate`` option to ``SQLSRV`` database connections in ``Config\Database``. Set it to ``true`` to trust the server certificate without CA validation when using encrypted connections.
136+
135137
Query Builder
136138
-------------
137139

user_guide_src/source/database/configuration.rst

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -140,64 +140,66 @@ and decode it in the constructor in the Config class:
140140
Description of Values
141141
*********************
142142

143-
================ ===========================================================================================================
144-
Config Name Description
145-
================ ===========================================================================================================
146-
**DSN** The DSN connect string (an all-in-one configuration sequence).
147-
**hostname** The hostname of your database server. Often this is 'localhost'.
148-
**username** The username used to connect to the database. (``SQLite3`` does not use this.)
149-
**password** The password used to connect to the database. (``SQLite3`` does not use this.)
150-
**database** The name of the database you want to connect to.
151-
152-
.. note:: CodeIgniter doesn't support dots (``.``) in the table and column names.
153-
Since v4.5.0, database names with dots are supported.
154-
**DBDriver** The database driver name. The case must match the driver name.
155-
You can set a fully qualified classname to use your custom driver.
156-
Supported drivers: ``MySQLi``, ``Postgre``, ``SQLite3``, ``SQLSRV``, and ``OCI8``.
157-
**DBPrefix** An optional table prefix which will be added to the table name when running
158-
:doc:`Query Builder <query_builder>` queries. This permits multiple CodeIgniter
159-
installations to share one database.
160-
**pConnect** true/false (boolean) - Whether to use a persistent connection.
161-
**DBDebug** true/false (boolean) - Whether to throw exceptions when database errors occur.
162-
**charset** The character set used in communicating with the database.
163-
**DBCollat** (``MySQLi`` only) The character collation used in communicating with the database.
164-
**swapPre** A default table prefix that should be swapped with ``DBPrefix``. This is useful for distributed
165-
applications where you might run manually written queries, and need the prefix to still be
166-
customizable by the end user.
167-
**schema** (``Postgre`` and ``SQLSRV`` only) The database schema, default value varies by driver.
168-
**encrypt** (``MySQLi`` and ``SQLSRV`` only) Whether to use an encrypted connection.
169-
See :ref:`MySQLi encrypt <mysqli-encrypt>` for ``MySQLi`` settings.
170-
``SQLSRV`` driver accepts true/false.
171-
**compress** (``MySQLi`` only) Whether to use client compression.
172-
**strictOn** (``MySQLi`` only) true/false (boolean) - Whether to force "Strict Mode" connections, good for ensuring
173-
strict SQL while developing an application.
174-
**port** The database port number - Empty string ``''`` for default port (or dynamic port with ``SQLSRV``).
175-
**foreignKeys** (``SQLite3`` only) true/false (boolean) - Whether to enable Foreign Key constraint.
176-
177-
.. important:: SQLite3 Foreign Key constraint is disabled by default.
178-
See `SQLite documentation <https://www.sqlite.org/pragma.html#pragma_foreign_keys>`_.
179-
To enforce Foreign Key constraint, set this config item to true.
180-
**busyTimeout** (``SQLite3`` only) milliseconds (int) - Sleeps for a specified amount of time when a table is locked.
181-
**synchronous** (``SQLite3`` only) flag (int) - How strict SQLite will be at flushing to disk during transactions.
182-
Use `null` to stay with the default setting. This can be used since v4.6.0.
183-
**numberNative** (``MySQLi`` only) true/false (boolean) - Whether to enable MYSQLI_OPT_INT_AND_FLOAT_NATIVE.
184-
**foundRows** (``MySQLi`` only) true/false (boolean) - Whether to enable MYSQLI_CLIENT_FOUND_ROWS.
185-
**dateFormat** The default date/time formats as PHP's `DateTime format`_.
186-
* ``date`` - date format
187-
* ``datetime`` - date and time format
188-
* ``datetime-ms`` - date and time with millisecond format
189-
* ``datetime-us`` - date and time with microsecond format
190-
* ``time`` - time format
191-
This can be used since v4.5.0, and you can get the value, e.g., ``$db->dateFormat['datetime']``.
192-
Currently, the database drivers do not use these values directly,
193-
but :ref:`Model <model-saving-dates>` uses them.
194-
**timezone** (``MySQLi``, ``Postgre``, and ``OCI8`` only) The database session timezone.
195-
* ``false`` - Don't set session timezone (default, backward compatible)
196-
* ``true`` - Automatically sync with ``App::$appTimezone``
197-
* ``string`` - Specific timezone offset (e.g., ``'+05:30'``) or named timezone (e.g., ``'America/New_York'``)
198-
Named timezones are automatically converted to offsets for database compatibility.
199-
See :ref:`database-config-timezone` for details.
200-
================ ===========================================================================================================
143+
=========================== =====================================================================================================
144+
Config Name Description
145+
=========================== =====================================================================================================
146+
**DSN** The DSN connect string (an all-in-one configuration sequence).
147+
**hostname** The hostname of your database server. Often this is 'localhost'.
148+
**username** The username used to connect to the database. (``SQLite3`` does not use this.)
149+
**password** The password used to connect to the database. (``SQLite3`` does not use this.)
150+
**database** The name of the database you want to connect to.
151+
152+
.. note:: CodeIgniter doesn't support dots (``.``) in the table and column names.
153+
Since v4.5.0, database names with dots are supported.
154+
**DBDriver** The database driver name. The case must match the driver name.
155+
You can set a fully qualified classname to use your custom driver.
156+
Supported drivers: ``MySQLi``, ``Postgre``, ``SQLite3``, ``SQLSRV``, and ``OCI8``.
157+
**DBPrefix** An optional table prefix which will be added to the table name when running
158+
:doc:`Query Builder <query_builder>` queries. This permits multiple CodeIgniter
159+
installations to share one database.
160+
**pConnect** true/false (boolean) - Whether to use a persistent connection.
161+
**DBDebug** true/false (boolean) - Whether to throw exceptions when database errors occur.
162+
**charset** The character set used in communicating with the database.
163+
**DBCollat** (``MySQLi`` only) The character collation used in communicating with the database.
164+
**swapPre** A default table prefix that should be swapped with ``DBPrefix``. This is useful for distributed
165+
applications where you might run manually written queries, and need the prefix to still be
166+
customizable by the end user.
167+
**schema** (``Postgre`` and ``SQLSRV`` only) The database schema, default value varies by driver.
168+
**encrypt** (``MySQLi`` and ``SQLSRV`` only) Whether to use an encrypted connection.
169+
See :ref:`MySQLi encrypt <mysqli-encrypt>` for ``MySQLi`` settings.
170+
``SQLSRV`` driver accepts true/false.
171+
**trustServerCertificate** (``SQLSRV`` only) true/false (boolean) - Whether to trust the server certificate
172+
without validating it against a trusted certificate authority.
173+
**compress** (``MySQLi`` only) Whether to use client compression.
174+
**strictOn** (``MySQLi`` only) true/false (boolean) - Whether to force "Strict Mode" connections, good for ensuring
175+
strict SQL while developing an application.
176+
**port** The database port number - Empty string ``''`` for default port (or dynamic port with ``SQLSRV``).
177+
**foreignKeys** (``SQLite3`` only) true/false (boolean) - Whether to enable Foreign Key constraint.
178+
179+
.. important:: SQLite3 Foreign Key constraint is disabled by default.
180+
See `SQLite documentation <https://www.sqlite.org/pragma.html#pragma_foreign_keys>`_.
181+
To enforce Foreign Key constraint, set this config item to true.
182+
**busyTimeout** (``SQLite3`` only) milliseconds (int) - Sleeps for a specified amount of time when a table is locked.
183+
**synchronous** (``SQLite3`` only) flag (int) - How strict SQLite will be at flushing to disk during transactions.
184+
Use `null` to stay with the default setting. This can be used since v4.6.0.
185+
**numberNative** (``MySQLi`` only) true/false (boolean) - Whether to enable MYSQLI_OPT_INT_AND_FLOAT_NATIVE.
186+
**foundRows** (``MySQLi`` only) true/false (boolean) - Whether to enable MYSQLI_CLIENT_FOUND_ROWS.
187+
**dateFormat** The default date/time formats as PHP's `DateTime format`_.
188+
* ``date`` - date format
189+
* ``datetime`` - date and time format
190+
* ``datetime-ms`` - date and time with millisecond format
191+
* ``datetime-us`` - date and time with microsecond format
192+
* ``time`` - time format
193+
This can be used since v4.5.0, and you can get the value, e.g., ``$db->dateFormat['datetime']``.
194+
Currently, the database drivers do not use these values directly,
195+
but :ref:`Model <model-saving-dates>` uses them.
196+
**timezone** (``MySQLi``, ``Postgre``, and ``OCI8`` only) The database session timezone.
197+
* ``false`` - Don't set session timezone (default, backward compatible)
198+
* ``true`` - Automatically sync with ``App::$appTimezone``
199+
* ``string`` - Specific timezone offset (e.g., ``'+05:30'``) or named timezone (e.g., ``'America/New_York'``)
200+
Named timezones are automatically converted to offsets for database compatibility.
201+
See :ref:`database-config-timezone` for details.
202+
=========================== =====================================================================================================
201203

202204
.. _DateTime format: https://www.php.net/manual/en/datetime.format.php
203205

0 commit comments

Comments
 (0)