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

[trafficcontrol] branch master updated (c05a699 -> 5019995)

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

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


    from c05a699  properly cast tenantID from json to bigint for tenancy check in deliveryservice_request
     new 26a0cc8  Add TO Go users/current
     new 91f4ed1  Add TO Go user/current client funcs, test
     new 5019995  Fix TO Go missing wrapAccessLog

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 lib/go-tc/users.go                                 | 30 ++++++++++
 traffic_ops/client/v13/user.go                     | 13 ++++-
 .../api/v13/{cdn_domains_test.go => user_test.go}  | 44 ++++++++++----
 traffic_ops/traffic_ops_golang/routes.go           |  5 +-
 .../postparameterprofile.go => user/current.go}    | 67 +++++++++++++---------
 5 files changed, 120 insertions(+), 39 deletions(-)
 copy traffic_ops/testing/api/v13/{cdn_domains_test.go => user_test.go} (50%)
 copy traffic_ops/traffic_ops_golang/{profileparameter/postparameterprofile.go => user/current.go} (50%)


[trafficcontrol] 02/03: Add TO Go user/current client funcs, test

Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 91f4ed116571136a6333bcfba6abecd61e708013
Author: Robert Butts <ro...@apache.org>
AuthorDate: Fri Jun 29 15:43:42 2018 -0600

    Add TO Go user/current client funcs, test
---
 lib/go-tc/users.go                       |  4 ++
 traffic_ops/client/v13/user.go           | 13 ++++++-
 traffic_ops/testing/api/v13/user_test.go | 64 ++++++++++++++++++++++++++++++++
 3 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/lib/go-tc/users.go b/lib/go-tc/users.go
index 43a2ee1..728908c 100644
--- a/lib/go-tc/users.go
+++ b/lib/go-tc/users.go
@@ -116,3 +116,7 @@ type UserCurrent struct {
 	UID             *int    `json:"uid,omitempty"`
 	UserName        *string `json:"username,omitempty"`
 }
+
+type UserCurrentResponse struct {
+	Response UserCurrent `json:"response"`
+}
diff --git a/traffic_ops/client/v13/user.go b/traffic_ops/client/v13/user.go
index ac8ff5b..0102498 100644
--- a/traffic_ops/client/v13/user.go
+++ b/traffic_ops/client/v13/user.go
@@ -18,7 +18,7 @@ package v13
 import (
 	"encoding/json"
 
-	tc "github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/lib/go-tc"
 )
 
 // Users gets an array of Users.
@@ -44,3 +44,14 @@ func (to *Session) GetUsers() ([]tc.User, ReqInf, error) {
 
 	return data.Response, reqInf, nil
 }
+
+// GetUserCurrent gets information about the current user
+func (to *Session) GetUserCurrent() (*tc.UserCurrent, ReqInf, error) {
+	url := apiBase + `/user/current`
+	resp := tc.UserCurrentResponse{}
+	reqInf, err := get(to, url, &resp)
+	if err != nil {
+		return nil, reqInf, err
+	}
+	return &resp.Response, reqInf, nil
+}
diff --git a/traffic_ops/testing/api/v13/user_test.go b/traffic_ops/testing/api/v13/user_test.go
new file mode 100644
index 0000000..abe8bbf
--- /dev/null
+++ b/traffic_ops/testing/api/v13/user_test.go
@@ -0,0 +1,64 @@
+package v13
+
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+	"testing"
+
+	"github.com/apache/trafficcontrol/lib/go-log"
+)
+
+func TestUsers(t *testing.T) {
+	CreateTestCDNs(t)
+	CreateTestTypes(t)
+	CreateTestProfiles(t)
+	CreateTestStatuses(t)
+	CreateTestDivisions(t)
+	CreateTestRegions(t)
+	CreateTestPhysLocations(t)
+	CreateTestCacheGroups(t)
+	CreateTestServers(t)
+	CreateTestDeliveryServices(t)
+
+	GetTestUserCurrent(t)
+
+	DeleteTestDeliveryServices(t)
+	DeleteTestServers(t)
+	DeleteTestCacheGroups(t)
+	DeleteTestPhysLocations(t)
+	DeleteTestRegions(t)
+	DeleteTestDivisions(t)
+	DeleteTestStatuses(t)
+	DeleteTestProfiles(t)
+	DeleteTestTypes(t)
+	DeleteTestCDNs(t)
+
+}
+
+const SessionUserName = "admin" // TODO make dynamic?
+
+func GetTestUserCurrent(t *testing.T) {
+	log.Debugln("GetTestUserCurrent")
+	user, _, err := TOSession.GetUserCurrent()
+	if err != nil {
+		t.Fatalf("cannot GET current user: %v\n", err)
+	}
+	if user.UserName == nil {
+		t.Fatalf("current user expected: %v actual: %v\n", SessionUserName, nil)
+	}
+	if *user.UserName != SessionUserName {
+		t.Fatalf("current user expected: %v actual: %v\n", SessionUserName, *user.UserName)
+	}
+}


[trafficcontrol] 01/03: Add TO Go users/current

Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 26a0cc89d304c58ed7ba9238d100845ac5f6c59e
Author: Robert Butts <ro...@apache.org>
AuthorDate: Tue Jun 12 16:26:32 2018 -0600

    Add TO Go users/current
---
 lib/go-tc/users.go                             | 26 ++++++++
 traffic_ops/traffic_ops_golang/routes.go       |  5 +-
 traffic_ops/traffic_ops_golang/user/current.go | 85 ++++++++++++++++++++++++++
 3 files changed, 115 insertions(+), 1 deletion(-)

diff --git a/lib/go-tc/users.go b/lib/go-tc/users.go
index ea56df6..43a2ee1 100644
--- a/lib/go-tc/users.go
+++ b/lib/go-tc/users.go
@@ -90,3 +90,29 @@ type APIUsersResponse struct {
 type UserDeliveryServiceDeleteResponse struct {
 	Alerts []Alert `json:"alerts"`
 }
+
+// UserCurrent contains all the user info about the current user, as returned by /api/1.x/user/current
+type UserCurrent struct {
+	AddressLine1    *string `json:"addressLine1"`
+	AddressLine2    *string `json:"addressLine2"`
+	City            *string `json:"city"`
+	Company         *string `json:"company,omitempty"`
+	Country         *string `json:"country"`
+	Email           *string `json:"email,omitempty"`
+	FullName        *string `json:"fullName,omitempty"`
+	GID             *int    `json:"gid,omitempty"`
+	ID              *int    `json:"id,omitempty"`
+	LastUpdated     *string `json:"lastUpdated,omitempty"`
+	LocalUser       *bool   `json:"localUser"`
+	NewUser         *bool   `json:"newUser,omitempty"`
+	PhoneNumber     *string `json:"phoneNumber"`
+	PostalCode      *string `json:"postalCode"`
+	PublicSSHKey    *string `json:"publicSshKey,omitempty"`
+	Role            *int    `json:"role,omitempty"`
+	RoleName        *string `json:"rolename,omitempty"`
+	StateOrProvince *string `json:"stateOrProvince"`
+	Tenant          *string `json:"tenant"`
+	TenantID        *uint64 `json:"tenantId"`
+	UID             *int    `json:"uid,omitempty"`
+	UserName        *string `json:"username,omitempty"`
+}
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index d1df3e8..b88aa27 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -143,7 +143,10 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
 		//Login
 		{1.1, http.MethodGet, `users/{id}/deliveryservices/?(\.json)?$`, user.GetDSes(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
 		{1.1, http.MethodGet, `user/{id}/deliveryservices/available/?(\.json)?$`, user.GetAvailableDSes(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
-		{1.2, http.MethodPost, `user/login/?$`, wrapAccessLog(d.Secrets[0], auth.LoginHandler(d.DB, d.Config)), 0, NoAuth, nil}, {1.3, http.MethodPost, `user/login/?$`, auth.LoginHandler(d.DB, d.Config), 0, NoAuth, nil},
+		{1.2, http.MethodPost, `user/login/?$`, wrapAccessLog(d.Secrets[0], auth.LoginHandler(d.DB, d.Config)), 0, NoAuth, nil},
+		{1.3, http.MethodPost, `user/login/?$`, auth.LoginHandler(d.DB, d.Config), 0, NoAuth, nil},
+
+		{1.1, http.MethodGet, `user/current/?(\.json)?$`, user.Current, auth.PrivLevelReadOnly, Authenticated, nil},
 
 		//Parameter: CRUD
 		{1.1, http.MethodGet, `parameters/?(\.json)?$`, api.ReadHandler(parameter.GetTypeSingleton()), auth.PrivLevelReadOnly, Authenticated, nil},
diff --git a/traffic_ops/traffic_ops_golang/user/current.go b/traffic_ops/traffic_ops_golang/user/current.go
new file mode 100644
index 0000000..440dc3b
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/user/current.go
@@ -0,0 +1,85 @@
+package user
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"database/sql"
+	"errors"
+	"net/http"
+
+	"github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/lib/go-util"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func Current(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()
+	currentUser, err := getUser(inf.Tx.Tx, inf.User.ID)
+	if err != nil {
+		api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("getting current user: "+err.Error()))
+		return
+	}
+	*inf.CommitTx = true
+	api.WriteResp(w, r, currentUser)
+}
+
+func getUser(tx *sql.Tx, id int) (tc.UserCurrent, error) {
+	q := `
+SELECT
+u.address_line1,
+u.address_line2,
+u.city,
+u.company,
+u.country,
+u.email,
+u.full_name,
+u.gid,
+u.id,
+u.last_updated,
+u.local_passwd,
+u.new_user,
+u.phone_number,
+u.postal_code,
+u.public_ssh_key,
+u.role,
+r.name as role_name,
+u.state_or_province,
+t.name as tenant,
+u.tenant_id,
+u.uid,
+u.username
+FROM tm_user as u
+LEFT JOIN role as r ON r.id = u.role
+LEFT JOIN tenant as t ON t.id = u.tenant_id
+WHERE u.id=$1
+`
+	u := tc.UserCurrent{}
+	localPassword := sql.NullString{}
+	if err := tx.QueryRow(q, id).Scan(&u.AddressLine1, &u.AddressLine2, &u.City, &u.Company, &u.Country, &u.Email, &u.FullName, &u.GID, &u.ID, &u.LastUpdated, &localPassword, &u.NewUser, &u.PhoneNumber, &u.PostalCode, &u.PublicSSHKey, &u.Role, &u.RoleName, &u.StateOrProvince, &u.Tenant, &u.TenantID, &u.UID, &u.UserName); err != nil {
+		return tc.UserCurrent{}, errors.New("querying current user: " + err.Error())
+	}
+	u.LocalUser = util.BoolPtr(localPassword.Valid)
+	return u, nil
+}


[trafficcontrol] 03/03: Fix TO Go missing wrapAccessLog

Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 5019995b70ab3378b52a25c4350680167273b052
Author: Robert Butts <ro...@apache.org>
AuthorDate: Thu Jul 5 16:05:19 2018 -0600

    Fix TO Go missing wrapAccessLog
---
 traffic_ops/traffic_ops_golang/routes.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index b88aa27..6ce58df 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -144,7 +144,7 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
 		{1.1, http.MethodGet, `users/{id}/deliveryservices/?(\.json)?$`, user.GetDSes(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
 		{1.1, http.MethodGet, `user/{id}/deliveryservices/available/?(\.json)?$`, user.GetAvailableDSes(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
 		{1.2, http.MethodPost, `user/login/?$`, wrapAccessLog(d.Secrets[0], auth.LoginHandler(d.DB, d.Config)), 0, NoAuth, nil},
-		{1.3, http.MethodPost, `user/login/?$`, auth.LoginHandler(d.DB, d.Config), 0, NoAuth, nil},
+		{1.3, http.MethodPost, `user/login/?$`, wrapAccessLog(d.Secrets[0], auth.LoginHandler(d.DB, d.Config)), 0, NoAuth, nil},
 
 		{1.1, http.MethodGet, `user/current/?(\.json)?$`, user.Current, auth.PrivLevelReadOnly, Authenticated, nil},