Skip to content

Commit

Permalink
Fix Bug on Things Authorization Cache (#1810)
Browse files Browse the repository at this point in the history
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
  • Loading branch information
rodneyosodo authored and dborovcanin committed Jun 13, 2023
1 parent 230ccb6 commit f97df0a
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 22 deletions.
8 changes: 2 additions & 6 deletions pkg/auth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func (c client) Identify(ctx context.Context, thingKey string) (string, error) {
thingID, err := c.redisClient.Get(ctx, tkey).Result()
if err != nil {
t := &policies.Key{
Value: string(thingKey),
Value: thingKey,
}

thid, err := c.thingsClient.Identify(context.TODO(), t)
thid, err := c.thingsClient.Identify(ctx, t)
if err != nil {
return "", err
}
Expand All @@ -53,10 +53,6 @@ func (c client) Identify(ctx context.Context, thingKey string) (string, error) {
}

func (c client) Authorize(ctx context.Context, chanID, thingID, action string) error {
if c.redisClient.SIsMember(ctx, chanPrefix+":"+chanID, thingID).Val() {
return nil
}

ar := &policies.AuthorizeReq{
Sub: thingID,
Obj: chanID,
Expand Down
4 changes: 2 additions & 2 deletions things/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type Service interface {
Identify(ctx context.Context, key string) (string, error)
}

// ClientCache contains thing caching interface.
type ClientCache interface {
// Cache contains thing caching interface.
type Cache interface {
// Save stores pair thing secret, thing id.
Save(ctx context.Context, thingSecret, thingID string) error

Expand Down
4 changes: 2 additions & 2 deletions things/clients/mocks/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ type clientCacheMock struct {
things map[string]string
}

// NewClientCache returns mock cache instance.
func NewClientCache() clients.ClientCache {
// NewCache returns mock cache instance.
func NewCache() clients.Cache {
return &clientCacheMock{
things: make(map[string]string),
}
Expand Down
4 changes: 2 additions & 2 deletions things/clients/redis/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const (
idPrefix = "thing_id"
)

var _ clients.ClientCache = (*thingCache)(nil)
var _ clients.Cache = (*thingCache)(nil)

type thingCache struct {
client *redis.Client
}

// NewCache returns redis thing cache implementation.
func NewCache(client *redis.Client) clients.ClientCache {
func NewCache(client *redis.Client) clients.Cache {
return &thingCache{
client: client,
}
Expand Down
4 changes: 2 additions & 2 deletions things/clients/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ type service struct {
uauth upolicies.AuthServiceClient
policies tpolicies.Service
clients mfclients.Repository
clientCache ClientCache
clientCache Cache
idProvider mainflux.IDProvider
grepo mfgroups.Repository
}

// NewService returns a new Clients service implementation.
func NewService(uauth upolicies.AuthServiceClient, policies tpolicies.Service, c mfclients.Repository, grepo mfgroups.Repository, tcache ClientCache, idp mainflux.IDProvider) Service {
func NewService(uauth upolicies.AuthServiceClient, policies tpolicies.Service, c mfclients.Repository, grepo mfgroups.Repository, tcache Cache, idp mainflux.IDProvider) Service {
return service{
uauth: uauth,
policies: policies,
Expand Down
2 changes: 1 addition & 1 deletion things/clients/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
func newService(tokens map[string]string) (clients.Service, *mocks.ClientRepository, *pmocks.PolicyRepository) {
adminPolicy := mocks.MockSubjectSet{Object: ID, Relation: clients.AdminRelationKey}
auth := mocks.NewAuthService(tokens, map[string][]mocks.MockSubjectSet{adminEmail: {adminPolicy}})
thingCache := mocks.NewClientCache()
thingCache := mocks.NewCache()
policiesCache := pmocks.NewChannelCache()
idProvider := uuid.NewMock()
cRepo := new(mocks.ClientRepository)
Expand Down
12 changes: 8 additions & 4 deletions things/policies/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ func (svc service) Authorize(ctx context.Context, ar AccessRequest) (Policy, err
// Replace Subject since AccessRequest Subject is Thing Key,
// and Policy subject is Thing ID.
policy.Subject = ar.Subject
if err := svc.policyCache.Put(ctx, policy); err != nil {
return policy, err
}

default:
return Policy{}, ErrInvalidEntityType
}

if err := svc.policyCache.Put(ctx, policy); err != nil {
return policy, err
}

return policy, nil
}

Expand Down Expand Up @@ -165,6 +165,10 @@ func (svc service) UpdatePolicy(ctx context.Context, token string, p Policy) (Po
p.UpdatedAt = time.Now()
p.UpdatedBy = userID

if err := svc.policyCache.Remove(ctx, p); err != nil {
return Policy{}, err
}

return svc.policies.Update(ctx, p)
}

Expand Down
2 changes: 1 addition & 1 deletion things/policies/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TracingMiddleware(psvc policies.Service, tracer trace.Tracer) policies.Serv
}

func (tm *tracingMiddleware) Authorize(ctx context.Context, ar policies.AccessRequest) (policies.Policy, error) {
ctx, span := tm.tracer.Start(ctx, "svc_authorize_by_key", trace.WithAttributes(attribute.String("subject", ar.Subject), attribute.String("object", ar.Object), attribute.String("action", ar.Action)))
ctx, span := tm.tracer.Start(ctx, "svc_authorize", trace.WithAttributes(attribute.String("subject", ar.Subject), attribute.String("object", ar.Object), attribute.String("action", ar.Action)))
defer span.End()

return tm.psvc.Authorize(ctx, ar)
Expand Down
3 changes: 1 addition & 2 deletions things/postgres/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ func Migration() *migrate.MemoryMigrationSource {
created_at TIMESTAMP,
updated_at TIMESTAMP,
updated_by VARCHAR(254),
status SMALLINT NOT NULL DEFAULT 0 CHECK (status >= 0),
UNIQUE (owner_id, secret)
status SMALLINT NOT NULL DEFAULT 0 CHECK (status >= 0)
)`,
`CREATE TABLE IF NOT EXISTS groups (
id VARCHAR(36) PRIMARY KEY,
Expand Down

0 comments on commit f97df0a

Please sign in to comment.