Skip to content

Commit c95dc88

Browse files
8W9aGNikhilSinha1
andauthored
Send explicit error if no token supplied (#2406)
* Send explicit error if no token supplied * Send an explicit error that no token has been supplied for an authenticated endpoint * Update pkg/http/transport.go Co-authored-by: Nikhil Sinha <[email protected]> Signed-off-by: Will Sackfield <[email protected]> --------- Signed-off-by: Will Sackfield <[email protected]> Co-authored-by: Nikhil Sinha <[email protected]>
1 parent 5df702a commit c95dc88

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

pkg/http/client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
const UserAgentHeader = "User-Agent"
13+
const BearerHeaderPrefix = "Bearer "
1314

1415
func ProvideHTTPClient(ctx context.Context, dockerCommand command.Command) (*http.Client, error) {
1516
userInfo, err := dockerCommand.LoadUserInformation(ctx, global.ReplicateRegistryHost)
@@ -24,8 +25,8 @@ func ProvideHTTPClient(ctx context.Context, dockerCommand command.Command) (*htt
2425
"Content-Type": "application/json",
2526
},
2627
authentication: map[string]string{
27-
env.MonobeamHostFromEnvironment(): "Bearer " + userInfo.Token,
28-
env.WebHostFromEnvironment(): "Bearer " + userInfo.Token,
28+
env.MonobeamHostFromEnvironment(): BearerHeaderPrefix + userInfo.Token,
29+
env.WebHostFromEnvironment(): BearerHeaderPrefix + userInfo.Token,
2930
},
3031
},
3132
}

pkg/http/transport.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package http
22

3-
import "net/http"
3+
import (
4+
"errors"
5+
"net/http"
6+
)
47

58
const AuthorizationHeader = "Authorization"
69

@@ -22,6 +25,9 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
2225
if req.Header.Get(AuthorizationHeader) == "" {
2326
authorisation, ok := t.authentication[req.URL.Host]
2427
if ok {
28+
if authorisation == BearerHeaderPrefix {
29+
return nil, errors.New("No token supplied for HTTP authorization. Have you run 'cog login'?")
30+
}
2531
req.Header.Set(AuthorizationHeader, authorisation)
2632
}
2733
}

pkg/http/transport_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package http
33
import (
44
"net/http"
55
"net/http/httptest"
6+
"net/url"
67
"testing"
78

89
"github.com/stretchr/testify/require"
@@ -51,3 +52,24 @@ func TestTransportOnlyAddsHeaderIfMissing(t *testing.T) {
5152
require.NoError(t, err)
5253
require.Equal(t, resp.Request.Header.Get(testHeader), expectedValue)
5354
}
55+
56+
func TestTransportSendsErrorWithMissingToken(t *testing.T) {
57+
// Setup mock http server
58+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
59+
w.WriteHeader(http.StatusOK)
60+
}))
61+
defer server.Close()
62+
u, err := url.Parse(server.URL)
63+
require.NoError(t, err)
64+
65+
transport := Transport{
66+
authentication: map[string]string{
67+
u.Host: BearerHeaderPrefix + "",
68+
},
69+
}
70+
req, err := http.NewRequest("GET", server.URL, nil)
71+
require.NoError(t, err)
72+
resp, err := transport.RoundTrip(req)
73+
require.Error(t, err)
74+
require.Nil(t, resp)
75+
}

0 commit comments

Comments
 (0)