1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-29 17:41:15 +03:00

feat: Introducing StreamingCredentialsProvider for token based authentication (#3320)

* wip

* update documentation

* add streamingcredentialsprovider in options

* fix: put back option in pool creation

* add package level comment

* Initial re authentication implementation

Introduces the StreamingCredentialsProvider as the CredentialsProvider
with the highest priority.

TODO: needs to be tested

* Change function type name

Change CancelProviderFunc to UnsubscribeFunc

* add tests

* fix race in tests

* fix example tests

* wip, hooks refactor

* fix build

* update README.md

* update wordlist

* update README.md

* refactor(auth): early returns in cred listener

* fix(doctest): simulate some delay

* feat(conn): add close hook on conn

* fix(tests): simulate start/stop in mock credentials provider

* fix(auth): don't double close the conn

* docs(README): mark streaming credentials provider as experimental

* fix(auth): streamline auth err proccess

* fix(auth): check err on close conn

* chore(entraid): use the repo under redis org
This commit is contained in:
Nedyalko Dyakov
2025-05-27 16:25:20 +03:00
committed by GitHub
parent 28a3c97409
commit 86d418f940
20 changed files with 1103 additions and 130 deletions

View File

@ -23,38 +23,47 @@ func (redisHook) DialHook(hook redis.DialHook) redis.DialHook {
func (redisHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
return func(ctx context.Context, cmd redis.Cmder) error {
fmt.Printf("starting processing: <%s>\n", cmd)
fmt.Printf("starting processing: <%v>\n", cmd.Args())
err := hook(ctx, cmd)
fmt.Printf("finished processing: <%s>\n", cmd)
fmt.Printf("finished processing: <%v>\n", cmd.Args())
return err
}
}
func (redisHook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.ProcessPipelineHook {
return func(ctx context.Context, cmds []redis.Cmder) error {
fmt.Printf("pipeline starting processing: %v\n", cmds)
names := make([]string, 0, len(cmds))
for _, cmd := range cmds {
names = append(names, fmt.Sprintf("%v", cmd.Args()))
}
fmt.Printf("pipeline starting processing: %v\n", names)
err := hook(ctx, cmds)
fmt.Printf("pipeline finished processing: %v\n", cmds)
fmt.Printf("pipeline finished processing: %v\n", names)
return err
}
}
func Example_instrumentation() {
rdb := redis.NewClient(&redis.Options{
Addr: ":6379",
Addr: ":6379",
DisableIdentity: true,
})
rdb.AddHook(redisHook{})
rdb.Ping(ctx)
// Output: starting processing: <ping: >
// Output:
// starting processing: <[ping]>
// dialing tcp :6379
// finished dialing tcp :6379
// finished processing: <ping: PONG>
// starting processing: <[hello 3]>
// finished processing: <[hello 3]>
// finished processing: <[ping]>
}
func ExamplePipeline_instrumentation() {
rdb := redis.NewClient(&redis.Options{
Addr: ":6379",
Addr: ":6379",
DisableIdentity: true,
})
rdb.AddHook(redisHook{})
@ -63,15 +72,19 @@ func ExamplePipeline_instrumentation() {
pipe.Ping(ctx)
return nil
})
// Output: pipeline starting processing: [ping: ping: ]
// Output:
// pipeline starting processing: [[ping] [ping]]
// dialing tcp :6379
// finished dialing tcp :6379
// pipeline finished processing: [ping: PONG ping: PONG]
// starting processing: <[hello 3]>
// finished processing: <[hello 3]>
// pipeline finished processing: [[ping] [ping]]
}
func ExampleClient_Watch_instrumentation() {
rdb := redis.NewClient(&redis.Options{
Addr: ":6379",
Addr: ":6379",
DisableIdentity: true,
})
rdb.AddHook(redisHook{})
@ -81,14 +94,16 @@ func ExampleClient_Watch_instrumentation() {
return nil
}, "foo")
// Output:
// starting processing: <watch foo: >
// starting processing: <[watch foo]>
// dialing tcp :6379
// finished dialing tcp :6379
// finished processing: <watch foo: OK>
// starting processing: <ping: >
// finished processing: <ping: PONG>
// starting processing: <ping: >
// finished processing: <ping: PONG>
// starting processing: <unwatch: >
// finished processing: <unwatch: OK>
// starting processing: <[hello 3]>
// finished processing: <[hello 3]>
// finished processing: <[watch foo]>
// starting processing: <[ping]>
// finished processing: <[ping]>
// starting processing: <[ping]>
// finished processing: <[ping]>
// starting processing: <[unwatch]>
// finished processing: <[unwatch]>
}