Permalink
Browse files

Add flag to upload coverage insecurely

Add an `--insecure` flag to both the `after-build` and `upload-coverage`
sub-commands to upload test coverage insecurely (without HTTPS).

This is *not* recommended for general use. This is intended for use in
private environments where the benefits of secure transfer outweigh the
operational costs.
  • Loading branch information...
toddmazierski committed Feb 22, 2018
1 parent a34ad3a commit 2c088e119e8f8edda43ef80ce979b77cbd146d24
Showing with 76 additions and 1 deletion.
  1. +3 −0 cmd/after-build.go
  2. +2 −0 cmd/upload-coverage.go
  3. +23 −1 upload/uploader.go
  4. +48 −0 upload/uploader_test.go
@@ -20,6 +20,7 @@ var afterBuildOptions = struct {
EndpointURL string
ReporterID string
ExitCode int
Insecure bool
}{}

var afterBuildCmd = &cobra.Command{
@@ -49,6 +50,7 @@ var afterBuildCmd = &cobra.Command{
ReporterID: afterBuildOptions.ReporterID,
EndpointURL: afterBuildOptions.EndpointURL,
BatchSize: afterBuildOptions.BatchSize,
Insecure: afterBuildOptions.Insecure,
}

logrus.Debug("about to run upload-coverage")
@@ -64,5 +66,6 @@ func init() {
afterBuildCmd.Flags().StringVarP(&afterBuildOptions.ReporterID, "id", "r", os.Getenv("CC_TEST_REPORTER_ID"), "reporter identifier")
afterBuildCmd.Flags().StringVarP(&afterBuildOptions.EndpointURL, "coverage-endpoint", "e", envy.Get("CC_TEST_REPORTER_COVERAGE_ENDPOINT", "https://api.codeclimate.com/v1/test_reports"), "endpoint to upload coverage information to")
afterBuildCmd.Flags().IntVarP(&afterBuildOptions.BatchSize, "batch-size", "s", 500, "batch size for source files")
afterBuildCmd.Flags().BoolVar(&afterBuildOptions.Insecure, "insecure", false, "send coverage insecurely (without HTTPS)")
RootCmd.AddCommand(afterBuildCmd)
}
@@ -35,5 +35,7 @@ func init() {
uploadCoverageCmd.Flags().StringVarP(&uploadOptions.ReporterID, "id", "r", os.Getenv("CC_TEST_REPORTER_ID"), "reporter identifier")
uploadCoverageCmd.Flags().StringVarP(&uploadOptions.EndpointURL, "endpoint", "e", envy.Get("CC_TEST_REPORTER_COVERAGE_ENDPOINT", "https://api.codeclimate.com/v1/test_reports"), "endpoint to upload coverage information to")
uploadCoverageCmd.Flags().IntVarP(&uploadOptions.BatchSize, "batch-size", "s", 500, "batch size for source files")
uploadCoverageCmd.Flags().BoolVar(&uploadOptions.Insecure, "insecure", false, "send coverage insecurely (without HTTPS)")

RootCmd.AddCommand(uploadCoverageCmd)
}
@@ -10,6 +10,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
@@ -25,6 +26,7 @@ type Uploader struct {
EndpointURL string
BatchSize int
Input io.Reader
Insecure bool
}

type ErrConflict struct {
@@ -86,7 +88,27 @@ func (u Uploader) Upload() error {
return errors.WithStack(err)
}

return u.SendBatches(testReport, batchLinks.Links.PostBatch)
postBatchURL, err := u.TransformPostBatchURL(batchLinks.Links.PostBatch)
if err != nil {
return errors.WithStack(err)
}

return u.SendBatches(testReport, postBatchURL)
}

func (u Uploader) TransformPostBatchURL(rawURL string) (string, error) {
parsed, err := url.Parse(rawURL)
if err != nil {
return "", err
}

if u.Insecure {
parsed.Scheme = "http"
} else {
parsed.Scheme = "https"
}

return parsed.String(), nil
}

func (u Uploader) SendBatches(rep *TestReport, url string) error {
@@ -0,0 +1,48 @@
package upload

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_TransformPostBatchURL_Secure(t *testing.T) {
r := require.New(t)

uploader := Uploader{
Insecure: false,
}

rawURL := "https://example.com/"
actualURL, err := uploader.TransformPostBatchURL(rawURL)

r.Equal("https://example.com/", actualURL)
r.Nil(err)
}
func Test_TransformPostBatchURL_Insecure_Success(t *testing.T) {
r := require.New(t)

uploader := Uploader{
Insecure: true,
}

rawURL := "https://example.com/"
actualURL, err := uploader.TransformPostBatchURL(rawURL)

r.Equal("http://example.com/", actualURL)
r.Nil(err)
}

func Test_TransformPostBatchURL_Insecure_Error(t *testing.T) {
r := require.New(t)

uploader := Uploader{
Insecure: true,
}

rawURL := "://example.com/"
actualURL, err := uploader.TransformPostBatchURL(rawURL)

r.Equal("", actualURL)
r.Equal("parse ://example.com/: missing protocol scheme", err.Error())
}

0 comments on commit 2c088e1

Please sign in to comment.