Skip to content

Commit 29d5108

Browse files
authored
Add TLS support for proxytest (#4745)
* Add TLS support for proxytest
1 parent 1c1d307 commit 29d5108

File tree

3 files changed

+382
-9
lines changed

3 files changed

+382
-9
lines changed

internal/pkg/agent/application/upgrade/artifact/download/http/verifier_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ func TestVerify(t *testing.T) {
5959
u.Host = serverURL.Host
6060
}),
6161
proxytest.WithRequestLog("proxy", func(_ string, _ ...any) {}))
62-
62+
err = proxy.Start()
63+
require.NoError(t, err, "error starting proxytest")
64+
defer proxy.Close()
6365
proxyURL, err := url.Parse(proxy.LocalhostURL)
6466
require.NoError(t, err, "could not parse server URL \"%s\"",
6567
server.URL)

testing/proxytest/proxytest.go

+47-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package proxytest
66

77
import (
8+
"crypto/tls"
89
"fmt"
910
"io"
1011
"log"
@@ -42,8 +43,9 @@ type options struct {
4243
rewriteHost func(string) string
4344
rewriteURL func(u *url.URL)
4445
// logFn if set will be used to log every request.
45-
logFn func(format string, a ...any)
46-
verbose bool
46+
logFn func(format string, a ...any)
47+
verbose bool
48+
serverTLSConfig *tls.Config
4749
}
4850

4951
// WithAddress will set the address the server will listen on. The format is as
@@ -90,6 +92,12 @@ func WithRewriteFn(f func(u *url.URL)) Option {
9092
}
9193
}
9294

95+
func WithServerTLSConfig(tc *tls.Config) Option {
96+
return func(o *options) {
97+
o.serverTLSConfig = tc
98+
}
99+
}
100+
93101
// New returns a new Proxy ready for use. Use:
94102
// - WithAddress to set the proxy's address,
95103
// - WithRewrite or WithRewriteFn to rewrite the URL before forwarding the request.
@@ -114,10 +122,8 @@ func New(t *testing.T, optns ...Option) *Proxy {
114122

115123
p := Proxy{opts: opts}
116124

117-
p.Server = &httptest.Server{
118-
Listener: l,
119-
//nolint:gosec,nolintlint // it's a test
120-
Config: &http.Server{Handler: http.HandlerFunc(func(ww http.ResponseWriter, r *http.Request) {
125+
p.Server = httptest.NewUnstartedServer(
126+
http.HandlerFunc(func(ww http.ResponseWriter, r *http.Request) {
121127
w := &statusResponseWriter{w: ww}
122128

123129
requestID := uuid.New().String()
@@ -128,8 +134,13 @@ func New(t *testing.T, optns ...Option) *Proxy {
128134

129135
opts.logFn(fmt.Sprintf("[%s] DONE %d - %s %s %s %s\n",
130136
requestID, w.statusCode, r.Method, r.URL, r.Proto, r.RemoteAddr))
131-
})}}
132-
p.Start()
137+
}),
138+
)
139+
p.Server.Listener = l
140+
141+
if opts.serverTLSConfig != nil {
142+
p.Server.TLS = opts.serverTLSConfig
143+
}
133144

134145
u, err := url.Parse(p.URL)
135146
if err != nil {
@@ -143,6 +154,34 @@ func New(t *testing.T, optns ...Option) *Proxy {
143154
return &p
144155
}
145156

157+
func (p *Proxy) Start() error {
158+
p.Server.Start()
159+
u, err := url.Parse(p.URL)
160+
if err != nil {
161+
return fmt.Errorf("could not parse fleet-server URL: %w", err)
162+
}
163+
164+
p.Port = u.Port()
165+
p.LocalhostURL = "http://localhost:" + p.Port
166+
167+
p.opts.logFn("running on %s -> %s", p.URL, p.LocalhostURL)
168+
return nil
169+
}
170+
171+
func (p *Proxy) StartTLS() error {
172+
p.Server.StartTLS()
173+
u, err := url.Parse(p.URL)
174+
if err != nil {
175+
return fmt.Errorf("could not parse fleet-server URL: %w", err)
176+
}
177+
178+
p.Port = u.Port()
179+
p.LocalhostURL = "http://localhost:" + p.Port
180+
181+
p.opts.logFn("running on %s -> %s", p.URL, p.LocalhostURL)
182+
return nil
183+
}
184+
146185
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
147186
origURL := r.URL.String()
148187

0 commit comments

Comments
 (0)