Skip to content

Commit b992475

Browse files
authored
Added the possibility to restrict the host IP address for port mappings. (#137)
1 parent 01879ed commit b992475

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

src/Core/ContainerPortMapping.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,12 @@ public class ContainerPortMapping
2727
/// External port that value will be resolved from variable
2828
/// </summary>
2929
public string ExternalPortVariableName { get; internal set; }
30+
31+
/// <summary>
32+
/// Allowed host IP. Restriction
33+
/// Example: HostIp = 127.0.0.1
34+
/// Default all IPs are allowed
35+
/// </summary>
36+
public string? HostIp { get; internal set; }
3037
}
3138
}

src/Core/ContainerResourceBuilder.cs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ public ContainerResourceBuilder Password(string password)
135135
return this;
136136
}
137137

138+
/// <summary>
139+
/// Sets the allowed host IP address to use the external port.
140+
/// Per default all IPs are allowed (0.0.0.0).
141+
/// Example usage: if only localhost shall be allowed, then use 127.0.0.1
142+
/// </summary>
143+
/// <param name="hostIp">The allowed host IP for which the external port is exposed.</param>
144+
public ContainerResourceBuilder HostIp(string? hostIp)
145+
{
146+
_options.HostIp = hostIp;
147+
return this;
148+
}
149+
138150
/// <summary>
139151
/// Sets the main internal port of this container to the given value.
140152
///
@@ -182,14 +194,21 @@ public ContainerResourceBuilder ExternalPort(int port)
182194
/// Only provide an external port if a static external port is required.
183195
/// When the given external port is already in use by a container, the creation will fail.
184196
/// </param>
197+
/// <param name="hostIp">
198+
/// Allowed host IP. Default all IPs are allowed
199+
/// </param>
185200
/// <returns></returns>
186-
public ContainerResourceBuilder AddPortMapping(int internalPort, int externalPort = 0)
201+
public ContainerResourceBuilder AddPortMapping(
202+
int internalPort,
203+
int externalPort = 0,
204+
string? hostIp = null)
187205
{
188206
_options.AdditionalPortMappings.Add(
189207
new ContainerPortMapping()
190208
{
191209
ExternalPort = externalPort,
192-
InternalPort = internalPort
210+
InternalPort = internalPort,
211+
HostIp = hostIp
193212
});
194213
return this;
195214
}
@@ -208,16 +227,21 @@ public ContainerResourceBuilder AddPortMapping(int internalPort, int externalPor
208227
/// Only provide an external port if a static external port is required.
209228
/// When the given external port is already in use by a container, the creation will fail.
210229
/// </param>
230+
/// <param name="hostIp">
231+
/// Allowed host IP. Default all IPs are allowed
232+
/// </param>
211233
/// <returns></returns>
212234
public ContainerResourceBuilder AddPortMapping(
213235
int internalPort,
214-
string externalPortVariableName)
236+
string externalPortVariableName,
237+
string? hostIp = null)
215238
{
216239
_options.AdditionalPortMappings.Add(
217240
new ContainerPortMapping()
218241
{
219242
InternalPort = internalPort,
220243
ExternalPortVariableName = externalPortVariableName,
244+
HostIp = hostIp
221245
});
222246
return this;
223247
}
@@ -239,16 +263,21 @@ public ContainerResourceBuilder AddPortMapping(
239263
/// Only provide an external port if a static external port is required.
240264
/// When the given external port is already in use by a container, the creation will fail.
241265
/// </param>
266+
/// <param name="hostIp">
267+
/// Allowed host IP. Default all IPs are allowed
268+
/// </param>
242269
/// <returns></returns>
243270
public ContainerResourceBuilder AddPortMapping(
244271
string internalPortVariableName,
245-
int externalPort = 0)
272+
int externalPort = 0,
273+
string? hostIp = null)
246274
{
247275
_options.AdditionalPortMappings.Add(
248276
new ContainerPortMapping()
249277
{
250278
InternalPortVariableName = internalPortVariableName,
251279
ExternalPort = externalPort,
280+
HostIp = hostIp
252281
});
253282
return this;
254283
}
@@ -268,16 +297,21 @@ public ContainerResourceBuilder AddPortMapping(
268297
/// Only provide an external port if a static external port is required.
269298
/// When the given external port is already in use by a container, the creation will fail.
270299
/// </param>
300+
/// <param name="hostIp">
301+
/// Allowed host IP. Default all IPs are allowed
302+
/// </param>
271303
/// <returns></returns>
272304
public ContainerResourceBuilder AddPortMapping(
273305
string internalPortVariableName,
274-
string externalPortVariableName)
306+
string externalPortVariableName,
307+
string? hostIp = null)
275308
{
276309
_options.AdditionalPortMappings.Add(
277310
new ContainerPortMapping()
278311
{
279312
InternalPortVariableName = internalPortVariableName,
280-
ExternalPortVariableName = externalPortVariableName
313+
ExternalPortVariableName = externalPortVariableName,
314+
HostIp = hostIp
281315
});
282316
return this;
283317
}

src/Core/ContainerResourceSettings.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public class ContainerResourceSettings
1818
/// </summary>
1919
public string Image { get; internal set; }
2020

21+
/// <summary>
22+
/// Allowed host IP. Restriction
23+
/// Example: HostIp = 127.0.0.1
24+
/// Default all IPs are allowed
25+
/// </summary>
26+
public string? HostIp { get; internal set; }
27+
2128
/// <summary>
2229
/// Returns the main internal port of the container
2330
/// </summary>

src/Core/DockerContainerManager.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ private async Task CreateContainerAsync()
302302
{
303303
InternalPort = _settings.InternalPort,
304304
ExternalPort = _settings.ExternalPort,
305+
HostIp = _settings.HostIp
305306
}
306307
};
307308
allPorts.AddRange(_settings.AdditionalPortMappings);
@@ -316,7 +317,7 @@ private async Task CreateContainerAsync()
316317
portMapping.Value.Add(
317318
new PortBinding()
318319
{
319-
HostIP = "",
320+
HostIP = containerPortMapping.HostIp ?? "",
320321
HostPort = containerPortMapping.ExternalPort != 0 ?
321322
containerPortMapping.ExternalPort.ToString()
322323
: ""
@@ -489,11 +490,12 @@ private async Task ResolveHostAddressAsync()
489490
{
490491
Instance.HostPort =
491492
ResolvePort(inspectResponse, $"{_settings.InternalPort}/tcp");
492-
foreach (ContainerPortMapping portMapping
493-
in _settings.AdditionalPortMappings)
493+
494+
foreach (ContainerPortMapping portMapping in _settings.AdditionalPortMappings)
494495
{
495496
Instance.AdditionalPorts.Add(new ContainerPortMapping()
496497
{
498+
HostIp = portMapping.HostIp,
497499
InternalPort = portMapping.InternalPort,
498500
ExternalPort = ResolvePort(
499501
inspectResponse,

0 commit comments

Comments
 (0)