Skip to content

test(examples): Add runnable examples for testing #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: intro
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 43 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
"os"
"strings"

"github.com/redis-developer/go-redis-entraid/entraid"
"github.com/redis/go-redis-entraid/entraid"
"github.com/redis/go-redis/v9"
)

Expand Down Expand Up @@ -97,7 +97,7 @@ export AZURE_AUTHORITY_HOST="https://login.microsoftonline.com" # For custom au
### Running the Example
```bash
go mod init your-app
go get github.com/redis-developer/go-redis-entraid
go get github.com/redis/go-redis-entraid
go run main.go
```

Expand Down Expand Up @@ -411,10 +411,10 @@ authority := identity.AuthorityConfiguration{
```go
// Create provider for system assigned identity
provider, err := entraid.NewManagedIdentityCredentialsProvider(entraid.ManagedIdentityCredentialsProviderOptions{
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
ClientID: os.Getenv("AZURE_CLIENT_ID"),
ManagedIdentityProviderOptions: identity.ManagedIdentityProviderOptions{
ManagedIdentityType: identity.SystemAssignedIdentity,
Scopes: []string{"https://redis.azure.com/.default"},
},
ManagedIdentityType: identity.SystemAssignedIdentity,
})
```

Expand All @@ -425,23 +425,27 @@ provider, err := entraid.NewManagedIdentityCredentialsProvider(entraid.ManagedId
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
ClientID: os.Getenv("AZURE_CLIENT_ID"),
},
ManagedIdentityType: identity.UserAssignedIdentity,
UserAssignedClientID: os.Getenv("USER_ASSIGNED_CLIENT_ID"),
ManagedIdentityProviderOptions: identity.ManagedIdentityProviderOptions{
ManagedIdentityType: identity.UserAssignedIdentity,
UserAssignedClientID: os.Getenv("AZURE_USER_ASSIGNED_MANAGED_ID"),
Scopes: []string{"https://redis.azure.com/.default"},
},
})
```

### Client Secret Authentication
```go
// Create provider for client secret authentication
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialIdentityProviderOptions{
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialCredentialsProviderOptions{
ConfidentialIdentityProviderOptions: identity.ConfidentialIdentityProviderOptions{
ClientID: os.Getenv("AZURE_CLIENT_ID"),
},
CredentialsType: identity.ClientSecretCredentialType,
ClientSecret: os.Getenv("AZURE_CLIENT_SECRET"),
Authority: identity.AuthorityConfiguration{
AuthorityType: identity.AuthorityTypeDefault,
TenantID: os.Getenv("AZURE_TENANT_ID"),
ClientSecret: os.Getenv("AZURE_CLIENT_SECRET"),
CredentialsType: identity.ClientSecretCredentialType,
Authority: identity.AuthorityConfiguration{
AuthorityType: identity.AuthorityTypeMultiTenant,
TenantID: os.Getenv("AZURE_TENANT_ID"),
},
Scopes: []string{"https://redis.azure.com/.default"},
},
})
```
Expand All @@ -454,16 +458,27 @@ if err != nil {
log.Fatal(err)
}

provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialIdentityProviderOptions{
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialCredentialsProviderOptions{
ConfidentialIdentityProviderOptions: identity.ConfidentialIdentityProviderOptions{
ClientID: os.Getenv("AZURE_CLIENT_ID"),
CredentialsType: identity.ClientCertificateCredentialType,
Authority: identity.AuthorityConfiguration{
AuthorityType: identity.AuthorityTypeMultiTenant,
TenantID: os.Getenv("AZURE_TENANT_ID"),
},
Scopes: []string{"https://redis.azure.com/.default"},
ClientCert: []*x509.Certificate{cert.Leaf},
ClientPrivateKey: cert.PrivateKey,
},
CredentialsType: identity.ClientCertificateCredentialType,
ClientCert: []*x509.Certificate{cert.Leaf},
ClientPrivateKey: cert.PrivateKey,
Authority: identity.AuthorityConfiguration{
AuthorityType: identity.AuthorityTypeDefault,
TenantID: os.Getenv("AZURE_TENANT_ID"),
})
```

### Default Azure Identity
```go
// Create a default credentials provider
provider, err := entraid.NewDefaultAzureCredentialsProvider(entraid.DefaultAzureCredentialsProviderOptions{
DefaultAzureIdentityProviderOptions: identity.DefaultAzureIdentityProviderOptions{
Scopes: []string{"https://redis.azure.com/.default"},
},
})
```
Expand All @@ -483,10 +498,10 @@ import (
"strings"
"time"

"github.com/redis-developer/go-redis-entraid/entraid"
"github.com/redis-developer/go-redis-entraid/entraid/identity"
"github.com/redis-developer/go-redis-entraid/entraid/manager"
"github.com/redis-developer/go-redis-entraid/entraid/shared"
"github.com/redis/go-redis-entraid/entraid"
"github.com/redis/go-redis-entraid/entraid/identity"
"github.com/redis/go-redis-entraid/entraid/manager"
"github.com/redis/go-redis-entraid/entraid/shared"
"github.com/redis/go-redis/v9"
)

Expand Down Expand Up @@ -855,7 +870,7 @@ The library provides several error types that you can check against:

```go
// Import the shared package to access error types
import "github.com/redis-developer/go-redis-entraid/shared"
import "github.com/redis/go-redis-entraid/shared"

// Available error types:
var (
Expand Down
4 changes: 2 additions & 2 deletions credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"fmt"
"sync"

"github.com/redis-developer/go-redis-entraid/manager"
"github.com/redis-developer/go-redis-entraid/token"
"github.com/redis/go-redis-entraid/manager"
"github.com/redis/go-redis-entraid/token"
"github.com/redis/go-redis/v9/auth"
)

Expand Down
8 changes: 4 additions & 4 deletions credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"
"time"

"github.com/redis-developer/go-redis-entraid/identity"
"github.com/redis-developer/go-redis-entraid/manager"
"github.com/redis-developer/go-redis-entraid/shared"
"github.com/redis-developer/go-redis-entraid/token"
"github.com/redis/go-redis-entraid/identity"
"github.com/redis/go-redis-entraid/manager"
"github.com/redis/go-redis-entraid/shared"
"github.com/redis/go-redis-entraid/token"
"github.com/redis/go-redis/v9/auth"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down
2 changes: 1 addition & 1 deletion entraid.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package entraid

import "github.com/redis-developer/go-redis-entraid/shared"
import "github.com/redis/go-redis-entraid/shared"

// IdentityProvider is an alias for the shared.IdentityProvider interface.
type IdentityProvider = shared.IdentityProvider
Expand Down
6 changes: 3 additions & 3 deletions entraid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"testing"
"time"

"github.com/redis-developer/go-redis-entraid/manager"
"github.com/redis-developer/go-redis-entraid/shared"
"github.com/redis-developer/go-redis-entraid/token"
"github.com/redis/go-redis-entraid/manager"
"github.com/redis/go-redis-entraid/shared"
"github.com/redis/go-redis-entraid/token"
"github.com/redis/go-redis/v9/auth"
"github.com/stretchr/testify/mock"
)
Expand Down
25 changes: 25 additions & 0 deletions examples/custom_idp/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module custom_example

go 1.23.4

require (
github.com/redis/go-redis-entraid v0.0.0-20250415111332-9d087bc29c12
github.com/redis/go-redis/v9 v9.5.3-0.20250415103233-40a89c56cc52
)

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
)
53 changes: 53 additions & 0 deletions examples/custom_idp/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 h1:g0EZJwz7xkXQiZAI5xi9f3WWFYBlX1CPTrR+NDToRkQ=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0/go.mod h1:XCW7KnZet0Opnr7HccfUw1PLc4CjHqpcaxW8DHklNkQ=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1 h1:iw4+KCeCoieuKodp1d5YhAa1TU/GgogCbw8RbGvsfLA=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1/go.mod h1:AP8cDnDTGIVvayqKAhwzpcAyTJosXpvLYNmVFJb98x8=
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.2.3 h1:BAUsn6/icUFtvUalVwCO0+hSF7qgU9DwwcEfCvtILtw=
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.2.3/go.mod h1:QlAsNp4gk9zLD2wiZIvIuv699ynpZ2Tq2ZBp+6MrSEw=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM=
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1/go.mod h1:tCcJZ0uHAmvjsVYzEFivsRTN00oz5BEsRgQHu5JZ9WE=
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1 h1:8BKxhZZLX/WosEeoCvWysmKUscfa9v8LIPEEU0JjE2o=
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6 h1:IsMZxCuZqKuao2vNdfD82fjjgPLfyHLpR41Z88viRWs=
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6/go.mod h1:3VeWNIJaW+O5xpRQbPp0Ybqu1vJd/pm7s2F473HRrkw=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis-entraid v0.0.0-20250415111332-9d087bc29c12 h1:H5ZfgueBAxs2eAvXtCMEbT2/fLQz/wxW5Ds4c0uzl50=
github.com/redis/go-redis-entraid v0.0.0-20250415111332-9d087bc29c12/go.mod h1:uXKLxCMUAu1VKgWdt8gWc4PWCygiL2pAI5XpnRSVc0w=
github.com/redis/go-redis/v9 v9.5.3-0.20250415103233-40a89c56cc52 h1:jRx2gINoJsGKxi/RYXCq1VneAAYes9JxUp13xH2oU2g=
github.com/redis/go-redis/v9 v9.5.3-0.20250415103233-40a89c56cc52/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
101 changes: 101 additions & 0 deletions examples/custom_idp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"context"
"fmt"
"strconv"
"strings"
"time"

entraid "github.com/redis/go-redis-entraid"
"github.com/redis/go-redis-entraid/manager"
"github.com/redis/go-redis-entraid/shared"
"github.com/redis/go-redis-entraid/token"
redis "github.com/redis/go-redis/v9"
)

func main() {
ctx := context.Background()
idp := NewFakeIdentityProvider("local", "pass")
parser := &fakeIdentityProviderResponseParser{}
// create token manager

tm, err := manager.NewTokenManager(idp, manager.TokenManagerOptions{
IdentityProviderResponseParser: parser,
})

cp, err := entraid.NewCredentialsProvider(tm, entraid.CredentialsProviderOptions{})
if err != nil {
panic(err)
}

client := redis.NewClient(&redis.Options{
Addr: ":6379",
StreamingCredentialsProvider: cp,
})

ok, err := client.Ping(ctx).Result()
if err != nil {
panic(err)
}
fmt.Println("Ping result:", ok)
}

var _ entraid.IdentityProvider = (*FakeIdentityProvider)(nil)

type FakeIdentityProvider struct {
username string
password string
}

// RequestToken simulates a request to an identity provider and returns a fake token.
// In a real implementation, this would involve making a network request to the identity provider.
func (f *FakeIdentityProvider) RequestToken() (entraid.IdentityProviderResponse, error) {
// Simulate a successful token request
return shared.NewIDPResponse(
shared.ResponseTypeRawToken,
fmt.Sprintf("%s:%s:%d", f.username, f.password, time.Now().Add(1*time.Hour).Unix()),
)
}

// NewFakeIdentityProvider creates a new instance of FakeIdentityProvider with the given username and password.
func NewFakeIdentityProvider(username, password string) *FakeIdentityProvider {
return &FakeIdentityProvider{
username: username,
password: password,
}
}

type fakeIdentityProviderResponseParser struct {
}

// ParseResponse simulates the parsing of a response from an identity provider.
func (f *fakeIdentityProviderResponseParser) ParseResponse(response entraid.IdentityProviderResponse) (*token.Token, error) {
if response.Type() == shared.ResponseTypeRawToken {
rawToken := response.RawToken()
username, password := "", ""
var expiresOnUnix int64

// parse the raw token string
// assuming the format is "username:password:expiresOnUnix"
// where expiresOnUnix is a unix timestamp
parts := strings.Split(rawToken, ":")
if len(parts) != 3 {
return nil, fmt.Errorf("invalid raw token format")
}
username = parts[0]
password = parts[1]
expiresOnUnix, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse raw token: %w", err)
}

// convert the unix timestamp to time.Time
expiresOn := time.Unix(expiresOnUnix, 0)
now := time.Now()
return token.New(username, password, rawToken, expiresOn, now, int64(expiresOn.Sub(now).Seconds())), nil
}
return nil, fmt.Errorf("unsupported response type: %s", response.Type())
}

var _ entraid.IdentityProviderResponseParser = (*fakeIdentityProviderResponseParser)(nil)
38 changes: 38 additions & 0 deletions examples/default_azure_identity/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"fmt"

entraid "github.com/redis/go-redis-entraid"
"github.com/redis/go-redis-entraid/identity"
redis "github.com/redis/go-redis/v9"
)

func main() {
ctx := context.Background()

// Create a default azure identity credentials provider
// This example uses the default Azure identity chain
cp, err := entraid.NewDefaultAzureCredentialsProvider(entraid.DefaultAzureCredentialsProviderOptions{
DefaultAzureIdentityProviderOptions: identity.DefaultAzureIdentityProviderOptions{
Scopes: []string{"https://redis.azure.com/.default"},
},
})
if err != nil {
panic(fmt.Errorf("failed to create default azure identity credentials provider: %w", err))
}

// Create Redis client with the credentials provider
redisClient := redis.NewClient(&redis.Options{
Addr: "your-redis-host:6379",
StreamingCredentialsProvider: cp,
})

// Test the connection
ok, err := redisClient.Ping(ctx).Result()
if err != nil {
panic(fmt.Errorf("failed to ping Redis: %w", err))
}
fmt.Println("Ping result:", ok)
}
Loading