Skip to content

Commit 08d0640

Browse files
committed
Add prompt to proceed if http proxy is set and https proxy unset
1 parent 4349fa9 commit 08d0640

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

cmd/installer/cli/proxy.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package cli
22

33
import (
4+
"errors"
45
"fmt"
56
"net"
67
"os"
78
"strings"
89

910
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
1011
"github.com/replicatedhq/embedded-cluster/pkg/netutils"
12+
"github.com/replicatedhq/embedded-cluster/pkg/prompts"
1113
"github.com/replicatedhq/embedded-cluster/pkg/runtimeconfig"
1214
"github.com/sirupsen/logrus"
1315
"github.com/spf13/cobra"
@@ -42,7 +44,7 @@ func addProxyFlags(cmd *cobra.Command) error {
4244
}
4345

4446
func parseProxyFlags(cmd *cobra.Command) (*ecv1beta1.ProxySpec, error) {
45-
p, err := getProxySpec(cmd)
47+
p, err := getProxySpec(cmd, prompts.New())
4648
if err != nil {
4749
return nil, fmt.Errorf("unable to get proxy spec from flags: %w", err)
4850
}
@@ -51,7 +53,7 @@ func parseProxyFlags(cmd *cobra.Command) (*ecv1beta1.ProxySpec, error) {
5153
return p, nil
5254
}
5355

54-
func getProxySpec(cmd *cobra.Command) (*ecv1beta1.ProxySpec, error) {
56+
func getProxySpec(cmd *cobra.Command, prompt prompts.Prompt) (*ecv1beta1.ProxySpec, error) {
5557
proxy := &ecv1beta1.ProxySpec{}
5658

5759
// Command-line flags have the highest precedence
@@ -99,6 +101,20 @@ func getProxySpec(cmd *cobra.Command) (*ecv1beta1.ProxySpec, error) {
99101
}
100102
}
101103

104+
// Check if user only provided HTTP proxy but not HTTPS proxy
105+
if httpProxy != "" && httpsProxy == "" {
106+
message := "Typically if `http_proxy` is set, `https_proxy` should be set too. Installation failures are likely otherwise. Do you want to continue anyway?"
107+
confirmed, err := prompt.Confirm(message, false)
108+
if err != nil {
109+
return nil, fmt.Errorf("failed to confirm proxy settings: %w", err)
110+
}
111+
if !confirmed {
112+
return nil, NewErrorNothingElseToAdd(errors.New("user aborted: HTTP proxy configured without HTTPS proxy"))
113+
}
114+
}
115+
116+
logrus.Debug("User confirmed prompt to proceed installing with `http_proxy` set and `https_proxy` unset")
117+
102118
// Set the values on the proxy object
103119
proxy.HTTPProxy = httpProxy
104120
proxy.HTTPSProxy = httpsProxy

cmd/installer/cli/proxy_test.go

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package cli
22

33
import (
4+
"bytes"
45
"net"
56
"testing"
67

78
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
9+
"github.com/replicatedhq/embedded-cluster/pkg/prompts"
10+
"github.com/replicatedhq/embedded-cluster/pkg/prompts/plain"
811
"github.com/spf13/cobra"
912
"github.com/spf13/pflag"
1013
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1115
)
1216

1317
// Mock network interface for testing
@@ -20,9 +24,13 @@ func (m *mockNetworkLookup) FirstValidIPNet(networkInterface string) (*net.IPNet
2024

2125
func Test_getProxySpecFromFlags(t *testing.T) {
2226
tests := []struct {
23-
name string
24-
init func(t *testing.T, flagSet *pflag.FlagSet)
25-
want *ecv1beta1.ProxySpec
27+
name string
28+
init func(t *testing.T, flagSet *pflag.FlagSet)
29+
confirm bool
30+
want *ecv1beta1.ProxySpec
31+
wantPrompt bool
32+
wantErr bool
33+
isErrNothingElseToAdd bool
2634
}{
2735
{
2836
name: "no flags set and no env vars should not set proxy",
@@ -147,6 +155,30 @@ func Test_getProxySpecFromFlags(t *testing.T) {
147155
NoProxy: "localhost,127.0.0.1,.cluster.local,.svc,169.254.169.254,10.244.0.0/17,10.244.128.0/17,lower-no-proxy-1,lower-no-proxy-2,192.168.1.0/24",
148156
},
149157
},
158+
{
159+
name: "http proxy set without https proxy and user confirms proceeding with installation",
160+
init: func(t *testing.T, flagSet *pflag.FlagSet) {
161+
flagSet.Set("http-proxy", "http://flag-proxy")
162+
},
163+
confirm: true,
164+
wantPrompt: true,
165+
want: &ecv1beta1.ProxySpec{
166+
HTTPProxy: "http://flag-proxy",
167+
HTTPSProxy: "",
168+
ProvidedNoProxy: "",
169+
NoProxy: "localhost,127.0.0.1,.cluster.local,.svc,169.254.169.254,10.244.0.0/17,10.244.128.0/17,192.168.1.0/24",
170+
},
171+
},
172+
{
173+
name: "http proxy set without https proxy and user declines proceeding with installation",
174+
init: func(t *testing.T, flagSet *pflag.FlagSet) {
175+
flagSet.Set("http-proxy", "http://flag-proxy")
176+
},
177+
confirm: false,
178+
wantPrompt: true,
179+
wantErr: true,
180+
isErrNothingElseToAdd: true,
181+
},
150182
}
151183
for _, tt := range tests {
152184
t.Run(tt.name, func(t *testing.T) {
@@ -163,9 +195,36 @@ func Test_getProxySpecFromFlags(t *testing.T) {
163195
// Override the network lookup with our mock
164196
defaultNetworkLookupImpl = &mockNetworkLookup{}
165197

166-
got, err := getProxySpec(cmd)
167-
assert.NoError(t, err, "unexpected error received")
168-
assert.Equal(t, tt.want, got)
198+
// Set up prompt if needed
199+
if tt.wantPrompt {
200+
var in *bytes.Buffer
201+
if tt.confirm {
202+
in = bytes.NewBuffer([]byte("y\n"))
203+
} else {
204+
in = bytes.NewBuffer([]byte("n\n"))
205+
}
206+
out := bytes.NewBuffer([]byte{})
207+
prompt := plain.New(plain.WithIn(in), plain.WithOut(out))
208+
209+
prompts.SetTerminal(true)
210+
t.Cleanup(func() { prompts.SetTerminal(false) })
211+
212+
got, err := getProxySpec(cmd, prompt)
213+
if tt.wantErr {
214+
require.Error(t, err)
215+
if tt.isErrNothingElseToAdd {
216+
assert.ErrorAs(t, err, &ErrorNothingElseToAdd{})
217+
}
218+
} else {
219+
assert.NoError(t, err, "unexpected error received")
220+
assert.Equal(t, tt.want, got)
221+
}
222+
assert.Contains(t, out.String(), "Typically if `http_proxy` is set, `https_proxy` should be set too", "Prompt should have been printed")
223+
} else {
224+
got, err := getProxySpec(cmd, prompts.New())
225+
assert.NoError(t, err, "unexpected error received")
226+
assert.Equal(t, tt.want, got)
227+
}
169228
})
170229
}
171230
}

0 commit comments

Comments
 (0)