Skip to content

Instantly share code, notes, and snippets.

@malc0mn
Last active December 17, 2021 09:37
Show Gist options
  • Save malc0mn/b0cddec5525b6590dd3f6d7020c27e03 to your computer and use it in GitHub Desktop.
Save malc0mn/b0cddec5525b6590dd3f6d7020c27e03 to your computer and use it in GitHub Desktop.
Quick and dirty, but mostly convenient, Apache Solr password hash generator in PHP (5.4+).
<?php
/**
* Function to generate a salted password hash to be used in a 'security.json'
* file to secure Apache Solr.
* This is the core function that you can extract for use in your own code.
*
* @see https://lucene.apache.org/solr/guide/basic-authentication-plugin.html#enable-basic-authentication
*
* @param string $pw The password to hash
*
* @return array [$hashedPass, $encodedSalt]
*/
function generateSolrPassHash($pw) {
$method = 'sha256';
$salt = openssl_random_pseudo_bytes(32);
$hashedPass = openssl_digest($salt . $pw, $method, true);
$hashedPass = base64_encode(openssl_digest($hashedPass, $method, true));
$encodedSalt = base64_encode($salt);
return [$hashedPass, $encodedSalt];
}
/**
* Helper to generate JSON output.
*
* @param string $user The username
* @param string $pass Hashed password
* @param string $salt Hashed salt
*
* @return string
*/
function generateJson($user, $pass, $salt) {
$output = <<<'JSON'
{
"authentication":{
"blockUnknown": true,
"class":"solr.BasicAuthPlugin",
"credentials":{"[user]":"[password]"}
},
"authorization":{
"class":"solr.RuleBasedAuthorizationPlugin",
"permissions":[
{
"name":"security-edit",
"role":"admin"
}
],
"user-role":{"[user]":"admin"}
}
}
JSON;
return str_replace(
['[user]', '[password]'],
[$user, "$pass $salt"],
$output
);
}
/**
* Helper function to check the PHP version running on this system.
*
* @param string $scriptName
*/
function versionCheck($scriptName) {
// PHP_VERSION_ID is available as of PHP 5.2.7, if our version is lower than
// that, then emulate it.
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
if (PHP_VERSION_ID < 50400) {
printf(PHP_EOL . "\033[31m" . 'You need at least PHP version 5.4 to use %s!' . "\033[0m" . PHP_EOL . PHP_EOL, $scriptName);
die();
}
}
/**
* Helper to cleanup argv for easy usage.
*
* @param array $argv
*
* @return array
*/
function processArgv($argv) {
return array_values(array_filter($argv, function($val) {
return stripos($val, '-') !== 0;
}));
}
/**
* Main program logic.
*/
function main($argv) {
$argv = processArgv($argv);
echo 'Quick and dirty, but mostly convenient, Apache Solr password hash generator in PHP.' . PHP_EOL;
versionCheck($argv[0]);
if (count($argv) < 2) {
echo "Usage:" . PHP_EOL;
printf(PHP_EOL . "\033[31m" . 'php %s "password to encode"' . "\033[0m" . PHP_EOL . PHP_EOL, $argv[0]);
echo " or to generate the full JSON file:" . PHP_EOL;
printf(PHP_EOL . "\033[31m" . 'php %s -j "password to encode" "user"' . "\033[0m" . PHP_EOL . PHP_EOL, $argv[0]);
die();
}
list($pass, $salt) = generateSolrPassHash($argv[1]);
$output = sprintf(
PHP_EOL . "Your password ('%s') in hashed form to be placed in the 'security.json' file is:" . PHP_EOL,
$argv[1]
);
$output .= " \033[32m$pass $salt\033[0m";
$options = getopt('j', ['json']);
if (
isset($argv[2]) && (
array_key_exists('j', $options) ||
array_key_exists('json', $options)
)
) {
$output = PHP_EOL . "The contents of your 'security.json' file is:" . PHP_EOL;
$output .= "\033[32m" . generateJson($argv[2], $pass, $salt) . "\033[0m";
}
echo $output . PHP_EOL . PHP_EOL;
}
main($argv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment