On Thu, Jun 28, 2012 at 11:32:34AM +0200, Olav Morken wrote:
>
> Either that, or move it into a separate config file, like we have for
> many other modules. E.g.: module_riak.php
i prefer as much config in one place as possible, so i kept them
in config.php.
> I'd write it as a form of step-by-step instruction for configuring SSP
> to use Riak for session store. E.g. enable the module, change
> configuration, download Riak PHP library, configure Riak, etc.
i gave it a shot, and reworked the code as a proper module to support
the cron hook.
the 'riak:Store' bit is working fine instead of the 'riak' option
from my first diff. i have ssp running on two separate boxes as the
same SP by sharing the same riak cluster and bucket. a load balancer
in front of them simply roundrobins requests across them. one of
the servers is still running with the code from my original diff,
and the other with the code below.
dlg
Index: docs/simplesamlphp-riak.php
===================================================================
--- docs/simplesamlphp-riak.php (revision 0)
+++ docs/simplesamlphp-riak.php (working copy)
@@ -0,0 +1,115 @@
+Riak Store module
+=================
+
+<!--
+ This file is written in Markdown syntax.
+ For more information about how to use the Markdown syntax, read here:
+
http://daringfireball.net/projects/markdown/syntax
+-->
+
+ * Version: `$Id$'
+
+<!-- {{TOC}} -->
+
+Introduction
+------------
+
+The riak module implements a Store that can be used as a backend
+for simpleSAMLphp session data like the phpsession, sql, or memcache
+backends.
+
+Preparations
+------------
+
+The obvious first step for using Riak as a backend is to install
+and configure a Riak cluster for SimpleSAMLphp to use. Please refer
+to the Riak documentation for this.
+
+This module requires the use of a Riak backend that supports secondary
+indexes. Refer to the Riak documentation on how to enable an
+appropriate backend for use by this module. Currently the only
+storage backend that supports secondary indexes is leveldb.
+
+Next, you will need to install the Riak PHP Client library, available
+from
https://github.com/basho/riak-php-client.
+
+Finally, you need to config SimpleSAMLphp to for the riak Store by
+enabling the following modules:
+
+ 1. cron
+ 2. riak
+
+The cron module allows you to do tasks regularly by setting up a
+cronjob that calls hooks in simpleSAMLphp. This is required by the
+riak module to remove expired entries in the store.
+
+Enabling the riak module allows it to be loaded and used as a storage
+backend.
+
+You also need to copy the `config-templates` files from the cron
+module above into the global `config/` directory.
+
+ $ cd /var/simplesamlphp
+ $ touch modules/cron/enable
+ $ cp modules/cron/config-templates/*.php config/
+ $ touch modules/riak/enable
+
+
+Configuring the cron module
+---------------------------
+
+At `/var/simplesamlphp/config`
+
+ $ vi module_cron.php
+
+edit:
+
+ $config = array (
+ 'key' => 'secret',
+ 'allowed_tags' => array('daily', 'hourly', 'frequent'),
+ 'debug_message' => TRUE,
+ 'sendemail' => TRUE,
+ );
+
+Then: With your browser go to =>
https://simplesamlphp_machine/simplesaml/module.php/cron/croninfo.php
+
+And copy the cron's sugestion:
+
+ -------------------------------------------------------------------------------------------------------------------
+ Cron is a way to run things regularly on unix systems.
+
+ Here is a suggestion for a crontab file:
+
+ # Run cron [daily]
+ 02 0 * * * curl --silent "
https://simplesamlphp_machine/simplesaml/module.php/cron/cron.php?key=secret&tag=daily" > /dev/null 2>&1
+ # Run cron [hourly]
+ 01 * * * * curl --silent "
https://simplesamlphp_machine/simplesaml/module.php/cron/cron.php?key=secret&tag=hourly" > /dev/null 2>&1
+ # Run cron [frequent]
+ XXXXXXXXXX curl --silent "
https://simplesamlphp_machine/simplesaml/module.php/cron/cron.php?key=secret&tag=frequent" > /dev/null 2>&1
+ Click here to run the cron jobs:
+
+ Run cron [daily]
+ Run cron [hourly]
+ Run cron [frequent]
+ -------------------------------------------------------------------------------------------------------------------
+
+Add to CRON with
+
+ # crontab -e
+
+Configuring the riak module
+---------------------------
+
+The riak module uses the following configuration options specified
+in the main `config/config.php`. The defaults are listed:
+
+ 'store.riak.path' => 'riak-php-client/riak.php',
+ 'store.riak.host' => 'localhost',
+ 'store.riak.port' => 8098,
+ 'store.riak.bucket' => 'SimpleSAMLphp',
+
+Finally, the module can be specified as the Store in `config/config.php`
+with the following setting:
+
+ 'store.type' => 'riak:Store',
+
Index: modules/riak/hooks/hook_cron.php
===================================================================
--- modules/riak/hooks/hook_cron.php (revision 0)
+++ modules/riak/hooks/hook_cron.php (working copy)
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Hook to run a cron job.
+ *
+ * @param array &$croninfo Output
+ */
+function riak_hook_cron(&$croninfo) {
+ assert('is_array($croninfo)');
+ assert('array_key_exists("summary", $croninfo)');
+ assert('array_key_exists("tag", $croninfo)');
+
+ if ($croninfo['tag'] !== 'hourly') return;
+
+ try {
+ $store = new sspmod_riak_Store_Store();
+ $result = $store->$bucket->indexSearch('expires', 'int',
+ 1, time() - 30);
+ foreach ($result as $link) {
+ $link->getBinary()->delete();
+ }
+
+ SimpleSAML_Logger::info(sprintf("deleted %s riak key%s",
+ sizeof($result), sizeof($result) == 1 ? '' : 's'));
+ } catch (Exception $e) {
+ $message = 'riak threw exception: ' . $e->getMessage();
+ SimpleSAML_Logger::warning($message);
+ $croninfo['summary'][] = $message;
+ }
+}
+?>
Index: modules/riak/lib/Store/Store.php
===================================================================
--- modules/riak/lib/Store/Store.php (revision 0)
+++ modules/riak/lib/Store/Store.php (working copy)
@@ -0,0 +1,79 @@
+<?php class sspmod_riak_Store_Store extends SimpleSAML_Store {
@@ -500,6 +500,7 @@
* - 'phpsession': Limited datastore, which uses the PHP session.
* - 'memcache': Key-value datastore, based on memcache.
* - 'sql': SQL datastore, using PDO.
+ * - 'riak:Store': Key-value datastore, uses riak-php-client
*
* The default datastore is 'phpsession'.
*
@@ -603,7 +604,16 @@
*/
'memcache_store.expires' => 36 * (60*60), // 36 hours.
+ /*
+ * The riak:Store module has the following config options and defaults.
+ *
+ * 'store.riak.path' => 'riak-php-client/riak.php',
+ * 'store.riak.host' => 'localhost',
+ * 'store.riak.port' => 8098,
+ * 'store.riak.bucket' => 'SimpleSAMLphp',
+ */
+
/*
* Should signing of generated metadata be enabled by default.
*