-
Notifications
You must be signed in to change notification settings - Fork 7
Add IAM authentication support for Valkey-GLIDE (ElastiCache/MemoryDB) #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Spring Data Valkey – IAM Authentication Example | ||
|
|
||
| This example demonstrates how to use **AWS IAM authentication** with Valkey-GLIDE for connecting | ||
| to **Amazon ElastiCache** or **Amazon MemoryDB** clusters. | ||
|
|
||
| ## Overview | ||
|
|
||
| Instead of using static passwords, IAM authentication allows you to authenticate using AWS IAM | ||
| credentials. The GLIDE client automatically generates and refreshes short-lived IAM authentication | ||
| tokens, providing a more secure authentication mechanism. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| 1. **AWS ElastiCache or MemoryDB cluster** with IAM authentication enabled | ||
| 2. **AWS credentials** configured in your environment (via environment variables, IAM role, or `~/.aws/credentials`) | ||
| 3. **IAM policy** granting `elasticache:Connect` (for ElastiCache) or `memorydb:Connect` (for MemoryDB) | ||
| 4. **TLS enabled** on the cluster (required for IAM authentication) | ||
|
|
||
| ## Configuration | ||
|
|
||
| Update `src/main/resources/application.properties` with your cluster details: | ||
|
|
||
| ```properties | ||
| spring.data.valkey.host=your-cluster-endpoint.cache.amazonaws.com | ||
| spring.data.valkey.port=6379 | ||
| spring.data.valkey.username=your-iam-user-id | ||
| spring.data.valkey.ssl.enabled=true | ||
| spring.data.valkey.client-type=valkeyglide | ||
|
|
||
| spring.data.valkey.valkey-glide.iam-authentication.cluster-name=your-cluster-name | ||
| spring.data.valkey.valkey-glide.iam-authentication.service=ELASTICACHE | ||
| spring.data.valkey.valkey-glide.iam-authentication.region=us-east-1 | ||
| ``` | ||
|
|
||
| ### Configuration Properties | ||
|
|
||
| | Property | Description | Required | | ||
| |---|---|---| | ||
| | `iam-authentication.cluster-name` | Name of the ElastiCache/MemoryDB cluster | Yes | | ||
| | `iam-authentication.service` | AWS service type: `ELASTICACHE` or `MEMORYDB` | Yes | | ||
| | `iam-authentication.region` | AWS region (e.g., `us-east-1`) | Yes | | ||
| | `iam-authentication.refresh-interval-seconds` | Token refresh interval (default: 300s) | No | | ||
|
|
||
| ## Programmatic Configuration | ||
|
|
||
| You can also configure IAM authentication programmatically: | ||
|
|
||
| ```java | ||
| @Bean | ||
| public ValkeyGlideConnectionFactory valkeyConnectionFactory() { | ||
| ValkeyClusterConfiguration clusterConfig = new ValkeyClusterConfiguration(); | ||
| clusterConfig.addClusterNode(new ValkeyClusterNode("your-endpoint", 6379)); | ||
| clusterConfig.setUsername("your-iam-user-id"); | ||
|
|
||
| ValkeyGlideClientConfiguration clientConfig = ValkeyGlideClientConfiguration.builder() | ||
| .useSsl() | ||
| .useIamAuthentication(new IamAuthenticationForGlide( | ||
| "your-cluster-name", | ||
| AwsServiceType.ELASTICACHE, | ||
| "us-east-1", | ||
| null // use default refresh interval | ||
| )) | ||
| .build(); | ||
|
|
||
| return new ValkeyGlideConnectionFactory(clusterConfig, clientConfig); | ||
| } | ||
| ``` | ||
|
|
||
| ## Running | ||
|
|
||
| ```bash | ||
| mvn exec:java -pl examples/boot-iam-auth | ||
| ``` | ||
|
|
||
| ## References | ||
|
|
||
| - [Valkey-GLIDE IAM Authentication](https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#aws-iam-authentication-with-glide) | ||
| - [Using IAM with GLIDE for ElastiCache and MemoryDB](https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#using-iam-authentication-with-glide-for-elasticache-and-memorydb) | ||
| - [Java Example](https://github.com/valkey-io/valkey-glide/wiki/Java-Wrapper#example---using-iam-authentication-with-glide-for-elasticache-and-memorydb) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>io.valkey.springframework</groupId> | ||
| <artifactId>spring-data-valkey-examples</artifactId> | ||
| <version>0.2.0</version> | ||
| </parent> | ||
|
|
||
| <artifactId>spring-data-valkey-example-boot-iam-auth</artifactId> | ||
| <name>Spring Data Valkey - Spring Boot IAM Authentication Example</name> | ||
|
|
||
| <properties> | ||
| <exec.mainClass>example.bootiamauth.SpringBootIamAuthExample</exec.mainClass> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.valkey.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-data-valkey</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <extensions> | ||
| <extension> | ||
| <groupId>kr.motd.maven</groupId> | ||
| <artifactId>os-maven-plugin</artifactId> | ||
| <version>1.7.1</version> | ||
| </extension> | ||
| </extensions> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>exec-maven-plugin</artifactId> | ||
| <configuration> | ||
| <mainClass>example.bootiamauth.SpringBootIamAuthExample</mainClass> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
77 changes: 77 additions & 0 deletions
77
examples/boot-iam-auth/src/main/java/example/bootiamauth/SpringBootIamAuthExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package example.bootiamauth; | ||
|
|
||
| import io.valkey.springframework.data.valkey.core.StringValkeyTemplate; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.CommandLineRunner; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
| /** | ||
| * Minimal Spring Boot example that demonstrates IAM authentication | ||
| * with Valkey-GLIDE for AWS ElastiCache or MemoryDB. | ||
| * | ||
| * <p>This example shows how to use AWS IAM-based authentication instead of | ||
| * password-based authentication when connecting to an ElastiCache or MemoryDB | ||
| * cluster via GLIDE.</p> | ||
| * | ||
| * <h2>Prerequisites</h2> | ||
| * <ul> | ||
| * <li>An ElastiCache or MemoryDB cluster with IAM authentication enabled</li> | ||
| * <li>AWS credentials configured (e.g., via environment variables, IAM role, or ~/.aws/credentials)</li> | ||
| * <li>The IAM user/role must have the {@code elasticache:Connect} or {@code memorydb:Connect} permission</li> | ||
| * <li>TLS must be enabled (required for IAM authentication)</li> | ||
| * </ul> | ||
| * | ||
| * <h2>Configuration</h2> | ||
| * <p>Update {@code application.properties} with your cluster details:</p> | ||
| * <pre> | ||
| * spring.data.valkey.host=your-cluster-endpoint.cache.amazonaws.com | ||
| * spring.data.valkey.port=6379 | ||
| * spring.data.valkey.username=your-iam-user-id | ||
| * spring.data.valkey.ssl.enabled=true | ||
| * spring.data.valkey.client-type=valkeyglide | ||
| * | ||
| * spring.data.valkey.valkey-glide.iam-authentication.cluster-name=your-cluster-name | ||
| * spring.data.valkey.valkey-glide.iam-authentication.service=ELASTICACHE | ||
| * spring.data.valkey.valkey-glide.iam-authentication.region=us-east-1 | ||
| * </pre> | ||
| * | ||
| * @see <a href="https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#aws-iam-authentication-with-glide"> | ||
| * Valkey-GLIDE IAM Authentication Documentation</a> | ||
| */ | ||
| @SpringBootApplication | ||
| public class SpringBootIamAuthExample implements CommandLineRunner { | ||
|
|
||
| @Autowired | ||
| private StringValkeyTemplate valkeyTemplate; | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(SpringBootIamAuthExample.class, args); | ||
| } | ||
|
|
||
| @Override | ||
| public void run(String... args) { | ||
|
|
||
| System.out.println("=== IAM Authentication with Valkey-GLIDE ==="); | ||
| System.out.println("Connected successfully using IAM authentication!"); | ||
|
|
||
| // Simple read/write operations | ||
| int iterations = 5; | ||
| for (int i = 0; i < iterations; i++) { | ||
| String key = "iam-test-key-" + i; | ||
| String value = "iam-test-value-" + i; | ||
|
|
||
| valkeyTemplate.opsForValue().set(key, value); | ||
| String readBack = valkeyTemplate.opsForValue().get(key); | ||
| System.out.println(" " + key + " = " + readBack); | ||
| } | ||
|
|
||
| System.out.println("Completed " + iterations + " iterations of Valkey commands with IAM auth."); | ||
|
|
||
| // Cleanup | ||
| for (int i = 0; i < iterations; i++) { | ||
| valkeyTemplate.delete("iam-test-key-" + i); | ||
| } | ||
| System.out.println("Cleanup complete."); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
examples/boot-iam-auth/src/main/resources/application.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # ============================================================ | ||
| # IAM Authentication Example for AWS ElastiCache / MemoryDB | ||
| # ============================================================ | ||
| # | ||
| # Update these properties with your actual cluster details. | ||
| # AWS credentials must be available in the environment | ||
| # (e.g., via environment variables, IAM role, or ~/.aws/credentials). | ||
|
|
||
| # Cluster endpoint | ||
| spring.data.valkey.host=your-cluster-endpoint.cache.amazonaws.com | ||
| spring.data.valkey.port=6379 | ||
|
|
||
| # The IAM user ID used for authentication | ||
| spring.data.valkey.username=your-iam-user-id | ||
|
|
||
| # TLS is required for IAM authentication | ||
| spring.data.valkey.ssl.enabled=true | ||
|
|
||
| # Use Valkey-GLIDE as the client driver | ||
| spring.data.valkey.client-type=valkeyglide | ||
|
|
||
| # IAM Authentication configuration | ||
| # All three properties (cluster-name, service, region) are required. | ||
| spring.data.valkey.valkey-glide.iam-authentication.cluster-name=your-cluster-name | ||
| spring.data.valkey.valkey-glide.iam-authentication.service=ELASTICACHE | ||
| spring.data.valkey.valkey-glide.iam-authentication.region=us-east-1 | ||
| # Optional: refresh interval in seconds (default: 300) | ||
| # spring.data.valkey.valkey-glide.iam-authentication.refresh-interval-seconds=300 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <configuration> | ||
| <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
| <encoder> | ||
| <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
| </encoder> | ||
| </appender> | ||
| <root level="INFO"> | ||
| <appender-ref ref="STDOUT" /> | ||
| </root> | ||
| </configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.