Skip to content

Commit 2c088e1

Browse files
committed
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.
1 parent a34ad3a commit 2c088e1

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

cmd/after-build.go

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var afterBuildOptions = struct {
2020
EndpointURL string
2121
ReporterID string
2222
ExitCode int
23+
Insecure bool
2324
}{}
2425

2526
var afterBuildCmd = &cobra.Command{
@@ -49,6 +50,7 @@ var afterBuildCmd = &cobra.Command{
4950
ReporterID: afterBuildOptions.ReporterID,
5051
EndpointURL: afterBuildOptions.EndpointURL,
5152
BatchSize: afterBuildOptions.BatchSize,
53+
Insecure: afterBuildOptions.Insecure,
5254
}
5355

5456
logrus.Debug("about to run upload-coverage")
@@ -64,5 +66,6 @@ func init() {
6466
afterBuildCmd.Flags().StringVarP(&afterBuildOptions.ReporterID, "id", "r", os.Getenv("CC_TEST_REPORTER_ID"), "reporter identifier")
6567
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")
6668
afterBuildCmd.Flags().IntVarP(&afterBuildOptions.BatchSize, "batch-size", "s", 500, "batch size for source files")
69+
afterBuildCmd.Flags().BoolVar(&afterBuildOptions.Insecure, "insecure", false, "send coverage insecurely (without HTTPS)")
6770
RootCmd.AddCommand(afterBuildCmd)
6871
}

cmd/upload-coverage.go

+2
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ func init() {
3535
uploadCoverageCmd.Flags().StringVarP(&uploadOptions.ReporterID, "id", "r", os.Getenv("CC_TEST_REPORTER_ID"), "reporter identifier")
3636
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")
3737
uploadCoverageCmd.Flags().IntVarP(&uploadOptions.BatchSize, "batch-size", "s", 500, "batch size for source files")
38+
uploadCoverageCmd.Flags().BoolVar(&uploadOptions.Insecure, "insecure", false, "send coverage insecurely (without HTTPS)")
39+
3840
RootCmd.AddCommand(uploadCoverageCmd)
3941
}

upload/uploader.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io/ioutil"
1111
"log"
1212
"net/http"
13+
"net/url"
1314
"os"
1415
"strings"
1516
"time"
@@ -25,6 +26,7 @@ type Uploader struct {
2526
EndpointURL string
2627
BatchSize int
2728
Input io.Reader
29+
Insecure bool
2830
}
2931

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

89-
return u.SendBatches(testReport, batchLinks.Links.PostBatch)
91+
postBatchURL, err := u.TransformPostBatchURL(batchLinks.Links.PostBatch)
92+
if err != nil {
93+
return errors.WithStack(err)
94+
}
95+
96+
return u.SendBatches(testReport, postBatchURL)
97+
}
98+
99+
func (u Uploader) TransformPostBatchURL(rawURL string) (string, error) {
100+
parsed, err := url.Parse(rawURL)
101+
if err != nil {
102+
return "", err
103+
}
104+
105+
if u.Insecure {
106+
parsed.Scheme = "http"
107+
} else {
108+
parsed.Scheme = "https"
109+
}
110+
111+
return parsed.String(), nil
90112
}
91113

92114
func (u Uploader) SendBatches(rep *TestReport, url string) error {

upload/uploader_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package upload
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func Test_TransformPostBatchURL_Secure(t *testing.T) {
10+
r := require.New(t)
11+
12+
uploader := Uploader{
13+
Insecure: false,
14+
}
15+
16+
rawURL := "https://example.com/"
17+
actualURL, err := uploader.TransformPostBatchURL(rawURL)
18+
19+
r.Equal("https://example.com/", actualURL)
20+
r.Nil(err)
21+
}
22+
func Test_TransformPostBatchURL_Insecure_Success(t *testing.T) {
23+
r := require.New(t)
24+
25+
uploader := Uploader{
26+
Insecure: true,
27+
}
28+
29+
rawURL := "https://example.com/"
30+
actualURL, err := uploader.TransformPostBatchURL(rawURL)
31+
32+
r.Equal("http://example.com/", actualURL)
33+
r.Nil(err)
34+
}
35+
36+
func Test_TransformPostBatchURL_Insecure_Error(t *testing.T) {
37+
r := require.New(t)
38+
39+
uploader := Uploader{
40+
Insecure: true,
41+
}
42+
43+
rawURL := "://example.com/"
44+
actualURL, err := uploader.TransformPostBatchURL(rawURL)
45+
46+
r.Equal("", actualURL)
47+
r.Equal("parse ://example.com/: missing protocol scheme", err.Error())
48+
}

0 commit comments

Comments
 (0)