Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ All notable changes to `src-cli` are documented in this file.

### Added

- Added a flag `-insecure-skip-verify` to disable TLS certificate validation.

### Changed

- Deprecated cache file formats are not read by `src campaign [apply|preview]` anymore.
Expand Down
2 changes: 1 addition & 1 deletion cmd/src/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func getRecommendedVersion(ctx context.Context, client api.Client) (string, erro
return "", err
}

resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
if err != nil {
return "", err
}
Expand Down
21 changes: 19 additions & 2 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package api
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -39,6 +40,9 @@ type Client interface {
// path is joined against the API route. For example on Sourcegraph.com this
// will result the URL: https://sourcegraph.com/.api/path.
NewHTTPRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error)

// Do runs an http.Request against the Sourcegraph API.
Do(req *http.Request) (*http.Response, error)
}

// Request instances represent GraphQL requests.
Expand All @@ -59,7 +63,8 @@ type Request interface {

// client is the internal concrete type implementing Client.
type client struct {
opts ClientOpts
opts ClientOpts
httpClient *http.Client
}

// request is the internal concrete type implementing Request.
Expand Down Expand Up @@ -96,6 +101,13 @@ func NewClient(opts ClientOpts) Client {
flags = defaultFlags()
}

httpClient := http.DefaultClient
if flags.insecureSkipVerify != nil && *flags.insecureSkipVerify {
httpClient = &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
}
}

return &client{
opts: ClientOpts{
Endpoint: opts.Endpoint,
Expand All @@ -104,6 +116,7 @@ func NewClient(opts ClientOpts) Client {
Flags: flags,
Out: opts.Out,
},
httpClient: httpClient,
}
}

Expand Down Expand Up @@ -132,6 +145,10 @@ func (c *client) NewGzippedQuery(query string) Request {
return c.NewGzippedRequest(query, nil)
}

func (c *client) Do(req *http.Request) (*http.Response, error) {
return c.httpClient.Do(req)
}

func (c *client) NewHTTPRequest(ctx context.Context, method, p string, body io.Reader) (*http.Request, error) {
req, err := c.createHTTPRequest(ctx, method, p, body)
if err != nil {
Expand Down Expand Up @@ -209,7 +226,7 @@ func (r *request) do(ctx context.Context, result interface{}) (bool, error) {
}

// Perform the request.
resp, err := http.DefaultClient.Do(req)
resp, err := r.client.httpClient.Do(req)
if err != nil {
return false, err
}
Expand Down
21 changes: 12 additions & 9 deletions internal/api/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@ import "flag"
// Flags encapsulates the standard flags that should be added to all commands
// that issue API requests.
type Flags struct {
dump *bool
getCurl *bool
trace *bool
dump *bool
getCurl *bool
trace *bool
insecureSkipVerify *bool
}

// NewFlags instantiates a new Flags structure and attaches flags to the given
// flag set.
func NewFlags(flagSet *flag.FlagSet) *Flags {
return &Flags{
dump: flagSet.Bool("dump-requests", false, "Log GraphQL requests and responses to stdout"),
getCurl: flagSet.Bool("get-curl", false, "Print the curl command for executing this query and exit (WARNING: includes printing your access token!)"),
trace: flagSet.Bool("trace", false, "Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracing"),
dump: flagSet.Bool("dump-requests", false, "Log GraphQL requests and responses to stdout"),
getCurl: flagSet.Bool("get-curl", false, "Print the curl command for executing this query and exit (WARNING: includes printing your access token!)"),
trace: flagSet.Bool("trace", false, "Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracing"),
insecureSkipVerify: flagSet.Bool("insecure-skip-verify", false, "Skip validating x.509 certificates against trusted chains"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although X.509 is more technically correct, I think we should put TLS in there instead:

Suggested change
insecureSkipVerify: flagSet.Bool("insecure-skip-verify", false, "Skip validating x.509 certificates against trusted chains"),
insecureSkipVerify: flagSet.Bool("insecure-skip-verify", false, "Skip validation of TLS certificates against trusted chains"),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I was inspired too much by the error message I was trying to overcome, I guess 😁

}
}

func defaultFlags() *Flags {
d := false
return &Flags{
dump: &d,
getCurl: &d,
trace: &d,
dump: &d,
getCurl: &d,
trace: &d,
insecureSkipVerify: &d,
}
}
2 changes: 1 addition & 1 deletion internal/campaigns/repo_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func fetchRepositoryFile(ctx context.Context, client api.Client, repo *graphql.R
req.Header.Set("Accept", "application/zip")
}

resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
if err != nil {
return false, err
}
Expand Down