Skip to content

Commit c71a072

Browse files
committed
ensure container ports are not exposed aimlessly
1 parent 07cd343 commit c71a072

File tree

6 files changed

+31
-49
lines changed

6 files changed

+31
-49
lines changed

frametests/definition/options.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package definition
22

33
import (
44
"context"
5-
"strconv"
65
"time"
76

87
"github.com/docker/docker/api/types/container"
@@ -17,11 +16,11 @@ type ContainerOpts struct {
1716
UserName string
1817
Password string
1918

20-
Port string
19+
Ports []string
2120
UseHostMode bool
2221
NetworkAliases []string
2322

24-
Dependancies []DependancyConn
23+
Dependencies []DependancyConn
2524

2625
EnableLogging bool
2726
LoggingTimeout time.Duration
@@ -50,7 +49,7 @@ func (o *ContainerOpts) Configure(
5049
hostConfig.NetworkMode = "host"
5150
}
5251
} else {
53-
containerRequest.ExposedPorts = []string{o.Port}
52+
containerRequest.ExposedPorts = o.Ports
5453

5554
containerRequest.Networks = []string{ntwk.Name}
5655
containerRequest.NetworkAliases = map[string][]string{
@@ -78,7 +77,7 @@ func (o *ContainerOpts) ConfigurationExtend(
7877
}))
7978
} else {
8079
containerCustomize = append(containerCustomize,
81-
testcontainers.WithExposedPorts(o.Port),
80+
// testcontainers.WithExposedPorts(o.Ports...),
8281
network.WithNetwork([]string{ntwk.Name}, ntwk),
8382
network.WithNetworkName(o.NetworkAliases, ntwk.Name))
8483
}
@@ -110,10 +109,10 @@ func WithPassword(password string) ContainerOption {
110109
}
111110
}
112111

113-
// WithPort allows to set the port to use for testing.
114-
func WithPort(port int) ContainerOption {
112+
// WithPorts allows to set the ports to use for testing.
113+
func WithPorts(ports ...string) ContainerOption {
115114
return func(original *ContainerOpts) {
116-
original.Port = strconv.Itoa(port)
115+
original.Ports = ports
117116
}
118117
}
119118

@@ -148,6 +147,6 @@ func WithLoggingTimeout(loggingTimeout time.Duration) ContainerOption {
148147
// WithDependancies allows to set the dependancies to use for testing.
149148
func WithDependancies(dependancies ...DependancyConn) ContainerOption {
150149
return func(original *ContainerOpts) {
151-
original.Dependancies = dependancies
150+
original.Dependencies = dependancies
152151
}
153152
}

frametests/deps/testnats/nats.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ func NewWithOpts(cluster string, containerOpts ...definition.ContainerOption) de
4444
ImageName: NatsImage,
4545
UserName: NatsUser,
4646
Password: NatsPass,
47-
Port: NatsPort,
47+
Ports: []string{NatsPort},
4848
NetworkAliases: []string{"nats", "queue-nats"},
49-
UseHostMode: false,
50-
EnableLogging: true,
5149
}
5250
opts.Setup(containerOpts...)
5351

@@ -77,6 +75,8 @@ func (d *natsDependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerN
7775
return fmt.Errorf("failed to start nats container: %w", err)
7876
}
7977

78+
d.container = natsqContainer
79+
8080
conn, err := natsqContainer.ConnectionString(ctx)
8181
if err != nil {
8282
return fmt.Errorf("failed to get connection string for container: %w", err)
@@ -88,9 +88,8 @@ func (d *natsDependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerN
8888
if err != nil {
8989
return fmt.Errorf("failed to get internal host ip for container: %w", err)
9090
}
91-
d.internalConn = frame.DataSource(fmt.Sprintf("nats://%s", net.JoinHostPort(internalIP, d.opts.Port)))
91+
d.internalConn = frame.DataSource(fmt.Sprintf("nats://%s", net.JoinHostPort(internalIP, d.opts.Ports[0])))
9292

93-
d.container = natsqContainer
9493
return nil
9594
}
9695

frametests/deps/testoryhydra/hydra.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
const (
2020
// OryHydraImage is the Ory Hydra Image.
2121
OryHydraImage = "oryd/hydra:latest"
22-
// HydraPort is the default port for Hydra.
23-
HydraPort = "4445"
2422

2523
HydraConfiguration = `
2624
## ORY Hydra Configuration
@@ -73,10 +71,8 @@ func New() definition.TestResource {
7371
func NewWithOpts(configuration string, containerOpts ...definition.ContainerOption) definition.TestResource {
7472
opts := definition.ContainerOpts{
7573
ImageName: OryHydraImage,
76-
Port: HydraPort,
74+
Ports: []string{"4444", "4445"},
7775
NetworkAliases: []string{"hydra", "auth-hydra"},
78-
UseHostMode: false,
79-
EnableLogging: true,
8076
}
8177
opts.Setup(containerOpts...)
8278

@@ -135,17 +131,17 @@ func (d *hydraDependancy) migrateContainer(
135131
}
136132

137133
func (d *hydraDependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerNetwork) error {
138-
if len(d.opts.Dependancies) == 0 || !d.opts.Dependancies[0].GetDS().IsDB() {
134+
if len(d.opts.Dependencies) == 0 || !d.opts.Dependencies[0].GetDS().IsDB() {
139135
return errors.New("no Database dependencies was supplied")
140136
}
141137

142-
databaseURL := d.opts.Dependancies[0].GetInternalDS().String()
138+
databaseURL := d.opts.Dependencies[0].GetInternalDS().String()
143139
err := d.migrateContainer(ctx, ntwk, databaseURL)
144140
if err != nil {
145141
return err
146142
}
147143

148-
hydraPort, err := nat.NewPort("tcp", d.opts.Port)
144+
adminPort, err := nat.NewPort("tcp", d.opts.Ports[1])
149145
if err != nil {
150146
return err
151147
}
@@ -164,15 +160,11 @@ func (d *hydraDependancy) Setup(ctx context.Context, ntwk *testcontainers.Docker
164160
FileMode: definition.ContainerFileMode,
165161
},
166162
},
167-
WaitingFor: wait.ForHTTP("/health/ready").WithPort(hydraPort),
163+
WaitingFor: wait.ForHTTP("/health/ready").WithPort(adminPort),
168164
}
169165

170166
d.opts.Configure(ctx, ntwk, &containerRequest)
171167

172-
if !d.opts.UseHostMode {
173-
containerRequest.ExposedPorts = []string{fmt.Sprintf("%s/tcp", d.opts.Port), "4444/tcp"}
174-
}
175-
176168
hydraContainer, err := testcontainers.GenericContainer(ctx,
177169
testcontainers.GenericContainerRequest{
178170
ContainerRequest: containerRequest,
@@ -183,7 +175,7 @@ func (d *hydraDependancy) Setup(ctx context.Context, ntwk *testcontainers.Docker
183175
return fmt.Errorf("failed to start hydraContainer: %w", err)
184176
}
185177

186-
port, err := hydraContainer.MappedPort(ctx, hydraPort)
178+
port, err := hydraContainer.MappedPort(ctx, adminPort)
187179
if err != nil {
188180
return fmt.Errorf("failed to get connection string for hydraContainer: %w", err)
189181
}
@@ -200,7 +192,7 @@ func (d *hydraDependancy) Setup(ctx context.Context, ntwk *testcontainers.Docker
200192
return fmt.Errorf("failed to get internal host ip for hydraContainer: %w", err)
201193
}
202194
d.internalConn = frame.DataSource(
203-
fmt.Sprintf("http://%s", net.JoinHostPort(internalIP, d.opts.Port)),
195+
fmt.Sprintf("http://%s", net.JoinHostPort(internalIP, adminPort.Port())),
204196
)
205197

206198
d.container = hydraContainer

frametests/deps/testoryketo/keto.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
const (
2020
// OryKetoImage is the Ory Keto Image.
2121
OryKetoImage = "oryd/keto:latest"
22-
// KetoPort is the default port for Keto.
23-
KetoPort = "4467"
2422

2523
KetoConfiguration = `
2624
version: v0.12.0
@@ -67,10 +65,8 @@ func NewWithOpts(
6765
) definition.TestResource {
6866
opts := definition.ContainerOpts{
6967
ImageName: OryKetoImage,
70-
Port: KetoPort,
68+
Ports: []string{"4466", "4467"},
7169
NetworkAliases: []string{"keto", "auth-keto"},
72-
UseHostMode: false,
73-
EnableLogging: true,
7470
}
7571
opts.Setup(containerOpts...)
7672

@@ -130,16 +126,16 @@ func (d *ketoDependancy) migrateContainer(
130126
}
131127

132128
func (d *ketoDependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerNetwork) error {
133-
if len(d.opts.Dependancies) == 0 || !d.opts.Dependancies[0].GetInternalDS().IsDB() {
129+
if len(d.opts.Dependencies) == 0 || !d.opts.Dependencies[0].GetInternalDS().IsDB() {
134130
return errors.New("no Database dependencies was supplied")
135131
}
136132

137-
databaseURL := d.opts.Dependancies[0].GetInternalDS().String()
133+
databaseURL := d.opts.Dependencies[0].GetInternalDS().String()
138134
err := d.migrateContainer(ctx, ntwk, databaseURL)
139135
if err != nil {
140136
return err
141137
}
142-
ketoPort, err := nat.NewPort("tcp", d.opts.Port)
138+
adminPort, err := nat.NewPort("tcp", d.opts.Ports[1])
143139
if err != nil {
144140
return err
145141
}
@@ -158,13 +154,13 @@ func (d *ketoDependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerN
158154
FileMode: definition.ContainerFileMode,
159155
},
160156
},
161-
WaitingFor: wait.ForHTTP("/health/ready").WithPort(ketoPort),
157+
WaitingFor: wait.ForHTTP("/health/ready").WithPort(adminPort),
162158
}
163159

164160
d.opts.Configure(ctx, ntwk, &containerRequest)
165161

166162
if !d.opts.UseHostMode {
167-
containerRequest.ExposedPorts = []string{fmt.Sprintf("%s/tcp", d.opts.Port), "4466/tcp"}
163+
containerRequest.ExposedPorts = []string{fmt.Sprintf("%s/tcp", d.opts.Ports), "4466/tcp"}
168164
}
169165

170166
ketoContainer, err := testcontainers.GenericContainer(ctx,
@@ -177,7 +173,7 @@ func (d *ketoDependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerN
177173
return fmt.Errorf("failed to start ketoContainer: %w", err)
178174
}
179175

180-
port, err := ketoContainer.MappedPort(ctx, ketoPort)
176+
port, err := ketoContainer.MappedPort(ctx, adminPort)
181177
if err != nil {
182178
return fmt.Errorf("failed to get connection string for ketoContainer: %w", err)
183179
}
@@ -193,7 +189,7 @@ func (d *ketoDependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerN
193189
if err != nil {
194190
return fmt.Errorf("failed to get internal host ip for ketoContainer: %w", err)
195191
}
196-
d.internalConn = frame.DataSource(fmt.Sprintf("http://%s", net.JoinHostPort(internalIP, d.opts.Port)))
192+
d.internalConn = frame.DataSource(fmt.Sprintf("http://%s", net.JoinHostPort(internalIP, adminPort.Port())))
197193

198194
d.container = ketoContainer
199195
return nil

frametests/deps/testpostgres/postgres.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ func NewWithOpts(dbName string, containerOpts ...definition.ContainerOption) def
6161
ImageName: PostgresqlDBImage,
6262
UserName: DBUser,
6363
Password: DBPassword,
64-
Port: DBPort,
64+
Ports: []string{DBPort},
6565
NetworkAliases: []string{"postgres", "db-postgres"},
66-
UseHostMode: false,
67-
EnableLogging: true,
6866
}
6967
opts.Setup(containerOpts...)
7068

@@ -116,7 +114,7 @@ func (d *postgreSQLDependancy) Setup(ctx context.Context, ntwk *testcontainers.D
116114
"postgres://%s:%s@%s/%s",
117115
d.opts.UserName,
118116
d.opts.Password,
119-
net.JoinHostPort(internalIP, d.opts.Port),
117+
net.JoinHostPort(internalIP, d.opts.Ports[0]),
120118
d.dbname,
121119
)
122120

frametests/deps/testvalkey/valkey.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ func NewWithOpts(cluster string, containerOpts ...definition.ContainerOption) de
4444
ImageName: ValKeyImage,
4545
UserName: ValKeyUser,
4646
Password: ValKeyPass,
47-
Port: ValKeyPort,
47+
Ports: []string{ValKeyPort},
4848
NetworkAliases: []string{"valkey", "cache-valkey"},
49-
UseHostMode: false,
50-
EnableLogging: true,
5149
}
5250
opts.Setup(containerOpts...)
5351

@@ -84,7 +82,7 @@ func (d *valKeyDependancy) Setup(ctx context.Context, ntwk *testcontainers.Docke
8482
if err != nil {
8583
return fmt.Errorf("failed to get internal host ip for valkeyContainer: %w", err)
8684
}
87-
d.internalConn = frame.DataSource(fmt.Sprintf("redis://%s", net.JoinHostPort(internalIP, d.opts.Port)))
85+
d.internalConn = frame.DataSource(fmt.Sprintf("redis://%s", net.JoinHostPort(internalIP, d.opts.Ports[0])))
8886
d.container = valkeyContainer
8987
return nil
9088
}

0 commit comments

Comments
 (0)