You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ro...@apache.org on 2020/08/23 16:53:36 UTC

[trafficcontrol] branch master updated: ort t3c command fixes: (#4980)

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

rob 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 5966e33  ort t3c command fixes: (#4980)
5966e33 is described below

commit 5966e33c906d26c7a37a02d9b02f36807d8395da
Author: John J. Rushford <jr...@apache.org>
AuthorDate: Sun Aug 23 10:53:21 2020 -0600

    ort t3c command fixes: (#4980)
    
    - use the atstccfg --traffic-ops-insecure flag to allow for self signed certs.
      - insure that atstccfg logging locations does not use 'stdout' as using 'stdout'
        will cause json parsing errors.
      - corrects the --help output to correctly show the default logging locations for
        t3c is 'stderr'.
---
 traffic_ops_ort/t3c/config/config.go       | 16 ++++++++----
 traffic_ops_ort/t3c/torequest/torequest.go | 42 +++++++++++++++++++++++++-----
 traffic_ops_ort/t3c/util/util.go           | 13 ++++++---
 3 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/traffic_ops_ort/t3c/config/config.go b/traffic_ops_ort/t3c/config/config.go
index dd99aa3..642c9d1 100644
--- a/traffic_ops_ort/t3c/config/config.go
+++ b/traffic_ops_ort/t3c/config/config.go
@@ -102,6 +102,7 @@ type Cfg struct {
 	ReverseProxyDisable bool
 	RunMode             Mode
 	SkipOSCheck         bool
+	TOInsecure          bool
 	TOTimeoutMS         time.Duration
 	TOUser              string
 	TOPass              string
@@ -176,6 +177,7 @@ func GetCfg() (Cfg, error) {
 	reverseProxyDisablePtr := getopt.BoolLong("reverse-proxy-disable", 'p', "[false | true] bypass the reverse proxy even if one has been configured default is false")
 	runModePtr := getopt.StringLong("run-mode", 'm', "report", "[badass | report | revalidate | syncds] run mode, default is 'report'")
 	skipOSCheckPtr := getopt.BoolLong("skip-os-check", 's', "[false | true] skip os check, default is false")
+	toInsecurePtr := getopt.BoolLong("traffic-ops-insecure", 'I', "[true | false] ignore certificate errors from Traffic Ops")
 	toTimeoutMSPtr := getopt.IntLong("traffic-ops-timeout-milliseconds", 't', 30000, "Timeout in milli-seconds for Traffic Ops requests, default is 30000")
 	toURLPtr := getopt.StringLong("traffic-ops-url", 'u', "", "Traffic Ops URL. Must be the full URL, including the scheme. Required. May also be set with the environment variable TO_URL")
 	toUserPtr := getopt.StringLong("traffic-ops-user", 'U', "", "Traffic Ops username. Required. May also be set with the environment variable TO_USER")
@@ -206,6 +208,7 @@ func GetCfg() (Cfg, error) {
 	revalWaitTime := time.Second * time.Duration(*revalWaitTimePtr)
 	reverseProxyDisable := *reverseProxyDisablePtr
 	skipOsCheck := *skipOSCheckPtr
+	toInsecure := *toInsecurePtr
 	toTimeoutMS := time.Millisecond * time.Duration(*toTimeoutMSPtr)
 	toURL := *toURLPtr
 	toUser := *toUserPtr
@@ -303,6 +306,7 @@ func GetCfg() (Cfg, error) {
 		ReverseProxyDisable: reverseProxyDisable,
 		RunMode:             runMode,
 		SkipOSCheck:         skipOsCheck,
+		TOInsecure:          toInsecure,
 		TOTimeoutMS:         toTimeoutMS,
 		TOUser:              toUser,
 		TOPass:              toPass,
@@ -373,9 +377,10 @@ func printConfig(cfg Cfg) {
 	log.Debugf("ReverseProxyDisable: %t\n", cfg.ReverseProxyDisable)
 	log.Debugf("RunMode: %s\n", cfg.RunMode)
 	log.Debugf("SkipOSCheck: %t\n", cfg.SkipOSCheck)
+	log.Debugf("TOInsecure: %t\n", cfg.TOInsecure)
 	log.Debugf("TOTimeoutMS: %d\n", cfg.TOTimeoutMS)
 	log.Debugf("TOUser: %s\n", cfg.TOUser)
-	log.Debugf("TOPass: %s\n", cfg.TOPass)
+	log.Debugf("TOPass: Pass len: '%d'\n", len(cfg.TOPass))
 	log.Debugf("TOURL: %s\n", cfg.TOURL)
 	log.Debugf("TSHome: %s\n", TSHome)
 	log.Debugf("WaitForParents: %t\n", cfg.WaitForParents)
@@ -387,16 +392,17 @@ func Usage() {
 	fmt.Println("\t[options]:")
 	fmt.Println("\t  --dispersion=[time in seconds] | -D, [time in seconds] wait a random number between 0 and <time in seconds> before starting, default = 300s")
 	fmt.Println("\t  --login-dispersion=[time in seconds] | -l, [time in seconds] wait a random number between 0 and <time in seconds> befor login, default = 0")
-	fmt.Println("\t  --log-location-debug=[value] | -d [value], Where to log debugs. May be a file path, stdout, stderr, or null, default stdout")
-	fmt.Println("\t  --log-location-error=[value] | -e [value], Where to log errors. May be a file path, stdout, stderr, or null, default stdout")
-	fmt.Println("\t  --log-location-info=[value] | -i [value], Where to log info. May be a file path, stdout, stderr, or null, default stdout")
-	fmt.Println("\t  --log-location-warning=[value] | -w [value], Where to log warnings. May be a file path, stdout, stderr, or null, default stdout")
+	fmt.Println("\t  --log-location-debug=[value] | -d [value], Where to log debugs. May be a file path, stdout, stderr, or null, default stderr")
+	fmt.Println("\t  --log-location-error=[value] | -e [value], Where to log errors. May be a file path, stdout, stderr, or null, default stderr")
+	fmt.Println("\t  --log-location-info=[value] | -i [value], Where to log info. May be a file path, stdout, stderr, or null, default stderr")
+	fmt.Println("\t  --log-location-warning=[value] | -w [value], Where to log warnings. May be a file path, stdout, stderr, or null, default stderr")
 	fmt.Println("\t  --run-mode=[mode] | -m [mode] where mode is one of [ report | badass | syncds | revalidate ], default = report")
 	fmt.Println("\t  --cache-hostname=[hostname] | -H [hostname], Host name of the cache to generate config for. Must be the server host name in Traffic Ops, not a URL, and not the FQDN")
 	fmt.Println("\t  --num-retries=[number] | -r [number], retry connection to Traffic Ops URL [number] times, default is 3")
 	fmt.Println("\t  --reval-wait-time=[seconds] | -T [seconds] wait a random number of seconds between 0 and [seconds] before revlidation, default is 60")
 	fmt.Println("\t  --rev-proxy-disable=[true|false] | -p [true|false] bypass the reverse proxy even if one has been configured, default = false")
 	fmt.Println("\t  --skip-os-check=[true|false] | -s [true | false] bypass the check for a supported CentOS version. default = false")
+	fmt.Println("\t  --traffic-ops-insecure=[true|false] -I [true | false] Whether to ignore HTTPS certificate errors from Traffic Ops. It is HIGHLY RECOMMENDED to never use this in a production environment, but only for debugging, default = false")
 	fmt.Println("\t  --traffic-ops-timeout-milliseconds=[milliseconds] | -t [milliseconds] the Traffic Ops request timeout in milliseconds. Default = 30000 (30 seconds)")
 	fmt.Println("\t  --traffic-ops-url=[url] | -u [url], Traffic Ops URL. Must be the full URL, including the scheme. Required. May also be set with the environment variable TO_URL")
 	fmt.Println("\t  --traffic-ops-user=[username] | -U [username], Traffic Ops username. Required. May also be set with the environment variable TO_USER")
diff --git a/traffic_ops_ort/t3c/torequest/torequest.go b/traffic_ops_ort/t3c/torequest/torequest.go
index 3868057..b413142 100644
--- a/traffic_ops_ort/t3c/torequest/torequest.go
+++ b/traffic_ops_ort/t3c/torequest/torequest.go
@@ -193,6 +193,24 @@ func (r *TrafficOpsReq) atsTcExec(cmdstr string) ([]byte, error) {
 
 // atsTcExecCommand is used to run the atstccfg command.
 func (r *TrafficOpsReq) atsTcExecCommand(cmdstr string, queueState int, revalState int) ([]byte, error) {
+	// adjust log locations used for atstccfg
+	// cannot use stdout as this will cause json parsing errors.
+	errorLocation := r.Cfg.LogLocationErr
+	if errorLocation == "stdout" {
+		errorLocation = "stderr"
+		log.Infoln("atstccfg error logging has been re-directed to 'stderr'")
+	}
+	infoLocation := r.Cfg.LogLocationInfo
+	if infoLocation == "stdout" {
+		infoLocation = "stderr"
+		log.Infoln("atstccfg info logging has been re-directed to 'stderr'")
+	}
+	warningLocation := r.Cfg.LogLocationWarn
+	if warningLocation == "stdout" {
+		warningLocation = "stderr"
+		log.Infoln("atstccfg warning logging has been re-directed to 'stderr'")
+	}
+
 	args := []string{
 		"--traffic-ops-timeout-milliseconds=" + strconv.FormatInt(int64(r.Cfg.TOTimeoutMS), 10),
 		"--traffic-ops-disable-proxy=" + strconv.FormatBool(r.Cfg.ReverseProxyDisable),
@@ -200,9 +218,13 @@ func (r *TrafficOpsReq) atsTcExecCommand(cmdstr string, queueState int, revalSta
 		"--traffic-ops-password=" + r.Cfg.TOPass,
 		"--traffic-ops-url=" + r.Cfg.TOURL,
 		"--cache-host-name=" + r.Cfg.CacheHostName,
-		"--log-location-error=" + r.Cfg.LogLocationErr,
-		"--log-location-info=" + r.Cfg.LogLocationInfo,
-		"--log-location-warning=" + r.Cfg.LogLocationWarn,
+		"--log-location-error=" + errorLocation,
+		"--log-location-info=" + infoLocation,
+		"--log-location-warning=" + warningLocation,
+	}
+
+	if r.Cfg.TOInsecure == true {
+		args = append(args, "--traffic-ops-insecure")
 	}
 
 	switch cmdstr {
@@ -236,10 +258,18 @@ func (r *TrafficOpsReq) atsTcExecCommand(cmdstr string, queueState int, revalSta
 	}
 
 	cmd := exec.Command(config.AtsTcConfig, args...)
-	var out bytes.Buffer
-	cmd.Stdout = &out
+	var outbuf bytes.Buffer
+	var errbuf bytes.Buffer
+
+	cmd.Stdout = &outbuf
+	cmd.Stderr = &errbuf
+
 	err := cmd.Run()
-	return out.Bytes(), err
+	if err != nil {
+		return nil, errors.New("Error from atstccfg: " + err.Error() + ": " + errbuf.String())
+	}
+
+	return outbuf.Bytes(), nil
 }
 
 // backUpFile makes a backup of a config file.
diff --git a/traffic_ops_ort/t3c/util/util.go b/traffic_ops_ort/t3c/util/util.go
index 4757ac5..ec5e165 100644
--- a/traffic_ops_ort/t3c/util/util.go
+++ b/traffic_ops_ort/t3c/util/util.go
@@ -99,11 +99,18 @@ func DirectoryExists(dir string) (bool, os.FileInfo) {
 }
 
 func ExecCommand(fullCommand string, arg ...string) ([]byte, int, error) {
-	var output bytes.Buffer
+	var outbuf bytes.Buffer
+	var errbuf bytes.Buffer
 	cmd := exec.Command(fullCommand, arg...)
-	cmd.Stdout = &output
+	cmd.Stdout = &outbuf
+	cmd.Stderr = &errbuf
 	err := cmd.Run()
-	return output.Bytes(), cmd.ProcessState.ExitCode(), err
+
+  if err != nil {
+    return outbuf.Bytes(), cmd.ProcessState.ExitCode(), 
+      errors.New("Error executing '" + fullCommand + "': " + errbuf.String())
+  }
+	return outbuf.Bytes(), cmd.ProcessState.ExitCode(), err
 }
 
 func FileExists(fn string) (bool, os.FileInfo) {