Skip to content

Commit c1ae771

Browse files
committed
chore(exampels): Add runnable examples
1 parent a0cf97a commit c1ae771

File tree

24 files changed

+1178
-122
lines changed

24 files changed

+1178
-122
lines changed

README.md

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,10 @@ authority := identity.AuthorityConfiguration{
411411
```go
412412
// Create provider for system assigned identity
413413
provider, err := entraid.NewManagedIdentityCredentialsProvider(entraid.ManagedIdentityCredentialsProviderOptions{
414-
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
415-
ClientID: os.Getenv("AZURE_CLIENT_ID"),
414+
ManagedIdentityProviderOptions: identity.ManagedIdentityProviderOptions{
415+
ManagedIdentityType: identity.SystemAssignedIdentity,
416+
Scopes: []string{"https://redis.azure.com/.default"},
416417
},
417-
ManagedIdentityType: identity.SystemAssignedIdentity,
418418
})
419419
```
420420

@@ -425,23 +425,27 @@ provider, err := entraid.NewManagedIdentityCredentialsProvider(entraid.ManagedId
425425
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
426426
ClientID: os.Getenv("AZURE_CLIENT_ID"),
427427
},
428-
ManagedIdentityType: identity.UserAssignedIdentity,
429-
UserAssignedClientID: os.Getenv("USER_ASSIGNED_CLIENT_ID"),
428+
ManagedIdentityProviderOptions: identity.ManagedIdentityProviderOptions{
429+
ManagedIdentityType: identity.UserAssignedIdentity,
430+
UserAssignedClientID: os.Getenv("AZURE_USER_ASSIGNED_MANAGED_ID"),
431+
Scopes: []string{"https://redis.azure.com/.default"},
432+
},
430433
})
431434
```
432435

433436
### Client Secret Authentication
434437
```go
435438
// Create provider for client secret authentication
436-
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialIdentityProviderOptions{
437-
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
439+
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialCredentialsProviderOptions{
440+
ConfidentialIdentityProviderOptions: identity.ConfidentialIdentityProviderOptions{
438441
ClientID: os.Getenv("AZURE_CLIENT_ID"),
439-
},
440-
CredentialsType: identity.ClientSecretCredentialType,
441-
ClientSecret: os.Getenv("AZURE_CLIENT_SECRET"),
442-
Authority: identity.AuthorityConfiguration{
443-
AuthorityType: identity.AuthorityTypeDefault,
444-
TenantID: os.Getenv("AZURE_TENANT_ID"),
442+
ClientSecret: os.Getenv("AZURE_CLIENT_SECRET"),
443+
CredentialsType: identity.ClientSecretCredentialType,
444+
Authority: identity.AuthorityConfiguration{
445+
AuthorityType: identity.AuthorityTypeMultiTenant,
446+
TenantID: os.Getenv("AZURE_TENANT_ID"),
447+
},
448+
Scopes: []string{"https://redis.azure.com/.default"},
445449
},
446450
})
447451
```
@@ -454,16 +458,27 @@ if err != nil {
454458
log.Fatal(err)
455459
}
456460

457-
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialIdentityProviderOptions{
458-
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
461+
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialCredentialsProviderOptions{
462+
ConfidentialIdentityProviderOptions: identity.ConfidentialIdentityProviderOptions{
459463
ClientID: os.Getenv("AZURE_CLIENT_ID"),
464+
CredentialsType: identity.ClientCertificateCredentialType,
465+
Authority: identity.AuthorityConfiguration{
466+
AuthorityType: identity.AuthorityTypeMultiTenant,
467+
TenantID: os.Getenv("AZURE_TENANT_ID"),
468+
},
469+
Scopes: []string{"https://redis.azure.com/.default"},
470+
ClientCert: []*x509.Certificate{cert.Leaf},
471+
ClientPrivateKey: cert.PrivateKey,
460472
},
461-
CredentialsType: identity.ClientCertificateCredentialType,
462-
ClientCert: []*x509.Certificate{cert.Leaf},
463-
ClientPrivateKey: cert.PrivateKey,
464-
Authority: identity.AuthorityConfiguration{
465-
AuthorityType: identity.AuthorityTypeDefault,
466-
TenantID: os.Getenv("AZURE_TENANT_ID"),
473+
})
474+
```
475+
476+
### Default Azure Identity
477+
```go
478+
// Create a default credentials provider
479+
provider, err := entraid.NewDefaultAzureCredentialsProvider(entraid.DefaultAzureCredentialsProviderOptions{
480+
DefaultAzureIdentityProviderOptions: identity.DefaultAzureIdentityProviderOptions{
481+
Scopes: []string{"https://redis.azure.com/.default"},
467482
},
468483
})
469484
```

examples/confidential_identity/main.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

examples/entraid/clientcert/go.mod

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module clientcert
2+
3+
go 1.23.4
4+
5+
require (
6+
config v0.0.0
7+
github.com/redis-developer/go-redis-entraid v0.0.0-20250519132904-1e25b29e9a07
8+
github.com/redis/go-redis/v9 v9.5.3-0.20250519143649-1628b87c162b
9+
)
10+
11+
require (
12+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 // indirect
13+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1 // indirect
14+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
15+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1 // indirect
16+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
17+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
18+
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
19+
github.com/google/uuid v1.6.0 // indirect
20+
github.com/kylelemons/godebug v1.1.0 // indirect
21+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
22+
golang.org/x/crypto v0.33.0 // indirect
23+
golang.org/x/net v0.35.0 // indirect
24+
golang.org/x/sys v0.30.0 // indirect
25+
golang.org/x/text v0.22.0 // indirect
26+
)
27+
28+
replace (
29+
config => ../config
30+
github.com/redis-developer/go-redis-entraid => ../../../
31+
github.com/redis/go-redis/v9 => github.com/redis/go-redis/v9 v9.5.3-0.20250519143649-1628b87c162b
32+
)

examples/entraid/clientcert/go.sum

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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/azidentity v1.8.0-beta.1 h1:iw4+KCeCoieuKodp1d5YhAa1TU/GgogCbw8RbGvsfLA=
4+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1/go.mod h1:AP8cDnDTGIVvayqKAhwzpcAyTJosXpvLYNmVFJb98x8=
5+
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.2.3 h1:BAUsn6/icUFtvUalVwCO0+hSF7qgU9DwwcEfCvtILtw=
6+
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.2.3/go.mod h1:QlAsNp4gk9zLD2wiZIvIuv699ynpZ2Tq2ZBp+6MrSEw=
7+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
8+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
9+
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM=
10+
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1/go.mod h1:tCcJZ0uHAmvjsVYzEFivsRTN00oz5BEsRgQHu5JZ9WE=
11+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1 h1:8BKxhZZLX/WosEeoCvWysmKUscfa9v8LIPEEU0JjE2o=
12+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
13+
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
14+
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
15+
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
16+
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
17+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
18+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
19+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
20+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
21+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
22+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
23+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
24+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
25+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
26+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
27+
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6 h1:IsMZxCuZqKuao2vNdfD82fjjgPLfyHLpR41Z88viRWs=
28+
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6/go.mod h1:3VeWNIJaW+O5xpRQbPp0Ybqu1vJd/pm7s2F473HRrkw=
29+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
30+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
31+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
32+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
33+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
34+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
35+
github.com/redis-developer/go-redis-entraid v0.0.0-20250519132904-1e25b29e9a07 h1:l/sz658wqg5ZxqT1cSrmTxGAEXVoBpVOjsZUgMHSJYw=
36+
github.com/redis-developer/go-redis-entraid v0.0.0-20250519132904-1e25b29e9a07/go.mod h1:4DrBBopECIq9LRo1hok3rgXUC4AGBhnZiz0Rea2tikc=
37+
github.com/redis/go-redis/v9 v9.5.3-0.20250519143649-1628b87c162b h1:I6Y+sXfQLIUo8vkx+EcuTcAcs0ZnPceNe8cdQ0HsjQI=
38+
github.com/redis/go-redis/v9 v9.5.3-0.20250519143649-1628b87c162b/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
39+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
40+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
41+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
42+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
43+
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
44+
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
45+
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
46+
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
47+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
48+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
49+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
50+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
51+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
52+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
53+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

examples/entraid/clientcert/main.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"crypto/x509"
6+
"encoding/pem"
7+
"fmt"
8+
"log"
9+
"os"
10+
11+
"config"
12+
13+
entraid "github.com/redis-developer/go-redis-entraid"
14+
"github.com/redis-developer/go-redis-entraid/identity"
15+
"github.com/redis/go-redis/v9"
16+
)
17+
18+
func main() {
19+
ctx := context.Background()
20+
21+
// Load configuration
22+
cfg, err := config.LoadConfig(os.Getenv("REDIS_ENDPOINTS_CONFIG_PATH"))
23+
if err != nil {
24+
log.Fatalf("Failed to load config: %v", err)
25+
}
26+
27+
// Create a confidential identity credentials provider with certificate authentication
28+
cp, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialCredentialsProviderOptions{
29+
ConfidentialIdentityProviderOptions: identity.ConfidentialIdentityProviderOptions{
30+
ClientID: cfg.AzureClientID,
31+
ClientSecret: cfg.AzureClientSecret,
32+
CredentialsType: "Certificate",
33+
Authority: identity.AuthorityConfiguration{
34+
AuthorityType: identity.AuthorityTypeMultiTenant,
35+
TenantID: cfg.AzureTenantID,
36+
},
37+
Scopes: cfg.GetRedisScopes(),
38+
ClientCert: parseCertificates(cfg.AzureCert),
39+
ClientPrivateKey: []byte(cfg.AzurePrivateKey),
40+
},
41+
})
42+
if err != nil {
43+
log.Fatalf("Failed to create credentials provider: %v", err)
44+
}
45+
46+
// Create Redis client with streaming credentials provider
47+
redisClient := redis.NewClusterClient(&redis.ClusterOptions{
48+
Addrs: []string{cfg.Endpoints["standalone-entraid-acl"].Endpoints[0]},
49+
StreamingCredentialsProvider: cp,
50+
})
51+
52+
// Create second Redis client for cluster
53+
clusterClient := redis.NewClusterClient(&redis.ClusterOptions{
54+
Addrs: cfg.Endpoints["cluster-entraid-acl"].Endpoints,
55+
StreamingCredentialsProvider: cp,
56+
})
57+
58+
// Test the connection
59+
pong, err := redisClient.Ping(ctx).Result()
60+
if err != nil {
61+
log.Fatalf("Failed to ping Redis: %v", err)
62+
}
63+
fmt.Printf("Successfully connected to Redis standalone: %s\n", pong)
64+
65+
// Test cluster connection
66+
clusterPong, err := clusterClient.Ping(ctx).Result()
67+
if err != nil {
68+
log.Fatalf("Failed to ping Redis cluster: %v", err)
69+
}
70+
fmt.Printf("Successfully connected to Redis cluster: %s\n", clusterPong)
71+
72+
// Set a test key
73+
err = redisClient.Set(ctx, "test-key", "test-value", 0).Err()
74+
if err != nil {
75+
log.Fatalf("Failed to set test key: %v", err)
76+
}
77+
78+
// Get the test key
79+
val, err := redisClient.Get(ctx, "test-key").Result()
80+
if err != nil {
81+
log.Fatalf("Failed to get test key: %v", err)
82+
}
83+
fmt.Printf("Retrieved value from standalone: %s\n", val)
84+
85+
// Set a test key in cluster
86+
err = clusterClient.Set(ctx, "test-key", "test-value", 0).Err()
87+
if err != nil {
88+
log.Fatalf("Failed to set test key in cluster: %v", err)
89+
}
90+
91+
// Get the test key from cluster
92+
clusterVal, err := clusterClient.Get(ctx, "test-key").Result()
93+
if err != nil {
94+
log.Fatalf("Failed to get test key from cluster: %v", err)
95+
}
96+
fmt.Printf("Retrieved value from cluster: %s\n", clusterVal)
97+
}
98+
99+
func parseCertificates(pemData string) []*x509.Certificate {
100+
var certs []*x509.Certificate
101+
for {
102+
block, rest := pem.Decode([]byte(pemData))
103+
if block == nil {
104+
break
105+
}
106+
if block.Type == "CERTIFICATE" {
107+
cert, err := x509.ParseCertificate(block.Bytes)
108+
if err != nil {
109+
log.Fatalf("Failed to parse certificate: %v", err)
110+
}
111+
certs = append(certs, cert)
112+
}
113+
pemData = string(rest)
114+
}
115+
return certs
116+
}

examples/entraid/clientsecret/go.mod

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module clientsecret
2+
3+
go 1.23.4
4+
5+
require (
6+
config v0.0.0
7+
github.com/redis-developer/go-redis-entraid v0.0.0-20250519132904-1e25b29e9a07
8+
github.com/redis/go-redis/v9 v9.5.3-0.20250519143649-1628b87c162b
9+
)
10+
11+
replace (
12+
config => ../config
13+
github.com/redis-developer/go-redis-entraid => ../../../
14+
github.com/redis/go-redis/v9 => github.com/redis/go-redis/v9 v9.5.3-0.20250519143649-1628b87c162b
15+
)
16+
17+
require (
18+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 // indirect
19+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1 // indirect
20+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
21+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1 // indirect
22+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
23+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
24+
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
25+
github.com/google/uuid v1.6.0 // indirect
26+
github.com/kylelemons/godebug v1.1.0 // indirect
27+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
28+
golang.org/x/crypto v0.33.0 // indirect
29+
golang.org/x/net v0.35.0 // indirect
30+
golang.org/x/sys v0.30.0 // indirect
31+
golang.org/x/text v0.22.0 // indirect
32+
)

0 commit comments

Comments
 (0)