mirror of
https://github.com/rancher/rke.git
synced 2025-09-02 07:24:20 +00:00
Log proxy environment variables if set
This commit is contained in:
committed by
Alena Prokharchyk
parent
d094535add
commit
e923730388
23
main.go
23
main.go
@@ -4,15 +4,19 @@ import (
|
||||
"github.com/rancher/rke/metadata"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/mattn/go-colorable"
|
||||
"github.com/rancher/rke/cmd"
|
||||
"github.com/rancher/rke/util"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var VERSION = "v0.0.12-dev"
|
||||
// VERSION gets overriden at build time using -X main.VERSION=$VERSION
|
||||
var VERSION = "dev"
|
||||
var released = regexp.MustCompile(`^v[0-9]+\.[0-9]+\.[0-9]+$`)
|
||||
var proxyEnvVars = [3]string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}
|
||||
|
||||
func main() {
|
||||
logrus.SetOutput(colorable.NewColorableStdout())
|
||||
@@ -36,6 +40,23 @@ func mainErr() error {
|
||||
return nil
|
||||
}
|
||||
logrus.Warnf("This is not an officially supported version (%s) of RKE. Please download the latest official release at https://github.com/rancher/rke/releases/latest", app.Version)
|
||||
// Print proxy related environment variables
|
||||
for _, proxyEnvVar := range proxyEnvVars {
|
||||
var err error
|
||||
// Lookup environment variable
|
||||
if key, value, ok := util.GetEnvVar(proxyEnvVar); ok {
|
||||
// If it can contain a password, strip it (HTTP_PROXY or HTTPS_PROXY)
|
||||
if strings.HasPrefix(strings.ToUpper(proxyEnvVar), "HTTP") {
|
||||
value, err = util.StripPasswordFromURL(value)
|
||||
if err != nil {
|
||||
// Don't error out of provisioning when parsing of environment variable fails
|
||||
logrus.Warnf("Error parsing proxy environment variable %s", key)
|
||||
continue
|
||||
}
|
||||
}
|
||||
logrus.Infof("Using proxy environment variable %s with value [%s]", key, value)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
app.Author = "Rancher Labs, Inc."
|
||||
|
26
util/util.go
26
util/util.go
@@ -3,6 +3,7 @@ package util
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/rancher/rke/metadata"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -108,3 +109,28 @@ func GetImageTagFromImage(image string) (string, error) {
|
||||
logrus.Debugf("Extracted version [%s] from image [%s]", imageTag, image)
|
||||
return imageTag, nil
|
||||
}
|
||||
|
||||
func StripPasswordFromURL(URL string) (string, error) {
|
||||
u, err := url.Parse(URL)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, passSet := u.User.Password()
|
||||
if passSet {
|
||||
return strings.Replace(u.String(), u.User.String()+"@", u.User.Username()+":***@", 1), nil
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
// GetEnvVar will lookup a given environment variable by key and return the key and value (to show what case got matched) with uppercase key being preferred
|
||||
func GetEnvVar(key string) (string, string, bool) {
|
||||
// Uppercase (has precedence over lowercase)
|
||||
if value, ok := os.LookupEnv(strings.ToUpper(key)); ok {
|
||||
return strings.ToUpper(key), value, true
|
||||
}
|
||||
// Lowercase
|
||||
if value, ok := os.LookupEnv(strings.ToLower(key)); ok {
|
||||
return strings.ToLower(key), value, true
|
||||
}
|
||||
return "", "", false
|
||||
}
|
||||
|
Reference in New Issue
Block a user