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

migrate add_node_ssh_key #2193

Merged
merged 8 commits into from
Aug 2, 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
57 changes: 57 additions & 0 deletions src/ApiService/ApiService/Functions/NodeAddSshKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace Microsoft.OneFuzz.Service.Functions;

public class NodeAddSshKey {

private readonly ILogTracer _log;
private readonly IEndpointAuthorization _auth;
private readonly IOnefuzzContext _context;

public NodeAddSshKey(ILogTracer log, IEndpointAuthorization auth, IOnefuzzContext context) {
_log = log;
_auth = auth;
_context = context;
}

private async Async.Task<HttpResponseData> Post(HttpRequestData req) {
var request = await RequestHandling.ParseRequest<NodeAddSshKeyPost>(req);
if (!request.IsOk) {
return await _context.RequestHandling.NotOk(
req,
request.ErrorV,
"NodeAddSshKey");
}

var node = await _context.NodeOperations.GetByMachineId(request.OkV.MachineId);

if (node == null) {
return await _context.RequestHandling.NotOk(
req,
new Error(ErrorCode.UNABLE_TO_FIND, new[] { "unable to find node" }),
$"{request.OkV.MachineId}");
}

var result = await _context.NodeOperations.AddSshPublicKey(node, request.OkV.PublicKey);
if (!result.IsOk) {
return await _context.RequestHandling.NotOk(req, result.ErrorV, "NodeAddSshKey");
}

var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteAsJsonAsync(new BoolResult(true));
return response;


}

[Function("node_add_ssh_key")]
public Async.Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "POST", Route = "node/add_ssh_key")] HttpRequestData req) {
return _auth.CallIfUser(req, r => r.Method switch {
"POST" => Post(r),
_ => throw new InvalidOperationException("Unsupported HTTP method"),
});
}

}
2 changes: 2 additions & 0 deletions src/ApiService/ApiService/OneFuzzTypes/Requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,5 @@ public record JobSearch(
List<TaskState>? TaskState = null,
bool? WithTasks = null
);

public record NodeAddSshKeyPost(Guid MachineId, string PublicKey);
19 changes: 19 additions & 0 deletions src/ApiService/ApiService/onefuzzlib/NodeOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Async.Task<Node> Create(
static readonly TimeSpan NODE_REIMAGE_TIME = TimeSpan.FromDays(6.0);

Async.Task StopTask(Guid task_id);

Async.Task<OneFuzzResult<bool>> AddSshPublicKey(Node node, string publicKey);
}


Expand Down Expand Up @@ -421,6 +423,23 @@ public async Async.Task StopTask(Guid task_id) {

}

public async Task<OneFuzzResult<bool>> AddSshPublicKey(Node node, string publicKey) {
if (publicKey == null) {
throw new ArgumentNullException(nameof(publicKey));
}

if (node.ScalesetId == null) {
return OneFuzzResult<bool>.Error(new Error(ErrorCode.INVALID_REQUEST,
new[] { "only able to add ssh keys to scaleset nodes" }));
}

var key = publicKey.EndsWith('\n') ? publicKey : $"{publicKey}\n";

await SendMessage(node, new NodeCommand { AddSshKey = new NodeCommandAddSshKey(key) });

return OneFuzzResult.Ok<bool>(true);
}

/// returns True on stopping the node and False if this doesn't stop the node
private async Task<bool> StopIfComplete(Node node, bool done = false) {
var nodeTaskIds = await _context.NodeTasksOperations.GetByMachineId(node.MachineId).Select(nt => nt.TaskId).ToArrayAsync();
Expand Down
2 changes: 1 addition & 1 deletion src/ApiService/ApiService/onefuzzlib/TaskOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public async Async.Task MarkFailed(Task task, Error error, List<Task>? taskInJob
}

private async Async.Task MarkDependantsFailed(Task task, List<Task>? taskInJob = null) {
taskInJob ??= await SearchByPartitionKeys(new[] { task.JobId.ToString() }).ToListAsync();
taskInJob ??= await SearchByPartitionKeys(new[] { $"{task.JobId}" }).ToListAsync();

foreach (var t in taskInJob) {
if (t.Config.PrereqTasks != null) {
Expand Down