Skip to content

Commit 1e6d3fa

Browse files
committed
Add --insecure flag to skip TLS certificate validation
1 parent 800a61a commit 1e6d3fa

2 files changed

Lines changed: 46 additions & 18 deletions

File tree

cmd/emcee/main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"log/slog"
8+
"crypto/tls"
89
"net/http"
910
"os"
1011
"os/signal"
@@ -97,7 +98,11 @@ Authentication values can be provided directly or as 1Password secret references
9798
}
9899

99100
// Make HTTP request
100-
resp, err := http.DefaultClient.Do(req)
101+
client := http.DefaultClient
102+
if insecure {
103+
client = &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
104+
}
105+
resp, err := client.Do(req)
101106
if err != nil {
102107
return fmt.Errorf("error downloading spec: %w", err)
103108
}
@@ -144,7 +149,13 @@ Authentication values can be provided directly or as 1Password secret references
144149
}
145150

146151
// Build HTTP client with optional auth header
147-
client, err := internal.RetryableClient(retries, timeout, rps, logger)
152+
client, err := internal.RetryableClient(internal.RetryableClientOptions{
153+
Retries: retries,
154+
Timeout: timeout,
155+
RPS: rps,
156+
Logger: logger,
157+
Insecure: insecure,
158+
})
148159
if err != nil {
149160
return fmt.Errorf("error creating client: %w", err)
150161
}
@@ -216,6 +227,7 @@ var (
216227
retries int
217228
timeout time.Duration
218229
rps int
230+
insecure bool
219231

220232
verbose bool
221233
silent bool
@@ -235,6 +247,7 @@ func init() {
235247
rootCmd.Flags().IntVar(&retries, "retries", 3, "Maximum number of retries for failed requests")
236248
rootCmd.Flags().DurationVar(&timeout, "timeout", 60*time.Second, "HTTP request timeout")
237249
rootCmd.Flags().IntVarP(&rps, "rps", "r", 0, "Maximum requests per second (0 for no limit)")
250+
rootCmd.Flags().BoolVar(&insecure, "insecure", false, "Allow insecure TLS connections (skip certificate verification)")
238251

239252
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable debug level logging to stderr")
240253
rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Disable all logging")

internal/http.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package internal
22

33
import (
4-
"fmt"
5-
"net/http"
6-
"time"
4+
"fmt"
5+
"crypto/tls"
6+
"net/http"
7+
"time"
78

8-
"github.com/hashicorp/go-retryablehttp"
9+
"github.com/hashicorp/go-retryablehttp"
910
)
1011

1112
// HeaderTransport is a custom RoundTripper that adds default headers to requests
@@ -28,29 +29,43 @@ func (t *HeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
2829
return base.RoundTrip(req)
2930
}
3031

31-
// RetryableClient returns a new http.Client with a retryablehttp.Client
32-
// configured with the provided parameters.
33-
func RetryableClient(retries int, timeout time.Duration, rps int, logger interface{}) (*http.Client, error) {
34-
if retries < 0 {
32+
// RetryableClientOptions configures the retryable HTTP client.
33+
type RetryableClientOptions struct {
34+
Retries int
35+
Timeout time.Duration
36+
RPS int
37+
Logger interface{}
38+
Insecure bool
39+
}
40+
41+
// RetryableClient returns a new http.Client with a retryablehttp.Client configured per opts.
42+
func RetryableClient(opts RetryableClientOptions) (*http.Client, error) {
43+
if opts.Retries < 0 {
3544
return nil, fmt.Errorf("retries must be greater than 0")
3645
}
37-
if timeout < 0 {
46+
if opts.Timeout < 0 {
3847
return nil, fmt.Errorf("timeout must be greater than 0")
3948
}
40-
if rps < 0 {
49+
if opts.RPS < 0 {
4150
return nil, fmt.Errorf("rps must be greater than 0")
4251
}
4352

4453
retryClient := retryablehttp.NewClient()
45-
retryClient.RetryMax = retries
54+
retryClient.RetryMax = opts.Retries
4655
retryClient.RetryWaitMin = 1 * time.Second
4756
retryClient.RetryWaitMax = 30 * time.Second
48-
retryClient.HTTPClient.Timeout = timeout
49-
retryClient.Logger = logger
50-
if rps > 0 {
51-
retryClient.Backoff = func(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
57+
retryClient.HTTPClient.Timeout = opts.Timeout
58+
retryClient.Logger = opts.Logger
59+
if opts.Insecure {
60+
// Minimal transport overriding to skip TLS verification.
61+
retryClient.HTTPClient.Transport = &http.Transport{
62+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
63+
}
64+
}
65+
if opts.RPS > 0 {
66+
retryClient.Backoff = func(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
5267
// Ensure we wait at least 1/rps between requests
53-
minWait := time.Second / time.Duration(rps)
68+
minWait := time.Second / time.Duration(opts.RPS)
5469
if min < minWait {
5570
min = minWait
5671
}

0 commit comments

Comments
 (0)