Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/attachNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func attachNodeRun(cmd *cobra.Command, args []string) {
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")

var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, nc); err != nil {
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, nc); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/authoriseNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func authNodeRun(cmd *cobra.Command, args []string) {
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")

var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, nc); err != nil {
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, nc); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func bootstrapCmdRun(cmd *cobra.Command, args []string) {
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")

var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, bootConfig); err != nil {
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, bootConfig); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/checkNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func checkNodeRun(cmd *cobra.Command, args []string) {
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")

var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, nc); err != nil {
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, nc); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func init() {
configCmdSet.Flags().StringVarP(&cfg.Region, "region", "r", "", "sets region")
configCmdSet.Flags().StringVarP(&cfg.Tenant, "tenant", "t", "", "sets tenant")
configCmdSet.Flags().StringVar(&cfg.MfaToken, "mfa", "", "set MFA token")
configCmdSet.Flags().StringVarP(&cfg.NoProxy, "noproxy", "", "", "exclude proxy for specified urls. this should be comma-separated list of hostnames or domain names")
}

func configCmdCreateRun(cmd *cobra.Command, args []string) {
Expand All @@ -86,6 +87,10 @@ func configCmdCreateRun(cmd *cobra.Command, args []string) {
zap.S().Fatal(color.Red("x "), err)
}

if err = config.SetNoProxy(cfg.NoProxy); err != nil {
zap.S().Fatal(color.Red("x "), err)
}

if cmd.Flags().Changed("no-prompt") {
if err = config.ValidateUserCredentials(&cfg, objects.NodeConfig{}); err != nil {
zap.S().Fatal(color.Red("x "), err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/deauthoriseNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func deauthNodeRun(cmd *cobra.Command, args []string) {
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")

var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, nc); err != nil {
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, nc); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/decomissionNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func decommissionNodeRun(cmd *cobra.Command, args []string) {
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")

var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, nc); err != nil {
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, nc); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/deleteCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func deleteClusterRun(cmd *cobra.Command, args []string) {
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")

var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, nc); err != nil {
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, nc); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/detachNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func detachNodeRun(cmd *cobra.Command, args []string) {
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")

var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, nc); err != nil {
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, nc); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/prepNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func prepNodeRun(cmd *cobra.Command, args []string) {
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")

var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, nodeConfig); err != nil {
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, nodeConfig); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/supportBundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func supportBundleUpload(cmd *cobra.Command, args []string) {
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")

var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, bundleConfig); err != nil {
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, bundleConfig); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}

Expand Down
37 changes: 27 additions & 10 deletions pkg/cmdexec/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var StdErrSudoPassword string

const (
httpsProxy = "https_proxy"
noProxy = "no_proxy"
env_path = "PATH"
)

Expand All @@ -31,31 +32,42 @@ type Executor interface {
// LocalExecutor as the name implies executes commands locally
type LocalExecutor struct {
ProxyUrl string
NoProxy string
}

// Run runs a command locally returning just success or failure
func (c LocalExecutor) Run(name string, args ...string) error {
if c.ProxyUrl != "" {
args = append([]string{httpsProxy + "=" + c.ProxyUrl, name}, args...)
if c.NoProxy != "" {
args = append([]string{httpsProxy + "=" + c.ProxyUrl, noProxy + "=" + c.NoProxy, name}, args...)
} else {
args = append([]string{httpsProxy + "=" + c.ProxyUrl, name}, args...)
}
} else {
args = append([]string{name}, args...)
}
cmd := exec.Command("sudo", args...)
cmd.Env = append(cmd.Env, httpsProxy+"="+c.ProxyUrl)
cmd.Env = append(cmd.Env, httpsProxy+"="+c.ProxyUrl, noProxy+"="+c.NoProxy)
cmd.Env = append(cmd.Env, env_path+"="+os.Getenv("PATH"))
return cmd.Run()
}

// RunWithStdout runs a command locally returning stdout and err
func (c LocalExecutor) RunWithStdout(name string, args ...string) (string, error) {
if c.ProxyUrl != "" {
args = append([]string{httpsProxy + "=" + c.ProxyUrl, name}, args...)
if c.NoProxy != "" {
args = append([]string{httpsProxy + "=" + c.ProxyUrl, noProxy + "=" + c.NoProxy, name}, args...)
} else {
args = append([]string{httpsProxy + "=" + c.ProxyUrl, name}, args...)
}
} else {
args = append([]string{name}, args...)
}
cmd := exec.Command("sudo", args...)
cmd.Env = append(cmd.Env, httpsProxy+"="+c.ProxyUrl)

cmd.Env = append(cmd.Env, httpsProxy+"="+c.ProxyUrl, noProxy+"="+c.NoProxy)
cmd.Env = append(cmd.Env, env_path+"="+os.Getenv("PATH"))

byt, err := cmd.Output()
stderr := ""
if exitError, ok := err.(*exec.ExitError); ok {
Expand All @@ -80,6 +92,7 @@ func (c LocalExecutor) RunWithStdout(name string, args ...string) (string, error
type RemoteExecutor struct {
Client ssh.Client
proxyURL string
NoProxy string
}

// Run runs a command locally returning just success or failure
Expand All @@ -95,7 +108,11 @@ func (r *RemoteExecutor) RunWithStdout(name string, args ...string) (string, err
cmd = fmt.Sprintf("%s \"%s\"", cmd, arg)
}
if r.proxyURL != "" {
cmd = fmt.Sprintf("%s=%s %s", httpsProxy, r.proxyURL, cmd)
if r.NoProxy != "" {
cmd = fmt.Sprintf("%s=%s %s=%s %s", httpsProxy, r.proxyURL, noProxy, r.NoProxy, cmd)
} else {
cmd = fmt.Sprintf("%s=%s %s", httpsProxy, r.proxyURL, cmd)
}
}
stdout, stderr, err := r.Client.RunCommand(cmd)
// To fetch the stderr after executing command.
Expand All @@ -109,12 +126,12 @@ func (r *RemoteExecutor) RunWithStdout(name string, args ...string) (string, err
}

// NewRemoteExecutor create an Executor interface to execute commands remotely
func NewRemoteExecutor(host string, port int, username string, privateKey []byte, password, proxyURL string) (Executor, error) {
func NewRemoteExecutor(host string, port int, username string, privateKey []byte, password, proxyURL string, noProxy string) (Executor, error) {
client, err := ssh.NewClient(host, port, username, privateKey, password, proxyURL)
if err != nil {
return nil, err
}
re := &RemoteExecutor{Client: client, proxyURL: proxyURL}
re := &RemoteExecutor{Client: client, proxyURL: proxyURL, NoProxy: noProxy}
return re, nil
}

Expand All @@ -141,7 +158,7 @@ func ConfidentialInfoRemover(cmd string) string {
return cmd
}

func GetExecutor(proxyURL string, nc objects.NodeConfig) (Executor, error) {
func GetExecutor(proxyURL string, noProxy string, nc objects.NodeConfig) (Executor, error) {
if CheckRemote(nc) {
var pKey []byte
var err error
Expand All @@ -151,10 +168,10 @@ func GetExecutor(proxyURL string, nc objects.NodeConfig) (Executor, error) {
zap.S().Fatalf("Unable to read the sshKey %s, %s", nc.SshKey, err.Error())
}
}
return NewRemoteExecutor(nc.IPs[0], 22, nc.User, pKey, nc.Password, proxyURL)
return NewRemoteExecutor(nc.IPs[0], 22, nc.User, pKey, nc.Password, proxyURL, noProxy)
}
zap.S().Debug("Using local executor")
return LocalExecutor{ProxyUrl: proxyURL}, nil
return LocalExecutor{ProxyUrl: proxyURL, NoProxy: noProxy}, nil
}

func CheckRemote(nc objects.NodeConfig) bool {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmdexec/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

func TestGetExecutor(t *testing.T) {
proxyURL := "127.0.0.1"

noProxy := ""
localExecutor := LocalExecutor{ProxyUrl: proxyURL}

// Test local executor
executor, err := GetExecutor(proxyURL, objects.NodeConfig{})
executor, err := GetExecutor(proxyURL, noProxy, objects.NodeConfig{})
assert.Equal(t, nil, err)
assert.Equal(t, localExecutor, executor)
}
17 changes: 16 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ func ConfigCmdCreateRun(cfg *objects.Config) error {
cfg.ProxyURL = strings.TrimSuffix(proxyURL, "\n")
}

if cfg.NoProxy == "" {
fmt.Print("Noproxy [None]: ")
URL, _ := reader.ReadString('\n')
cfg.NoProxy = strings.TrimSuffix(URL, "\n")
}

if cfg.Region == "" {
cfg.Region = "RegionOne"
}
Expand All @@ -359,7 +365,7 @@ func ConfigCmdCreateRun(cfg *objects.Config) error {
}

func createClient(cfg *objects.Config, nc objects.NodeConfig) (client.Client, error) {
executor, err := cmdexec.GetExecutor(cfg.ProxyURL, nc)
executor, err := cmdexec.GetExecutor(cfg.ProxyURL, cfg.NoProxy, nc)
if err != nil {
//debug first since Fatalf calls os.Exit
zap.S().Debug("Error connecting to host %s", err.Error())
Expand Down Expand Up @@ -422,6 +428,15 @@ func SetProxy(proxyURL string) error {
return nil
}

func SetNoProxy(NoProxy string) error {
if NoProxy != "" {
if err := os.Setenv("no_proxy", NoProxy); err != nil {
return errors.New("Error setting no_proxy as environment variable")
}
}
return nil
}

func validateConfigFields(cfg *objects.Config) error {
if cfg.Fqdn == "" || cfg.Username == "" || cfg.Password == "" || cfg.Region == "" || cfg.Tenant == "" {
return MISSSING_FIELDS
Expand Down
1 change: 1 addition & 0 deletions pkg/objects/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Config struct {
WaitPeriod time.Duration `json:"wait_period"`
AllowInsecure bool `json:"allow_insecure"`
ProxyURL string `json:"proxy_url"`
NoProxy string `json:"no_proxy"`
MfaToken string `json:"mfa_token"`
AwsIamUsername string `json:"aws_iam_username"`
AwsAccessKey string `json:"aws_access_key"`
Expand Down