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

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


The following commit(s) were added to refs/heads/master by this push:
     new d0bab33474 Fix t3c TO API fallback (#6756)
d0bab33474 is described below

commit d0bab3347435a137799859e07a8338e3bf55b550
Author: Robert O Butts <[email protected]>
AuthorDate: Fri Apr 15 09:11:27 2022 -0600

    Fix t3c TO API fallback (#6756)
    
    * Fix t3c nil pointer crash on TO API fallback
    
    Also fixes fallback with cookie login
    
    * Add github action gofmt success messages
---
 .github/actions/go-fmt/entrypoint.sh               | 12 +++++
 cache-config/t3cutil/toreq/client.go               | 60 ++++++++++++++++------
 cache-config/t3cutil/toreq/clientfuncs.go          |  6 +--
 cache-config/t3cutil/toreq/toreqold/client.go      |  5 ++
 cache-config/t3cutil/toreq/toreqold/clientfuncs.go |  4 +-
 5 files changed, 66 insertions(+), 21 deletions(-)

diff --git a/.github/actions/go-fmt/entrypoint.sh 
b/.github/actions/go-fmt/entrypoint.sh
index 598e2b0817..ccacfd47e8 100755
--- a/.github/actions/go-fmt/entrypoint.sh
+++ b/.github/actions/go-fmt/entrypoint.sh
@@ -34,9 +34,21 @@ mkdir -p "$SRCDIR"
 ln -s "$PWD" "$SRCDIR/trafficcontrol"
 cd "$SRCDIR/trafficcontrol"
 
+printf "about to gofmt, pwd: %s\n" "$(pwd)"
+
 /usr/local/go/bin/go fmt ./...
+printf "gofmt returned %d\n" "$?"
+
+
+git config --global --add safe.directory /github/workspace
+printf "git config add safe.directory returned  %d\n" "$?"
+
+#git status
+
+printf "about to git-diff, pwd: %s\n" "$(pwd)"
 DIFF_FILE="$(mktemp)"
 git diff >"$DIFF_FILE"
+printf "git diff returned %d\n" "$?"
 
 if [ -s "$DIFF_FILE" ]; then
        ./misc/parse_diffs.py <"$DIFF_FILE";
diff --git a/cache-config/t3cutil/toreq/client.go 
b/cache-config/t3cutil/toreq/client.go
index 5f5d97ddfb..30e45637b2 100644
--- a/cache-config/t3cutil/toreq/client.go
+++ b/cache-config/t3cutil/toreq/client.go
@@ -48,8 +48,22 @@ import (
 )
 
 type TOClient struct {
-       c          *toclient.Session
-       old        *toreqold.TOClient
+       // c is the "new" Traffic Ops client, for the latest major API.
+       //
+       // This MUST NOT be accessed without checking for nil.
+       // If the Traffic Ops server doesn't support the latest API, it will 
fall back,
+       // and c will be nil and old will not, and old must be used.
+       //
+       // **WARNING** This MUST NOT be accessed without checking for nil. See 
above.
+       c *toclient.Session
+
+       // old is the "old" Traffic Ops client, for the previous major API.
+       // This will be nil unless the Traffic Ops Server doesn't support the 
latest API,
+       // in which case this will exist and c will be nil.
+       old *toreqold.TOClient
+
+       // NumRetries is the number of times to retry Traffic Ops server 
failures
+       // before giving up and returning an error to the caller.
        NumRetries int
 }
 
@@ -68,6 +82,13 @@ func (cl *TOClient) SetURL(newURL string) {
        }
 }
 
+func (cl *TOClient) HTTPClient() *http.Client {
+       if cl.c == nil {
+               return cl.old.HTTPClient()
+       }
+       return cl.c.Client
+}
+
 const FsCookiePath = `/var/lib/trafficcontrol-cache-config/`
 
 // New logs into Traffic Ops, returning the TOClient which contains the 
logged-in client.
@@ -83,6 +104,7 @@ func New(url *url.URL, user string, pass string, insecure 
bool, timeout time.Dur
        log.Infoln("TO URL string: '" + toURLStr + "'")
        log.Infoln("TO URL: '" + url.String() + "'")
 
+       latestSupported := false
        if fsCookie.Cookies != nil {
                toIP, err := net.LookupIP(url.Hostname())
                if err != nil {
@@ -111,7 +133,7 @@ func New(url *url.URL, user string, pass string, insecure 
bool, timeout time.Dur
                opts.UserAgent = userAgent
                opts.RequestTimeout = timeout
                toClient, inf, err := toclient.Login(toURLStr, user, pass, opts)
-               latestSupported := inf.StatusCode != 404 && inf.StatusCode != 
501
+               latestSupported = inf.StatusCode != 404 && inf.StatusCode != 501
 
                if err != nil && latestSupported {
                        return nil, fmt.Errorf("Logging in to Traffic Ops '%v' 
code %v: %v", torequtil.MaybeIPStr(inf.RemoteAddr), inf.StatusCode, err)
@@ -127,24 +149,30 @@ func New(url *url.URL, user string, pass string, insecure 
bool, timeout time.Dur
                                return nil, errors.New("checking Traffic Ops '" 
+ torequtil.MaybeIPStr(toAddr) + "' support: " + err.Error())
                        }
                }
-
                client = &TOClient{c: toClient}
-               if !latestSupported {
-                       log.Warnln("toreqnew.New Traffic Ops '" + 
torequtil.MaybeIPStr(inf.RemoteAddr) + "' does not support the latest client, 
falling back ot the previous")
+       }
 
-                       oldClient, err := toreqold.New(url, user, pass, 
insecure, timeout, userAgent)
-                       if err != nil {
-                               return nil, errors.New("logging into old 
client: " + err.Error())
-                       }
-                       client.c = nil
-                       client.old = oldClient
+       latestSupported, toAddr, err := IsLatestSupported(client.c)
+       if err != nil {
+               return nil, errors.New("checking Traffic Ops '" + 
torequtil.MaybeIPStr(toAddr) + "' support: " + err.Error())
+       }
 
-                       {
-                               newClient := toclient.NewNoAuthSession("", 
false, "", false, 0) // only used for the version, because toClient could be 
nil if it had an error
-                               log.Infof("cache-config Traffic Ops client: %v 
not supported, falling back to %v\n", newClient.APIVersion(), 
oldClient.APIVersion())
-                       }
+       if !latestSupported {
+               log.Warnln("toreqnew.New Traffic Ops '" + 
torequtil.MaybeIPStr(toAddr) + "' does not support the latest client, falling 
back ot the previous")
+
+               oldClient, err := toreqold.New(url, user, pass, insecure, 
timeout, userAgent)
+               if err != nil {
+                       return nil, errors.New("logging into old client: " + 
err.Error())
+               }
+               client.c = nil
+               client.old = oldClient
+
+               {
+                       newClient := toclient.NewNoAuthSession("", false, "", 
false, 0) // only used for the version, because toClient could be nil if it had 
an error
+                       log.Infof("cache-config Traffic Ops (cookie login) 
client: %v not supported, falling back to %v\n", newClient.APIVersion(), 
oldClient.APIVersion())
                }
        }
+
        return client, nil
 }
 
diff --git a/cache-config/t3cutil/toreq/clientfuncs.go 
b/cache-config/t3cutil/toreq/clientfuncs.go
index aa1e7aca24..4efeef2adb 100644
--- a/cache-config/t3cutil/toreq/clientfuncs.go
+++ b/cache-config/t3cutil/toreq/clientfuncs.go
@@ -75,7 +75,7 @@ func (cl *TOClient) WriteFsCookie(fileName string) {
                log.Warnln("Error parsing Traffic ops URL: ", err)
                return
        }
-       for _, c := range cl.c.Client.Jar.Cookies(u) {
+       for _, c := range cl.HTTPClient().Jar.Cookies(u) {
                fsCookie := torequtil.Cookie{Cookie: &http.Cookie{
                        Name:  c.Name,
                        Value: c.Value,
@@ -701,7 +701,7 @@ func (cl *TOClient) GetStatuses(reqHdr http.Header) 
([]tc.Status, toclientlib.Re
        err := torequtil.GetRetry(cl.NumRetries, "statuses", &statuses, 
func(obj interface{}) error {
                toStatus, toReqInf, err := cl.c.GetStatuses(*ReqOpts(reqHdr))
                if err != nil {
-                       return errors.New("getting server update status from 
Traffic Ops '" + torequtil.MaybeIPStr(reqInf.RemoteAddr) + "': " + err.Error())
+                       return errors.New("getting server update statuses from 
Traffic Ops '" + torequtil.MaybeIPStr(reqInf.RemoteAddr) + "': " + err.Error())
                }
                status := obj.(*[]tc.Status)
                *status = toStatus.Response
@@ -709,7 +709,7 @@ func (cl *TOClient) GetStatuses(reqHdr http.Header) 
([]tc.Status, toclientlib.Re
                return nil
        })
        if err != nil {
-               return nil, reqInf, errors.New("getting server update status: " 
+ err.Error())
+               return nil, reqInf, errors.New("getting server update statuses: 
" + err.Error())
        }
        return statuses, reqInf, nil
 }
diff --git a/cache-config/t3cutil/toreq/toreqold/client.go 
b/cache-config/t3cutil/toreq/toreqold/client.go
index 5b716fcba9..6efc0b748a 100644
--- a/cache-config/t3cutil/toreq/toreqold/client.go
+++ b/cache-config/t3cutil/toreq/toreqold/client.go
@@ -27,6 +27,7 @@ package toreqold
 
 import (
        "errors"
+       "net/http"
        "net/url"
        "strconv"
        "time"
@@ -49,6 +50,10 @@ func (cl *TOClient) SetURL(newURL string) {
        cl.c.URL = newURL
 }
 
+func (cl *TOClient) HTTPClient() *http.Client {
+       return cl.c.Client
+}
+
 func (cl *TOClient) APIVersion() string {
        return cl.c.APIVersion()
 }
diff --git a/cache-config/t3cutil/toreq/toreqold/clientfuncs.go 
b/cache-config/t3cutil/toreq/toreqold/clientfuncs.go
index 86c57a87ff..cab0563f8c 100644
--- a/cache-config/t3cutil/toreq/toreqold/clientfuncs.go
+++ b/cache-config/t3cutil/toreq/toreqold/clientfuncs.go
@@ -533,7 +533,7 @@ func (cl *TOClient) GetStatuses() ([]tc.Status, 
toclientlib.ReqInf, error) {
        err := torequtil.GetRetry(cl.NumRetries, "statuses", &statuses, 
func(obj interface{}) error {
                toStatus, toReqInf, err := cl.c.GetStatuses()
                if err != nil {
-                       return errors.New("getting server update status from 
Traffic Ops '" + torequtil.MaybeIPStr(reqInf.RemoteAddr) + "': " + err.Error())
+                       return errors.New("getting old server update status 
from Traffic Ops '" + torequtil.MaybeIPStr(reqInf.RemoteAddr) + "': " + 
err.Error())
                }
                status := obj.(*[]tc.Status)
                *status = toStatus
@@ -541,7 +541,7 @@ func (cl *TOClient) GetStatuses() ([]tc.Status, 
toclientlib.ReqInf, error) {
                return nil
        })
        if err != nil {
-               return nil, reqInf, errors.New("getting server update status: " 
+ err.Error())
+               return nil, reqInf, errors.New("getting old server update 
status: " + err.Error())
        }
        return statuses, reqInf, nil
 }

Reply via email to