Skip to content

Commit a6331d0

Browse files
committed
test(example): Add minimal example for system assigned
1 parent e4df648 commit a6331d0

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module managedidentity_systemassigned
2+
3+
go 1.23.4
4+
5+
require (
6+
config v0.0.0
7+
github.com/redis/go-redis-entraid v1.0.1
8+
github.com/redis/go-redis/v9 v9.9.0
9+
)
10+
11+
replace (
12+
config => ../config
13+
github.com/redis/go-redis-entraid => ../../../
14+
)
15+
16+
require (
17+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.18.0 // indirect
18+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.10.0 // indirect
19+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.1 // indirect
20+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.2 // indirect
21+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
22+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
23+
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
24+
github.com/google/uuid v1.6.0 // indirect
25+
github.com/kylelemons/godebug v1.1.0 // indirect
26+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
27+
golang.org/x/crypto v0.38.0 // indirect
28+
golang.org/x/net v0.40.0 // indirect
29+
golang.org/x/sys v0.33.0 // indirect
30+
golang.org/x/text v0.25.0 // indirect
31+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 h1:g0EZJwz7xkXQiZAI5xi9f3WWFYBlX1CPTrR+NDToRkQ=
2+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0/go.mod h1:XCW7KnZet0Opnr7HccfUw1PLc4CjHqpcaxW8DHklNkQ=
3+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.18.0/go.mod h1:Ot/6aikWnKWi4l9QB7qVSwa8iMphQNqkWALMoNT3rzM=
4+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1 h1:iw4+KCeCoieuKodp1d5YhAa1TU/GgogCbw8RbGvsfLA=
5+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1/go.mod h1:AP8cDnDTGIVvayqKAhwzpcAyTJosXpvLYNmVFJb98x8=
6+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.10.0/go.mod h1:JdM5psgjfBf5fo2uWOZhflPWyDBZ/O/CNAH9CtsuZE4=
7+
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.2.3 h1:BAUsn6/icUFtvUalVwCO0+hSF7qgU9DwwcEfCvtILtw=
8+
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.2.3/go.mod h1:QlAsNp4gk9zLD2wiZIvIuv699ynpZ2Tq2ZBp+6MrSEw=
9+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
10+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
11+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.1/go.mod h1:j2chePtV91HrC22tGoRX3sGY42uF13WzmmV80/OdVAA=
12+
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM=
13+
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1/go.mod h1:tCcJZ0uHAmvjsVYzEFivsRTN00oz5BEsRgQHu5JZ9WE=
14+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1 h1:8BKxhZZLX/WosEeoCvWysmKUscfa9v8LIPEEU0JjE2o=
15+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
16+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
17+
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
18+
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
19+
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
20+
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
21+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
22+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
23+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
24+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
25+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
26+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
27+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
28+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
29+
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
30+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
31+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
32+
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6 h1:IsMZxCuZqKuao2vNdfD82fjjgPLfyHLpR41Z88viRWs=
33+
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6/go.mod h1:3VeWNIJaW+O5xpRQbPp0Ybqu1vJd/pm7s2F473HRrkw=
34+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
35+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
36+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
37+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
38+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
39+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
40+
github.com/redis/go-redis/v9 v9.5.3-0.20250519143649-1628b87c162b h1:I6Y+sXfQLIUo8vkx+EcuTcAcs0ZnPceNe8cdQ0HsjQI=
41+
github.com/redis/go-redis/v9 v9.5.3-0.20250519143649-1628b87c162b/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
42+
github.com/redis/go-redis/v9 v9.9.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
43+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
44+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
45+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
46+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
47+
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
48+
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
49+
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
50+
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
51+
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
52+
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
53+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
54+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
55+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
56+
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
57+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
58+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
59+
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
60+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
61+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
"time"
9+
10+
"config"
11+
12+
entraid "github.com/redis/go-redis-entraid"
13+
"github.com/redis/go-redis/v9"
14+
)
15+
16+
func main() {
17+
ctx := context.Background()
18+
19+
// Load configuration
20+
cfg, err := config.LoadConfig(os.Getenv("REDIS_ENDPOINTS_CONFIG_PATH"))
21+
if err != nil {
22+
log.Fatalf("Failed to load config: %v", err)
23+
}
24+
25+
// Create a managed identity credentials provider for system-assigned identity
26+
cp, err := entraid.NewManagedIdentityCredentialsProvider(entraid.ManagedIdentityCredentialsProviderOptions{})
27+
if err != nil {
28+
log.Fatalf("Failed to create credentials provider: %v", err)
29+
}
30+
31+
// Create Redis client with streaming credentials provider
32+
opts, err := redis.ParseURL(cfg.Endpoints["standalone-entraid-acl"].Endpoints[0])
33+
if err != nil {
34+
log.Fatalf("Failed to parse Redis URL: %v", err)
35+
}
36+
opts.StreamingCredentialsProvider = cp
37+
redisClient := redis.NewClient(opts)
38+
39+
// Create second Redis client for cluster
40+
clusterOpts, err := redis.ParseURL(cfg.Endpoints["cluster-entraid-acl"].Endpoints[0])
41+
if err != nil {
42+
log.Fatalf("Failed to parse Redis URL: %v", err)
43+
}
44+
clusterClient := redis.NewClusterClient(&redis.ClusterOptions{
45+
Addrs: []string{clusterOpts.Addr},
46+
StreamingCredentialsProvider: cp,
47+
})
48+
49+
// Test the connection
50+
pong, err := redisClient.Ping(ctx).Result()
51+
if err != nil {
52+
log.Fatalf("Failed to ping Redis: %v", err)
53+
}
54+
fmt.Printf("Successfully connected to Redis standalone: %s\n", pong)
55+
56+
// Test cluster connection
57+
clusterPong, err := clusterClient.Ping(ctx).Result()
58+
if err != nil {
59+
log.Fatalf("Failed to ping Redis cluster: %v", err)
60+
}
61+
fmt.Printf("Successfully connected to Redis cluster: %s\n", clusterPong)
62+
63+
// Set a test key
64+
err = redisClient.Set(ctx, "test-key", "test-value", 0).Err()
65+
if err != nil {
66+
log.Fatalf("Failed to set test key: %v", err)
67+
}
68+
69+
// Get the test key
70+
val, err := redisClient.Get(ctx, "test-key").Result()
71+
if err != nil {
72+
log.Fatalf("Failed to get test key: %v", err)
73+
}
74+
fmt.Printf("Retrieved value from standalone: %s\n", val)
75+
76+
// Set a test key in cluster
77+
err = clusterClient.Set(ctx, "test-key", "test-value", 0).Err()
78+
if err != nil {
79+
log.Fatalf("Failed to set test key in cluster: %v", err)
80+
}
81+
82+
// Get the test key from cluster
83+
clusterVal, err := clusterClient.Get(ctx, "test-key").Result()
84+
if err != nil {
85+
log.Fatalf("Failed to get test key from cluster: %v", err)
86+
}
87+
fmt.Printf("Retrieved value from cluster: %s\n", clusterVal)
88+
89+
// Wait for token to expire
90+
fmt.Println("Waiting for token to expire...")
91+
time.Sleep(3 * time.Second)
92+
93+
// Test token refresh by retrying operations
94+
fmt.Println("Testing token refresh...")
95+
96+
// Retry standalone operations
97+
for i := 0; i < 3; i++ {
98+
pong, err = redisClient.Ping(ctx).Result()
99+
if err != nil {
100+
log.Printf("Failed to ping Redis (attempt %d): %v", i+1, err)
101+
continue
102+
}
103+
fmt.Printf("Successfully pinged Redis standalone after token refresh: %s\n", pong)
104+
break
105+
}
106+
107+
// Retry cluster operations
108+
for i := 0; i < 3; i++ {
109+
clusterPong, err = clusterClient.Ping(ctx).Result()
110+
if err != nil {
111+
log.Printf("Failed to ping Redis cluster (attempt %d): %v", i+1, err)
112+
continue
113+
}
114+
fmt.Printf("Successfully pinged Redis cluster after token refresh: %s\n", clusterPong)
115+
break
116+
}
117+
}

0 commit comments

Comments
 (0)