Skip to content

Commit 6b8e0e2

Browse files
committed
Show specific error messages on errors creating the tunnel. Fixes #972
1 parent ea1c4a3 commit 6b8e0e2

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed

cmd/cloudflared/tunnel/quick_tunnel.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tunnel
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"net/http"
78
"strings"
89
"time"
@@ -47,9 +48,23 @@ func RunQuickTunnel(sc *subcommandContext) error {
4748
}
4849
defer resp.Body.Close()
4950

51+
// Report sensible errors rather than just failing to parse the non-JSON body
52+
if resp.StatusCode == http.StatusTooManyRequests {
53+
return errors.New("rate limit exceeded; wait a while and try again")
54+
} else if resp.StatusCode != http.StatusOK {
55+
return errors.New(fmt.Sprintf("HTTP error %d", resp.StatusCode))
56+
}
57+
58+
// Need to read the body, so we can include it in the error if we fail to parse it
59+
body, err := io.ReadAll(resp.Body)
60+
if err != nil {
61+
return errors.Wrap(err, "error reading HTTP response")
62+
}
63+
5064
var data QuickTunnelResponse
51-
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
52-
return errors.Wrap(err, "failed to unmarshal quick Tunnel")
65+
if err := json.Unmarshal(body, &data); err != nil {
66+
message := fmt.Sprintf("failed to unmarshal quick Tunnel \"%v\"", string(body))
67+
return errors.Wrap(err, message)
5368
}
5469

5570
tunnelID, err := uuid.Parse(data.Result.ID)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package tunnel
2+
3+
import (
4+
"errors"
5+
"flag"
6+
"fmt"
7+
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/urfave/cli/v2"
10+
"net/http"
11+
"net/http/httptest"
12+
"testing"
13+
)
14+
15+
func makeContext(i int, serverUrl string) *cli.Context {
16+
flagSet := flag.NewFlagSet(fmt.Sprintf("test%d", i), flag.PanicOnError)
17+
flagSet.String("edge-ip-version", "", "")
18+
flagSet.String("protocol", "", "")
19+
flagSet.String("url", "", "")
20+
flagSet.String("quick-service", "", "")
21+
22+
c := cli.NewContext(cli.NewApp(), flagSet, nil)
23+
_ = c.Set("edge-ip-version", "auto")
24+
_ = c.Set("protocol", "quic")
25+
_ = c.Set("url", "http://localhost:8080")
26+
_ = c.Set("quick-service", serverUrl)
27+
return c
28+
}
29+
30+
// @noinspection SpellCheckingInspection
31+
func TestQuickTunnel(t *testing.T) {
32+
var tests = []struct {
33+
name string
34+
statusCode int
35+
response string
36+
wantErr bool
37+
expectedErr error
38+
}{
39+
{
40+
name: "200 OK response from server, valid response",
41+
statusCode: http.StatusOK,
42+
response: `{"success":true,"result":{"id":"0347c3ea-504b-47bc-8e2c-339961e6ea3e","name":"qt-not-a-real-name","hostname":"not-a-real-hostname.trycloudflare.com","account_tag":"not-an-account-tag","secret":"notreallyasecret"},"errors":[]}`,
43+
},
44+
{
45+
name: "200 OK response from server, bad tunnel ID",
46+
statusCode: http.StatusOK,
47+
response: `{"success":true,"result":{"id":"not-a-uuid","name":"qt-not-a-real-name","hostname":"not-a-real-hostname.trycloudflare.com","account_tag":"not-an-account-tag","secret":"notreallyasecret"},"errors":[]}`,
48+
wantErr: true,
49+
expectedErr: errors.New("failed to parse quick Tunnel ID: invalid UUID length: 10"),
50+
},
51+
{
52+
name: "200 OK response from server, bad JSON",
53+
statusCode: http.StatusOK,
54+
response: `This is not JSON!`,
55+
wantErr: true,
56+
expectedErr: errors.New("failed to unmarshal quick Tunnel \"This is not JSON!\": invalid character 'T' looking for beginning of value"),
57+
},
58+
{
59+
name: "429 Too Many Requests response from server",
60+
statusCode: http.StatusTooManyRequests,
61+
response: `error`,
62+
wantErr: true,
63+
expectedErr: errors.New("rate limit exceeded; wait a while and try again"),
64+
},
65+
{
66+
name: "400 Bad Request response from server",
67+
statusCode: http.StatusBadRequest,
68+
response: `error`,
69+
wantErr: true,
70+
expectedErr: errors.New("HTTP error 400"),
71+
},
72+
}
73+
74+
for i, tt := range tests {
75+
t.Run(tt.name, func(t *testing.T) {
76+
// Initialize tunnel subcommand
77+
bInfo := cliutil.GetBuildInfo("", "DEV")
78+
graceShutdownC := make(chan struct{})
79+
Init(bInfo, graceShutdownC)
80+
81+
// Create a test HTTP server to act in place of the Cloudflare service
82+
serverReceivedRequest := false
83+
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
84+
// All requests should be to /tunnel
85+
assert.Equal(t, "/tunnel", req.URL.String())
86+
87+
serverReceivedRequest = true
88+
89+
rw.WriteHeader(tt.statusCode)
90+
_, _ = rw.Write([]byte(tt.response))
91+
}))
92+
defer server.Close()
93+
94+
if !tt.wantErr {
95+
// Close the shutdown channel now so that the tunnel subcommand doesn't proceed to the quic negotiation
96+
close(graceShutdownC)
97+
}
98+
99+
err := TunnelCommand(makeContext(i, server.URL))
100+
101+
if tt.wantErr {
102+
assert.Equal(t, tt.expectedErr.Error(), err.Error())
103+
} else {
104+
assert.Nil(t, err)
105+
}
106+
assert.True(t, serverReceivedRequest)
107+
})
108+
}
109+
}

0 commit comments

Comments
 (0)