From 0ffaa5110a7662142b36f6fce3904485740d687b Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 7 Oct 2017 09:18:38 -0700 Subject: [PATCH] internal/server: remove the spans added in common.go --- internal/server/backend.go | 11 ++--------- internal/server/common.go | 15 ++++++--------- internal/server/route.go | 21 ++++----------------- internal/server/token.go | 26 +++++--------------------- 4 files changed, 17 insertions(+), 56 deletions(-) diff --git a/internal/server/backend.go b/internal/server/backend.go index 3efa441..6ab792b 100644 --- a/internal/server/backend.go +++ b/internal/server/backend.go @@ -5,7 +5,6 @@ import ( proto "git.xeserv.us/xena/route/proto" "github.com/Xe/ln" "golang.org/x/net/context" - "golang.org/x/net/trace" ) // Backend implements proto.BackendsServer for gRPC. @@ -15,14 +14,11 @@ type Backend struct { // List returns a list of backends given filtering parameters. 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 { 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{} switch { @@ -61,14 +57,11 @@ func (b *Backend) List(ctx context.Context, sel *proto.BackendSelector) (*proto. // Kill removes a backend's connection by ID. 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 { 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 { return nil, handleError(ctx, clitok, err, ln.F{"action": "killing backend", "backend_id": bid.Id}) } diff --git a/internal/server/common.go b/internal/server/common.go index e7b79a9..281bf37 100644 --- a/internal/server/common.go +++ b/internal/server/common.go @@ -17,23 +17,23 @@ var ( 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 md, ok := metadata.FromIncomingContext(ctx) 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"] 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] t, err := s.db.GetToken(ctx, val) 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 @@ -43,15 +43,12 @@ func (s *Server) getAuth(ctx context.Context, operation, scope string) (context. } } 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) - return ctx, t, nil + return t, nil } func handleError(ctx context.Context, clitok database.Token, err error, f ln.F) error { diff --git a/internal/server/route.go b/internal/server/route.go index a633c8a..137736a 100644 --- a/internal/server/route.go +++ b/internal/server/route.go @@ -5,7 +5,6 @@ import ( proto "git.xeserv.us/xena/route/proto" "github.com/Xe/ln" "golang.org/x/net/context" - "golang.org/x/net/trace" ) // Route implements rout.RoutesServer for gRPC @@ -20,14 +19,11 @@ var ( // Get fetches a route from the database. 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 { 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) if err != nil { 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. 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 { 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) if err != nil { 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) { - ctx, clitok, err := r.getAuth(ctx, "put new route", "route:put") + clitok, err := r.getAuth(ctx, "put new route", "route:put") if err != nil { return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.Put_getAuth"}) } - tr, _ := trace.FromContext(ctx) - defer tr.Finish() - if rt.Host == "" { 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) { - ctx, clitok, err := r.getAuth(ctx, "delete single route", "route:delete") + clitok, err := r.getAuth(ctx, "delete single route", "route:delete") if err != nil { 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) if err != nil { return nil, handleError(ctx, clitok, err, ln.F{"action": "fetch route from database", "host": rt.Host}) diff --git a/internal/server/token.go b/internal/server/token.go index 1f49431..a7bc1f1 100644 --- a/internal/server/token.go +++ b/internal/server/token.go @@ -5,7 +5,6 @@ import ( "github.com/Xe/ln" "github.com/Xe/uuid" "golang.org/x/net/context" - "golang.org/x/net/trace" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) @@ -21,14 +20,11 @@ var ( ) 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 { return nil, handleError(ctx, clitok, err, ln.F{"action": "not authorized"}) } - tr, _ := trace.FromContext(ctx) - defer tr.Finish() - if req.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) { - ctx, clitok, err := t.getAuth(ctx, "get all tokens", "token:getall") + clitok, err := t.getAuth(ctx, "get all tokens", "token:getall") if err != nil { 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) if err != nil { 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) { - ctx, clitok, err := t.getAuth(ctx, "put new token", "token:put") + clitok, err := t.getAuth(ctx, "put new token", "token:put") if err != nil { 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) if err != nil { 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) { - ctx, clitok, err := t.getAuth(ctx, "delete single token", "token:delete") + clitok, err := t.getAuth(ctx, "delete single token", "token:delete") if err != nil { 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) if err != nil { 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) { - ctx, clitok, err := t.getAuth(ctx, "deactivate single token", "token:deactivate") + clitok, err := t.getAuth(ctx, "deactivate single token", "token:deactivate") if err != nil { 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) if err != nil { return nil, handleError(ctx, clitok, err, ln.F{"action": "deactivate token in database", "token_id": tok.Id})