Skip to content

Commit e74a79e

Browse files
committed
add additional examples
1 parent fb31141 commit e74a79e

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
entraid "github.com/redis-developer/go-redis-entraid"
8+
"github.com/redis-developer/go-redis-entraid/identity"
9+
redis "github.com/redis/go-redis/v9"
10+
)
11+
12+
func main() {
13+
ctx := context.Background()
14+
15+
// Create a confidential identity credentials provider
16+
// This example uses client secret authentication
17+
cp, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialCredentialsProviderOptions{
18+
ConfidentialIdentityProviderOptions: identity.ConfidentialIdentityProviderOptions{
19+
ClientID: "your-client-id",
20+
ClientSecret: "your-client-secret",
21+
CredentialsType: "ClientSecret",
22+
Authority: identity.AuthorityConfiguration{
23+
AuthorityType: identity.AuthorityTypeMultiTenant,
24+
TenantID: "your-tenant-id",
25+
},
26+
Scopes: []string{"https://redis.azure.com/.default"},
27+
},
28+
})
29+
if err != nil {
30+
panic(fmt.Errorf("failed to create confidential identity credentials provider: %w", err))
31+
}
32+
33+
// Create Redis client with the credentials provider
34+
redisClient := redis.NewClient(&redis.Options{
35+
Addr: "your-redis-host:6379",
36+
StreamingCredentialsProvider: cp,
37+
})
38+
39+
// Test the connection
40+
ok, err := redisClient.Ping(ctx).Result()
41+
if err != nil {
42+
panic(fmt.Errorf("failed to ping Redis: %w", err))
43+
}
44+
fmt.Println("Ping result:", ok)
45+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
entraid "github.com/redis-developer/go-redis-entraid"
8+
"github.com/redis-developer/go-redis-entraid/identity"
9+
redis "github.com/redis/go-redis/v9"
10+
)
11+
12+
func main() {
13+
ctx := context.Background()
14+
15+
// Create a default azure identity credentials provider
16+
// This example uses the default Azure identity chain
17+
cp, err := entraid.NewDefaultAzureCredentialsProvider(entraid.DefaultAzureCredentialsProviderOptions{
18+
DefaultAzureIdentityProviderOptions: identity.DefaultAzureIdentityProviderOptions{
19+
Scopes: []string{"https://redis.azure.com/.default"},
20+
},
21+
})
22+
if err != nil {
23+
panic(fmt.Errorf("failed to create default azure identity credentials provider: %w", err))
24+
}
25+
26+
// Create Redis client with the credentials provider
27+
redisClient := redis.NewClient(&redis.Options{
28+
Addr: "your-redis-host:6379",
29+
StreamingCredentialsProvider: cp,
30+
})
31+
32+
// Test the connection
33+
ok, err := redisClient.Ping(ctx).Result()
34+
if err != nil {
35+
panic(fmt.Errorf("failed to ping Redis: %w", err))
36+
}
37+
fmt.Println("Ping result:", ok)
38+
}

examples/managed_identity/main.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
entraid "github.com/redis-developer/go-redis-entraid"
8+
"github.com/redis-developer/go-redis-entraid/identity"
9+
redis "github.com/redis/go-redis/v9"
10+
)
11+
12+
func main() {
13+
ctx := context.Background()
14+
15+
// Example 1: System-assigned managed identity
16+
// This is the simplest configuration, as it uses the system-assigned identity
17+
// automatically managed by Azure
18+
systemAssignedCP, err := entraid.NewManagedIdentityCredentialsProvider(entraid.ManagedIdentityCredentialsProviderOptions{
19+
ManagedIdentityProviderOptions: identity.ManagedIdentityProviderOptions{
20+
ManagedIdentityType: identity.SystemAssignedIdentity,
21+
Scopes: []string{"https://redis.azure.com/.default"},
22+
},
23+
})
24+
if err != nil {
25+
panic(fmt.Errorf("failed to create system-assigned managed identity credentials provider: %w", err))
26+
}
27+
28+
// Example 2: User-assigned managed identity
29+
// Uncomment and use this example if you want to use a user-assigned managed identity
30+
/*
31+
userAssignedCP, err := entraid.NewManagedIdentityCredentialsProvider(entraid.ManagedIdentityCredentialsProviderOptions{
32+
ManagedIdentityProviderOptions: identity.ManagedIdentityProviderOptions{
33+
ManagedIdentityType: identity.UserAssignedIdentity,
34+
UserAssignedClientID: "your-user-assigned-identity-client-id",
35+
Scopes: []string{"https://redis.azure.com/.default"},
36+
},
37+
})
38+
if err != nil {
39+
panic(fmt.Errorf("failed to create user-assigned managed identity credentials provider: %w", err))
40+
}
41+
*/
42+
43+
// Create Redis client with the credentials provider
44+
redisClient := redis.NewClient(&redis.Options{
45+
Addr: "your-redis-host:6379",
46+
StreamingCredentialsProvider: systemAssignedCP, // Change to userAssignedCP if using user-assigned identity
47+
})
48+
49+
// Test the connection
50+
ok, err := redisClient.Ping(ctx).Result()
51+
if err != nil {
52+
panic(fmt.Errorf("failed to ping Redis: %w", err))
53+
}
54+
fmt.Println("Ping result:", ok)
55+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ require (
99
)
1010

1111
require (
12+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1213
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
1315
github.com/pmezard/go-difflib v1.0.0 // indirect
1416
github.com/stretchr/objx v0.5.2 // indirect
1517
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkY
88
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM=
99
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1 h1:8BKxhZZLX/WosEeoCvWysmKUscfa9v8LIPEEU0JjE2o=
1010
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
11+
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
12+
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
13+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
14+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
1115
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1216
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
17+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
18+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
1319
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
1420
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
1521
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=

0 commit comments

Comments
 (0)