Skip to content

Instantly share code, notes, and snippets.

@faust64
Last active August 21, 2018 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faust64/c5f8e73aefe442d100f229fa4f62c9ae to your computer and use it in GitHub Desktop.
Save faust64/c5f8e73aefe442d100f229fa4f62c9ae to your computer and use it in GitHub Desktop.
Patching MediaWiki-1.30.0 introducing Azure's Postgres PAAS support
diff -urNi /usr/src/mediawiki/includes/db/MWLBFactory.php /usr/src/mediawiki/includes/db/MWLBFactory.php
--- /usr/src/mediawiki/includes/db/MWLBFactory.php 2017-12-09 00:19:51.000000000 +0100
+++ /usr/src/mediawiki/includes/db/MWLBFactory.php 2018-08-07 17:20:57.672257718 +0200
@@ -100,7 +100,7 @@
} else {
$flags = DBO_DEFAULT;
$flags |= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG : 0;
- $flags |= $mainConfig->get( 'DBssl' ) ? DBO_SSL : 0;
+ $flags |= ($mainConfig->get( 'DBssl' ) || getenv('PGSSL')) ? DBO_SSL : 0;
$flags |= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
$server = [
'host' => $mainConfig->get( 'DBserver' ),
diff -urNi /usr/src/mediawiki/includes/installer/PostgresInstaller.php /usr/src/mediawiki/includes/installer/PostgresInstaller.php
--- /usr/src/mediawiki/includes/installer/PostgresInstaller.php 2017-12-09 00:19:51.000000000 +0100
+++ /usr/src/mediawiki/includes/installer/PostgresInstaller.php 2018-08-08 15:30:17.536003796 +0200
@@ -245,7 +245,7 @@
* @var $conn Database
*/
$conn = $status->value;
- $safeRole = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
+ $safeRole = $conn->addIdentifierQuotes(preg_replace('/@.*/', '', $this->getVar( 'wgDBuser' )));
$conn->query( "SET ROLE $safeRole" );
}
@@ -545,19 +545,21 @@
$safeuser = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
$safepass = $conn->addQuotes( $this->getVar( 'wgDBpassword' ) );
+ $saferole = preg_replace('/@.*/', '', $this->getVar( 'wgDBuser' ) );
// Check if the user already exists
- $userExists = $conn->roleExists( $this->getVar( 'wgDBuser' ) );
+ $userExists = $conn->roleExists( $saferole );
if ( !$userExists ) {
// Create the user
try {
- $sql = "CREATE ROLE $safeuser NOCREATEDB LOGIN PASSWORD $safepass";
+ $sql = "CREATE ROLE $saferole NOCREATEDB LOGIN PASSWORD $safepass";
// If the install user is not a superuser, we need to make the install
// user a member of the new user's group, so that the install user will
// be able to create a schema and other objects on behalf of the new user.
if ( !$this->isSuperUser() ) {
- $sql .= ' ROLE' . $conn->addIdentifierQuotes( $this->getVar( '_InstallUser' ) );
+ $otherrole = preg_replace('/@.*/', '', $this->getVar( '_InstallUser' ) );
+ $sql .= ' ROLE' . $conn->addIdentifierQuotes( $otherrole );
}
$conn->query( $sql, __METHOD__ );
diff -urNi /usr/src/mediawiki/includes/libs/rdbms/database/DatabasePostgres.php /usr/src/mediawiki/includes/libs/rdbms/database/DatabasePostgres.php
--- /usr/src/mediawiki/includes/libs/rdbms/database/DatabasePostgres.php 2017-12-09 00:19:51.000000000 +0100
+++ /usr/src/mediawiki/includes/libs/rdbms/database/DatabasePostgres.php 2018-08-08 11:36:21.263730475 +0200
@@ -116,8 +116,8 @@
if ( (int)$this->port > 0 ) {
$connectVars['port'] = (int)$this->port;
}
- if ( $this->mFlags & self::DBO_SSL ) {
- $connectVars['sslmode'] = 1;
+ if (( $this->mFlags & self::DBO_SSL ) || getenv('PGSSL')) {
+ $connectVars['sslmode'] = "require";
}
$this->connectString = $this->makeConnectionString( $connectVars );
@faust64
Copy link
Author

faust64 commented Aug 9, 2018

In practice:

  • introduces PGSSL env var allowing to enable postgres sslmode during install (otherwise relies on prior-existing configuration, could use some refactoring using some argument to properly initialize $this->mFlags, instead of some environment variable)
  • fixes Postgres SSL support: 1 isn't recognized as a valid value, error message suggested setting it to require, which works
  • fixes ROLES related queries. Azures' Postgres service usernames involve a domain part (eg: pguser@pgdomain). MediaWiki installer failed to init database, until I tried and removed the @domain part from our database username.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment