Skip to content

Commit

Permalink
Added the possibility to restrict the host IP address for port mappin…
Browse files Browse the repository at this point in the history
…gs. (#137)
  • Loading branch information
nscheibe authored Feb 22, 2024
1 parent 01879ed commit b992475
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/Core/ContainerPortMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@ public class ContainerPortMapping
/// External port that value will be resolved from variable
/// </summary>
public string ExternalPortVariableName { get; internal set; }

Check warning on line 29 in src/Core/ContainerPortMapping.cs

View workflow job for this annotation

GitHub Actions / release

Non-nullable property 'ExternalPortVariableName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 29 in src/Core/ContainerPortMapping.cs

View workflow job for this annotation

GitHub Actions / sonar

Non-nullable property 'ExternalPortVariableName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 29 in src/Core/ContainerPortMapping.cs

View workflow job for this annotation

GitHub Actions / release

Non-nullable property 'ExternalPortVariableName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 29 in src/Core/ContainerPortMapping.cs

View workflow job for this annotation

GitHub Actions / sonar

Non-nullable property 'ExternalPortVariableName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// Allowed host IP. Restriction
/// Example: HostIp = 127.0.0.1
/// Default all IPs are allowed
/// </summary>
public string? HostIp { get; internal set; }
}
}
46 changes: 40 additions & 6 deletions src/Core/ContainerResourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ public ContainerResourceBuilder Password(string password)
return this;
}

/// <summary>
/// Sets the allowed host IP address to use the external port.
/// Per default all IPs are allowed (0.0.0.0).
/// Example usage: if only localhost shall be allowed, then use 127.0.0.1
/// </summary>
/// <param name="hostIp">The allowed host IP for which the external port is exposed.</param>
public ContainerResourceBuilder HostIp(string? hostIp)
{
_options.HostIp = hostIp;
return this;
}

/// <summary>
/// Sets the main internal port of this container to the given value.
///
Expand Down Expand Up @@ -182,14 +194,21 @@ public ContainerResourceBuilder ExternalPort(int port)
/// Only provide an external port if a static external port is required.
/// When the given external port is already in use by a container, the creation will fail.
/// </param>
/// <param name="hostIp">
/// Allowed host IP. Default all IPs are allowed
/// </param>
/// <returns></returns>
public ContainerResourceBuilder AddPortMapping(int internalPort, int externalPort = 0)
public ContainerResourceBuilder AddPortMapping(
int internalPort,
int externalPort = 0,
string? hostIp = null)
{
_options.AdditionalPortMappings.Add(
new ContainerPortMapping()
{
ExternalPort = externalPort,
InternalPort = internalPort
InternalPort = internalPort,
HostIp = hostIp
});
return this;
}
Expand All @@ -208,16 +227,21 @@ public ContainerResourceBuilder AddPortMapping(int internalPort, int externalPor
/// Only provide an external port if a static external port is required.
/// When the given external port is already in use by a container, the creation will fail.
/// </param>
/// <param name="hostIp">
/// Allowed host IP. Default all IPs are allowed
/// </param>
/// <returns></returns>
public ContainerResourceBuilder AddPortMapping(
int internalPort,
string externalPortVariableName)
string externalPortVariableName,
string? hostIp = null)
{
_options.AdditionalPortMappings.Add(
new ContainerPortMapping()
{
InternalPort = internalPort,
ExternalPortVariableName = externalPortVariableName,
HostIp = hostIp
});
return this;
}
Expand All @@ -239,16 +263,21 @@ public ContainerResourceBuilder AddPortMapping(
/// Only provide an external port if a static external port is required.
/// When the given external port is already in use by a container, the creation will fail.
/// </param>
/// <param name="hostIp">
/// Allowed host IP. Default all IPs are allowed
/// </param>
/// <returns></returns>
public ContainerResourceBuilder AddPortMapping(
string internalPortVariableName,
int externalPort = 0)
int externalPort = 0,
string? hostIp = null)
{
_options.AdditionalPortMappings.Add(
new ContainerPortMapping()
{
InternalPortVariableName = internalPortVariableName,
ExternalPort = externalPort,
HostIp = hostIp
});
return this;
}
Expand All @@ -268,16 +297,21 @@ public ContainerResourceBuilder AddPortMapping(
/// Only provide an external port if a static external port is required.
/// When the given external port is already in use by a container, the creation will fail.
/// </param>
/// <param name="hostIp">
/// Allowed host IP. Default all IPs are allowed
/// </param>
/// <returns></returns>
public ContainerResourceBuilder AddPortMapping(
string internalPortVariableName,
string externalPortVariableName)
string externalPortVariableName,
string? hostIp = null)
{
_options.AdditionalPortMappings.Add(
new ContainerPortMapping()
{
InternalPortVariableName = internalPortVariableName,
ExternalPortVariableName = externalPortVariableName
ExternalPortVariableName = externalPortVariableName,
HostIp = hostIp
});
return this;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Core/ContainerResourceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public class ContainerResourceSettings
/// </summary>
public string Image { get; internal set; }

/// <summary>
/// Allowed host IP. Restriction
/// Example: HostIp = 127.0.0.1
/// Default all IPs are allowed
/// </summary>
public string? HostIp { get; internal set; }

/// <summary>
/// Returns the main internal port of the container
/// </summary>
Expand Down
8 changes: 5 additions & 3 deletions src/Core/DockerContainerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ private async Task CreateContainerAsync()
{
InternalPort = _settings.InternalPort,
ExternalPort = _settings.ExternalPort,
HostIp = _settings.HostIp
}
};
allPorts.AddRange(_settings.AdditionalPortMappings);
Expand All @@ -316,7 +317,7 @@ private async Task CreateContainerAsync()
portMapping.Value.Add(
new PortBinding()
{
HostIP = "",
HostIP = containerPortMapping.HostIp ?? "",
HostPort = containerPortMapping.ExternalPort != 0 ?
containerPortMapping.ExternalPort.ToString()
: ""
Expand Down Expand Up @@ -489,11 +490,12 @@ private async Task ResolveHostAddressAsync()
{
Instance.HostPort =
ResolvePort(inspectResponse, $"{_settings.InternalPort}/tcp");
foreach (ContainerPortMapping portMapping
in _settings.AdditionalPortMappings)

foreach (ContainerPortMapping portMapping in _settings.AdditionalPortMappings)
{
Instance.AdditionalPorts.Add(new ContainerPortMapping()
{
HostIp = portMapping.HostIp,
InternalPort = portMapping.InternalPort,
ExternalPort = ResolvePort(
inspectResponse,
Expand Down

0 comments on commit b992475

Please sign in to comment.