You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2018/06/21 17:08:50 UTC

[trafficcontrol] 02/03: Add TO Go dskeys, urisigning transactions

This is an automated email from the ASF dual-hosted git repository.

mitchell852 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit d59a475ac307a729b4e4234596ba66cc7fa2c3bc
Author: Robert Butts <ro...@apache.org>
AuthorDate: Sat Jun 2 12:25:08 2018 -0600

    Add TO Go dskeys, urisigning transactions
---
 traffic_ops/traffic_ops_golang/api/api.go          |   4 +-
 .../traffic_ops_golang/api/shared_handlers.go      |   2 +-
 .../crconfig/deliveryservice_test.go               |   2 +-
 .../traffic_ops_golang/dbhelpers/db_helpers.go     |   6 +-
 .../deliveryservice/deliveryservicesv13.go         |   1 -
 .../deliveryservice/servers/servers_test.go        |   2 -
 .../traffic_ops_golang/deliveryservices_keys.go    | 397 ++++++---------------
 .../profileparameter/profile_parameters.go         |   2 +-
 .../traffic_ops_golang/riaksvc/riak_services.go    |  19 +
 traffic_ops/traffic_ops_golang/routes.go           |  14 +-
 traffic_ops/traffic_ops_golang/tenant/tenancy.go   |  15 +-
 traffic_ops/traffic_ops_golang/urisigning.go       | 397 +++++++--------------
 12 files changed, 280 insertions(+), 581 deletions(-)

diff --git a/traffic_ops/traffic_ops_golang/api/api.go b/traffic_ops/traffic_ops_golang/api/api.go
index 0ff3ade..5310fa5 100644
--- a/traffic_ops/traffic_ops_golang/api/api.go
+++ b/traffic_ops/traffic_ops_golang/api/api.go
@@ -32,6 +32,7 @@ import (
 
 	"github.com/apache/trafficcontrol/lib/go-log"
 	"github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/lib/go-util"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/config"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
@@ -266,7 +267,6 @@ func NewInfo(r *http.Request, requiredParams []string, intParamNames []string) (
 	if err != nil {
 		return nil, userErr, errors.New("could not begin transaction: " + err.Error()), http.StatusInternalServerError
 	}
-	falsePtr := false
 	return &APIInfo{
 		Config:    cfg,
 		ReqID:     reqID,
@@ -274,7 +274,7 @@ func NewInfo(r *http.Request, requiredParams []string, intParamNames []string) (
 		IntParams: intParams,
 		User:      user,
 		Tx:        tx,
-		CommitTx:  &falsePtr,
+		CommitTx:  util.BoolPtr(false),
 	}, nil, nil, http.StatusOK
 }
 
diff --git a/traffic_ops/traffic_ops_golang/api/shared_handlers.go b/traffic_ops/traffic_ops_golang/api/shared_handlers.go
index b4d583a..c6f645c 100644
--- a/traffic_ops/traffic_ops_golang/api/shared_handlers.go
+++ b/traffic_ops/traffic_ops_golang/api/shared_handlers.go
@@ -47,7 +47,7 @@ type KeyFieldInfo struct {
 
 func GetIntKey(s string) (interface{}, error) {
 	if strings.HasSuffix(s, ".json") {
-		s = s[:len(s) - len(".json")]
+		s = s[:len(s)-len(".json")]
 	}
 	return strconv.Atoi(s)
 }
diff --git a/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go b/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
index b37b7f3..25c233f 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/deliveryservice_test.go
@@ -82,7 +82,7 @@ func randDS() tc.CRConfigDeliveryService {
 	geoProviderStr := GeoProviderMaxmindStr
 	return tc.CRConfigDeliveryService{
 		AnonymousBlockingEnabled: &falseStrPtr,
-		CoverageZoneOnly: false,
+		CoverageZoneOnly:         false,
 		Dispersion: &tc.CRConfigDispersion{
 			Limit:    42,
 			Shuffled: true,
diff --git a/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go b/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
index 06c3e2d..04dd8ce 100644
--- a/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
+++ b/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
@@ -145,14 +145,14 @@ func FinishTxX(tx *sqlx.Tx, commit *bool) {
 	tx.Commit()
 }
 
-func AddTenancyCheck(where string, queryValues map[string]interface{},tenantColumnName string, tenantIDs []int) (string, map[string]interface{}) {
+func AddTenancyCheck(where string, queryValues map[string]interface{}, tenantColumnName string, tenantIDs []int) (string, map[string]interface{}) {
 	if where == "" {
 		where = BaseWhere + " " + tenantColumnName + " = ANY(CAST(:accessibleTenants AS bigint[]))"
 	} else {
-		where += " AND "+ tenantColumnName + " = ANY(CAST(:accessibleTenants AS bigint[]))"
+		where += " AND " + tenantColumnName + " = ANY(CAST(:accessibleTenants AS bigint[]))"
 	}
 
 	queryValues["accessibleTenants"] = pq.Array(tenantIDs)
 
 	return where, queryValues
-}
\ No newline at end of file
+}
diff --git a/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go b/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go
index be448f7..d332c3e 100644
--- a/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go
+++ b/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go
@@ -364,7 +364,6 @@ func getDSType(tx *sql.Tx, xmlid string) (tc.DSType, bool, error) {
 }
 
 func UpdateV13(w http.ResponseWriter, r *http.Request) {
-	defer r.Body.Close()
 	inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"id"}, []string{"id"})
 	if userErr != nil || sysErr != nil {
 		api.HandleErr(w, r, errCode, userErr, sysErr)
diff --git a/traffic_ops/traffic_ops_golang/deliveryservice/servers/servers_test.go b/traffic_ops/traffic_ops_golang/deliveryservice/servers/servers_test.go
index 58d556e..c87bd0a 100644
--- a/traffic_ops/traffic_ops_golang/deliveryservice/servers/servers_test.go
+++ b/traffic_ops/traffic_ops_golang/deliveryservice/servers/servers_test.go
@@ -18,5 +18,3 @@ package servers
  * specific language governing permissions and limitations
  * under the License.
  */
-
-
diff --git a/traffic_ops/traffic_ops_golang/deliveryservices_keys.go b/traffic_ops/traffic_ops_golang/deliveryservices_keys.go
index 9c165d8..50d4f5f 100644
--- a/traffic_ops/traffic_ops_golang/deliveryservices_keys.go
+++ b/traffic_ops/traffic_ops_golang/deliveryservices_keys.go
@@ -28,20 +28,13 @@ import (
 	"encoding/pem"
 	"errors"
 	"fmt"
-	"io/ioutil"
 	"net/http"
 	"strings"
 
-	"github.com/apache/trafficcontrol/lib/go-log"
 	"github.com/apache/trafficcontrol/lib/go-tc"
 	"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/config"
-	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/riaksvc"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/tenant"
-
-	"github.com/jmoiron/sqlx"
 )
 
 // Delivery Services: SSL Keys.
@@ -76,37 +69,6 @@ WHERE r.pattern = $2
 	return xmlID, true, nil
 }
 
-func getDeliveryServiceSSLKeysByXMLID(xmlID string, version string, tx *sql.Tx, cfg config.Config) ([]byte, error) {
-	if cfg.RiakEnabled == false {
-		err := errors.New("Riak is not configured!")
-		log.Errorln("getting delivery services SSL keys: " + err.Error())
-		return nil, err
-	}
-	key, ok, err := riaksvc.GetDeliveryServiceSSLKeysObj(xmlID, version, tx, cfg.RiakAuthOptions)
-	if err != nil {
-		log.Errorln("getting delivery service keys: " + err.Error())
-		return nil, err
-	}
-	if !ok {
-		alert := tc.CreateAlerts(tc.InfoLevel, "no object found for the specified key")
-		respBytes, err := json.Marshal(alert)
-		if err != nil {
-			log.Errorf("failed to marshal an alert response: %s\n", err)
-			return nil, err
-		}
-		return respBytes, nil
-	}
-
-	respBytes := []byte{}
-	resp := tc.DeliveryServiceSSLKeysResponse{Response: key}
-	respBytes, err = json.Marshal(resp)
-	if err != nil {
-		log.Errorf("failed to marshal a sslkeys response: %s\n", err)
-		return nil, err
-	}
-	return respBytes, nil
-}
-
 // verify the server certificate chain and return the
 // certificate and its chain in the proper order. Returns a  verified,
 // ordered, and base64 encoded certificate and CA chain.
@@ -192,259 +154,134 @@ func verifyAndEncodeCertificate(certificate string, rootCA string) (string, erro
 	return base64EncodedStr, nil
 }
 
-func addDeliveryServiceSSLKeysHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
-	return func(w http.ResponseWriter, r *http.Request) {
-		handleErr := tc.GetHandleErrorsFunc(w, r)
-		if !cfg.RiakEnabled {
-			err := errors.New("Riak is not configured!")
-			log.Errorln("adding Riak SSL keys for delivery service: " + err.Error())
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		defer r.Body.Close()
-
-		ctx := r.Context()
-		user, err := auth.GetCurrentUser(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		data, err := ioutil.ReadAll(r.Body)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		keysObj := tc.DeliveryServiceSSLKeys{}
-		if err := json.Unmarshal(data, &keysObj); err != nil {
-			log.Errorf("ERROR: could not unmarshal the request, %v\n", err)
-			handleErr(http.StatusBadRequest, err)
-			return
-		}
-
-		tx, err := db.DB.Begin()
-		if err != nil {
-			api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("beginning transaction: "+err.Error()))
-			return
-		}
-		commitTx := false
-		defer dbhelpers.FinishTx(tx, &commitTx)
-
-		// check user tenancy access to this resource.
-		hasAccess, err, apiStatus := tenant.HasTenant(user, keysObj.DeliveryService, tx)
-		if !hasAccess {
-			switch apiStatus {
-			case tc.SystemError:
-				handleErr(http.StatusInternalServerError, err)
-				return
-			case tc.DataMissingError:
-				handleErr(http.StatusBadRequest, err)
-				return
-			case tc.ForbiddenError:
-				handleErr(http.StatusForbidden, err)
-				return
-			}
-		}
-
-		var certChain string
-		if certChain, err = verifyAndEncodeCertificate(keysObj.Certificate.Crt, ""); err != nil {
-			log.Errorf("ERROR: could not unmarshal the request, %v\n", err)
-			handleErr(http.StatusBadRequest, err)
-			return
-		}
-		keysObj.Certificate.Crt = certChain
-
-		// marshal the keysObj
-		keysJSON, err := json.Marshal(&keysObj)
-		if err != nil {
-			log.Errorf("ERROR: could not marshal the keys object, %v\n", err)
-			handleErr(http.StatusBadRequest, err)
-			return
-		}
-
-		if err := riaksvc.PutDeliveryServiceSSLKeysObj(keysObj, tx, cfg.RiakAuthOptions); err != nil {
-			log.Errorln("putting Riak SSL keys for delivery service '" + keysObj.DeliveryService + "': " + err.Error())
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		commitTx = true
-		w.Header().Set("Content-Type", "application/json")
-		w.Write(keysJSON)
+func addDeliveryServiceSSLKeysHandler(w http.ResponseWriter, r *http.Request) {
+	inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
+	defer inf.Close()
+	if !inf.Config.RiakEnabled {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("adding Riak SSL keys for delivery service:: riak is not configured"))
+		return
+	}
+	keysObj := tc.DeliveryServiceSSLKeys{}
+	if err := json.NewDecoder(r.Body).Decode(&keysObj); err != nil {
+		api.HandleErr(w, r, http.StatusBadRequest, errors.New("malformed JSON"), nil)
+		return
+	}
+	if userErr, sysErr, errCode := tenant.Check(inf.User, keysObj.DeliveryService, inf.Tx.Tx); userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
+	certChain, err := verifyAndEncodeCertificate(keysObj.Certificate.Crt, "")
+	if err != nil {
+		api.HandleErr(w, r, http.StatusBadRequest, errors.New("verifying certificate: "+err.Error()), nil)
+		return
 	}
+	keysObj.Certificate.Crt = certChain
+	if err := riaksvc.PutDeliveryServiceSSLKeysObj(keysObj, inf.Tx.Tx, inf.Config.RiakAuthOptions); err != nil {
+		api.HandleErr(w, r, http.StatusBadRequest, nil, errors.New("putting Riak SSL keys for delivery service '"+keysObj.DeliveryService+"': "+err.Error()))
+		return
+	}
+	*inf.CommitTx = true
+	api.WriteRespRaw(w, r, keysObj)
 }
 
 // fetch the ssl keys for a deliveryservice specified by the fully qualified hostname
-func getDeliveryServiceSSLKeysByHostNameHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
-	return func(w http.ResponseWriter, r *http.Request) {
-		handleErr := tc.GetHandleErrorsFunc(w, r)
-		var respBytes []byte
-		var domainName string
-		var hostName string
-		var hostRegex string
-
-		if cfg.RiakEnabled == false {
-			handleErr(http.StatusServiceUnavailable, fmt.Errorf("The RIAK service is unavailable"))
-			return
-		}
-
-		version := r.URL.Query().Get("version")
-
-		ctx := r.Context()
-		user, err := auth.GetCurrentUser(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		pathParams, err := api.GetPathParams(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		hostName = pathParams["hostName"]
+func getDeliveryServiceSSLKeysByHostNameHandler(w http.ResponseWriter, r *http.Request) {
+	inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"hostName"}, nil)
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
+	defer inf.Close()
 
-		strArr := strings.Split(hostName, ".")
-		ln := len(strArr)
+	if inf.Config.RiakEnabled == false {
+		api.HandleErr(w, r, http.StatusServiceUnavailable, errors.New("The RIAK service is unavailable"), errors.New("getting Riak SSL keys by host name: riak is not configured"))
+		return
+	}
 
-		if ln > 1 {
-			for i := 2; i < ln-1; i++ {
-				domainName += strArr[i] + "."
-			}
-			domainName += strArr[ln-1]
-			hostRegex = ".*\\." + strArr[1] + "\\..*"
-		}
+	version := inf.Params["version"]
+	hostName := inf.Params["hostName"]
+	domainName := ""
+	hostRegex := ""
+	strArr := strings.Split(hostName, ".")
+	ln := len(strArr)
+	if ln > 1 {
+		for i := 2; i < ln-1; i++ {
+			domainName += strArr[i] + "."
+		}
+		domainName += strArr[ln-1]
+		hostRegex = ".*\\." + strArr[1] + "\\..*"
+	}
 
-		tx, err := db.DB.Begin()
-		if err != nil {
-			api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("beginning transaction: "+err.Error()))
-			return
-		}
-		commitTx := false
-		defer dbhelpers.FinishTx(tx, &commitTx)
+	// lookup the cdnID
+	cdnID, ok, err := getCDNIDByDomainname(domainName, inf.Tx.Tx)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("getting cdn id by domain name: "+err.Error()))
+		return
+	}
+	if !ok {
+		api.WriteRespAlert(w, r, tc.InfoLevel, " - a cdn does not exist for the domain: "+domainName+" parsed from hostname: "+hostName)
+		return
+	}
+	// now lookup the deliveryservice xmlID
+	xmlID, ok, err := getXMLID(cdnID, hostRegex, inf.Tx.Tx)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("getting xml id: "+err.Error()))
+		return
+	}
+	if !ok {
+		api.WriteRespAlert(w, r, tc.InfoLevel, "  - a delivery service does not exist for a host with hostname of "+hostName)
+		return
+	}
 
-		// lookup the cdnID
-		cdnID, ok, err := getCDNIDByDomainname(domainName, tx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		if !ok {
-			alert := tc.CreateAlerts(tc.InfoLevel, fmt.Sprintf(" - a cdn does not exist for the domain: %s parsed from hostname: %s",
-				domainName, hostName))
-			respBytes, err = json.Marshal(alert)
-			if err != nil {
-				log.Errorf("failed to marshal an alert response: %s\n", err)
-				return
-			}
-			w.Header().Set("Content-Type", "application/json")
-			w.Write(respBytes)
-		}
-		// now lookup the deliveryservice xmlID
-		xmlID, ok, err := getXMLID(cdnID, hostRegex, tx)
-		if err != nil {
-			api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("getting xml id: "+err.Error()))
-			return
-		}
-		if !ok {
-			alert := tc.CreateAlerts(tc.InfoLevel, fmt.Sprintf("  - a delivery service does not exist for a host with hostname of %s",
-				hostName))
-			respBytes, err = json.Marshal(alert)
-			if err != nil {
-				log.Errorf("failed to marshal an alert response: %s\n", err)
-				handleErr(http.StatusInternalServerError, err)
-				return
-			}
-			w.Header().Set("Content-Type", "application/json")
-			w.Write(respBytes)
-		}
+	if userErr, sysErr, errCode := tenant.Check(inf.User, xmlID, inf.Tx.Tx); userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
 
-		// check user tenancy access to this resource.
-		hasAccess, err, apiStatus := tenant.HasTenant(user, xmlID, tx)
-		if !hasAccess {
-			switch apiStatus {
-			case tc.SystemError:
-				handleErr(http.StatusInternalServerError, err)
-				return
-			case tc.DataMissingError:
-				handleErr(http.StatusBadRequest, err)
-				return
-			case tc.ForbiddenError:
-				handleErr(http.StatusForbidden, err)
-				return
-			}
-		}
-		respBytes, err = getDeliveryServiceSSLKeysByXMLID(xmlID, version, tx, cfg)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		commitTx = true
-		w.Header().Set("Content-Type", "application/json")
-		w.Write(respBytes)
+	keyObj, ok, err := riaksvc.GetDeliveryServiceSSLKeysObj(xmlID, version, inf.Tx.Tx, inf.Config.RiakAuthOptions)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("getting ssl keys: "+err.Error()))
+		return
 	}
+	if !ok {
+		api.WriteRespAlert(w, r, tc.InfoLevel, "no object found for the specified key")
+		return
+	}
+	*inf.CommitTx = true
+	api.WriteResp(w, r, keyObj)
 }
 
-// fetch the deliveryservice ssl keys by the specified xmlID.
-func getDeliveryServiceSSLKeysByXMLIDHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
-	return func(w http.ResponseWriter, r *http.Request) {
-		handleErr := tc.GetHandleErrorsFunc(w, r)
-		var respBytes []byte
-
-		if cfg.RiakEnabled == false {
-			handleErr(http.StatusServiceUnavailable, fmt.Errorf("The RIAK service is unavailable"))
-			return
-		}
-
-		version := r.URL.Query().Get("version")
-
-		ctx := r.Context()
-		user, err := auth.GetCurrentUser(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		pathParams, err := api.GetPathParams(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		xmlID := pathParams["xmlID"]
-
-		tx, err := db.DB.Begin()
-		if err != nil {
-			api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("beginning transaction: "+err.Error()))
-			return
-		}
-		commitTx := false
-		defer dbhelpers.FinishTx(tx, &commitTx)
-
-		// check user tenancy access to this resource.
-		hasAccess, err, apiStatus := tenant.HasTenant(user, xmlID, tx)
-		if !hasAccess {
-			switch apiStatus {
-			case tc.SystemError:
-				handleErr(http.StatusInternalServerError, err)
-				return
-			case tc.DataMissingError:
-				handleErr(http.StatusBadRequest, err)
-				return
-			case tc.ForbiddenError:
-				handleErr(http.StatusForbidden, err)
-				return
-			}
-		}
-
-		respBytes, err = getDeliveryServiceSSLKeysByXMLID(xmlID, version, tx, cfg)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		w.Header().Set("Content-Type", "application/json")
-		commitTx = true
-		w.Write(respBytes)
+// getDeliveryServiceSSLKeysByXMLIDHandler fetches the deliveryservice ssl keys by the specified xmlID.
+func getDeliveryServiceSSLKeysByXMLIDHandler(w http.ResponseWriter, r *http.Request) {
+	inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"xmlID"}, nil)
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
+	defer inf.Close()
+	if inf.Config.RiakEnabled == false {
+		api.HandleErr(w, r, http.StatusServiceUnavailable, errors.New("The RIAK service is unavailable"), errors.New("getting Riak SSL keys by xml id: riak is not configured"))
+		return
+	}
+	version := inf.Params["version"]
+	xmlID := inf.Params["xmlID"]
+	if userErr, sysErr, errCode := tenant.Check(inf.User, xmlID, inf.Tx.Tx); userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
+	keyObj, ok, err := riaksvc.GetDeliveryServiceSSLKeysObj(xmlID, version, inf.Tx.Tx, inf.Config.RiakAuthOptions)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("getting ssl keys: "+err.Error()))
+		return
+	}
+	if !ok {
+		api.WriteRespAlert(w, r, tc.InfoLevel, "no object found for the specified key")
+		return
 	}
+	*inf.CommitTx = true
+	api.WriteResp(w, r, keyObj)
 }
diff --git a/traffic_ops/traffic_ops_golang/profileparameter/profile_parameters.go b/traffic_ops/traffic_ops_golang/profileparameter/profile_parameters.go
index 34bd4c6..9920228 100644
--- a/traffic_ops/traffic_ops_golang/profileparameter/profile_parameters.go
+++ b/traffic_ops/traffic_ops_golang/profileparameter/profile_parameters.go
@@ -25,7 +25,7 @@ import (
 	"strconv"
 
 	"github.com/apache/trafficcontrol/lib/go-log"
-	tc "github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/lib/go-tc"
 	"github.com/apache/trafficcontrol/lib/go-tc/tovalidate"
 	"github.com/apache/trafficcontrol/lib/go-tc/v13"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
diff --git a/traffic_ops/traffic_ops_golang/riaksvc/riak_services.go b/traffic_ops/traffic_ops_golang/riaksvc/riak_services.go
index d8c289b..fd8a699 100644
--- a/traffic_ops/traffic_ops_golang/riaksvc/riak_services.go
+++ b/traffic_ops/traffic_ops_golang/riaksvc/riak_services.go
@@ -324,3 +324,22 @@ func WithClusterTx(tx *sql.Tx, authOpts *riak.AuthOptions, f func(StorageCluster
 	}()
 	return f(cluster)
 }
+
+// StartCluster gets and starts a riak cluster, returning an error if either getting or starting fails.
+func StartCluster(tx *sql.Tx, authOptions *riak.AuthOptions) (StorageCluster, error) {
+	cluster, err := GetRiakClusterTx(tx, authOptions)
+	if err != nil {
+		return nil, errors.New("getting cluster: " + err.Error())
+	}
+	if err = cluster.Start(); err != nil {
+		return nil, errors.New("starting cluster: " + err.Error())
+	}
+	return cluster, nil
+}
+
+// StopCluster stops the cluster, logging any error rather than returning it. This is designed to be called in a defer.
+func StopCluster(c StorageCluster) {
+	if err := c.Stop(); err != nil {
+		log.Errorln("stopping riak cluster: " + err.Error())
+	}
+}
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index 0296172..341b88a 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -237,10 +237,10 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
 		{1.3, http.MethodDelete, `deliveryservice_request_comments/?$`, api.DeleteHandler(comment.GetRefType(), d.DB), auth.PrivLevelPortal, Authenticated, nil},
 
 		//Delivery service uri signing keys: CRUD
-		{1.3, http.MethodGet, `deliveryservices/{xmlID}/urisignkeys$`, getURIsignkeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
-		{1.3, http.MethodPost, `deliveryservices/{xmlID}/urisignkeys$`, saveDeliveryServiceURIKeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
-		{1.3, http.MethodPut, `deliveryservices/{xmlID}/urisignkeys$`, saveDeliveryServiceURIKeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
-		{1.3, http.MethodDelete, `deliveryservices/{xmlID}/urisignkeys$`, removeDeliveryServiceURIKeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
+		{1.3, http.MethodGet, `deliveryservices/{xmlID}/urisignkeys$`, getURIsignkeysHandler, auth.PrivLevelAdmin, Authenticated, nil},
+		{1.3, http.MethodPost, `deliveryservices/{xmlID}/urisignkeys$`, saveDeliveryServiceURIKeysHandler, auth.PrivLevelAdmin, Authenticated, nil},
+		{1.3, http.MethodPut, `deliveryservices/{xmlID}/urisignkeys$`, saveDeliveryServiceURIKeysHandler, auth.PrivLevelAdmin, Authenticated, nil},
+		{1.3, http.MethodDelete, `deliveryservices/{xmlID}/urisignkeys$`, removeDeliveryServiceURIKeysHandler, auth.PrivLevelAdmin, Authenticated, nil},
 
 		//Origins
 		{1.3, http.MethodGet, `origins/?(\.json)?$`, api.ReadHandler(origin.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
@@ -289,9 +289,9 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
 		{1.1, http.MethodPut, `snapshot/{cdn}/?$`, crconfig.SnapshotHandler(d.DB, d.Config), crconfig.PrivLevel, Authenticated, nil},
 
 		//SSLKeys deliveryservice endpoints here that are marked  marked as '-wip' need to have tenancy checks added
-		{1.3, http.MethodGet, `deliveryservices-wip/xmlId/{xmlID}/sslkeys$`, getDeliveryServiceSSLKeysByXMLIDHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
-		{1.3, http.MethodGet, `deliveryservices-wip/hostname/{hostName}/sslkeys$`, getDeliveryServiceSSLKeysByHostNameHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
-		{1.3, http.MethodPost, `deliveryservices-wip/hostname/{hostName}/sslkeys/add$`, addDeliveryServiceSSLKeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
+		{1.3, http.MethodGet, `deliveryservices-wip/xmlId/{xmlID}/sslkeys$`, getDeliveryServiceSSLKeysByXMLIDHandler, auth.PrivLevelAdmin, Authenticated, nil},
+		{1.3, http.MethodGet, `deliveryservices-wip/hostname/{hostName}/sslkeys$`, getDeliveryServiceSSLKeysByHostNameHandler, auth.PrivLevelAdmin, Authenticated, nil},
+		{1.3, http.MethodPost, `deliveryservices-wip/hostname/{hostName}/sslkeys/add$`, addDeliveryServiceSSLKeysHandler, auth.PrivLevelAdmin, Authenticated, nil},
 
 		//DeliveryServices
 		{1.3, http.MethodGet, `deliveryservices/?(\.json)?$`, api.ReadHandler(deliveryservice.GetRefTypeV13(d.Config, d.DB), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
diff --git a/traffic_ops/traffic_ops_golang/tenant/tenancy.go b/traffic_ops/traffic_ops_golang/tenant/tenancy.go
index 8987f4e..177a32d 100644
--- a/traffic_ops/traffic_ops_golang/tenant/tenancy.go
+++ b/traffic_ops/traffic_ops_golang/tenant/tenancy.go
@@ -23,6 +23,7 @@ import (
 	"database/sql"
 	"errors"
 	"fmt"
+	"net/http"
 
 	"github.com/apache/trafficcontrol/lib/go-log"
 	"github.com/apache/trafficcontrol/lib/go-tc"
@@ -61,24 +62,24 @@ func GetDeliveryServiceTenantInfo(xmlID string, tx *sql.Tx) (*DeliveryServiceTen
 	return &ds, nil
 }
 
-// tenancy check wrapper for deliveryservice
-func HasTenant(user *auth.CurrentUser, XMLID string, tx *sql.Tx) (bool, error, tc.ApiErrorType) {
+// Check checks that the given user has access to the given XMLID. Returns a user error, system error, and the HTTP status code to be returned to the user if an error occurred. On success, the user error and system error will both be nil, and the error code should be ignored.
+func Check(user *auth.CurrentUser, XMLID string, tx *sql.Tx) (error, error, int) {
 	dsInfo, err := GetDeliveryServiceTenantInfo(XMLID, tx)
 	if err != nil {
 		if dsInfo == nil {
-			return false, fmt.Errorf("deliveryservice lookup failure: %v", err), tc.SystemError
+			return nil, errors.New("deliveryservice lookup failure: " + err.Error()), http.StatusInternalServerError
 		} else {
-			return false, fmt.Errorf("no such deliveryservice: '%s'", XMLID), tc.DataMissingError
+			return errors.New("no such deliveryservice: '" + XMLID + "'"), nil, http.StatusBadRequest
 		}
 	}
 	hasAccess, err := dsInfo.IsTenantAuthorized(user, tx)
 	if err != nil {
-		return false, fmt.Errorf("user tenancy check failure: %v", err), tc.SystemError
+		return nil, errors.New("user tenancy check failure: " + err.Error()), http.StatusInternalServerError
 	}
 	if !hasAccess {
-		return false, fmt.Errorf("Access to this resource is not authorized"), tc.ForbiddenError
+		return nil, errors.New("Access to this resource is not authorized"), http.StatusForbidden
 	}
-	return true, nil, tc.NoError
+	return nil, nil, http.StatusOK
 }
 
 // returns a Tenant list that the specified user has access too.
diff --git a/traffic_ops/traffic_ops_golang/urisigning.go b/traffic_ops/traffic_ops_golang/urisigning.go
index e53a3ad..29df797 100644
--- a/traffic_ops/traffic_ops_golang/urisigning.go
+++ b/traffic_ops/traffic_ops_golang/urisigning.go
@@ -22,22 +22,16 @@ package main
 import (
 	"encoding/json"
 	"errors"
-	"fmt"
 	"io/ioutil"
 	"net/http"
 	"strings"
 
-	"github.com/apache/trafficcontrol/lib/go-log"
 	"github.com/apache/trafficcontrol/lib/go-tc"
 	"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/config"
-	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/riaksvc"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/tenant"
 
 	"github.com/basho/riak-go-client"
-	"github.com/jmoiron/sqlx"
 	"github.com/lestrrat/go-jwx/jwk"
 )
 
@@ -51,301 +45,152 @@ type URISignerKeyset struct {
 }
 
 // endpoint handler for fetching uri signing keys from riak
-func getURIsignkeysHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
-	return func(w http.ResponseWriter, r *http.Request) {
-		handleErr := tc.GetHandleErrorsFunc(w, r)
-
-		if cfg.RiakEnabled == false {
-			handleErr(http.StatusServiceUnavailable, fmt.Errorf("The RIAK service is unavailable"))
-			return
-		}
-
-		ctx := r.Context()
-		pathParams, err := api.GetPathParams(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		user, err := auth.GetCurrentUser(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		xmlID := pathParams["xmlID"]
+func getURIsignkeysHandler(w http.ResponseWriter, r *http.Request) {
+	inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
+	defer inf.Close()
 
-		tx, err := db.DB.Begin()
-		if err != nil {
-			api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("beginning transaction: "+err.Error()))
-			return
-		}
-		commitTx := false
-		defer dbhelpers.FinishTx(tx, &commitTx)
-
-		// check user tenancy access to this resource.
-		hasAccess, err, apiStatus := tenant.HasTenant(user, xmlID, tx)
-		if !hasAccess {
-			switch apiStatus {
-			case tc.SystemError:
-				handleErr(http.StatusInternalServerError, err)
-				return
-			case tc.DataMissingError:
-				handleErr(http.StatusBadRequest, err)
-				return
-			case tc.ForbiddenError:
-				handleErr(http.StatusForbidden, err)
-				return
-			}
-		}
+	if inf.Config.RiakEnabled == false {
+		api.HandleErr(w, r, http.StatusServiceUnavailable, errors.New("The RIAK service is unavailable"), errors.New("getting Riak SSL keys by host name: riak is not configured"))
+		return
+	}
 
-		// create and start a cluster
-		cluster, err := riaksvc.GetRiakCluster(db.DB, cfg.RiakAuthOptions)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		if err = cluster.Start(); err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		defer func() {
-			if err := cluster.Stop(); err != nil {
-				log.Errorf("%v\n", err)
-			}
-		}()
+	xmlID := inf.Params["xmlID"]
 
-		ro, err := riaksvc.FetchObjectValues(xmlID, CDNURIKeysBucket, cluster)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
+	if userErr, sysErr, errCode := tenant.Check(inf.User, xmlID, inf.Tx.Tx); userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
 
-		var respBytes []byte
+	cluster, err := riaksvc.StartCluster(inf.Tx.Tx, inf.Config.RiakAuthOptions)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("starting riak cluster: "+err.Error()))
+		return
+	}
+	defer riaksvc.StopCluster(cluster)
 
-		if ro == nil {
-			var empty URISignerKeyset
-			respBytes, err = json.Marshal(empty)
-			if err != nil {
-				log.Errorf("failed to marshal an empty response: %s\n", err)
-				w.WriteHeader(http.StatusInternalServerError)
-				fmt.Fprintf(w, http.StatusText(http.StatusInternalServerError))
-				return
-			}
-		} else {
-			respBytes = ro[0].Value
-		}
-		commitTx = true
-		w.Header().Set("Content-Type", "application/json")
-		w.Write(respBytes)
+	ro, err := riaksvc.FetchObjectValues(xmlID, CDNURIKeysBucket, cluster)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("fetching riak objects: "+err.Error()))
+		return
+	}
+	if len(ro) == 0 {
+		api.WriteRespRaw(w, r, URISignerKeyset{})
+		return
 	}
+	*inf.CommitTx = true
+	w.Header().Set("Content-Type", "application/json")
+	w.Write(ro[0].Value)
 }
 
-// Http DELETE handler used to remove urisigning keys assigned to a delivery service.
-func removeDeliveryServiceURIKeysHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
-	return func(w http.ResponseWriter, r *http.Request) {
-		handleErr := tc.GetHandleErrorsFunc(w, r)
-
-		if cfg.RiakEnabled == false {
-			handleErr(http.StatusServiceUnavailable, fmt.Errorf("The RIAK service is unavailable"))
-			return
-		}
-
-		ctx := r.Context()
-		pathParams, err := api.GetPathParams(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		user, err := auth.GetCurrentUser(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		xmlID := pathParams["xmlID"]
-
-		tx, err := db.DB.Begin()
-		if err != nil {
-			api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("beginning transaction: "+err.Error()))
-			return
-		}
-		commitTx := false
-		defer dbhelpers.FinishTx(tx, &commitTx)
-
-		// check user tenancy access to this resource.
-		hasAccess, err, apiStatus := tenant.HasTenant(user, xmlID, tx)
-		if !hasAccess {
-			switch apiStatus {
-			case tc.SystemError:
-				handleErr(http.StatusInternalServerError, err)
-				return
-			case tc.DataMissingError:
-				handleErr(http.StatusBadRequest, err)
-				return
-			case tc.ForbiddenError:
-				handleErr(http.StatusForbidden, err)
-				return
-			}
-		}
+// removeDeliveryServiceURIKeysHandler is the HTTP DELETE handler used to remove urisigning keys assigned to a delivery service.
+func removeDeliveryServiceURIKeysHandler(w http.ResponseWriter, r *http.Request) {
+	inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
+	defer inf.Close()
 
-		// create and start a cluster
-		cluster, err := riaksvc.GetRiakCluster(db.DB, cfg.RiakAuthOptions)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		if err = cluster.Start(); err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		defer func() {
-			if err := cluster.Stop(); err != nil {
-				log.Errorf("%v\n", err)
-			}
-		}()
+	if inf.Config.RiakEnabled == false {
+		api.HandleErr(w, r, http.StatusServiceUnavailable, errors.New("The RIAK service is unavailable"), errors.New("getting Riak SSL keys by host name: riak is not configured"))
+		return
+	}
 
-		ro, err := riaksvc.FetchObjectValues(xmlID, CDNURIKeysBucket, cluster)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
+	xmlID := inf.Params["xmlID"]
+	if userErr, sysErr, errCode := tenant.Check(inf.User, xmlID, inf.Tx.Tx); userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
 
-		// fetch the object and delete it if it exists.
-		var alert tc.Alerts
+	cluster, err := riaksvc.StartCluster(inf.Tx.Tx, inf.Config.RiakAuthOptions)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("starting riak cluster: "+err.Error()))
+		return
+	}
+	defer riaksvc.StopCluster(cluster)
 
-		if ro == nil || ro[0].Value == nil {
-			alert = tc.CreateAlerts(tc.InfoLevel, "not deleted, no object found to delete")
-		} else if err := riaksvc.DeleteObject(xmlID, CDNURIKeysBucket, cluster); err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		} else { // object successfully deleted
-			alert = tc.CreateAlerts(tc.SuccessLevel, "object deleted")
-		}
+	ro, err := riaksvc.FetchObjectValues(xmlID, CDNURIKeysBucket, cluster)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("fetching riak objects: "+err.Error()))
+		return
+	}
 
-		// send response
-		respBytes, err := json.Marshal(alert)
-		if err != nil {
-			log.Errorf("failed to marshal an alert response: %s\n", err)
-			w.WriteHeader(http.StatusInternalServerError)
-			fmt.Fprintf(w, http.StatusText(http.StatusInternalServerError))
-			return
-		}
-		commitTx = true
-		w.Header().Set("Content-Type", "application/json")
-		w.Write(respBytes)
+	if len(ro) == 0 || ro[0].Value == nil {
+		api.WriteRespAlert(w, r, tc.InfoLevel, "not deleted, no object found to delete")
+		return
 	}
+	if err := riaksvc.DeleteObject(xmlID, CDNURIKeysBucket, cluster); err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("deleting riak object: "+err.Error()))
+		return
+	}
+	*inf.CommitTx = true
+	api.WriteRespAlert(w, r, tc.SuccessLevel, "object deleted")
+	return
 }
 
-// Http POST or PUT handler used to store urisigning keys to a delivery service.
-func saveDeliveryServiceURIKeysHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
-	return func(w http.ResponseWriter, r *http.Request) {
-		handleErr := tc.GetHandleErrorsFunc(w, r)
-
-		defer r.Body.Close()
-
-		if cfg.RiakEnabled == false {
-			handleErr(http.StatusServiceUnavailable, fmt.Errorf("The RIAK service is unavailable"))
-			return
-		}
-
-		ctx := r.Context()
-		pathParams, err := api.GetPathParams(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		user, err := auth.GetCurrentUser(ctx)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-
-		xmlID := pathParams["xmlID"]
+// saveDeliveryServiceURIKeysHandler is the HTTP POST or PUT handler used to store urisigning keys to a delivery service.
+func saveDeliveryServiceURIKeysHandler(w http.ResponseWriter, r *http.Request) {
+	inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
+	defer inf.Close()
 
-		tx, err := db.DB.Begin()
-		if err != nil {
-			api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("beginning transaction: "+err.Error()))
-			return
-		}
-		commitTx := false
-		defer dbhelpers.FinishTx(tx, &commitTx)
-
-		// check user tenancy access to this resource.
-		hasAccess, err, apiStatus := tenant.HasTenant(user, xmlID, tx)
-		if !hasAccess {
-			switch apiStatus {
-			case tc.SystemError:
-				handleErr(http.StatusInternalServerError, err)
-				return
-			case tc.DataMissingError:
-				handleErr(http.StatusBadRequest, err)
-				return
-			case tc.ForbiddenError:
-				handleErr(http.StatusForbidden, err)
-				return
-			}
-		}
+	if inf.Config.RiakEnabled == false {
+		api.HandleErr(w, r, http.StatusServiceUnavailable, errors.New("The RIAK service is unavailable"), errors.New("getting Riak SSL keys by host name: riak is not configured"))
+		return
+	}
 
-		data, err := ioutil.ReadAll(r.Body)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
+	xmlID := inf.Params["xmlID"]
 
-		// validate that the received data is a valid jwk keyset
-		var keySet map[string]URISignerKeyset
-		if err := json.Unmarshal(data, &keySet); err != nil {
-			log.Errorf("%v\n", err)
-			handleErr(http.StatusBadRequest, err)
-			return
-		}
-		if err := validateURIKeyset(keySet); err != nil {
-			log.Errorf("%v\n", err)
-			handleErr(http.StatusBadRequest, err)
-			return
-		}
+	if userErr, sysErr, errCode := tenant.Check(inf.User, xmlID, inf.Tx.Tx); userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, errCode, userErr, sysErr)
+		return
+	}
 
-		// create and start a cluster
-		cluster, err := riaksvc.GetRiakCluster(db.DB, cfg.RiakAuthOptions)
-		if err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		if err = cluster.Start(); err != nil {
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
-		defer func() {
-			if err := cluster.Stop(); err != nil {
-				log.Errorf("%v\n", err)
-			}
-		}()
-
-		// create a storage object and store the data
-		obj := &riak.Object{
-			ContentType:     "text/json",
-			Charset:         "utf-8",
-			ContentEncoding: "utf-8",
-			Key:             xmlID,
-			Value:           []byte(data),
-		}
+	data, err := ioutil.ReadAll(r.Body)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, errors.New("failed to read body"), errors.New("failed to read body: "+err.Error()))
+		return
+	}
+	keySet := map[string]URISignerKeyset{}
+	if err := json.Unmarshal(data, &keySet); err != nil {
+		api.HandleErr(w, r, http.StatusBadRequest, errors.New("malformed JSON"), nil)
+	}
+	if err := validateURIKeyset(keySet); err != nil {
+		api.HandleErr(w, r, http.StatusBadRequest, errors.New("invalid keyset: "+err.Error()), nil)
+		return
+	}
 
-		err = riaksvc.SaveObject(obj, CDNURIKeysBucket, cluster)
-		if err != nil {
-			log.Errorf("%v\n", err)
-			handleErr(http.StatusInternalServerError, err)
-			return
-		}
+	cluster, err := riaksvc.StartCluster(inf.Tx.Tx, inf.Config.RiakAuthOptions)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("starting riak cluster: "+err.Error()))
+		return
+	}
+	defer riaksvc.StopCluster(cluster)
+
+	obj := &riak.Object{
+		ContentType:     "text/json",
+		Charset:         "utf-8",
+		ContentEncoding: "utf-8",
+		Key:             xmlID,
+		Value:           []byte(data),
+	}
 
-		w.Header().Set("Content-Type", "application/json")
-		w.Write(data)
+	if err = riaksvc.SaveObject(obj, CDNURIKeysBucket, cluster); err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("saving riak object: "+err.Error()))
+		return
 	}
+	w.Header().Set("Content-Type", tc.ApplicationJson)
+	w.Write(data)
 }
 
-// validates URISigingKeyset json.
+// validateURIKeyset validates URISigingKeyset json.
 func validateURIKeyset(msg map[string]URISignerKeyset) error {
 	var renewalKidFound int
 	var renewalKidMatched = false