lockr.module in Lockr 7.3
Same filename and directory in other branches
Hook implementations and callbacks for lockr.
File
lockr.moduleView source
<?php
/**
* @file
* Hook implementations and callbacks for lockr.
*/
use Lockr\Exception\LockrApiException;
use Lockr\Lockr;
use Lockr\LockrClient;
/**
* Include our autoloader.
*/
require_once __DIR__ . '/vendor/autoload.php';
/**
* Implements hook_ctools_plugin_directory().
*/
function lockr_ctools_plugin_directory($module, $plugin) {
if ($module === 'key') {
return "plugins/{$plugin}";
}
}
/**
* Implements hook_menu().
*/
function lockr_menu() {
$items['admin/config/system/lockr'] = [
'title' => 'Lockr',
'description' => 'Store keys securely off-site.',
'page callback' => 'lockr_admin_page',
'access arguments' => [
'administer keys',
],
'file' => 'lockr.admin.inc',
];
$items['admin/config/system/lockr/migrate'] = [
'title' => t('Migrate Legacy Keys'),
'page callback' => 'drupal_get_form',
'page arguments' => [
'lockr_migrate_keys_form',
],
'access arguments' => [
'administer keys',
],
'file' => 'lockr.forms.inc',
'type' => MENU_CALLBACK,
];
return $items;
}
/**
* Returns the detected partner, if available.
*/
function lockr_get_partner() {
if (defined('PANTHEON_BINDING')) {
$desc = <<<EOL
The Pantheor is strong with this one.
We're detecting you're on Pantheon and a friend of theirs is a friend of ours.
Welcome to Lockr.
EOL;
$cert_path = '/srv/bindings/' . PANTHEON_BINDING . '/certs/binding.pem';
if (!is_file($cert_path)) {
// Pantheon is rolling out a new file system layout for their
// containers.
// This is the new path where certs will live.
// XXX: When rollout is complete, the old path can be phased out.
$cert_path = '/certs/binding.pem';
}
return [
'name' => 'pantheon',
'title' => t('Pantheon'),
'description' => $desc,
'cert' => $cert_path,
];
}
return NULL;
}
/**
* Returns the Lockr client for this site.
*/
function lockr_client() {
$lockr =& drupal_static(__FUNCTION__);
if (!$lockr) {
$settings = lockr_settings();
$client = LockrClient::createFromSettings($settings);
$secret_info = lockr_secret_info();
$accounts_host = variable_get('lockr_accounts_host', 'accounts.lockr.io');
$lockr = new Lockr($client, $secret_info, $accounts_host);
}
return $lockr;
}
/**
* Returns the secret info instance.
*/
function lockr_secret_info() {
$secret_info =& drupal_static(__FUNCTION__);
if (is_null($secret_info)) {
$secret_info = new LockrDrupal7SecretInfo();
}
return $secret_info;
}
/**
* Returns the Lockr settings for this site.
*/
function lockr_settings() {
if (variable_get('lockr_custom')) {
$cert_path = drupal_realpath(variable_get('lockr_cert'));
}
else {
$partner = lockr_get_partner();
$cert_path = $partner ? $partner['cert'] : NULL;
}
$cls = variable_get('lockr_settings_class', '\\Lockr\\LockrSettings');
$host = variable_get('lockr_api_host');
return new $cls($cert_path, $host);
}
/**
* Gets a key from Lockr.
*
* @param string $key_name
* The key name.
*
* @return string | FALSE
* Returns the key value, or FALSE on failure.
*/
function _lockr_get_key($name) {
$lc = lockr_client();
try {
return $lc
->getSecretValue($name);
} catch (\Exception $e) {
watchdog_exception('lockr', $e);
watchdog('lockr', '<pre>' . $e
->getTraceAsString() . '</pre>');
return FALSE;
}
}
/**
* Sets a key value in lockr.
*
* @param string $name
* The key name.
* @param string $value
* The key value.
* @param string $label
* The key label.
*
* @return bool
* TRUE if they key set successfully, FALSE if not.
*/
function _lockr_set_key($name, $value, $label) {
$lc = lockr_client();
try {
$lc
->createSecretValue($name, $value, $label);
} catch (\Exception $e) {
watchdog_exception('lockr', $e);
return FALSE;
}
return TRUE;
}
/**
* Deletes a key from Lockr.
*
* @param string $key_name
* The key name.
*/
function _lockr_delete_key($key_name) {
$lc = lockr_client();
try {
$lc
->deleteSecretValue($key_name);
} catch (\Exception $e) {
watchdog_exception('lockr', $e);
return FALSE;
}
return TRUE;
}
/**
* Writes key files.
*
* @param string $dir
* @param array $texts
*/
function _lockr_write_key_files($dir, array $texts) {
@mkdir($dir, 0700, TRUE);
$key_file = "{$dir}/key.pem";
$key_fd = fopen($key_file, 'w');
fwrite($key_fd, $texts['key_text']);
fclose($key_fd);
chmod($key_file, 0600);
$cert_file = "{$dir}/crt.pem";
$cert_fd = fopen($cert_file, 'w');
fwrite($cert_fd, $texts['cert_text']);
fclose($cert_fd);
chmod($cert_file, 0600);
$pair_file = "{$dir}/pair.pem";
$pair_fd = fopen($pair_file, 'w');
fwrite($pair_fd, $texts['key_text']);
fwrite($pair_fd, $texts['cert_text']);
fclose($pair_fd);
chmod($pair_file, 0600);
}
Functions
Name | Description |
---|---|
lockr_client | Returns the Lockr client for this site. |
lockr_ctools_plugin_directory | Implements hook_ctools_plugin_directory(). |
lockr_get_partner | Returns the detected partner, if available. |
lockr_menu | Implements hook_menu(). |
lockr_secret_info | Returns the secret info instance. |
lockr_settings | Returns the Lockr settings for this site. |
_lockr_delete_key | Deletes a key from Lockr. |
_lockr_get_key | Gets a key from Lockr. |
_lockr_set_key | Sets a key value in lockr. |
_lockr_write_key_files | Writes key files. |