Skip to content

Commit 8442d5b

Browse files
authored
feat: add prompt to proceed if http proxy is set and https proxy unset (#2186)
* feat: add prompt to proceed if HTTP proxy is set without HTTPS
1 parent 2972965 commit 8442d5b

File tree

3 files changed

+106
-4
lines changed

3 files changed

+106
-4
lines changed

cmd/installer/cli/install.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ func waitForInstallAPI(ctx context.Context, addr string) error {
527527
}
528528

529529
func runInstall(ctx context.Context, name string, flags InstallCmdFlags, metricsReporter preflights.MetricsReporter) error {
530-
if err := runInstallVerifyAndPrompt(ctx, name, &flags); err != nil {
530+
if err := runInstallVerifyAndPrompt(ctx, name, &flags, prompts.New()); err != nil {
531531
return err
532532
}
533533

@@ -691,7 +691,7 @@ func markUIInstallComplete(password string, managerPort int) error {
691691
return nil
692692
}
693693

694-
func runInstallVerifyAndPrompt(ctx context.Context, name string, flags *InstallCmdFlags) error {
694+
func runInstallVerifyAndPrompt(ctx context.Context, name string, flags *InstallCmdFlags, prompt prompts.Prompt) error {
695695
logrus.Debugf("checking if k0s is already installed")
696696
err := verifyNoInstallation(name, "reinstall")
697697
if err != nil {
@@ -716,7 +716,7 @@ func runInstallVerifyAndPrompt(ctx context.Context, name string, flags *InstallC
716716
}
717717

718718
if !flags.isAirgap {
719-
if err := maybePromptForAppUpdate(ctx, prompts.New(), license, flags.assumeYes); err != nil {
719+
if err := maybePromptForAppUpdate(ctx, prompt, license, flags.assumeYes); err != nil {
720720
if errors.As(err, &ErrorNothingElseToAdd{}) {
721721
return err
722722
}
@@ -726,6 +726,11 @@ func runInstallVerifyAndPrompt(ctx context.Context, name string, flags *InstallC
726726
}
727727
}
728728

729+
if err := verifyProxyConfig(flags.proxy, prompt, flags.assumeYes); err != nil {
730+
return err
731+
}
732+
logrus.Debug("User confirmed prompt to proceed installing with `http_proxy` set and `https_proxy` unset")
733+
729734
if err := preflights.ValidateApp(); err != nil {
730735
return err
731736
}
@@ -1111,6 +1116,22 @@ func maybePromptForAppUpdate(ctx context.Context, prompt prompts.Prompt, license
11111116
return nil
11121117
}
11131118

1119+
// verifyProxyConfig prompts for confirmation when HTTP proxy is set without HTTPS proxy,
1120+
// returning an error if the user declines to proceed.
1121+
func verifyProxyConfig(proxy *ecv1beta1.ProxySpec, prompt prompts.Prompt, assumeYes bool) error {
1122+
if proxy != nil && proxy.HTTPProxy != "" && proxy.HTTPSProxy == "" && !assumeYes {
1123+
message := "Typically --https-proxy should be set if --http-proxy is set. Installation failures are likely otherwise. Do you want to continue anyway?"
1124+
confirmed, err := prompt.Confirm(message, false)
1125+
if err != nil {
1126+
return fmt.Errorf("failed to confirm proxy settings: %w", err)
1127+
}
1128+
if !confirmed {
1129+
return NewErrorNothingElseToAdd(errors.New("user aborted: HTTP proxy configured without HTTPS proxy"))
1130+
}
1131+
}
1132+
return nil
1133+
}
1134+
11141135
// Minimum character length for the Admin Console password
11151136
const minAdminPasswordLength = 6
11161137

cmd/installer/cli/install_runpreflights.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/replicatedhq/embedded-cluster/pkg/configutils"
99
"github.com/replicatedhq/embedded-cluster/pkg/netutils"
1010
"github.com/replicatedhq/embedded-cluster/pkg/preflights"
11+
"github.com/replicatedhq/embedded-cluster/pkg/prompts"
1112
"github.com/replicatedhq/embedded-cluster/pkg/runtimeconfig"
1213
"github.com/sirupsen/logrus"
1314
"github.com/spf13/cobra"
@@ -50,7 +51,7 @@ func InstallRunPreflightsCmd(ctx context.Context, name string) *cobra.Command {
5051
}
5152

5253
func runInstallRunPreflights(ctx context.Context, name string, flags InstallCmdFlags) error {
53-
if err := runInstallVerifyAndPrompt(ctx, name, &flags); err != nil {
54+
if err := runInstallVerifyAndPrompt(ctx, name, &flags, prompts.New()); err != nil {
5455
return err
5556
}
5657

cmd/installer/cli/install_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/replicatedhq/embedded-cluster/api"
19+
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
1920
"github.com/replicatedhq/embedded-cluster/pkg-new/tlsutils"
2021
"github.com/replicatedhq/embedded-cluster/pkg/prompts"
2122
"github.com/replicatedhq/embedded-cluster/pkg/prompts/plain"
@@ -616,3 +617,82 @@ kind: Application
616617
assert.ErrorIs(t, <-errCh, http.ErrServerClosed)
617618
t.Logf("Install API exited")
618619
}
620+
621+
func Test_verifyProxyConfig(t *testing.T) {
622+
tests := []struct {
623+
name string
624+
proxy *ecv1beta1.ProxySpec
625+
confirm bool
626+
assumeYes bool
627+
wantErr bool
628+
isErrNothingElseToAdd bool
629+
}{
630+
{
631+
name: "no proxy set",
632+
proxy: nil,
633+
wantErr: false,
634+
},
635+
{
636+
name: "http proxy set without https proxy and user confirms",
637+
proxy: &ecv1beta1.ProxySpec{
638+
HTTPProxy: "http://proxy:8080",
639+
},
640+
confirm: true,
641+
wantErr: false,
642+
},
643+
{
644+
name: "http proxy set without https proxy and user declines",
645+
proxy: &ecv1beta1.ProxySpec{
646+
HTTPProxy: "http://proxy:8080",
647+
},
648+
confirm: false,
649+
wantErr: true,
650+
isErrNothingElseToAdd: true,
651+
},
652+
{
653+
name: "http proxy set without https proxy and assumeYes is true",
654+
proxy: &ecv1beta1.ProxySpec{
655+
HTTPProxy: "http://proxy:8080",
656+
},
657+
assumeYes: true,
658+
wantErr: false,
659+
},
660+
{
661+
name: "both proxies set",
662+
proxy: &ecv1beta1.ProxySpec{
663+
HTTPProxy: "http://proxy:8080",
664+
HTTPSProxy: "https://proxy:8080",
665+
},
666+
wantErr: false,
667+
},
668+
}
669+
670+
for _, tt := range tests {
671+
t.Run(tt.name, func(t *testing.T) {
672+
var in *bytes.Buffer
673+
if tt.confirm {
674+
in = bytes.NewBuffer([]byte("y\n"))
675+
} else {
676+
in = bytes.NewBuffer([]byte("n\n"))
677+
}
678+
out := bytes.NewBuffer([]byte{})
679+
mockPrompt := plain.New(plain.WithIn(in), plain.WithOut(out))
680+
681+
prompts.SetTerminal(true)
682+
t.Cleanup(func() { prompts.SetTerminal(false) })
683+
684+
err := verifyProxyConfig(tt.proxy, mockPrompt, tt.assumeYes)
685+
if tt.wantErr {
686+
require.Error(t, err)
687+
if tt.isErrNothingElseToAdd {
688+
assert.ErrorAs(t, err, &ErrorNothingElseToAdd{})
689+
}
690+
if tt.proxy != nil && tt.proxy.HTTPProxy != "" && tt.proxy.HTTPSProxy == "" && !tt.assumeYes {
691+
assert.Contains(t, out.String(), "Typically --https-proxy should be set if --http-proxy is set")
692+
}
693+
} else {
694+
assert.NoError(t, err)
695+
}
696+
})
697+
}
698+
}

0 commit comments

Comments
 (0)