Skip to content

Instantly share code, notes, and snippets.

@heri16
Created March 11, 2023 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heri16/b4100631cfba9dd981a5a4e1d9e2a472 to your computer and use it in GitHub Desktop.
Save heri16/b4100631cfba9dd981a5a4e1d9e2a472 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elasticache"
)
type IAMAuthTokenRequest struct {
hostname string
port string
auth_token string
region string
access_key_id string
secret_key string
}
func NewIAMAuthTokenRequest(hostname string, port string, auth_token string, region string, access_key_id string, secret_key string) *IAMAuthTokenRequest {
return &IAMAuthTokenRequest{
hostname: hostname,
port: port,
auth_token: auth_token,
region: region,
access_key_id: access_key_id,
secret_key: secret_key,
}
}
func (i IAMAuthTokenRequest) generateAuthToken() (string, error) {
svc := elasticache.New(session.New(), aws.NewConfig().WithRegion(i.region).WithCredentials(credentials.NewStaticCredentials(i.access_key_id, i.secret_key, "")))
input := &elasticache.AuthTokenRequest{
AuthToken: aws.String(i.auth_token),
Hostname: aws.String(i.hostname),
Port: aws.String(i.port),
TimeUnit: aws.String("seconds"),
Duration: aws.Int64(3600),
CacheEngine: aws.String("memcached"),
}
req, resp := svc.AuthToken(input)
if err := req.Send(); err != nil {
return "", err
}
if err := resp.Error; err != nil {
return "", err
}
return aws.StringValue(resp.AuthToken), nil
}
func main() {
i := NewIAMAuthTokenRequest("my-cluster.qiw1yg.cfg.use1.cache.amazonaws.com", "11211", "my_auth_token", "us-east-1", "ACCESS_KEY_ID", "SECRET_KEY")
token, err := i.generateAuthToken()
if err != nil {
fmt.Println("Error generating auth token:", err)
return
}
fmt.Println("Auth token:", token)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment