-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache policies when the first authorization request happens and delete them when removing policies Signed-off-by: Rodney Osodo <[email protected]>
- Loading branch information
1 parent
871998c
commit b019cc6
Showing
10 changed files
with
648 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) Abstract Machines | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package cache contains the domain concept definitions needed to | ||
// support Magistrala auth cache service functionality. | ||
package cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) Abstract Machines | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cache | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"github.com/absmach/magistrala/auth" | ||
"github.com/absmach/magistrala/pkg/errors" | ||
repoerr "github.com/absmach/magistrala/pkg/errors/repository" | ||
"github.com/go-redis/redis/v8" | ||
) | ||
|
||
const defLimit = 100 | ||
|
||
var _ auth.Cache = (*policiesCache)(nil) | ||
|
||
type policiesCache struct { | ||
client *redis.Client | ||
keyDuration time.Duration | ||
} | ||
|
||
// NewPoliciesCache returns redis auth cache implementation. | ||
func NewPoliciesCache(client *redis.Client, duration time.Duration) auth.Cache { | ||
return &policiesCache{ | ||
client: client, | ||
keyDuration: duration, | ||
} | ||
} | ||
|
||
func (pc *policiesCache) Save(ctx context.Context, key, value string) error { | ||
if err := pc.client.Set(ctx, key, value, pc.keyDuration).Err(); err != nil { | ||
return errors.Wrap(repoerr.ErrCreateEntity, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (pc *policiesCache) Contains(ctx context.Context, key, value string) bool { | ||
rval, err := pc.client.Get(ctx, key).Result() | ||
if err != nil { | ||
return false | ||
} | ||
if rval == value { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (pc *policiesCache) Remove(ctx context.Context, key string) error { | ||
if strings.Contains(key, "*") { | ||
return pc.delete(ctx, key) | ||
} | ||
|
||
if err := pc.client.Del(ctx, key).Err(); err != nil { | ||
return errors.Wrap(repoerr.ErrRemoveEntity, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (pc *policiesCache) delete(ctx context.Context, key string) error { | ||
keys, cursor, err := pc.client.Scan(ctx, 0, key, defLimit).Result() | ||
if err != nil { | ||
return errors.Wrap(repoerr.ErrRemoveEntity, err) | ||
} | ||
for cursor != 0 { | ||
var newKeys []string | ||
newKeys, cursor, err = pc.client.Scan(ctx, cursor, key, defLimit).Result() | ||
if err != nil { | ||
return errors.Wrap(repoerr.ErrRemoveEntity, err) | ||
} | ||
keys = append(keys, newKeys...) | ||
} | ||
|
||
for _, key := range keys { | ||
if err := pc.client.Del(ctx, key).Err(); err != nil { | ||
return errors.Wrap(repoerr.ErrRemoveEntity, err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.