Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#657 Ability to set UseDefaultCredentials per route #1521

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Ocelot/Configuration/Creator/HttpHandlerOptionsCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class HttpHandlerOptionsCreator : IHttpHandlerOptionsCreator
{
private readonly ITracer _tracer;

//todo: this should be configurable and available as global config parameter in ocelot.json
// TODO: This should be configurable and available as global config parameter in ocelot.json
public const int DefaultPooledConnectionLifetimeSeconds = 120;

public HttpHandlerOptionsCreator(IServiceProvider services)
Expand All @@ -20,12 +20,19 @@ public HttpHandlerOptions Create(FileHttpHandlerOptions options)
{
var useTracing = _tracer != null && options.UseTracing;

//be sure that maxConnectionPerServer is in correct range of values
// Be sure that maxConnectionPerServer is in correct range of values
var maxConnectionPerServer = (options.MaxConnectionsPerServer > 0) ? options.MaxConnectionsPerServer : int.MaxValue;
var pooledConnectionLifetime = TimeSpan.FromSeconds(options.PooledConnectionLifetimeSeconds ?? DefaultPooledConnectionLifetimeSeconds);

return new HttpHandlerOptions(options.AllowAutoRedirect,
options.UseCookieContainer, useTracing, options.UseProxy, maxConnectionPerServer, pooledConnectionLifetime);
return new HttpHandlerOptions(
options.AllowAutoRedirect,
options.UseCookieContainer,
useTracing,
options.UseProxy,
maxConnectionPerServer,
pooledConnectionLifetime,
options.UseDefaultCredentials
);
}
}
}
27 changes: 15 additions & 12 deletions src/Ocelot/Configuration/File/FileHttpHandlerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@ public class FileHttpHandlerOptions
public FileHttpHandlerOptions()
{
AllowAutoRedirect = false;
MaxConnectionsPerServer = int.MaxValue;
UseCookieContainer = false;
UseProxy = true;
PooledConnectionLifetimeSeconds = null;
MaxConnectionsPerServer = int.MaxValue;
UseCookieContainer = false;
UseProxy = true;
PooledConnectionLifetimeSeconds = null;
UseDefaultCredentials = false;
}

public FileHttpHandlerOptions(FileHttpHandlerOptions from)
{
AllowAutoRedirect = from.AllowAutoRedirect;
MaxConnectionsPerServer = from.MaxConnectionsPerServer;
UseCookieContainer = from.UseCookieContainer;
UseProxy = from.UseProxy;
PooledConnectionLifetimeSeconds = from.PooledConnectionLifetimeSeconds;
MaxConnectionsPerServer = from.MaxConnectionsPerServer;
UseCookieContainer = from.UseCookieContainer;
UseProxy = from.UseProxy;
PooledConnectionLifetimeSeconds = from.PooledConnectionLifetimeSeconds;
UseDefaultCredentials = from.UseDefaultCredentials;
}

public bool AllowAutoRedirect { get; set; }
public int MaxConnectionsPerServer { get; set; }
public bool UseCookieContainer { get; set; }
public bool UseProxy { get; set; }
public bool UseTracing { get; set; }
public bool UseProxy { get; set; }
public bool UseTracing { get; set; }
public int? PooledConnectionLifetimeSeconds { get; set; }
public bool UseDefaultCredentials { get; set; }
}
}
}
17 changes: 15 additions & 2 deletions src/Ocelot/Configuration/HttpHandlerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
public class HttpHandlerOptions
{
public HttpHandlerOptions(bool allowAutoRedirect, bool useCookieContainer, bool useTracing, bool useProxy,
int maxConnectionsPerServer, TimeSpan pooledConnectionLifeTime)
int maxConnectionsPerServer, TimeSpan pooledConnectionLifeTime, bool useDefaultCredentials)
{
AllowAutoRedirect = allowAutoRedirect;
UseCookieContainer = useCookieContainer;
UseTracing = useTracing;
UseProxy = useProxy;
MaxConnectionsPerServer = maxConnectionsPerServer;
PooledConnectionLifeTime = pooledConnectionLifeTime;
UseDefaultCredentials = useDefaultCredentials;
}

/// <summary>
Expand Down Expand Up @@ -43,13 +44,25 @@ public HttpHandlerOptions(bool allowAutoRedirect, bool useCookieContainer, bool
/// <summary>
/// Specify the maximum of concurrent connection to a network endpoint.
/// </summary>
/// <value>MaxConnectionsPerServer.</value>
/// <value>
/// The maximum number of concurrent connections (per server endpoint) allowed by an <see cref="HttpClient"/> object.
/// The property value is assignable to the <see cref="HttpClientHandler.MaxConnectionsPerServer"/> one.
/// </value>
public int MaxConnectionsPerServer { get; }

/// <summary>
/// Specify the maximum of time a connection can be pooled.
/// </summary>
/// <value>PooledConnectionLifeTime.</value>
public TimeSpan PooledConnectionLifeTime { get; }

/// <summary>
/// Specify is UseDefaultCredentials set on HttpClientHandler.
/// </summary>
/// <value>
/// <see langword="true"/> if the default credentials are used; otherwise <see langword="false"/>. The default value is <see langword="false"/>.
/// The property value is assignable to the <see cref="HttpClientHandler.UseDefaultCredentials"/> one.
/// </value>
public bool UseDefaultCredentials { get; }
}
}
15 changes: 13 additions & 2 deletions src/Ocelot/Configuration/HttpHandlerOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class HttpHandlerOptionsBuilder
private bool _useProxy;
private int _maxConnectionPerServer;
private TimeSpan _pooledConnectionLifetime = TimeSpan.FromSeconds(HttpHandlerOptionsCreator.DefaultPooledConnectionLifetimeSeconds);
private bool _useDefaultCredentials;

public HttpHandlerOptionsBuilder WithAllowAutoRedirect(bool input)
{
Expand Down Expand Up @@ -47,9 +48,19 @@ public HttpHandlerOptionsBuilder WithPooledConnectionLifetimeSeconds(TimeSpan po
return this;
}

public HttpHandlerOptions Build()
public HttpHandlerOptionsBuilder WithUseDefaultCredentials(bool input)
{
return new HttpHandlerOptions(_allowAutoRedirect, _useCookieContainer, _useTracing, _useProxy, _maxConnectionPerServer, _pooledConnectionLifetime);
_useDefaultCredentials = input;
return this;
}

public HttpHandlerOptions Build() => new(
_allowAutoRedirect,
_useCookieContainer,
_useTracing,
_useProxy,
_maxConnectionPerServer,
_pooledConnectionLifetime,
_useDefaultCredentials);
}
}
17 changes: 10 additions & 7 deletions src/Ocelot/Requester/MessageInvokerPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class MessageInvokerPool : IMessageInvokerPool

public MessageInvokerPool(IDelegatingHandlerHandlerFactory handlerFactory, IOcelotLoggerFactory loggerFactory)
{
_handlerFactory = handlerFactory ?? throw new ArgumentNullException(nameof(handlerFactory));
ArgumentNullException.ThrowIfNull(handlerFactory);
_handlerFactory = handlerFactory;
_handlersPool = new ConcurrentDictionary<MessageInvokerCacheKey, Lazy<HttpMessageInvoker>>();

ArgumentNullException.ThrowIfNull(loggerFactory);
Expand Down Expand Up @@ -70,16 +71,18 @@ private HttpMessageInvoker CreateMessageInvoker(DownstreamRoute downstreamRoute)

private HttpMessageHandler CreateHandler(DownstreamRoute downstreamRoute)
{
var options = downstreamRoute.HttpHandlerOptions;
var handler = new SocketsHttpHandler
{
AllowAutoRedirect = downstreamRoute.HttpHandlerOptions.AllowAutoRedirect,
UseCookies = downstreamRoute.HttpHandlerOptions.UseCookieContainer,
UseProxy = downstreamRoute.HttpHandlerOptions.UseProxy,
MaxConnectionsPerServer = downstreamRoute.HttpHandlerOptions.MaxConnectionsPerServer,
PooledConnectionLifetime = downstreamRoute.HttpHandlerOptions.PooledConnectionLifeTime,
AllowAutoRedirect = options.AllowAutoRedirect,
UseCookies = options.UseCookieContainer,
UseProxy = options.UseProxy,
MaxConnectionsPerServer = options.MaxConnectionsPerServer,
PooledConnectionLifetime = options.PooledConnectionLifeTime,
Credentials = options.UseDefaultCredentials ? CredentialCache.DefaultCredentials : null,
};

if (downstreamRoute.HttpHandlerOptions.UseCookieContainer)
if (options.UseCookieContainer)
{
handler.CookieContainer = new CookieContainer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public DownstreamRouteExtensionsTests()
new List<DownstreamHostAndPort>(),
null,
null,
new HttpHandlerOptions(false, false, false, false, 0, TimeSpan.Zero),
new HttpHandlerOptions(false, false, false, false, 0, TimeSpan.Zero, false),
default,
default,
new QoSOptions(0, 0, 0, null),
Expand Down
Loading