Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Use records to unpack the request parameters in AgentRegistration #2570

Merged
merged 1 commit into from
Oct 27, 2022
Merged
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
55 changes: 35 additions & 20 deletions src/ApiService/ApiService/Functions/AgentRegistration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Web;
using Azure.Storage.Sas;
using Azure.Storage.Sas;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

Expand Down Expand Up @@ -31,9 +30,14 @@ public Async.Task<HttpResponseData> Run(
});

private async Async.Task<HttpResponseData> Get(HttpRequestData req) {
var uri = HttpUtility.ParseQueryString(req.Url.Query);
var rawMachineId = uri["machine_id"];
if (rawMachineId is null || !Guid.TryParse(rawMachineId, out var machineId)) {
var request = await RequestHandling.ParseUri<AgentRegistrationGet>(req);
if (!request.IsOk) {
return await _context.RequestHandling.NotOk(req, request.ErrorV, "agent registration");
}

var machineId = request.OkV.MachineId;

if (machineId == Guid.Empty) {
return await _context.RequestHandling.NotOk(
req,
new Error(
Expand Down Expand Up @@ -83,9 +87,19 @@ private async Async.Task<AgentRegistrationResponse> CreateRegistrationResponse(S
}

private async Async.Task<HttpResponseData> Post(HttpRequestData req) {
var uri = HttpUtility.ParseQueryString(req.Url.Query);
var rawMachineId = uri["machine_id"];
if (rawMachineId is null || !Guid.TryParse(rawMachineId, out var machineId)) {
var request = await RequestHandling.ParseUri<AgentRegistrationPost>(req);
if (!request.IsOk) {
return await _context.RequestHandling.NotOk(req, request.ErrorV, "agent registration");
}

var machineId = request.OkV.MachineId;
var poolName = request.OkV.PoolName;
var scalesetId = request.OkV.ScalesetId;
var version = request.OkV.Version;
var os = request.OkV.Os;
var machineName = request.OkV.MachineName;

if (machineId == Guid.Empty) {
return await _context.RequestHandling.NotOk(
req,
new Error(
Expand All @@ -94,8 +108,7 @@ private async Async.Task<HttpResponseData> Post(HttpRequestData req) {
"agent registration");
}

var rawPoolName = uri["pool_name"];
if (rawPoolName is null || !PoolName.TryParse(rawPoolName, out var poolName)) {
if (poolName is null) {
return await _context.RequestHandling.NotOk(
req,
new Error(
Expand All @@ -104,16 +117,8 @@ private async Async.Task<HttpResponseData> Post(HttpRequestData req) {
"agent registration");
}

var rawScalesetId = uri["scaleset_id"];
var scalesetId = rawScalesetId is null ? (Guid?)null : Guid.Parse(rawScalesetId);
var instanceId = machineName is not null ? InstanceIds.InstanceIdFromMachineName(machineName) : null;

var version = uri["version"] ?? "1.0.0";

var machineName = uri["machine_name"];
string? instanceId = null;
if (machineName is not null) {
instanceId = InstanceIds.InstanceIdFromMachineName(machineName);
}

_log.Info($"registration request: {machineId:Tag:MachineId} {poolName:Tag:PoolName} {scalesetId:Tag:ScalesetId} {version:Tag:Version}");
var poolResult = await _context.PoolOperations.GetByName(poolName);
Expand All @@ -133,13 +138,23 @@ private async Async.Task<HttpResponseData> Post(HttpRequestData req) {
await _context.NodeOperations.Delete(existingNode);
}

if (os != null && pool.Os != os) {
return await _context.RequestHandling.NotOk(
req,
new Error(
Code: ErrorCode.INVALID_REQUEST,
Errors: new[] { $"OS mismatch: pool '{poolName}' is configured for '{pool.Os}', but agent is running '{os}'" }),
"agent registration");
}

var node = new Service.Node(
PoolName: poolName,
PoolId: pool.PoolId,
MachineId: machineId,
ScalesetId: scalesetId,
InstanceId: instanceId,
Version: version);
Version: version
);

var r = await _context.NodeOperations.Replace(node);
if (!r.IsOk) {
Expand Down
16 changes: 16 additions & 0 deletions src/ApiService/ApiService/OneFuzzTypes/Requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,19 @@ public record WebhookUpdate(
public record InstanceConfigUpdate(
[property: Required] InstanceConfig config
) : BaseRequest;


public record AgentRegistrationGet(
[property: Required] Guid MachineId
) : BaseRequest;


public record AgentRegistrationPost(
[property: Required] PoolName PoolName,
Guid? ScalesetId,
[property: Required] Guid MachineId,
Os? Os,
string? MachineName,
[property: Required] string Version = "1.0.0"
) : BaseRequest;