Skip to content

Commit e73d115

Browse files
authored
Add IAM authentication support for ElastiCache and MemoryDB (#69)
* feat(auth): Add IAM authentication support for ElastiCache and MemoryDB - Extend `valkey_glide_server_credentials_t` to support IAM authentication - Add `valkey_glide_service_type_t` enum for ElastiCache and MemoryDB service types - Create `valkey_glide_iam_config_t` struct to store IAM authentication configuration - Implement new IAM authentication test suite in `tests/iam_auth_test.php` - Add support for configuring IAM authentication with cluster name, region, and service type - Include optional refresh interval for IAM credentials Enables secure authentication for AWS ElastiCache and MemoryDB clusters using IAM roles and credentials. Signed-off-by: affonsov <[email protected]> * chore(submodule): Update valkey-glide submodule reference - Bump valkey-glide submodule to latest commit - Synchronize submodule pointer with upstream repository - Ensures project is using most recent version of valkey-glide Signed-off-by: affonsov <[email protected]> * fix lint Signed-off-by: affonsov <[email protected]> * fix lint Signed-off-by: affonsov <[email protected]> * - Add new constants for IAM configuration options in ValkeyGlide class - Update README.md with IAM authentication code examples - Modify configuration example to demonstrate IAM authentication scenarios - Add helper function `get_exception_ce_for_client_type()` in common.h for consistent error handling - Enhance error handling for IAM authentication configuration - Update example configuration to include IAM authentication notes and best practices This feature enables secure authentication for AWS ElastiCache and MemoryDB using IAM credentials. Signed-off-by: affonsov <[email protected]> * missed some changes to use the constants Signed-off-by: affonsov <[email protected]> * fix lint Signed-off-by: affonsov <[email protected]> --------- Signed-off-by: affonsov <[email protected]>
1 parent ca90649 commit e73d115

File tree

9 files changed

+651
-13
lines changed

9 files changed

+651
-13
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,41 @@ try {
255255
?>
256256
```
257257

258+
### AWS ElastiCache/MemoryDB with IAM Authentication:
259+
260+
```php
261+
<?php
262+
try {
263+
// Create client with IAM authentication for AWS ElastiCache
264+
$client = new ValkeyGlide(
265+
addresses: [
266+
['host' => 'my-cluster.xxxxx.use1.cache.amazonaws.com', 'port' => 6379]
267+
],
268+
use_tls: true, // REQUIRED for IAM authentication
269+
credentials: [
270+
'username' => 'my-iam-user', // REQUIRED for IAM
271+
'iamConfig' => [
272+
ValkeyGlide::IAM_CONFIG_CLUSTER_NAME => 'my-cluster',
273+
ValkeyGlide::IAM_CONFIG_REGION => 'us-east-1',
274+
ValkeyGlide::IAM_CONFIG_SERVICE => ValkeyGlide::IAM_SERVICE_ELASTICACHE,
275+
ValkeyGlide::IAM_CONFIG_REFRESH_INTERVAL => 300 // Optional, defaults to 300 seconds
276+
]
277+
]
278+
);
279+
280+
// Use the client normally - IAM tokens are managed automatically
281+
$client->set('key', 'value');
282+
$value = $client->get('key');
283+
echo "Value: " . $value . "\n";
284+
285+
$client->close();
286+
287+
} catch (Exception $e) {
288+
echo "Error: " . $e->getMessage() . "\n";
289+
}
290+
?>
291+
```
292+
258293
### Cluster Valkey:
259294

260295
```php

common.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,22 @@ typedef struct {
6363
int port;
6464
} valkey_glide_node_address_t;
6565

66+
typedef enum {
67+
VALKEY_GLIDE_SERVICE_TYPE_ELASTICACHE = 0,
68+
VALKEY_GLIDE_SERVICE_TYPE_MEMORYDB = 1
69+
} valkey_glide_service_type_t;
70+
71+
typedef struct {
72+
char* cluster_name;
73+
char* region;
74+
valkey_glide_service_type_t service_type;
75+
int refresh_interval_seconds; /* 0 means use default (300s) */
76+
} valkey_glide_iam_config_t;
77+
6678
typedef struct {
67-
char* password;
68-
char* username; /* Optional */
79+
char* password;
80+
char* username; /* Optional for password auth, REQUIRED for IAM */
81+
valkey_glide_iam_config_t* iam_config; /* NULL if using password auth */
6982
} valkey_glide_server_credentials_t;
7083

7184
/* Default values for connection configuration options. */
@@ -76,6 +89,14 @@ typedef struct {
7689

7790
#define VALKEY_GLIDE_DEFAULT_CONNECTION_TIMEOUT 250
7891

92+
/* IAM Authentication Constants */
93+
#define VALKEY_GLIDE_IAM_SERVICE_ELASTICACHE "Elasticache"
94+
#define VALKEY_GLIDE_IAM_SERVICE_MEMORYDB "MemoryDB"
95+
#define VALKEY_GLIDE_IAM_CONFIG_CLUSTER_NAME "clusterName"
96+
#define VALKEY_GLIDE_IAM_CONFIG_REGION "region"
97+
#define VALKEY_GLIDE_IAM_CONFIG_SERVICE "service"
98+
#define VALKEY_GLIDE_IAM_CONFIG_REFRESH_INTERVAL "refreshIntervalSeconds"
99+
79100

80101
typedef struct {
81102
int num_of_retries;
@@ -219,4 +240,9 @@ zend_class_entry* get_valkey_glide_exception_ce(void);
219240
zend_class_entry* get_valkey_glide_cluster_ce(void);
220241
zend_class_entry* get_valkey_glide_cluster_exception_ce(void);
221242

243+
/* Helper function to get the appropriate exception class based on client type */
244+
static inline zend_class_entry* get_exception_ce_for_client_type(bool is_cluster) {
245+
return is_cluster ? get_valkey_glide_cluster_exception_ce() : get_valkey_glide_exception_ce();
246+
}
247+
222248
#endif // VALKEY_GLIDE

examples/basic/configuration.php

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,71 @@
192192
}
193193
echo "\n";
194194

195+
// =============================================================================
196+
// IAM AUTHENTICATION (AWS ElastiCache/MemoryDB)
197+
// =============================================================================
198+
echo "☁️ IAM Authentication Configuration (AWS):\n";
199+
echo "-------------------------------------------\n";
200+
201+
// IAM authentication for AWS ElastiCache
202+
echo "Creating client with IAM authentication for ElastiCache...\n";
203+
try {
204+
$iamClient = new ValkeyGlide(
205+
[['host' => 'my-cluster.xxxxx.use1.cache.amazonaws.com', 'port' => 6379]],
206+
true, // use_tls (REQUIRED for IAM)
207+
[ // credentials
208+
'username' => 'my-iam-user', // REQUIRED for IAM
209+
'iamConfig' => [
210+
ValkeyGlide::IAM_CONFIG_CLUSTER_NAME => 'my-cluster',
211+
ValkeyGlide::IAM_CONFIG_REGION => 'us-east-1',
212+
ValkeyGlide::IAM_CONFIG_SERVICE => ValkeyGlide::IAM_SERVICE_ELASTICACHE,
213+
ValkeyGlide::IAM_CONFIG_REFRESH_INTERVAL => 300 // Optional
214+
]
215+
],
216+
0, // read_from
217+
5000 // request_timeout
218+
);
219+
220+
echo "✅ IAM ElastiCache client created\n";
221+
$iamClient->ping();
222+
$iamClient->close();
223+
} catch (Exception $e) {
224+
echo "❌ IAM ElastiCache authentication failed (expected if not on AWS): " . $e->getMessage() . "\n";
225+
}
226+
227+
// IAM authentication for AWS MemoryDB
228+
echo "Creating client with IAM authentication for MemoryDB...\n";
229+
try {
230+
$memorydbClient = new ValkeyGlide(
231+
[['host' => 'clustercfg.my-memorydb.xxxxx.memorydb.us-east-1.amazonaws.com', 'port' => 6379]],
232+
true, // use_tls (REQUIRED for IAM)
233+
[ // credentials
234+
'username' => 'my-iam-user',
235+
'iamConfig' => [
236+
ValkeyGlide::IAM_CONFIG_CLUSTER_NAME => 'my-memorydb',
237+
ValkeyGlide::IAM_CONFIG_REGION => 'us-east-1',
238+
ValkeyGlide::IAM_CONFIG_SERVICE => ValkeyGlide::IAM_SERVICE_MEMORYDB,
239+
ValkeyGlide::IAM_CONFIG_REFRESH_INTERVAL => 120 // Refresh every 2 minutes
240+
]
241+
]
242+
);
243+
244+
echo "✅ IAM MemoryDB client created\n";
245+
$memorydbClient->ping();
246+
$memorydbClient->close();
247+
} catch (Exception $e) {
248+
echo "❌ IAM MemoryDB authentication failed (expected if not on AWS): " . $e->getMessage() . "\n";
249+
}
250+
251+
echo "\nℹ️ IAM Authentication Notes:\n";
252+
echo " - Requires TLS to be enabled (use_tls: true)\n";
253+
echo " - Username is REQUIRED for IAM authentication\n";
254+
echo " - AWS credentials must be configured (IAM role, env vars, or credentials file)\n";
255+
echo " - Tokens are automatically refreshed in the background\n";
256+
echo " - Default refresh interval is 300 seconds (5 minutes)\n";
257+
echo " - IAM permissions required: elasticache:Connect or memorydb:Connect\n";
258+
echo "\n";
259+
195260
// =============================================================================
196261
// TLS CONFIGURATION
197262
// =============================================================================
@@ -393,9 +458,10 @@
393458
echo "2. Set appropriate timeouts based on your network latency\n";
394459
echo "3. Configure reconnection strategy based on your availability needs\n";
395460
echo "4. Use TLS in production environments\n";
396-
echo "5. Set client names for easier debugging and monitoring\n";
397-
echo "6. Consider read preferences when using replicas\n";
398-
echo "7. Limit in-flight requests to prevent memory issues\n";
399-
echo "8. Use lazy connection for applications with conditional Redis usage\n";
461+
echo "5. Use IAM authentication for AWS ElastiCache/MemoryDB (more secure than passwords)\n";
462+
echo "6. Set client names for easier debugging and monitoring\n";
463+
echo "7. Consider read preferences when using replicas\n";
464+
echo "8. Limit in-flight requests to prevent memory issues\n";
465+
echo "9. Use lazy connection for applications with conditional Redis usage\n";
400466

401467
echo "\n✅ Configuration examples completed!\n";

0 commit comments

Comments
 (0)