You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/08/01 21:19:10 UTC

[GitHub] elsloo closed pull request #2608: remove the prepared transaction from the auth wrapper that was a bottleneck

elsloo closed pull request #2608: remove the prepared transaction from the auth wrapper that was a bottleneck
URL: https://github.com/apache/trafficcontrol/pull/2608
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/traffic_ops/traffic_ops_golang/auth/authorize.go b/traffic_ops/traffic_ops_golang/auth/authorize.go
index b0b9cb9b0..89fe2c9bc 100644
--- a/traffic_ops/traffic_ops_golang/auth/authorize.go
+++ b/traffic_ops/traffic_ops_golang/auth/authorize.go
@@ -63,9 +63,9 @@ type key int
 const CurrentUserKey key = iota
 
 // GetCurrentUserFromDB  - returns the id and privilege level of the given user along with the username, or -1 as the id, - as the userName and PrivLevelInvalid if the user doesn't exist.
-func GetCurrentUserFromDB(CurrentUserStmt *sqlx.Stmt, user string) CurrentUser {
+func GetCurrentUserFromDB(DB *sqlx.DB, CurrentUserStmt, user string) CurrentUser {
 	var currentUserInfo CurrentUser
-	err := CurrentUserStmt.Get(&currentUserInfo, user)
+	err := DB.Get(&currentUserInfo, CurrentUserStmt, user)
 	switch {
 	case err == sql.ErrNoRows:
 		log.Errorf("checking user %v info: user not in database", user)
diff --git a/traffic_ops/traffic_ops_golang/routing.go b/traffic_ops/traffic_ops_golang/routing.go
index db364e750..62c52fdad 100644
--- a/traffic_ops/traffic_ops_golang/routing.go
+++ b/traffic_ops/traffic_ops_golang/routing.go
@@ -21,7 +21,6 @@ package main
 
 import (
 	"context"
-	"fmt"
 	"net/http"
 	"regexp"
 	"sort"
@@ -217,10 +216,7 @@ func RegisterRoutes(d ServerData) error {
 		return err
 	}
 
-	userInfoStmt, err := prepareUserInfoStmt(d.DB)
-	if err != nil {
-		return fmt.Errorf("Error preparing db priv level query: %s", err)
-	}
+	userInfoStmt := getUserInfoStmt()
 
 	authBase := AuthBase{secret: d.Config.Secrets[0], getCurrentUserInfoStmt: userInfoStmt, override: nil} //we know d.Config.Secrets is a slice of at least one or start up would fail.
 	routes := CreateRouteMap(routeSlice, rawRoutes, authBase, d.RequestTimeout)
@@ -232,8 +228,8 @@ func RegisterRoutes(d ServerData) error {
 	return nil
 }
 
-func prepareUserInfoStmt(db *sqlx.DB) (*sqlx.Stmt, error) {
-	return db.Preparex("SELECT r.priv_level, u.id, u.username, COALESCE(u.tenant_id, -1) AS tenant_id, ARRAY(SELECT rc.cap_name FROM role_capability AS rc WHERE rc.role_id=r.id) AS capabilities FROM tm_user AS u JOIN role AS r ON u.role = r.id WHERE u.username = $1")
+func getUserInfoStmt() string {
+	return "SELECT r.priv_level, u.id, u.username, COALESCE(u.tenant_id, -1) AS tenant_id, ARRAY(SELECT rc.cap_name FROM role_capability AS rc WHERE rc.role_id=r.id) AS capabilities FROM tm_user AS u JOIN role AS r ON u.role = r.id WHERE u.username = $1"
 }
 
 func use(h http.HandlerFunc, middlewares []Middleware) http.HandlerFunc {
diff --git a/traffic_ops/traffic_ops_golang/wrappers.go b/traffic_ops/traffic_ops_golang/wrappers.go
index 6bc57f83f..bcac30d26 100644
--- a/traffic_ops/traffic_ops_golang/wrappers.go
+++ b/traffic_ops/traffic_ops_golang/wrappers.go
@@ -36,6 +36,7 @@ import (
 	"github.com/apache/trafficcontrol/lib/go-log"
 	tc "github.com/apache/trafficcontrol/lib/go-tc"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/about"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/tocookie"
 	"github.com/jmoiron/sqlx"
@@ -47,7 +48,7 @@ var ServerName = "traffic_ops_golang" + "/" + about.About.Version
 // AuthBase ...
 type AuthBase struct {
 	secret                 string
-	getCurrentUserInfoStmt *sqlx.Stmt
+	getCurrentUserInfoStmt string
 	override               Middleware
 }
 
@@ -98,8 +99,20 @@ func (a AuthBase) GetWrapper(privLevelRequired int) Middleware {
 				handleErr(http.StatusUnauthorized, errors.New("Unauthorized, please log in."))
 				return
 			}
+			var DB *sqlx.DB
+			val := r.Context().Value(api.DBContextKey)
+			if val != nil {
+				switch v := val.(type) {
+				case *sqlx.DB:
+					DB = v
+				default:
+					handleErr(http.StatusInternalServerError, errors.New("No DB found"))
+				}
+			} else {
+				handleErr(http.StatusInternalServerError, errors.New("No DB found"))
+			}
 
-			currentUserInfo := auth.GetCurrentUserFromDB(a.getCurrentUserInfoStmt, username)
+			currentUserInfo := auth.GetCurrentUserFromDB(DB, a.getCurrentUserInfoStmt, username)
 			if currentUserInfo.PrivLevel < privLevelRequired {
 				handleErr(http.StatusForbidden, errors.New("Forbidden."))
 				return


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services