Skip to content

Authenticate to self-designed Elasticache Redis with IAM

0

I followed this guide to setup the role/policies/user/group https://docs.aws.amazon.com/AmazonElastiCache/latest/dg/auth-iam.html and used the following code to generate a token. Then connected to the cluster via redis-cli and used the auth "user" "very-long-string" to authenticate. The error i get is

Error: Server closed the connection

Tried setting REDISCLI_AUTH env and tried again. The error i got was

(error) ERR Protocol version is not an integer or out of range

My question is - did anyone successfully tried using IAM to auth to self-designed elasticache for Redis and could offer some tips or gotchas?

package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/smithy-go/aws-http-auth/credentials"
	v4 "github.com/aws/smithy-go/aws-http-auth/sigv4"
)

var (
	userId    = flag.String("user_id", "", "user id")
	cacheName = flag.String("cache_name", "", "cache name")
	region    = flag.String("region", "us-east-1", "region")
	service   = flag.String("service", "elasticache", "service")
	// algorithm = flag.String("algorithm", "AWS4-HMAC-SHA256", "algorithm")
	profile = flag.String("profile", "default", "AWS profile")
)

func main() {
	flag.Parse()

	if *userId == "" || *cacheName == "" || *region == "" || *profile == "" {
		log.Fatalf("user_id and cache_name and region and profile are required")
	}

	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(*region), config.WithSharedConfigProfile(*profile))
	if err != nil {
		log.Fatalf("failed to load config: %v", err)
	}

	creds, err := cfg.Credentials.Retrieve(ctx)
	if err != nil {
		log.Fatalf("failed to retrieve credentials: %v", err)
	}

	endpoint := fmt.Sprintf("https://%s.%s.amazonaws.com", "elasticache", *region)
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, http.NoBody)
	if err != nil {
		log.Fatalf("failed to create request: %v", err)
	}

	in := &v4.SignRequestInput{
		Request: req,
		Service: *service,
		Region:  *region,
		Credentials: credentials.Credentials{
			AccessKeyID:     creds.AccessKeyID,
			SecretAccessKey: creds.SecretAccessKey,
			SessionToken:    creds.SessionToken,
		},
	}

	signer := v4.New()
	if err := signer.SignRequest(in); err != nil {
		log.Fatalf("failed to sign request: %v", err)
	}

	token := in.Request.Header.Get("X-Amz-Security-Token")

	fmt.Printf("Auth to redis with\nauth %q %q\n", *userId, token)
}
2 Answers
0

IAM authentication is available when using ElastiCache for Valkey 7.2 and above or Redis OSS version 7.0 and above. Did you check your Redis version?

Also as it is mentioned in the AWS Authenticating with IAM documentation generate the short-lived IAM authentication token using an AWS SigV4 pre-signed request.

answered a year ago

  • I am on Redis OSS 7.1.0 and the token generation code is in the post.

0

Hello,

Greetings of the day!! Thank you for contacting AWS.

I understand you're experiencing authentication errors while trying to set up IAM authentication for ElastiCache for Redis, specifically receiving "Server closed the connection" and "Protocol version is not an integer or out of range" errors when attempting to connect using redis-cli with the generated token.

Can you please verify your IAM Authentication setup i.e in-transit is enabled and tls is configured at client side. Also, make sure you use --tls flag with redis-cli and installed redis-cli supports TLS.

To check if the installed redis-cli supports TLS, run below command: $ redis-cli --help | grep tls

If TLS is not supported, then reinstall the same using below document: https://docs.aws.amazon.com/AmazonElastiCache/latest/dg/connect-tls.html#connect-tls.linux

For reference: https://aws.amazon.com/blogs/database/simplify-managing-access-to-amazon-elasticache-for-redis-clusters-with-iam/

If you've verified all the above configurations and are still experiencing issues, we require details that are non-public information i.e details that are specific to your AWS account and would need more details on your setup. Hence, please open a support case with AWS using the following link https://console.aws.amazon.com/support/home#/case/create.

AWS

answered a year ago

  • 'Transit encryption mode' is set to required and the client is supporting TLS and i am using '--tls' while connecting.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.