-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamodb_diff.txt
More file actions
313 lines (308 loc) · 8.78 KB
/
dynamodb_diff.txt
File metadata and controls
313 lines (308 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
diff -Naur simplesamlphp-1.11.0/config/config.php simplesamlphp-1.11.0-dynamodb/config/config.php
--- simplesamlphp-1.11.0/config/config.php 2013-06-05 07:44:23.000000000 -0400
+++ simplesamlphp-1.11.0-dynamodb/config/config.php 2013-12-26 15:08:48.000000000 -0500
@@ -533,13 +533,43 @@
* - 'phpsession': Limited datastore, which uses the PHP session.
* - 'memcache': Key-value datastore, based on memcache.
* - 'sql': SQL datastore, using PDO.
+ * - 'dynamodb': Key-value datastore using Amazon Web Service's DynamoDB.
*
* The default datastore is 'phpsession'.
*
* (This option replaces the old 'session.handler'-option.)
*/
- 'store.type' => 'phpsession',
+ 'store.type' => 'dynamodb',
+ /*
+ * The AWS_ACCESS_KEY_ID and AWS_SECRET_KEY for accessing DynamoDB.
+ * You can chose to omit these if your AWS EC2 instance is provisoned with an IAM Instance Role.
+ */
+ 'store.dynamodb.aws_key' => NULL,
+ 'store.dynamodb.aws_secret' => NULL,
+
+ /*
+ * The prefix for the table name.
+ */
+ 'store.dynamodb.prefix' => 'myapp_simpleSAMLphp',
+
+ /*
+ * The region in which the table should be created.
+ *
+ * See http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.Common.Enum.Region.html
+ * for list of available regions.
+ * Should be one of :us-east-1, ap-northeast-1, sa-east-1, ap-southeast-1, ap-southeast-2,
+ * us-west-2, us-gov-west-1, us-west-1, cn-north-1, eu-west-1.
+ */
+ 'store.dynamodb.region' => 'us-east-1',
+
+ /*
+ * The Read and Write capacity for the DynamoDB Table.
+ *
+ * See http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ProvisionedThroughputIntro.html
+ */
+ 'store.dynamodb.readCapacityUnits' => 5,
+ 'store.dynamodb.writeCapacityUnits' => 5,
/*
* The DSN the sql datastore should connect to.
diff -Naur simplesamlphp-1.11.0/lib/SimpleSAML/Store/DynamoDB.php simplesamlphp-1.11.0-dynamodb/lib/SimpleSAML/Store/DynamoDB.php
--- simplesamlphp-1.11.0/lib/SimpleSAML/Store/DynamoDB.php 1969-12-31 19:00:00.000000000 -0500
+++ simplesamlphp-1.11.0-dynamodb/lib/SimpleSAML/Store/DynamoDB.php 2013-12-26 15:08:48.000000000 -0500
@@ -0,0 +1,245 @@
+<?php
+
+/**
+ * A DynamoDB datastore.
+ *
+ * @package simpleSAMLphp
+ * @version $Id$
+ */
+
+require_once 'AWSSDKforPHP/aws.phar';
+
+use Aws\DynamoDb\DynamoDbClient;
+
+class SimpleSAML_Store_DynamoDB extends SimpleSAML_Store {
+
+ /**
+ * The client object for our database.
+ *
+ * @var client
+ */
+ public $client;
+
+ /**
+ * The prefix we should use for our tables.
+ *
+ * @var string
+ */
+ public $prefix;
+
+
+ /**
+ * Initialize the SQL datastore.
+ */
+ protected function __construct() {
+ SimpleSAML_Logger::debug('SimpleSAML_Store_DynamoDB::__construct()');
+
+ $config = SimpleSAML_Configuration::getInstance();
+
+ $this->prefix = $config->getString('store.dynamodb.prefix', 'simpleSAMLphp');
+
+ $key = $config->getString('store.dynamodb.aws_key', NULL);
+ $secret = $config->getString('store.dynamodb.aws_secret', NULL);
+
+ if ($key && $secret) {
+ //If the AWS KEY ID and SECRET are provided, authenticate using them
+ $this->client = DynamoDbClient::factory(array(
+ 'key' => $key,
+ 'secret' => $secret,
+ 'region' => $config->getString('store.dynamodb.region','us-east-1')
+ ));
+
+ } else {
+ // Otherwise, Hope that the Instance IAM Profile is configured
+ $this->client = DynamoDbClient::factory(array(
+ 'region' => $config->getString('store.dynamodb.region','us-east-1')
+ ));
+ }
+
+ $this->initKVTable();
+ }
+
+ /**
+ * Initialize key-value table.
+ */
+ private function initKVTable() {
+ SimpleSAML_Logger::debug('SimpleSAML_Store_DynamoDB::initKVTable()');
+
+ $config = SimpleSAML_Configuration::getInstance();
+
+ $result = $this->client->listTables();
+
+ // TableNames contains an array of table names
+ foreach ($result['TableNames'] as $tableName) {
+ if ($tableName == $this->prefix."_kvstore") {
+ return;
+ }
+ }
+
+ // Create the table
+ $this->client->createTable(array(
+ 'TableName' => $this->prefix."_kvstore",
+ 'AttributeDefinitions' => array(
+ array(
+ 'AttributeName' => 'id',
+ 'AttributeType' => 'S'
+ )
+ ),
+ 'KeySchema' => array(
+ array(
+ 'AttributeName' => 'id',
+ 'KeyType' => 'HASH'
+ )
+ ),
+ 'ProvisionedThroughput' => array(
+ 'ReadCapacityUnits' => intval($config->getString('store.dynamodb.readCapacityUnits',5)),
+ 'WriteCapacityUnits' => intval($config->getString('store.dynamodb.writeCapacityUnits',5))
+ )
+ ));
+
+ $this->client->waitUntilTableExists(array(
+ 'TableName' => $this->prefix."_kvstore" ));
+ }
+
+ /**
+ * Clean the key-value table of expired entries.
+ */
+ private function cleanKVStore() {
+
+ SimpleSAML_Logger::debug('SimpleSAML_Store_DynamoDB::cleanKVStore()');
+
+ $iterator = $this->client->getIterator('Scan', array(
+ 'TableName' => $this->prefix."_kvstore",
+ 'ScanFilter' => array(
+ 'expire' => array(
+ 'AttributeValueList' => array(
+ array('N' => time())
+ ),
+ 'ComparisonOperator' => 'LT'
+ )
+ )
+ ));
+
+ // Each item will contain the attributes we added
+ foreach ($iterator as $item) {
+ if ($item['expire']['N'] > 0) {
+ $this->client->deleteItem(array(
+ 'TableName' => $this->prefix."_kvstore",
+ 'Key' => array(
+ 'id' => array('S' => $item['id']['S'])
+ )
+ ));
+ }
+ }
+ }
+
+ /**
+ * Retrieve a value from the datastore.
+ *
+ * @param string $type The datatype.
+ * @param string $key The key.
+ * @return mixed|NULL The value.
+ */
+ public function get($type, $key) {
+ assert('is_string($type)');
+ assert('is_string($key)');
+
+ if (strlen($key) > 50) {
+ $key = sha1($key);
+ }
+
+ $iterator = $this->client->getIterator('Query', array(
+ 'TableName' => $this->prefix."_kvstore",
+ 'KeyConditions' => array(
+ 'id' => array(
+ 'AttributeValueList' => array(
+ array('S' => 'simpleSAMLphp.' . $type . '.' . $key)
+ ),
+ 'ComparisonOperator' => 'EQ'
+ ),
+ )
+ ));
+
+ if ($iterator == NULL)
+ return NULL;
+
+ foreach ($iterator as $item) {
+
+ if ( ($item['expire']['N'] == 0) || ($item['expire']['N'] > time()) ) {
+ $value = $item['value']['S'];
+
+ if (is_resource($value)) {
+ $value = stream_get_contents($value);
+ }
+ $value = urldecode($value);
+ $value = unserialize($value);
+
+ if ($value === FALSE) {
+ return NULL;
+ }
+ return $value;
+ }
+ }
+ }
+
+ /**
+ * Save a value to the datastore.
+ *
+ * @param string $type The datatype.
+ * @param string $key The key.
+ * @param mixed $value The value.
+ * @param int|NULL $expire The expiration time (unix timestamp), or NULL if it never expires (1 month).
+ */
+ public function set($type, $key, $value, $expire = NULL) {
+ assert('is_string($type)');
+ assert('is_string($key)');
+ assert('is_null($expire) || (is_int($expire) && $expire > 2592000)');
+
+ if (rand(0, 1000) < 10) {
+ $this->cleanKVStore();
+ }
+
+ if (strlen($key) > 50) {
+ $key = sha1($key);
+ }
+ if ($expire == NULL)
+ $expire = 0;
+
+ $value = serialize($value);
+ $value = rawurlencode($value);
+
+ $result = $this->client->putItem(array(
+ 'TableName' => $this->prefix."_kvstore",
+ 'Item' => $this->client->formatAttributes(array(
+ 'id' => 'simpleSAMLphp.' . $type . '.' . $key,
+ 'expire' => $expire,
+ 'value' => $value
+ )),
+ 'ReturnConsumedCapacity' => 'TOTAL'
+ ));
+
+ }
+
+ /**
+ * Delete a value from the datastore.
+ *
+ * @param string $type The datatype.
+ * @param string $key The key.
+ */
+ public function delete($type, $key) {
+ assert('is_string($type)');
+ assert('is_string($key)');
+
+ if (strlen($key) > 50) {
+ $key = sha1($key);
+ }
+
+ $this->client->deleteItem(array(
+ 'TableName' => $this->prefix."_kvstore",
+ 'Key' => array(
+ 'id' => array('S' => 'simpleSAMLphp.' . $type . '.' . $key)
+ )
+ ));
+ }
+
+}
diff -Naur simplesamlphp-1.11.0/lib/SimpleSAML/Store.php simplesamlphp-1.11.0-dynamodb/lib/SimpleSAML/Store.php
--- simplesamlphp-1.11.0/lib/SimpleSAML/Store.php 2010-11-03 09:23:56.000000000 -0400
+++ simplesamlphp-1.11.0-dynamodb/lib/SimpleSAML/Store.php 2013-12-26 15:08:48.000000000 -0500
@@ -47,6 +47,9 @@
case 'sql':
self::$instance = new SimpleSAML_Store_SQL();
break;
+ case 'dynamodb':
+ self::$instance = new SimpleSAML_Store_DynamoDB();
+ break;
default:
if (strpos($storeType, ':') === FALSE) {
throw new SimpleSAML_Error_Exception('Unknown datastore type: ' . var_export($storeType, TRUE));