Skip to content
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

MG-2048 - Authorize clients and users with PATs #2499

Merged
merged 72 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
9a3a710
First implementation
nyagamunene Oct 28, 2024
33db8b3
Update middleware, service and api
nyagamunene Oct 28, 2024
a63ab4e
Add grpc authorizePAT
nyagamunene Oct 30, 2024
61190f7
Move pat to auth
nyagamunene Oct 31, 2024
9fdeeea
Update grpc api
nyagamunene Oct 31, 2024
c416f55
Update cmd file
nyagamunene Oct 31, 2024
4b671f1
Update pat service
nyagamunene Oct 31, 2024
34c971d
Update nginx variables
nyagamunene Oct 31, 2024
833f795
Update protoc version
nyagamunene Nov 7, 2024
3cae2a2
Add authenticate PAT grpc endpoint
nyagamunene Nov 13, 2024
77277de
Update middleware, service and api
nyagamunene Oct 28, 2024
6087cd7
Add grpc authorizePAT
nyagamunene Oct 30, 2024
5f5b671
fix failing linter
nyagamunene Dec 3, 2024
58b8ae5
fix protoc linter
nyagamunene Dec 3, 2024
392e752
fix indentation
nyagamunene Dec 3, 2024
08d46f5
remove unused code
nyagamunene Dec 3, 2024
a6c5b8c
fix proto buffer files
nyagamunene Dec 4, 2024
e527c51
fix imports
nyagamunene Dec 5, 2024
c52275c
update protoc files
nyagamunene Dec 5, 2024
d08ff05
fix auth test
nyagamunene Dec 5, 2024
bf9f113
fix nginx
nyagamunene Dec 5, 2024
7aa73c2
add check for token type
nyagamunene Dec 6, 2024
7aeab47
refactor keys endpoints
nyagamunene Dec 6, 2024
860687c
address comments
nyagamunene Dec 11, 2024
cecc2bf
remove pat check in keys api
nyagamunene Dec 17, 2024
8c51950
revert pat changes and update mocks
nyagamunene Dec 17, 2024
693d8ef
address comments
nyagamunene Dec 17, 2024
ffbcd33
First implementation
nyagamunene Oct 28, 2024
f5757fb
fix protoc linter
nyagamunene Dec 3, 2024
56266e4
NOISSUE - Rename Things to Clients
felixgateru Nov 11, 2024
4af3b94
First implementation
nyagamunene Oct 28, 2024
ac14bce
Update middleware, service and api
nyagamunene Oct 28, 2024
10a393e
Add grpc authorizePAT
nyagamunene Oct 30, 2024
ef78deb
Move pat to auth
nyagamunene Oct 31, 2024
8edbae7
Update grpc api
nyagamunene Oct 31, 2024
aefea86
Update pat service
nyagamunene Oct 31, 2024
c963c3e
Update protoc version
nyagamunene Nov 7, 2024
0914dc6
Add authenticate PAT grpc endpoint
nyagamunene Nov 13, 2024
9562d31
Add grpc authorizePAT
nyagamunene Oct 30, 2024
9fe4e3a
Update grpc api
nyagamunene Oct 31, 2024
9c45bd3
Add grpc authorizePAT
nyagamunene Oct 30, 2024
ede9e50
Initial implementation of Authorize
nyagamunene Oct 31, 2024
974eedf
Remove pat folder
nyagamunene Oct 31, 2024
dff4d04
Update user and things middleware
nyagamunene Oct 31, 2024
d9d642a
Authorize and Authenticate method
nyagamunene Nov 3, 2024
95801ed
Update returned errors
nyagamunene Nov 3, 2024
cb84baf
Update variable assignment
nyagamunene Nov 3, 2024
8bd2afa
Update any ids method
nyagamunene Nov 3, 2024
68def70
Update users middleware
nyagamunene Nov 4, 2024
104d8fb
Update middleware, service and api
nyagamunene Oct 28, 2024
284c842
Update protoc version
nyagamunene Nov 7, 2024
7aed9c5
Update middleware, service and api
nyagamunene Oct 28, 2024
4d7f16e
Add grpc authorizePAT
nyagamunene Oct 30, 2024
591e613
Initial implementation of Authorize
nyagamunene Oct 31, 2024
257e03b
Remove pat folder
nyagamunene Oct 31, 2024
7df363b
Update user and things middleware
nyagamunene Oct 31, 2024
00a0e69
Authorize and Authenticate method
nyagamunene Nov 3, 2024
aa216d7
Update any ids method
nyagamunene Nov 3, 2024
27a4174
Resolve conflicts
nyagamunene Nov 7, 2024
96f43d0
Update protoc and mocks
nyagamunene Nov 11, 2024
fd8490f
Address comments
nyagamunene Nov 13, 2024
26e9843
Resolve conflicts
nyagamunene Nov 13, 2024
04e5a65
rename mg to smq
nyagamunene Dec 5, 2024
32cb4f5
update go mod file and proto files
nyagamunene Dec 5, 2024
0f38649
remove unused code
nyagamunene Dec 5, 2024
052a771
revert changes in docker-compose.yml
nyagamunene Dec 5, 2024
c94a788
fix auth tests
nyagamunene Dec 5, 2024
e1a2d8c
solve failing complitation
nyagamunene Dec 6, 2024
0b45d76
fix failing linter
nyagamunene Dec 6, 2024
18eb5f0
update method name
nyagamunene Dec 17, 2024
8278fa5
revert changes
nyagamunene Dec 18, 2024
40f4a8c
fix: rebase
arvindh123 Dec 30, 2024
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
1 change: 1 addition & 0 deletions api/http/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
switch {
case errors.Contains(err, svcerr.ErrAuthorization),
errors.Contains(err, svcerr.ErrDomainAuthorization),
errors.Contains(err, svcerr.ErrUnauthorizedPAT),
errors.Contains(err, bootstrap.ErrExternalKey),
errors.Contains(err, bootstrap.ErrExternalKeySecure):
err = unwrap(err)
Expand Down
4 changes: 3 additions & 1 deletion auth/api/grpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func EncodeError(err error) error {
err == apiutil.ErrMissingMemberType,
err == apiutil.ErrMissingPolicySub,
err == apiutil.ErrMissingPolicyObj,
err == apiutil.ErrMalformedPolicyAct:
err == apiutil.ErrMalformedPolicyAct,
err == apiutil.ErrMissingUserID,
err == apiutil.ErrMissingPATID:
return status.Error(codes.InvalidArgument, err.Error())
case errors.Contains(err, svcerr.ErrAuthentication),
errors.Contains(err, auth.ErrKeyExpired),
Expand Down
2 changes: 1 addition & 1 deletion auth/hasher/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// SPDX-License-Identifier: Apache-2.0

// Package hasher contains the domain concept definitions needed to
// support Magistrala users password hasher sub-service functionality.
// support Supermq users password hasher sub-service functionality.
package hasher
2 changes: 1 addition & 1 deletion auth/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
func (tm *tracingMiddleware) CheckPAT(ctx context.Context, userID, patID string, platformEntityType auth.PlatformEntityType, optionalDomainID string, optionalDomainEntityType auth.DomainEntityType, operation auth.OperationType, entityIDs ...string) error {
ctx, span := tm.tracer.Start(ctx, "check_pat", trace.WithAttributes(
attribute.String("user_id", userID),
attribute.String("patID", patID),
attribute.String("pat_id", patID),

Check warning on line 206 in auth/tracing/tracing.go

View check run for this annotation

Codecov / codecov/patch

auth/tracing/tracing.go#L206

Added line #L206 was not covered by tests
attribute.String("platform_entity", platformEntityType.String()),
attribute.String("optional_domain_id", optionalDomainID),
attribute.String("optional_domain_entity", optionalDomainEntityType.String()),
Expand Down
223 changes: 189 additions & 34 deletions clients/middleware/authorization.go

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions pkg/authz/authsvc/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,23 @@ func (a authorization) checkDomain(ctx context.Context, subjectType, subject, do
return svcerr.ErrInvalidStatus
}
}

func (a authorization) AuthorizePAT(ctx context.Context, pr authz.PatReq) error {
req := grpcAuthV1.AuthZPatReq{
UserId: pr.UserID,
PatId: pr.PatID,
PlatformEntityType: uint32(pr.PlatformEntityType),
OptionalDomainId: pr.OptionalDomainID,
OptionalDomainEntityType: uint32(pr.OptionalDomainEntityType),
Operation: uint32(pr.Operation),
EntityIds: pr.EntityIDs,
}
res, err := a.authSvcClient.AuthorizePAT(ctx, &req)
if err != nil {
return errors.Wrap(errors.ErrAuthorization, err)
}
if !res.Authorized {
return errors.ErrAuthorization
}
return nil
}
17 changes: 16 additions & 1 deletion pkg/authz/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package authz

import "context"
import (
"context"

"github.com/absmach/supermq/auth"
)

type PolicyReq struct {
// Domain contains the domain ID.
Expand Down Expand Up @@ -42,9 +46,20 @@ type PolicyReq struct {
Permission string `json:"permission,omitempty"`
}

type PatReq struct {
UserID string `json:"user_id,omitempty"` // UserID
PatID string `json:"pat_id,omitempty"` // UserID
PlatformEntityType auth.PlatformEntityType `json:"platform_entity_type,omitempty"` // Platform entity type
OptionalDomainID string `json:"optional_domainID,omitempty"` // Optional domain id
OptionalDomainEntityType auth.DomainEntityType `json:"optional_domain_entity_type,omitempty"` // Optional domain entity type
Operation auth.OperationType `json:"operation,omitempty"` // Operation
EntityIDs []string `json:"entityIDs,omitempty"` // EntityIDs
}

// Authz is supermq authorization library.
//
//go:generate mockery --name Authorization --output=./mocks --filename authz.go --quiet --note "Copyright (c) Abstract Machines"
type Authorization interface {
Authorize(ctx context.Context, pr PolicyReq) error
AuthorizePAT(ctx context.Context, pr PatReq) error
}
18 changes: 18 additions & 0 deletions pkg/authz/mocks/authz.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/errors/service/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,7 @@ var (

// ErrRollbackRepo indicates a failure to rollback repository.
ErrRollbackRepo = errors.New("failed to rollback repo")

// ErrUnauthorizedPAT indicates failure occurred while authorizing PAT.
ErrUnauthorizedPAT = errors.New("failed to authorize PAT")
)
Loading
Loading