-
Hello, I would like to add a custom header to all http requests made by the IRaftHttpCluster to validate an api key. Otherwise people could just call the internal endpoints of the raft cluster and mess with it (e.g. calling fake election to disrupt operations)? I found some information on it here but I do not see how I could use that to access the requests and set headers. Does someone have an idea how to achieve this? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
You can't.. and you really don't need this. Prior to 4.x, the library had two Raft hosting modes: embedded and hosted. In embedded mode, Raft was hosted on the same
These things are not related directly to Raft. That's why they are not included in the library. |
Beta Was this translation helpful? Give feedback.
-
I have no idea if this is a "clean" solution or could otherwise be optimized but I wanted to share my working approach if others ever need it. @sakno Thank you for the hint with the delegating handlers! I only looked at the public class RaftClientHandlerFactory : IHttpMessageHandlerFactory
{
public HttpMessageHandler CreateHandler(string name)
{
if (name == "raftClient") return new RaftMessageHandler(new SocketsHttpHandler { ConnectTimeout = TimeSpan.FromMilliseconds(AppSettings.ConnectionTimeout) });
return new SocketsHttpHandler();
}
public class RaftMessageHandler : MessageProcessingHandler
{
public RaftMessageHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}
protected override HttpRequestMessage ProcessRequest(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Add("X-Api-Key", "XXX-XXX-XXX-XXX-XXX");
return request;
}
protected override HttpResponseMessage ProcessResponse(HttpResponseMessage response, CancellationToken cancellationToken)
{
return response;
}
}
} |
Beta Was this translation helpful? Give feedback.
I have no idea if this is a "clean" solution or could otherwise be optimized but I wanted to share my working approach if others ever need it. @sakno Thank you for the hint with the delegating handlers! I only looked at the
SocketsHttpHandler
and tried to make it add my headers ... I did not really understand the concept of how theIHttpMessageHandlerFactory
can be used.