Skip to content

Commit 65144f4

Browse files
authored
Merge pull request #185 from PRX/fix/config-decode-secrets-json
HOTFIX: config: parse secrets.json into env vars
2 parents 7eed6d2 + 4a4c9d3 commit 65144f4

File tree

3 files changed

+68
-49
lines changed

3 files changed

+68
-49
lines changed

wp-config-constants.php

-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,3 @@
1515
* Name of the environment variable that acts as a flag for the platform server.
1616
*/
1717
define( 'SERVER_PLATFORM_ENVIRONMENT_VARIABLE_NAME', 'PANTHEON_ENVIRONMENT' );
18-
19-
/**
20-
* Wire up S3 Uploads key and secret values to ENV variable.
21-
*/
22-
define( 'S3_UPLOADS_KEY', getenv( 'S3_KEY' ) );
23-
define( 'S3_UPLOADS_SECRET', getenv( 'S3_SECRET' ) );

wp-config-pantheon.php

+61-43
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212

1313
// ** MySQL settings - included in the Pantheon Environment ** //
1414
/** The name of the database for WordPress */
15-
define('DB_NAME', $_ENV['DB_NAME']);
15+
define( 'DB_NAME', $_ENV['DB_NAME'] );
1616

1717
/** MySQL database username */
18-
define('DB_USER', $_ENV['DB_USER']);
18+
define( 'DB_USER', $_ENV['DB_USER'] );
1919

2020
/** MySQL database password */
21-
define('DB_PASSWORD', $_ENV['DB_PASSWORD']);
21+
define( 'DB_PASSWORD', $_ENV['DB_PASSWORD'] );
2222

2323
/** MySQL hostname; on Pantheon this includes a specific port number. */
24-
define('DB_HOST', $_ENV['DB_HOST'] . ':' . $_ENV['DB_PORT']);
24+
define( 'DB_HOST', $_ENV['DB_HOST'] . ':' . $_ENV['DB_PORT'] );
2525

2626
/** Database Charset to use in creating database tables. */
27-
define('DB_CHARSET', 'utf8mb4');
27+
define( 'DB_CHARSET', 'utf8mb4' );
2828

2929
/** The Database Collate type. Don't change this if in doubt. */
30-
define('DB_COLLATE', '');
30+
define( 'DB_COLLATE', '' );
3131

3232
/**#@+
3333
* Authentication Unique Keys and Salts.
@@ -39,53 +39,71 @@
3939
*
4040
* @since 2.6.0
4141
*/
42-
define('AUTH_KEY', $_ENV['AUTH_KEY']);
43-
define('SECURE_AUTH_KEY', $_ENV['SECURE_AUTH_KEY']);
44-
define('LOGGED_IN_KEY', $_ENV['LOGGED_IN_KEY']);
45-
define('NONCE_KEY', $_ENV['NONCE_KEY']);
46-
define('AUTH_SALT', $_ENV['AUTH_SALT']);
47-
define('SECURE_AUTH_SALT', $_ENV['SECURE_AUTH_SALT']);
48-
define('LOGGED_IN_SALT', $_ENV['LOGGED_IN_SALT']);
49-
define('NONCE_SALT', $_ENV['NONCE_SALT']);
42+
define( 'AUTH_KEY', $_ENV['AUTH_KEY'] );
43+
define( 'SECURE_AUTH_KEY', $_ENV['SECURE_AUTH_KEY'] );
44+
define( 'LOGGED_IN_KEY', $_ENV['LOGGED_IN_KEY'] );
45+
define( 'NONCE_KEY', $_ENV['NONCE_KEY'] );
46+
define( 'AUTH_SALT', $_ENV['AUTH_SALT'] );
47+
define( 'SECURE_AUTH_SALT', $_ENV['SECURE_AUTH_SALT'] );
48+
define( 'LOGGED_IN_SALT', $_ENV['LOGGED_IN_SALT'] );
49+
define( 'NONCE_SALT', $_ENV['NONCE_SALT'] );
5050
/**#@-*/
5151

52-
/** A couple extra tweaks to help things run well on Pantheon. **/
53-
if (isset($_SERVER['HTTP_HOST'])) {
54-
// HTTP is still the default scheme for now.
55-
$scheme = 'http';
56-
// If we have detected that the end use is HTTPS, make sure we pass that
57-
// through here, so <img> tags and the like don't generate mixed-mode
58-
// content warnings.
59-
if (isset($_SERVER['HTTP_USER_AGENT_HTTPS']) && $_SERVER['HTTP_USER_AGENT_HTTPS'] == 'ON') {
60-
$scheme = 'https';
61-
$_SERVER['HTTPS'] = 'on';
62-
}
63-
define('WP_HOME', $scheme . '://' . $_SERVER['HTTP_HOST']);
64-
define('WP_SITEURL', $scheme . '://' . $_SERVER['HTTP_HOST']);
52+
/** A couple extra tweaks to help things run well on Pantheon. */
53+
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
54+
// HTTP is still the default scheme for now.
55+
$scheme = 'http';
56+
// If we have detected that the end use is HTTPS, make sure we pass that
57+
// through here, so <img> tags and the like don't generate mixed-mode
58+
// content warnings.
59+
if ( isset( $_SERVER['HTTP_USER_AGENT_HTTPS'] ) && $_SERVER['HTTP_USER_AGENT_HTTPS'] == 'ON' ) {
60+
$scheme = 'https';
61+
$_SERVER['HTTPS'] = 'on';
62+
}
63+
define( 'WP_HOME', $scheme . '://' . $_SERVER['HTTP_HOST'] );
64+
define( 'WP_SITEURL', $scheme . '://' . $_SERVER['HTTP_HOST'] );
6565
}
6666
// Don't show deprecations; useful under PHP 5.5
67-
error_reporting(E_ALL ^ E_DEPRECATED);
67+
error_reporting( E_ALL ^ E_DEPRECATED );
6868
/** Define appropriate location for default tmp directory on Pantheon */
69-
define('WP_TEMP_DIR', sys_get_temp_dir());
69+
define( 'WP_TEMP_DIR', sys_get_temp_dir() );
7070

7171
// FS writes aren't permitted in test or live, so we should let WordPress know to disable relevant UI
72-
if (in_array($_ENV['PANTHEON_ENVIRONMENT'], array( 'test', 'live' )) && ! defined('DISALLOW_FILE_MODS')) {
73-
define('DISALLOW_FILE_MODS', true);
72+
if ( in_array( $_ENV['PANTHEON_ENVIRONMENT'], array( 'test', 'live' ) ) && ! defined( 'DISALLOW_FILE_MODS' ) ) {
73+
define( 'DISALLOW_FILE_MODS', true );
7474
}
7575

7676
/**
7777
* Set WP_ENVIRONMENT_TYPE according to the Pantheon Environment
7878
*/
79-
if (getenv('WP_ENVIRONMENT_TYPE') === false) {
80-
switch ($_ENV['PANTHEON_ENVIRONMENT']) {
81-
case 'live':
82-
putenv('WP_ENVIRONMENT_TYPE=production');
83-
break;
84-
case 'test':
85-
putenv('WP_ENVIRONMENT_TYPE=staging');
86-
break;
87-
default:
88-
putenv('WP_ENVIRONMENT_TYPE=development');
89-
break;
90-
}
79+
if ( getenv( 'WP_ENVIRONMENT_TYPE' ) === false ) {
80+
switch ( $_ENV['PANTHEON_ENVIRONMENT'] ) {
81+
case 'live':
82+
putenv( 'WP_ENVIRONMENT_TYPE=production' );
83+
break;
84+
case 'test':
85+
putenv( 'WP_ENVIRONMENT_TYPE=staging' );
86+
break;
87+
default:
88+
putenv( 'WP_ENVIRONMENT_TYPE=development' );
89+
break;
90+
}
91+
}
92+
93+
/**
94+
* Decode secrets.json and convert key/value pairs into environment variables.
95+
*/
96+
if ( file_exists( __DIR__ . '/wp-content/uploads/private/secrets.json' ) && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {
97+
// Decode as associative array...
98+
$json = json_decode( file_get_contents( __DIR__ . '/wp-content/uploads/private/secrets.json' ), true );
99+
100+
if ( ! empty( $json ) && ! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {
101+
// Loop over key/value pairs...
102+
foreach ( $json as $key => $value ) {
103+
// Define environment variable if one doesn't already exists...
104+
if ( ! isset( $_ENV[ $key ] ) ) {
105+
putenv( "{$key}={$value}" );
106+
}
107+
}
108+
}
91109
}

wp-config.php

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
require_once __DIR__ . '/wp-config-' . SERVER_PLATFORM_NAME . '.php';
1717
}
1818

19+
/**
20+
* Wire up S3 Uploads key and secret values to ENV variable.
21+
* Needs to be assigned AFTER platform config.
22+
*/
23+
define( 'S3_UPLOADS_KEY', getenv( 'S3_KEY' ) );
24+
define( 'S3_UPLOADS_SECRET', getenv( 'S3_SECRET' ) );
25+
1926

2027
/** Standard wp-config.php stuff from here on down. */
2128

0 commit comments

Comments
 (0)