fix(agent): authentifie le heartbeat via X-API-Key au lieu de Bearer
Le central (ApiKeyAuthenticationHandler) ne lit que le header X-API-Key. L'agent posait Authorization: Bearer <apiKey> sur le client 'Central' → tout heartbeat et le heartbeat final de StopAsync renvoyait 401. Corrige les deux chemins (identité existante + post-registration) et verrouille le comportement avec deux tests (nouvelle identité, identité réutilisée).
This commit is contained in:
1 parent
42a441a7ab
commit
4516761a3a
2 files changed
+80
-5
No files matched your search
@@ -77,8 +77,8 @@ public class AgentRegistrationService : BackgroundService
|
||||
_state.ApiKeyPrefix = existing.ApiKeyPrefix;
|
||||
_state.PublicIp = existing.PublicIp;
|
||||
|
||||
client.DefaultRequestHeaders.Remove("Authorization");
|
||||
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {existing.ApiKey}");
|
||||
client.DefaultRequestHeaders.Remove("X-API-Key");
|
||||
client.DefaultRequestHeaders.Add("X-API-Key", existing.ApiKey);
|
||||
_client = client;
|
||||
|
||||
_logger.LogInformation(
|
||||
@@ -131,9 +131,9 @@ public class AgentRegistrationService : BackgroundService
|
||||
// Persist identity so subsequent restarts reuse it
|
||||
await _state.SaveAsync(stoppingToken);
|
||||
|
||||
// Set API Key header for subsequent requests
|
||||
client.DefaultRequestHeaders.Remove("Authorization");
|
||||
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {registration.ApiKey}");
|
||||
// Set API Key header for subsequent requests (Central's ApiKey scheme reads X-API-Key)
|
||||
client.DefaultRequestHeaders.Remove("X-API-Key");
|
||||
client.DefaultRequestHeaders.Add("X-API-Key", registration.ApiKey);
|
||||
_client = client;
|
||||
|
||||
_logger.LogInformation(
|
||||
|
||||
@@ -164,6 +164,81 @@ public class AgentRegistrationServiceTests : IDisposable
|
||||
_state.AgentId.ShouldNotBe(Guid.Empty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Heartbeat_UsesXApiKeyHeader_NotBearer()
|
||||
{
|
||||
string? xApiKey = null;
|
||||
string? authorization = null;
|
||||
var handler = new MockHttpMessageHandler(req =>
|
||||
{
|
||||
if (req.RequestUri?.ToString().Contains("/register") == true)
|
||||
return CreateRegistrationResponse();
|
||||
|
||||
if (req.RequestUri?.ToString().Contains("/heartbeat") == true)
|
||||
{
|
||||
xApiKey = req.Headers.TryGetValues("X-API-Key", out var values)
|
||||
? values.FirstOrDefault()
|
||||
: null;
|
||||
authorization = req.Headers.Authorization?.ToString();
|
||||
return new HttpResponseMessage(HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
return new HttpResponseMessage(HttpStatusCode.OK);
|
||||
});
|
||||
|
||||
var factory = CreateHttpClientFactory(handler);
|
||||
var service = CreateService(factory);
|
||||
|
||||
await service.StartAsync(CancellationToken.None);
|
||||
await Task.Delay(500);
|
||||
|
||||
await service.StopAsync(CancellationToken.None);
|
||||
|
||||
xApiKey.ShouldNotBeNull(); // API key sent via X-API-Key header
|
||||
authorization.ShouldBeNull(); // never as Bearer
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistingIdentity_Heartbeat_UsesXApiKeyHeader()
|
||||
{
|
||||
// Pre-populate the state file so LoadAsync() finds an identity
|
||||
var saved = new AgentState();
|
||||
saved.ConfigureStateDirectory(_tmp.Path);
|
||||
saved.AgentId = Guid.NewGuid();
|
||||
saved.ApiKey = "existing-key";
|
||||
saved.ApiKeyPrefix = "existing";
|
||||
await saved.SaveAsync();
|
||||
|
||||
_state.AgentId = saved.AgentId;
|
||||
_state.ApiKey = saved.ApiKey;
|
||||
_state.ApiKeyPrefix = saved.ApiKeyPrefix;
|
||||
|
||||
string? xApiKey = null;
|
||||
string? authorization = null;
|
||||
var handler = new MockHttpMessageHandler(req =>
|
||||
{
|
||||
if (req.RequestUri?.ToString().Contains("/heartbeat") == true)
|
||||
{
|
||||
xApiKey = req.Headers.TryGetValues("X-API-Key", out var values)
|
||||
? values.FirstOrDefault()
|
||||
: null;
|
||||
authorization = req.Headers.Authorization?.ToString();
|
||||
}
|
||||
return new HttpResponseMessage(HttpStatusCode.OK);
|
||||
});
|
||||
|
||||
var factory = CreateHttpClientFactory(handler);
|
||||
var service = CreateService(factory);
|
||||
|
||||
await service.StartAsync(CancellationToken.None);
|
||||
await Task.Delay(500);
|
||||
|
||||
await service.StopAsync(CancellationToken.None);
|
||||
|
||||
xApiKey.ShouldBe("existing-key");
|
||||
authorization.ShouldBeNull();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_tmp.Dispose();
|
||||
|
||||
Reference in new issue
Block a user