2020-02-03 08:16:02 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-24 13:48:37 +00:00
|
|
|
"fmt"
|
2020-03-23 16:19:30 +00:00
|
|
|
"strings"
|
2020-03-24 13:48:37 +00:00
|
|
|
"sync"
|
2020-02-03 08:16:02 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/v2/auth"
|
2020-03-26 13:12:43 +00:00
|
|
|
authPb "github.com/micro/go-micro/v2/auth/service/proto/auth"
|
|
|
|
rulePb "github.com/micro/go-micro/v2/auth/service/proto/rules"
|
2020-03-23 16:19:30 +00:00
|
|
|
"github.com/micro/go-micro/v2/auth/token"
|
|
|
|
"github.com/micro/go-micro/v2/auth/token/jwt"
|
2020-02-03 08:26:57 +00:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
2020-03-24 13:48:37 +00:00
|
|
|
log "github.com/micro/go-micro/v2/logger"
|
2020-03-25 18:34:13 +00:00
|
|
|
"github.com/micro/go-micro/v2/util/jitter"
|
2020-02-03 08:16:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewAuth returns a new instance of the Auth service
|
|
|
|
func NewAuth(opts ...auth.Option) auth.Auth {
|
|
|
|
svc := new(svc)
|
|
|
|
svc.Init(opts...)
|
|
|
|
return svc
|
|
|
|
}
|
|
|
|
|
|
|
|
// svc is the service implementation of the Auth interface
|
|
|
|
type svc struct {
|
|
|
|
options auth.Options
|
2020-03-26 13:12:43 +00:00
|
|
|
auth authPb.AuthService
|
|
|
|
rule rulePb.RulesService
|
2020-03-23 16:19:30 +00:00
|
|
|
jwt token.Provider
|
2020-03-24 13:48:37 +00:00
|
|
|
|
2020-03-26 13:12:43 +00:00
|
|
|
rules []*rulePb.Rule
|
2020-03-24 13:48:37 +00:00
|
|
|
sync.Mutex
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *svc) String() string {
|
|
|
|
return "service"
|
|
|
|
}
|
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
func (s *svc) Init(opts ...auth.Option) {
|
2020-02-03 08:16:02 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
dc := client.DefaultClient
|
2020-03-26 13:12:43 +00:00
|
|
|
s.auth = authPb.NewAuthService("go.micro.auth", dc)
|
|
|
|
s.rule = rulePb.NewRulesService("go.micro.auth", dc)
|
2020-02-03 08:16:02 +00:00
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
// if we have a JWT public key passed as an option,
|
|
|
|
// we can decode tokens with the type "JWT" locally
|
|
|
|
// and not have to make an RPC call
|
|
|
|
if key := s.options.PublicKey; len(key) > 0 {
|
|
|
|
s.jwt = jwt.NewTokenProvider(token.WithPublicKey(key))
|
|
|
|
}
|
2020-03-24 13:48:37 +00:00
|
|
|
|
|
|
|
// load rules periodically from the auth service
|
|
|
|
timer := time.NewTicker(time.Second * 30)
|
|
|
|
go func() {
|
2020-03-25 18:34:13 +00:00
|
|
|
// load rules immediately on startup
|
|
|
|
s.loadRules()
|
|
|
|
|
2020-03-24 14:16:57 +00:00
|
|
|
for {
|
|
|
|
<-timer.C
|
2020-03-25 18:34:13 +00:00
|
|
|
|
|
|
|
// jitter for up to 5 seconds, this stops
|
|
|
|
// all the services calling the auth service
|
|
|
|
// at the exact same time
|
|
|
|
time.Sleep(jitter.Do(time.Second * 5))
|
|
|
|
s.loadRules()
|
2020-03-24 14:16:57 +00:00
|
|
|
}
|
2020-03-24 13:48:37 +00:00
|
|
|
}()
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 08:26:28 +00:00
|
|
|
func (s *svc) Options() auth.Options {
|
|
|
|
return s.options
|
|
|
|
}
|
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
// Generate a new account
|
2020-02-03 08:16:02 +00:00
|
|
|
func (s *svc) Generate(id string, opts ...auth.GenerateOption) (*auth.Account, error) {
|
|
|
|
options := auth.NewGenerateOptions(opts...)
|
|
|
|
|
2020-03-26 13:12:43 +00:00
|
|
|
rsp, err := s.auth.Generate(context.TODO(), &authPb.GenerateRequest{
|
2020-03-23 16:19:30 +00:00
|
|
|
Id: id,
|
|
|
|
Roles: options.Roles,
|
|
|
|
Metadata: options.Metadata,
|
2020-03-25 17:03:45 +00:00
|
|
|
SecretExpiry: int64(options.SecretExpiry.Seconds()),
|
2020-03-23 16:19:30 +00:00
|
|
|
})
|
2020-02-03 08:16:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
return serializeAccount(rsp.Account), nil
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
// Grant access to a resource
|
|
|
|
func (s *svc) Grant(role string, res *auth.Resource) error {
|
2020-03-26 13:12:43 +00:00
|
|
|
_, err := s.rule.Create(context.TODO(), &rulePb.CreateRequest{
|
|
|
|
Role: role,
|
|
|
|
Access: rulePb.Access_GRANTED,
|
|
|
|
Resource: &authPb.Resource{
|
2020-03-23 16:19:30 +00:00
|
|
|
Type: res.Type,
|
|
|
|
Name: res.Name,
|
|
|
|
Endpoint: res.Endpoint,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 08:16:02 +00:00
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
// Revoke access to a resource
|
|
|
|
func (s *svc) Revoke(role string, res *auth.Resource) error {
|
2020-03-26 13:12:43 +00:00
|
|
|
_, err := s.rule.Delete(context.TODO(), &rulePb.DeleteRequest{
|
|
|
|
Role: role,
|
|
|
|
Access: rulePb.Access_GRANTED,
|
|
|
|
Resource: &authPb.Resource{
|
2020-03-23 16:19:30 +00:00
|
|
|
Type: res.Type,
|
|
|
|
Name: res.Name,
|
|
|
|
Endpoint: res.Endpoint,
|
|
|
|
},
|
|
|
|
})
|
2020-02-03 08:16:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
// Verify an account has access to a resource
|
|
|
|
func (s *svc) Verify(acc *auth.Account, res *auth.Resource) error {
|
2020-03-24 13:48:37 +00:00
|
|
|
queries := [][]string{
|
2020-03-26 16:30:31 +00:00
|
|
|
{res.Type, res.Name, res.Endpoint}, // check for specific role, e.g. service.foo.ListFoo:admin (role is checked in accessForRule)
|
|
|
|
{res.Type, res.Name, "*"}, // check for wildcard endpoint, e.g. service.foo*
|
|
|
|
{res.Type, "*"}, // check for wildcard name, e.g. service.*
|
|
|
|
{"*"}, // check for wildcard type, e.g. *
|
2020-03-24 13:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// endpoint is a url which can have wildcard excludes, e.g.
|
|
|
|
// "/foo/*" will allow "/foo/bar"
|
|
|
|
if comps := strings.Split(res.Endpoint, "/"); len(comps) > 1 {
|
|
|
|
for i := 1; i < len(comps); i++ {
|
|
|
|
wildcard := fmt.Sprintf("%v/*", strings.Join(comps[0:i], "/"))
|
|
|
|
queries = append(queries, []string{res.Type, res.Name, wildcard})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, q := range queries {
|
|
|
|
for _, rule := range s.listRules(q...) {
|
2020-03-26 13:12:43 +00:00
|
|
|
switch accessForRule(rule, acc, res) {
|
|
|
|
case rulePb.Access_UNKNOWN:
|
|
|
|
continue // rule did not specify access, check the next rule
|
|
|
|
case rulePb.Access_GRANTED:
|
2020-03-26 17:35:28 +00:00
|
|
|
log.Infof("%v granted access to %v:%v:%v by rule %v", acc.ID, res.Type, res.Name, res.Endpoint, rule.Id)
|
2020-03-26 13:12:43 +00:00
|
|
|
return nil // rule grants the account access to the resource
|
|
|
|
case rulePb.Access_DENIED:
|
2020-03-26 17:35:28 +00:00
|
|
|
log.Infof("%v denied access to %v:%v:%v by rule %v", acc.ID, res.Type, res.Name, res.Endpoint, rule.Id)
|
|
|
|
return auth.ErrForbidden // rule denies access to the resource
|
2020-03-24 13:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 13:12:43 +00:00
|
|
|
// no rules were found for the resource, default to denying access
|
2020-03-26 17:55:35 +00:00
|
|
|
log.Infof("%v denied access to %v:%v:%v by lack of rule (%v rules found)", acc.ID, res.Type, res.Name, res.Endpoint, len(s.rules))
|
2020-03-24 13:48:37 +00:00
|
|
|
return auth.ErrForbidden
|
2020-03-23 16:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inspect a token
|
|
|
|
func (s *svc) Inspect(token string) (*auth.Account, error) {
|
|
|
|
// try to decode JWT locally and fall back to srv if an error
|
|
|
|
// occurs, TODO: find a better way of determining if the token
|
|
|
|
// is a JWT, possibly update the interface to take an auth.Token
|
|
|
|
// and not just the string
|
|
|
|
if len(strings.Split(token, ".")) == 3 && s.jwt != nil {
|
|
|
|
if tok, err := s.jwt.Inspect(token); err == nil {
|
|
|
|
return &auth.Account{
|
|
|
|
ID: tok.Subject,
|
|
|
|
Roles: tok.Roles,
|
|
|
|
Metadata: tok.Metadata,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 13:12:43 +00:00
|
|
|
rsp, err := s.auth.Inspect(context.TODO(), &authPb.InspectRequest{
|
2020-03-23 16:19:30 +00:00
|
|
|
Token: token,
|
|
|
|
})
|
2020-02-03 08:16:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
return serializeAccount(rsp.Account), nil
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
// Refresh an account using a secret
|
|
|
|
func (s *svc) Refresh(secret string, opts ...auth.RefreshOption) (*auth.Token, error) {
|
|
|
|
options := auth.NewRefreshOptions(opts...)
|
2020-02-03 08:16:02 +00:00
|
|
|
|
2020-03-26 13:12:43 +00:00
|
|
|
rsp, err := s.auth.Refresh(context.Background(), &authPb.RefreshRequest{
|
2020-03-23 16:19:30 +00:00
|
|
|
Secret: secret,
|
|
|
|
TokenExpiry: int64(options.TokenExpiry.Seconds()),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
return serializeToken(rsp.Token), nil
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 13:48:37 +00:00
|
|
|
var ruleJoinKey = ":"
|
|
|
|
|
2020-03-26 13:12:43 +00:00
|
|
|
// accessForRule returns a rule status, indicating if a rule permits access to a
|
2020-03-24 13:48:37 +00:00
|
|
|
// resource for a given account
|
2020-03-26 13:12:43 +00:00
|
|
|
func accessForRule(rule *rulePb.Rule, acc *auth.Account, res *auth.Resource) rulePb.Access {
|
2020-03-24 13:48:37 +00:00
|
|
|
if rule.Role == "*" {
|
2020-03-26 13:12:43 +00:00
|
|
|
return rule.Access
|
2020-03-24 13:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, role := range acc.Roles {
|
|
|
|
if rule.Role == role {
|
2020-03-26 13:12:43 +00:00
|
|
|
return rule.Access
|
2020-03-24 13:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// allow user.anything if role is user.*
|
|
|
|
if strings.HasSuffix(rule.Role, ".*") && strings.HasPrefix(rule.Role, role+".") {
|
2020-03-26 13:12:43 +00:00
|
|
|
return rule.Access
|
2020-03-24 13:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 15:52:48 +00:00
|
|
|
return rulePb.Access_UNKNOWN
|
2020-03-24 13:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// listRules gets all the rules from the store which have an id
|
|
|
|
// prefix matching the filters
|
2020-03-26 13:12:43 +00:00
|
|
|
func (s *svc) listRules(filters ...string) []*rulePb.Rule {
|
2020-03-24 13:48:37 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
prefix := strings.Join(filters, ruleJoinKey)
|
|
|
|
|
2020-03-26 13:12:43 +00:00
|
|
|
var rules []*rulePb.Rule
|
2020-03-24 13:48:37 +00:00
|
|
|
for _, r := range s.rules {
|
|
|
|
if strings.HasPrefix(r.Id, prefix) {
|
|
|
|
rules = append(rules, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rules
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadRules retrieves the rules from the auth service
|
|
|
|
func (s *svc) loadRules() {
|
2020-03-26 13:12:43 +00:00
|
|
|
rsp, err := s.rule.List(context.TODO(), &rulePb.ListRequest{})
|
2020-03-24 13:48:37 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error listing rules: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.rules = rsp.Rules
|
|
|
|
}
|
|
|
|
|
2020-03-26 13:12:43 +00:00
|
|
|
func serializeToken(t *authPb.Token) *auth.Token {
|
2020-03-23 16:19:30 +00:00
|
|
|
return &auth.Token{
|
|
|
|
Token: t.Token,
|
|
|
|
Type: t.Type,
|
|
|
|
Created: time.Unix(t.Created, 0),
|
|
|
|
Expiry: time.Unix(t.Expiry, 0),
|
|
|
|
Subject: t.Subject,
|
|
|
|
Roles: t.Roles,
|
|
|
|
Metadata: t.Metadata,
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
2020-03-23 16:19:30 +00:00
|
|
|
}
|
2020-02-03 08:16:02 +00:00
|
|
|
|
2020-03-26 13:12:43 +00:00
|
|
|
func serializeAccount(a *authPb.Account) *auth.Account {
|
2020-03-23 16:19:30 +00:00
|
|
|
var secret *auth.Token
|
|
|
|
if a.Secret != nil {
|
|
|
|
secret = serializeToken(a.Secret)
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
return &auth.Account{
|
|
|
|
ID: a.Id,
|
|
|
|
Roles: a.Roles,
|
|
|
|
Metadata: a.Metadata,
|
|
|
|
Secret: secret,
|
|
|
|
}
|
2020-02-03 08:16:02 +00:00
|
|
|
}
|