Skip to content

Commit af8f1dd

Browse files
committed
feat (testdirectory): add WithHost(...) option
1 parent 68826ab commit af8f1dd

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

testdirectory/directory.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type Directory struct {
6262
s *gldap.Server
6363
logger hclog.Logger
6464
port int
65+
host string
6566
useTLS bool
6667
client *tls.Config
6768
server *tls.Config
@@ -101,6 +102,7 @@ func Start(t TestingT, opt ...Option) *Directory {
101102
users: opts.withDefaults.Users,
102103
groups: opts.withDefaults.Groups,
103104
port: opts.withPort,
105+
host: opts.withHost,
104106
userDN: opts.withDefaults.UserDN,
105107
groupDN: opts.withDefaults.GroupDN,
106108
allowAnonymousBind: opts.withDefaults.AllowAnonymousBind,
@@ -148,7 +150,7 @@ func Start(t TestingT, opt ...Option) *Directory {
148150
d.logger.Debug("not using TLS")
149151
}
150152
go func() {
151-
_ = d.s.Run(fmt.Sprintf(":%d", opts.withPort), connOpts...)
153+
_ = d.s.Run(fmt.Sprintf("%s:%d", opts.withHost, opts.withPort), connOpts...)
152154
}()
153155

154156
if v, ok := interface{}(t).(CleanupT); ok {
@@ -737,12 +739,12 @@ func (d *Directory) Conn() *ldap.Conn {
737739
err := backoff.Retry(func() error {
738740
var connErr error
739741
if d.useTLS {
740-
if conn, connErr = ldap.DialURL(fmt.Sprintf("ldaps://localhost:%d", d.Port()), ldap.DialWithTLSConfig(d.client)); connErr != nil {
742+
if conn, connErr = ldap.DialURL(fmt.Sprintf("ldaps://%s:%d", d.Host(), d.Port()), ldap.DialWithTLSConfig(d.client)); connErr != nil {
741743
return retryErrFn(connErr)
742744
}
743745
return nil
744746
}
745-
if conn, connErr = ldap.DialURL(fmt.Sprintf("ldap://localhost:%d", d.Port())); connErr != nil {
747+
if conn, connErr = ldap.DialURL(fmt.Sprintf("ldap://%s:%d", d.Host(), d.Port())); connErr != nil {
746748
return retryErrFn(connErr)
747749
}
748750
return nil
@@ -777,6 +779,14 @@ func (d *Directory) Port() int {
777779
return d.port
778780
}
779781

782+
// Host returns the host the directory is listening on
783+
func (d *Directory) Host() string {
784+
if v, ok := interface{}(d.t).(HelperT); ok {
785+
v.Helper()
786+
}
787+
return d.host
788+
}
789+
780790
// ClientCert returns the pem-encoded certificate which can be used by a client
781791
// for mTLS.
782792
func (d *Directory) ClientCert() string {

testdirectory/directory_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func Test_Start(t *testing.T) {
3333
td := testdirectory.Start(
3434
t,
3535
testdirectory.WithPort(t, port),
36+
testdirectory.WithHost(t, "127.0.0.1"),
3637
testdirectory.WithDefaults(t, &testdirectory.Defaults{Users: testEntries}),
3738
testdirectory.WithLogger(t, testLogger),
3839
testdirectory.WithNoTLS(t),
@@ -50,6 +51,7 @@ func Test_Start(t *testing.T) {
5051
port := testdirectory.FreePort(t)
5152
td := testdirectory.Start(
5253
t,
54+
testdirectory.WithHost(t, "0.0.0.0"),
5355
testdirectory.WithPort(t, port),
5456
testdirectory.WithDefaults(t, &testdirectory.Defaults{Users: testEntries}),
5557
testdirectory.WithLogger(t, testLogger),

testdirectory/options.go

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package testdirectory
22

33
import (
4+
"strings"
5+
46
"github.com/hashicorp/go-hclog"
57
"github.com/jimlambrt/gldap"
68
)
@@ -33,6 +35,7 @@ func applyOpts(opts interface{}, opt ...Option) {
3335
// options are the set of available options for test functions
3436
type options struct {
3537
withPort int
38+
withHost string
3639
withLogger hclog.Logger
3740
withNoTLS bool
3841
withMTLS bool
@@ -56,6 +59,7 @@ func defaults(t TestingT) options {
5659

5760
return options{
5861
withLogger: debugLogger,
62+
withHost: "localhost",
5963
withDefaults: &Defaults{
6064
UserAttr: DefaultUserAttr,
6165
GroupAttr: DefaultGroupAttr,
@@ -172,6 +176,15 @@ func WithPort(t TestingT, port int) Option {
172176
}
173177
}
174178

179+
// WithHost provides an optional hostname for the directory
180+
func WithHost(t TestingT, host string) Option {
181+
return func(o interface{}) {
182+
if o, ok := o.(*options); ok {
183+
o.withHost = strings.TrimSpace(host)
184+
}
185+
}
186+
}
187+
175188
// withFirst provides the option to only find the first match.
176189
func withFirst(t TestingT) Option {
177190
return func(o interface{}) {

testdirectory/testing.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,18 @@ func GetTLSConfig(t TestingT, opt ...Option) (s *tls.Config, c *tls.Config) {
151151
Bytes: privBytes,
152152
})
153153
require.NoError(err)
154+
opts := getOpts(t, opt...)
155+
156+
var ipAddrs []net.IP
157+
if hostIp := net.ParseIP(opts.withHost); hostIp != nil {
158+
ipAddrs = append(ipAddrs, hostIp)
159+
}
154160

155161
cert := &x509.Certificate{
156162
SerialNumber: genSerialNumber(t),
157163
Subject: certSubject,
158-
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback},
159-
DNSNames: []string{"localhost"},
164+
IPAddresses: ipAddrs,
165+
DNSNames: []string{opts.withHost},
160166
NotBefore: time.Now(),
161167
NotAfter: time.Now().AddDate(1, 0, 0),
162168
SubjectKeyId: []byte{1, 2, 3, 4, 6},
@@ -178,7 +184,6 @@ func GetTLSConfig(t TestingT, opt ...Option) (s *tls.Config, c *tls.Config) {
178184
RootCAs: certpool,
179185
}
180186

181-
opts := getOpts(t, opt...)
182187
if opts.withMTLS {
183188
// setup mTLS for certs from the ca
184189
serverTLSConf.ClientCAs = certpool

0 commit comments

Comments
 (0)