internal/server: remove the spans added in common.go
This commit is contained in:
parent
d257215ff4
commit
0ffaa5110a
|
@ -5,7 +5,6 @@ import (
|
||||||
proto "git.xeserv.us/xena/route/proto"
|
proto "git.xeserv.us/xena/route/proto"
|
||||||
"github.com/Xe/ln"
|
"github.com/Xe/ln"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/net/trace"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Backend implements proto.BackendsServer for gRPC.
|
// Backend implements proto.BackendsServer for gRPC.
|
||||||
|
@ -15,14 +14,11 @@ type Backend struct {
|
||||||
|
|
||||||
// List returns a list of backends given filtering parameters.
|
// List returns a list of backends given filtering parameters.
|
||||||
func (b *Backend) List(ctx context.Context, sel *proto.BackendSelector) (*proto.BackendList, error) {
|
func (b *Backend) List(ctx context.Context, sel *proto.BackendSelector) (*proto.BackendList, error) {
|
||||||
ctx, clitok, err := b.getAuth(ctx, "backend list", "backend:list")
|
clitok, err := b.getAuth(ctx, "backend list", "backend:list")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "authentication for backend list"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "authentication for backend list"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
bl := map[string]tun2.Backend{}
|
bl := map[string]tun2.Backend{}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
@ -61,14 +57,11 @@ func (b *Backend) List(ctx context.Context, sel *proto.BackendSelector) (*proto.
|
||||||
|
|
||||||
// Kill removes a backend's connection by ID.
|
// Kill removes a backend's connection by ID.
|
||||||
func (b *Backend) Kill(ctx context.Context, bid *proto.BackendID) (*proto.Nil, error) {
|
func (b *Backend) Kill(ctx context.Context, bid *proto.BackendID) (*proto.Nil, error) {
|
||||||
ctx, clitok, err := b.getAuth(ctx, "backend list", "backend:list")
|
clitok, err := b.getAuth(ctx, "backend list", "backend:list")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "authentication for backend list"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "authentication for backend list"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
if err := b.ts.KillBackend(bid.Id); err != nil {
|
if err := b.ts.KillBackend(bid.Id); err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "killing backend", "backend_id": bid.Id})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "killing backend", "backend_id": bid.Id})
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,23 +17,23 @@ var (
|
||||||
ErrNotAuthorized = errors.New("server: not authorized")
|
ErrNotAuthorized = errors.New("server: not authorized")
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) getAuth(ctx context.Context, operation, scope string) (context.Context, database.Token, error) {
|
func (s *Server) getAuth(ctx context.Context, operation, scope string) (database.Token, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
md, ok := metadata.FromIncomingContext(ctx)
|
md, ok := metadata.FromIncomingContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, database.Token{}, grpc.Errorf(codes.Unauthenticated, "valid token required.")
|
return database.Token{}, grpc.Errorf(codes.Unauthenticated, "valid token required.")
|
||||||
}
|
}
|
||||||
|
|
||||||
jwtToken, ok := md["authorization"]
|
jwtToken, ok := md["authorization"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, database.Token{}, grpc.Errorf(codes.Unauthenticated, "valid token required.")
|
return database.Token{}, grpc.Errorf(codes.Unauthenticated, "valid token required.")
|
||||||
}
|
}
|
||||||
val := jwtToken[0]
|
val := jwtToken[0]
|
||||||
|
|
||||||
t, err := s.db.GetToken(ctx, val)
|
t, err := s.db.GetToken(ctx, val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, database.Token{}, grpc.Errorf(codes.Unauthenticated, "valid token required.")
|
return database.Token{}, grpc.Errorf(codes.Unauthenticated, "valid token required.")
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = false
|
ok = false
|
||||||
|
@ -43,15 +43,12 @@ func (s *Server) getAuth(ctx context.Context, operation, scope string) (context.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, database.Token{}, grpc.Errorf(codes.Unauthenticated, "invalid scope.")
|
return database.Token{}, grpc.Errorf(codes.Unauthenticated, "invalid scope.")
|
||||||
}
|
}
|
||||||
|
|
||||||
tr := trace.New("routed-grpc", operation)
|
|
||||||
ctx = trace.NewContext(ctx, tr)
|
|
||||||
|
|
||||||
ln.Log(ctx, t)
|
ln.Log(ctx, t)
|
||||||
|
|
||||||
return ctx, t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleError(ctx context.Context, clitok database.Token, err error, f ln.F) error {
|
func handleError(ctx context.Context, clitok database.Token, err error, f ln.F) error {
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
proto "git.xeserv.us/xena/route/proto"
|
proto "git.xeserv.us/xena/route/proto"
|
||||||
"github.com/Xe/ln"
|
"github.com/Xe/ln"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/net/trace"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Route implements rout.RoutesServer for gRPC
|
// Route implements rout.RoutesServer for gRPC
|
||||||
|
@ -20,14 +19,11 @@ var (
|
||||||
|
|
||||||
// Get fetches a route from the database.
|
// Get fetches a route from the database.
|
||||||
func (r *Route) Get(ctx context.Context, req *proto.GetRouteRequest) (*proto.Route, error) {
|
func (r *Route) Get(ctx context.Context, req *proto.GetRouteRequest) (*proto.Route, error) {
|
||||||
ctx, clitok, err := r.getAuth(ctx, "get single route", "route:get")
|
clitok, err := r.getAuth(ctx, "get single route", "route:get")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.Get_getAuth"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.Get_getAuth"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
val, err := r.db.GetRoute(ctx, req.Host)
|
val, err := r.db.GetRoute(ctx, req.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "get single route from database", "host": req.Host})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "get single route from database", "host": req.Host})
|
||||||
|
@ -42,14 +38,11 @@ func (r *Route) Get(ctx context.Context, req *proto.GetRouteRequest) (*proto.Rou
|
||||||
|
|
||||||
// GetAll fetches all of the routes that you own.
|
// GetAll fetches all of the routes that you own.
|
||||||
func (r *Route) GetAll(ctx context.Context, req *proto.Nil) (*proto.GetAllRoutesResponse, error) {
|
func (r *Route) GetAll(ctx context.Context, req *proto.Nil) (*proto.GetAllRoutesResponse, error) {
|
||||||
ctx, clitok, err := r.getAuth(ctx, "get all routes for user", "route:getall")
|
clitok, err := r.getAuth(ctx, "get all routes for user", "route:getall")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.GetAll_getAuth"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.GetAll_getAuth"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
routes, err := r.db.GetAllRoutes(ctx, clitok.Owner)
|
routes, err := r.db.GetAllRoutes(ctx, clitok.Owner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "get all routes for user from database"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "get all routes for user from database"})
|
||||||
|
@ -68,14 +61,11 @@ func (r *Route) GetAll(ctx context.Context, req *proto.Nil) (*proto.GetAllRoutes
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) Put(ctx context.Context, rt *proto.Route) (*proto.IDResponse, error) {
|
func (r *Route) Put(ctx context.Context, rt *proto.Route) (*proto.IDResponse, error) {
|
||||||
ctx, clitok, err := r.getAuth(ctx, "put new route", "route:put")
|
clitok, err := r.getAuth(ctx, "put new route", "route:put")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.Put_getAuth"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.Put_getAuth"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
if rt.Host == "" {
|
if rt.Host == "" {
|
||||||
rt.Host = elfs.MakeName() + r.cfg.DomainSuffix
|
rt.Host = elfs.MakeName() + r.cfg.DomainSuffix
|
||||||
}
|
}
|
||||||
|
@ -93,14 +83,11 @@ func (r *Route) Put(ctx context.Context, rt *proto.Route) (*proto.IDResponse, er
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) Delete(ctx context.Context, rt *proto.Route) (*proto.IDResponse, error) {
|
func (r *Route) Delete(ctx context.Context, rt *proto.Route) (*proto.IDResponse, error) {
|
||||||
ctx, clitok, err := r.getAuth(ctx, "delete single route", "route:delete")
|
clitok, err := r.getAuth(ctx, "delete single route", "route:delete")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.Delete_getAuth"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.Delete_getAuth"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
drt, err := r.db.GetRoute(ctx, rt.Host)
|
drt, err := r.db.GetRoute(ctx, rt.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "fetch route from database", "host": rt.Host})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "fetch route from database", "host": rt.Host})
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"github.com/Xe/ln"
|
"github.com/Xe/ln"
|
||||||
"github.com/Xe/uuid"
|
"github.com/Xe/uuid"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/net/trace"
|
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
@ -21,14 +20,11 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t *Token) Get(ctx context.Context, req *proto.GetTokenRequest) (*proto.Token, error) {
|
func (t *Token) Get(ctx context.Context, req *proto.GetTokenRequest) (*proto.Token, error) {
|
||||||
ctx, clitok, err := t.getAuth(ctx, "get single token", "token:get")
|
clitok, err := t.getAuth(ctx, "get single token", "token:get")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "not authorized"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "not authorized"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
if req.Id == "" {
|
if req.Id == "" {
|
||||||
return nil, status.Errorf(codes.InvalidArgument, "must specify ID")
|
return nil, status.Errorf(codes.InvalidArgument, "must specify ID")
|
||||||
}
|
}
|
||||||
|
@ -46,14 +42,11 @@ func (t *Token) Get(ctx context.Context, req *proto.GetTokenRequest) (*proto.Tok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Token) GetAll(ctx context.Context, req *proto.Nil) (*proto.TokenSet, error) {
|
func (t *Token) GetAll(ctx context.Context, req *proto.Nil) (*proto.TokenSet, error) {
|
||||||
ctx, clitok, err := t.getAuth(ctx, "get all tokens", "token:getall")
|
clitok, err := t.getAuth(ctx, "get all tokens", "token:getall")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.GetAll_getAuth"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.GetAll_getAuth"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
toks, err := t.db.GetTokensForOwner(ctx, clitok.Owner)
|
toks, err := t.db.GetTokensForOwner(ctx, clitok.Owner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.GetAll_db.GetTokensForOwner"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.GetAll_db.GetTokensForOwner"})
|
||||||
|
@ -71,14 +64,11 @@ func (t *Token) GetAll(ctx context.Context, req *proto.Nil) (*proto.TokenSet, er
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Token) Put(ctx context.Context, tok *proto.Token) (*proto.Token, error) {
|
func (t *Token) Put(ctx context.Context, tok *proto.Token) (*proto.Token, error) {
|
||||||
ctx, clitok, err := t.getAuth(ctx, "put new token", "token:put")
|
clitok, err := t.getAuth(ctx, "put new token", "token:put")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "not authorized"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "not authorized"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
dbt, err := t.db.PutToken(ctx, uuid.New(), clitok.Owner, tok.Scopes)
|
dbt, err := t.db.PutToken(ctx, uuid.New(), clitok.Owner, tok.Scopes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "put token into database", "scopes": tok.Scopes})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "put token into database", "scopes": tok.Scopes})
|
||||||
|
@ -90,14 +80,11 @@ func (t *Token) Put(ctx context.Context, tok *proto.Token) (*proto.Token, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Token) Delete(ctx context.Context, tok *proto.Token) (*proto.Nil, error) {
|
func (t *Token) Delete(ctx context.Context, tok *proto.Token) (*proto.Nil, error) {
|
||||||
ctx, clitok, err := t.getAuth(ctx, "delete single token", "token:delete")
|
clitok, err := t.getAuth(ctx, "delete single token", "token:delete")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "not authorized"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "not authorized"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
err = t.db.DeleteToken(ctx, tok.Id)
|
err = t.db.DeleteToken(ctx, tok.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "delete token from database", "token_id": tok.Id})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "delete token from database", "token_id": tok.Id})
|
||||||
|
@ -109,14 +96,11 @@ func (t *Token) Delete(ctx context.Context, tok *proto.Token) (*proto.Nil, error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Token) Deactivate(ctx context.Context, tok *proto.Token) (*proto.Nil, error) {
|
func (t *Token) Deactivate(ctx context.Context, tok *proto.Token) (*proto.Nil, error) {
|
||||||
ctx, clitok, err := t.getAuth(ctx, "deactivate single token", "token:deactivate")
|
clitok, err := t.getAuth(ctx, "deactivate single token", "token:deactivate")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "not authorized"})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "not authorized"})
|
||||||
}
|
}
|
||||||
|
|
||||||
tr, _ := trace.FromContext(ctx)
|
|
||||||
defer tr.Finish()
|
|
||||||
|
|
||||||
err = t.db.DeactivateToken(ctx, tok.Id)
|
err = t.db.DeactivateToken(ctx, tok.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleError(ctx, clitok, err, ln.F{"action": "deactivate token in database", "token_id": tok.Id})
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "deactivate token in database", "token_id": tok.Id})
|
||||||
|
|
Loading…
Reference in New Issue