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

NOISSUE - Merge authz and authn into new service auth #1313

Merged
merged 15 commits into from
Dec 29, 2020

Conversation

mteodor
Copy link
Contributor

@mteodor mteodor commented Dec 22, 2020

What does this do?

New auth service will perform functions of authn service

  • issue token
  • identify
    Additionally it will support groups features
  • assign member to a group ( member can be user or thing )
  • get group members

default values.

| Variable | Description | Default |
|---------------------------|--------------------------------------------------------------------------|---------------|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align

auth/README.md Outdated
| MF_AUTH_SERVER_CERT | Path to server certificate in pem format | |
| MF_AUTH_SERVER_KEY | Path to server key in pem format | |
| MF_AUTH_SECRET | String used for signing tokens | auth |
| MF_JAEGER_URL | Jaeger server URL | localhost:6831 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align

@mteodor mteodor marked this pull request as ready for review December 24, 2020 14:19
auth/README.md Outdated
@@ -0,0 +1,118 @@
# Authentication service
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be updated (it's Auth service, not Authentication service) with the authorization section (at least mention it before we implement all the features).

Comment on lines 104 to 113
type AssignmentReq struct {
GroupID string
MemberID string
}

func (req AssignmentReq) validate() error {
if req.GroupID == "" {
return auth.ErrMalformedEntity
}

if req.MemberID == "" {
return auth.ErrMalformedEntity
}

return nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is not used. Please remove.

Act string
}

func (req AuthZReq) validate() error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be exported. Request and response structs are internal to the API package.

}

func (s *grpcServer) Assign(ctx context.Context, token *mainflux.Assignment) (*empty.Empty, error) {
_, res, err := s.identify.ServeGRPC(ctx, token)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.assign.ServeGRPC

}

func (s *grpcServer) Authorize(ctx context.Context, token *mainflux.AuthorizeReq) (*mainflux.AuthorizeRes, error) {
_, res, err := s.identify.ServeGRPC(ctx, token)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.authorize.ServeGRPC

}

func (s *grpcServer) Members(ctx context.Context, token *mainflux.MembersReq) (*mainflux.MembersRes, error) {
_, res, err := s.identify.ServeGRPC(ctx, token)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.members.ServeGRPC

@@ -46,6 +47,83 @@ func MakeHandler(svc authn.Service, tracer opentracing.Tracer) http.Handler {
opts...,
))

mux.Get("/members/:memberID/groups", kithttp.NewServer(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reorder methods in CRUD order (where read = get one, list all, list members, list member of...).

auth/postgres/init.go Show resolved Hide resolved
@@ -21,6 +21,12 @@ type database struct {
type Database interface {
NamedExecContext(context.Context, string, interface{}) (sql.Result, error)
QueryRowxContext(context.Context, string, ...interface{}) *sqlx.Row
QueryxContext(context.Context, string, ...interface{}) (*sqlx.Rows, error)
// NamedExecContext(context.Context, string, interface{}) (sql.Result, error)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove unused methods, both from the interface and from the implementation.

@@ -13,9 +13,12 @@ service ThingsService {
rpc Identify(Token) returns (ThingID) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be renamed to auth.proto.

auth/README.md Outdated
@@ -0,0 +1,118 @@
# Authentication service
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this Authentication and Authorization Service.

Also - capitalize words in titles.

auth/README.md Outdated
@@ -0,0 +1,118 @@
# Authentication service

Authentication service provides an API for managing authentication keys. User service is using AuthN service gRPC API to obtain login token or password reset token. Authentication key consists of the following fields:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use just Auth, to avoid using the word Authentication, because this service is both AuthN and AuthZ.

auth/README.md Outdated
make install

# set the environment variables and run the service
MF_AUTH_LOG_LEVEL=[Service log level] MF_AUTH_DB_HOST=[Database host address] MF_AUTH_DB_PORT=[Database host port] MF_AUTH_DB_USER=[Database user] MF_AUTH_DB_PASS=[Database password] MF_AUTH_DB=[Name of the database used by the service] MF_AUTH_DB_SSL_MODE=[SSL mode to connect to the database with] MF_AUTH_DB_SSL_CERT=[Path to the PEM encoded certificate file] MF_AUTH_DB_SSL_KEY=[Path to the PEM encoded key file] MF_AUTH_DB_SSL_ROOT_CERT=[Path to the PEM encoded root certificate file] MF_AUTH_HTTP_PORT=[Service HTTP port] MF_AUTH_GRPC_PORT=[Service gRPC port] MF_AUTH_SECRET=[String used for signing tokens] MF_AUTH_SERVER_CERT=[Path to server certificate] MF_AUTH_SERVER_KEY=[Path to server key] MF_JAEGER_URL=[Jaeger server URL] $GOBIN/mainflux-authn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mainflux-auth, not mainflux-authn

auth/README.md Outdated
locally:

```yaml
version: "2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use version 3.8 or something AFAIK. Please verify.


res, err := client.authorize(ctx, AuthZReq{Act: req.Act, Obj: req.Obj, Sub: req.Sub})
if err != nil {
return &mainflux.AuthorizeRes{Authorized: false, Err: err.Error()}, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please align naming of AuthZReq{} and AuthorizeRes{}

// 1. subject - an action invoker
// 2. object - an entity over which action will be executed
// 3. action - type of action that will be executed (read/write)
type AuthZReq struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider aligning this name (you are were using something like AuthorizeRequest / Response). I am not against AuthZReq also, but then align other entities.

auth/service.go Outdated
Total: p.Total,
Offset: p.Offset,
Limit: p.Limit,
// Name: things,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove dead code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove dead code.

auth.proto Show resolved Hide resolved
auth/README.md Show resolved Hide resolved
authorized bool
err string
}
type membersRes struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this contain an err field, as well? Please put an empty line between type definitions (membersRes and emptyRes).

auth.proto Show resolved Hide resolved
auth/service.go Show resolved Hide resolved

import (
"context"
"time"

"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/internal/groups"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Groups are now in the Auth service (we'll have generic groups), there is no need to put this in internal, since it won't be shared with other services. We can simply move it to auth package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about that since some endpoints may be in other services

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, some endpoints probably will be in other services, but we won't share them; we won't use the groups package as an internally shared package, but as a dependency from the auth service. That is, we won't have multiple implementations of the Groups.

auth.proto Outdated

message MembersRes {
uint64 total = 1;
uint64 offset = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check indentation here, looks like it's messed up

| MF_AUTHN_GRPC_URL | AuthN service gRPC URL | localhost:8181 |
| MF_AUTHN_GRPC_TIMEOUT | AuthN service gRPC request timeout in seconds | 1s |
| MF_AUTH_GRPC_URL | AuthN service gRPC URL | localhost:8181 |
| MF_AUTH_GRPC_TIMEOUT | AuthN service gRPC request timeout in seconds | 1s |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alignment of table is broken with new envars, just add one space

things/README.md Outdated
@@ -44,8 +44,8 @@ default values.
| MF_THINGS_SINGLE_USER_EMAIL | User email for single user mode (no gRPC communication with users) | |
| MF_THINGS_SINGLE_USER_TOKEN | User token for single user mode that should be passed in auth header | |
| MF_JAEGER_URL | Jaeger server URL | localhost:6831 |
| MF_AUTHN_GRPC_URL | AuthN service gRPC URL | localhost:8181 |
| MF_AUTHN_GRPC_TIMEOUT | AuthN service gRPC request timeout in seconds | 1s |
| MF_AUTH_GRPC_URL | AuthN service gRPC URL | localhost:8181 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add one space in renamed envars because of table alingment


// Connect creates a connection to the PostgreSQL instance and applies any
// unapplied database migrations. A non-nil error is returned to indicate
// failure.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this failure word to upper line for easier reading.

auth/service.go Outdated Show resolved Hide resolved
auth/service.go Outdated

// Revoke removes the Key with the provided id that is
// issued by the user identified by the provided key.
RevokeKey(ctx context.Context, token, id string) error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question, is Key is necessary ?

}

func (svc service) Authorize(ctx context.Context, token, sub, obj, act string) (bool, error) {
return true, nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this is a placeholder for impl ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

auth/service.go Outdated
// implementation, and all of its decorators (e.g. logging & metrics).
// Token is a string value of the actual Key and is used to authenticate
// an Auth service request.
type Service interface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would embeding interfaces makes more sense ? So i can see the union of multiple interfaces (Groups and Auth) in Auth spec. IMHO it would be much more obvious.

internal/groups/groups.go Show resolved Hide resolved
@@ -57,7 +57,7 @@ func TestParse(t *testing.T) {
require.Nil(t, err, fmt.Sprintf("issuing key expected to succeed: %s", err))

apiKey := key()
apiKey.Type = authn.APIKey
apiKey.Type = auth.APIKey
apiKey.ExpiresAt = time.Now().UTC().Add(-1 * time.Minute).Round(time.Second)
apiToken, err := tokenizer.Issue(apiKey)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you will follow naming, then token makes more sense than tokenizer (interface name).

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
@@ -30,14 +32,24 @@ func NewDatabase(db *sqlx.DB) Database {
}
}

func (d database) NamedExecContext(ctx context.Context, query string, args interface{}) (sql.Result, error) {
func (dm database) NamedQueryContext(ctx context.Context, query string, args interface{}) (*sqlx.Rows, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed to dm, and what m really represents here (a bit confusing)?

return &authNServiceClient{users}
}

func (svc authNServiceClient) Identify(ctx context.Context, in *mainflux.Token, opts ...grpc.CallOption) (*mainflux.UserIdentity, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

authN or auth?

@codecov-io
Copy link

codecov-io commented Dec 29, 2020

Codecov Report

Merging #1313 (477c5d3) into master (b2ccbae) will decrease coverage by 3.94%.
The diff coverage is 38.02%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1313      +/-   ##
==========================================
- Coverage   64.91%   60.97%   -3.95%     
==========================================
  Files         117      110       -7     
  Lines        8029     8581     +552     
==========================================
+ Hits         5212     5232      +20     
- Misses       2361     2904     +543     
+ Partials      456      445      -11     
Impacted Files Coverage Δ
auth/api/http/responses.go 100.00% <ø> (ø)
auth/keys.go 100.00% <ø> (ø)
auth/postgres/groups.go 0.00% <0.00%> (ø)
auth/postgres/tracing.go 75.00% <0.00%> (ø)
bootstrap/service.go 74.34% <ø> (ø)
things/service.go 54.54% <ø> (ø)
things/users/users.go 81.25% <0.00%> (-18.75%) ⬇️
twins/service.go 69.38% <ø> (ø)
users/api/metrics.go 0.00% <ø> (ø)
auth/api/grpc/requests.go 34.28% <34.28%> (ø)
... and 21 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b2ccbae...477c5d3. Read the comment docs.

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
@@ -84,7 +84,7 @@ func TestSave(t *testing.T) {
}

for _, tc := range cases {
// Clean previously saved messages.
// // Clean previously saved messages.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for double //. Please remove.

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
return issueRes{}, err
}

now := time.Now().UTC()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that we need this var - just call the fnc directly in the struct declaration below.

}

// MetricsMiddleware instruments core service by tracking request count and
// latency.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this word to upper line to simplify comment reading.

auth/service.go Outdated
Total: p.Total,
Offset: p.Offset,
Limit: p.Limit,
// Name: things,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove dead code.

dborovcanin
dborovcanin previously approved these changes Dec 29, 2020
Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
Copy link
Contributor

@drasko drasko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@drasko drasko changed the title NOISSUE - merge authz and authn into new service auth NOISSUE - Merge authz and authn into new service auth Dec 29, 2020
@drasko drasko merged commit 47217cb into absmach:master Dec 29, 2020
@mteodor mteodor deleted the auth branch September 13, 2021 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants