Skip to content

Commit

Permalink
add ErrInvalidScheme when HTTP is used
Browse files Browse the repository at this point in the history
HTTP can result in silent failure, as Cloud Run will redirect to HTTPS, which
in turn is a 200 for the HTML content
  • Loading branch information
tj committed Oct 14, 2020
1 parent 2600fc3 commit 2d8c5f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ package logs
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
)

// ErrInvalidScheme is returned when HTTP is used, which results in a silent failure
// due to Cloud Run's redirection of HTTP -> HTTPs.
var ErrInvalidScheme = errors.New("Client.URL must be HTTPS, not HTTP")

// Alert represents configuration for performing alerting.
type Alert struct {
// CreatedAt is a timestamp indicating when the alert was created. This field is read-only.
Expand Down Expand Up @@ -930,6 +936,17 @@ func (e Error) Error() string {
func call(client *http.Client, authToken, endpoint, method string, in, out interface{}) error {
var body io.Reader

// parse the URL
u, err := url.Parse(endpoint)
if err != nil {
return fmt.Errorf("parsing url: %w", err)
}

// ensure HTTPS
if u.Scheme == "http" {
return ErrInvalidScheme
}

// default client
if client == nil {
client = http.DefaultClient
Expand Down
12 changes: 12 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logs_test

import (
"os"
"strings"
"testing"

"github.com/apex/logs"
Expand All @@ -24,3 +25,14 @@ func TestService_GetProjects(t *testing.T) {
assert.NotEmpty(t, p.Description)
assert.NotEmpty(t, p.Name)
}

// Test erroring on HTTP.
func TestHTTP(t *testing.T) {
c := logs.Client{
URL: strings.Replace(os.Getenv("URL"), "https://", "http://", 1),
AuthToken: os.Getenv("AUTH_TOKEN"),
}

_, err := c.GetProjects()
assert.EqualError(t, err, "Client.URL must be HTTPS, not HTTP")
}

0 comments on commit 2d8c5f6

Please sign in to comment.