Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shuffle-salts command #62

Merged
merged 12 commits into from Jul 26, 2018
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -352,6 +352,16 @@ wp config set <name> <value> [--add] [--raw] [--anchor=<anchor>] [--placement=<p
# Set the WP_DEBUG constant to true.
$ wp config set WP_DEBUG true --raw



### wp config shuffle-salts

Refreshes the salts in the wp-config.php file

~~~
wp config shuffle-salts
~~~

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you manually changed the README.md file. This should be generated out of the source code docblock instead. The automatic regeneration is not yet included here so yu need to do this manually for now.

You can use the following to regenerate the README.md file:

  • Make sure the required package is installed (this one is not bundled)
    wp package install wp-cli/scaffold-package-command
    
  • Force a regeneration the README.md file
    wp scaffold package-readme . --force
    

## Installing

This package is included with WP-CLI itself, no additional installation necessary.
Expand Down
54 changes: 54 additions & 0 deletions features/config-shuffle-salts.feature
@@ -0,0 +1,54 @@
Feature: Refresh the salts in the wp-config.php file

Scenario: Salts are created properly when none initially exist
Given a WP install
When I try `wp config get AUTH_KEY --type=constant`
Then STDERR should contain:
"""
The constant 'AUTH_KEY' is not defined in the 'wp-config.php' file.
"""

When I run `wp config shuffle-salts`
Then STDOUT should contain:
"""
Shuffled the salt keys.
"""
And the wp-config.php file should contain:
"""
define( 'AUTH_KEY'
"""

Scenario: Shuffle the salts
Given a WP install
When I run `wp config shuffle-salts`
Then STDOUT should contain:
"""
Shuffled the salt keys.
"""
And the wp-config.php file should contain:
"""
define( 'AUTH_KEY'
"""
And the wp-config.php file should contain:
"""
define( 'LOGGED_IN_SALT'
"""

When I run `wp config get AUTH_KEY --type=constant`
Then save STDOUT as {AUTH_KEY_ORIG}
When I run `wp config get LOGGED_IN_SALT --type=constant`
Then save STDOUT as {LOGGED_IN_SALT_ORIG}

When I run `wp config shuffle-salts`
Then STDOUT should contain:
"""
Shuffled the salt keys.
"""
And the wp-config.php file should not contain:
"""
{AUTH_KEY_ORIG}
"""
And the wp-config.php file should not contain:
"""
{LOGGED_IN_SALT_ORIG}
"""
59 changes: 59 additions & 0 deletions src/Config_Command.php
Expand Up @@ -619,6 +619,65 @@ public function has( $args, $assoc_args ) {
}
}

/**
* Refreshes the salts defined in the wp-config.php file
*
* ## OPTIONS
*
* ## EXAMPLES
*
* # Get new salts for your wp-config.php file
* $ wp config shuffle-salts
*
* @subcommand shuffle-salts
* @when before_wp_load
*/
public function shuffle_salts( $args, $assoc_args ) {

$constant_list = array(
'AUTH_KEY',
'SECURE_AUTH_KEY',
'LOGGED_IN_KEY',
'NONCE_KEY',
'AUTH_SALT',
'SECURE_AUTH_SALT',
'LOGGED_IN_SALT',
'NONCE_SALT'
);

try {
foreach ( $constant_list as $key ) {
$secret_keys[ $key ] = trim( self::unique_key() );
}
throw new Exception( 'TEST' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's some debugging left-overs here.

} catch ( Exception $ex ) {

$remote_salts = self::_read( 'https://api.wordpress.org/secret-key/1.1/salt/' );
$remote_salts = explode( "\n", $remote_salts );
foreach ( $remote_salts as $k => $salt ) {
if ( ! empty( $salt ) ) {
$key = $constant_list[ $k ];
$secret_keys[ $key ] = trim( substr( $salt, 28, 64 ) );
}
}

}

$path = $this->get_config_path();

try {
$config_transformer = new WPConfigTransformer( $path );
foreach ( $secret_keys as $constant => $key ) {
$config_transformer->update( 'constant', $constant, (string) $key );
}
} catch ( Exception $exception ) {
WP_CLI::error( "Could not process the 'wp-config.php' transformation.\nReason: " . $exception->getMessage() );
}

WP_CLI::success( 'Shuffled the salt keys.' );

}

/**
* Filters wp-config.php file configurations.
*
Expand Down