I have a application need to access a LDAP server (Active Directory) and it works properly when is running locally. However when it runs inside a docker container the application cannot access the Active Directory server. I executed a ping command inside the container and it worked.
# ping 10.10.2.1
PING 10.10.2.1 (10.10.2.1) 56(84) bytes of data.
64 bytes from 10.10.2.1: icmp_seq=1 ttl=37 time=14.7 ms
64 bytes from 10.10.2.1: icmp_seq=2 ttl=37 time=16.5 ms
64 bytes from 10.10.2.1: icmp_seq=3 ttl=37 time=14.5 ms
64 bytes from 10.10.2.1: icmp_seq=4 ttl=37 time=11.8 ms
64 bytes from 10.10.2.1: icmp_seq=5 ttl=37 time=15.8 ms
^C
--- 10.10.2.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 11.854/14.700/16.554/1.606 ms
The code below is responsible for access the LDAP server.
/// <summary>
/// Construtor da classe
/// </summary>
/// <param name="configuration">Parâmetro de configuração</param>
public ActiveDirectoryClient(IConfiguration configuration, ILogger<ActiveDirectoryClient> logger)
{
_logger = logger;
_configuration = configuration;
_domain = _configuration["LDAPAdress"];
}
/// <summary>
/// Tenta conectar com o ActiveDirectory até uma quantidade de vezes e um intervalo de tempo fornecido em segundos
/// </summary>
/// <param name="attemps">Número de tentativas</param>
/// <param name="retryInterval">Intervalo de tempo entre as tentativas em segundos</param>
/// <returns>Retorna o contexto do Active Directory</returns>
/// <exception cref="CustomException">Lançada quando não é possível se conectar com o ActiveDiretory</exception>
private PrincipalContext TryConnectActiveDirectory(int attemps, double retryInterval)
{
while (attemps > 0)
{
_context = null;
var isRetry = false;
_logger.LogCritical(_domain);
try
{
_context = new PrincipalContext(ContextType.Domain, _domain);
}
catch (Exception)
{
isRetry = true;
attemps--;
Thread.Sleep(TimeSpan.FromSeconds(retryInterval));
}
if (isRetry || _context.Container == null)
{
attemps--;
Thread.Sleep(TimeSpan.FromSeconds(retryInterval));
}
}
_logger.LogCritical(_domain);
if (_context == null)
{
throw new CustomException(HttpStatusCode.ServiceUnavailable, "Não foi possível conectar-se com o servidor");
}
return _context;
}
This file have the configurations of the project
{
"LDAPAdress": "10.10.2.1",
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
I mapped the container port from 60331
to 80
. It is working because I have access to the swagger page throw browser on the host in the address http://localhost:60331.
I ran the command tcpdump -i eth0 port 389 -v
inside the container to sniff all request to 389
port and nothing was returned, however when I run the application locally some requests of 389
port can be sniffed.
Could someone help?