This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Checkpoint * Disable the function for now * snapshot * Tested locally * fmt
- Loading branch information
Showing
14 changed files
with
252 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
|
||
namespace Microsoft.OneFuzz.Service; | ||
|
||
public class AgentCommands { | ||
private readonly ILogTracer _log; | ||
|
||
private readonly IOnefuzzContext _context; | ||
|
||
public AgentCommands(ILogTracer log, IOnefuzzContext context) { | ||
_log = log; | ||
_context = context; | ||
} | ||
|
||
// [Function("AgentCommands")] | ||
public async Async.Task<HttpResponseData> Run([HttpTrigger("get", "delete")] HttpRequestData req) { | ||
return req.Method switch { | ||
"GET" => await Get(req), | ||
"DELETE" => await Delete(req), | ||
_ => throw new NotImplementedException($"HTTP Method {req.Method} is not supported for this method") | ||
}; | ||
} | ||
|
||
private async Async.Task<HttpResponseData> Get(HttpRequestData req) { | ||
var request = await RequestHandling.ParseRequest<NodeCommandGet>(req); | ||
if (!request.IsOk || request.OkV == null) { | ||
return await _context.RequestHandling.NotOk(req, request.ErrorV, typeof(NodeCommandGet).ToString()); | ||
} | ||
var nodeCommand = request.OkV; | ||
|
||
var message = await _context.NodeMessageOperations.GetMessage(nodeCommand.MachineId).FirstOrDefaultAsync(); | ||
if (message != null) { | ||
var command = message.Message; | ||
var messageId = message.MessageId; | ||
var envelope = new NodeCommandEnvelope(command, messageId); | ||
return await RequestHandling.Ok(req, new PendingNodeCommand(envelope)); | ||
} else { | ||
return await RequestHandling.Ok(req, new PendingNodeCommand(null)); | ||
} | ||
} | ||
|
||
private async Async.Task<HttpResponseData> Delete(HttpRequestData req) { | ||
var request = await RequestHandling.ParseRequest<NodeCommandDelete>(req); | ||
if (!request.IsOk || request.OkV == null) { | ||
return await _context.RequestHandling.NotOk(req, request.ErrorV, typeof(NodeCommandDelete).ToString()); | ||
} | ||
var nodeCommand = request.OkV; | ||
|
||
var message = await _context.NodeMessageOperations.GetEntityAsync(nodeCommand.MachineId.ToString(), nodeCommand.MessageId); | ||
if (message != null) { | ||
await _context.NodeMessageOperations.Delete(message); | ||
} | ||
|
||
return await RequestHandling.Ok(req, new BoolResult(true)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,32 @@ | ||
namespace Microsoft.OneFuzz.Service; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
public record BaseResponse(); | ||
namespace Microsoft.OneFuzz.Service; | ||
|
||
[JsonConverter(typeof(BaseResponseConverter))] | ||
public abstract record BaseResponse(); | ||
|
||
public record CanSchedule( | ||
bool Allowed, | ||
bool WorkStopped | ||
) : BaseResponse; | ||
) : BaseResponse(); | ||
|
||
public record PendingNodeCommand( | ||
NodeCommandEnvelope? Envelope | ||
) : BaseResponse(); | ||
|
||
public record BoolResult( | ||
bool Result | ||
) : BaseResponse(); | ||
|
||
|
||
public class BaseResponseConverter : JsonConverter<BaseResponse> { | ||
public override BaseResponse? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { | ||
return null; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, BaseResponse value, JsonSerializerOptions options) { | ||
var eventType = value.GetType(); | ||
JsonSerializer.Serialize(writer, value, eventType, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/ApiService/ApiService/onefuzzlib/EndpointAuthorization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System.Net; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
|
||
namespace Microsoft.OneFuzz.Service; | ||
|
||
public class EndpointAuthorization { | ||
private readonly IOnefuzzContext _context; | ||
private readonly ILogTracer _log; | ||
|
||
public EndpointAuthorization(IOnefuzzContext context, ILogTracer log) { | ||
_context = context; | ||
_log = log; | ||
} | ||
public async Async.Task<HttpResponseData> CallIfAgent(HttpRequestData req, Func<HttpRequestData, Async.Task<HttpResponseData>> method) { | ||
return await CallIf(req, method, allowAgent: true); | ||
} | ||
|
||
public async Async.Task<HttpResponseData> CallIf(HttpRequestData req, Func<HttpRequestData, Async.Task<HttpResponseData>> method, bool allowUser = false, bool allowAgent = false) { | ||
var tokenResult = await _context.UserCredentials.ParseJwtToken(req); | ||
|
||
if (!tokenResult.IsOk) { | ||
return await _context.RequestHandling.NotOk(req, tokenResult.ErrorV, "token verification", HttpStatusCode.Unauthorized); | ||
} | ||
var token = tokenResult.OkV!; | ||
|
||
if (await IsUser(token)) { | ||
if (!allowUser) { | ||
return await Reject(req, token); | ||
} | ||
|
||
var access = CheckAccess(req); | ||
if (!access.IsOk) { | ||
return await _context.RequestHandling.NotOk(req, access.ErrorV, "access control", HttpStatusCode.Unauthorized); | ||
} | ||
} | ||
|
||
|
||
if (await IsAgent(token) && !allowAgent) { | ||
return await Reject(req, token); | ||
} | ||
|
||
return await method(req); | ||
} | ||
|
||
public async Async.Task<bool> IsUser(UserInfo tokenData) { | ||
return !await IsAgent(tokenData); | ||
} | ||
|
||
public async Async.Task<HttpResponseData> Reject(HttpRequestData req, UserInfo token) { | ||
_log.Error( | ||
$"reject token. url:{req.Url} token:{token} body:{await req.ReadAsStringAsync()}" | ||
); | ||
|
||
return await _context.RequestHandling.NotOk( | ||
req, | ||
new Error( | ||
ErrorCode.UNAUTHORIZED, | ||
new string[] { "Unrecognized agent" } | ||
), | ||
"token verification", | ||
HttpStatusCode.Unauthorized | ||
); | ||
} | ||
|
||
public OneFuzzResultVoid CheckAccess(HttpRequestData req) { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public async Async.Task<bool> IsAgent(UserInfo tokenData) { | ||
if (tokenData.ObjectId != null) { | ||
var scalesets = _context.ScalesetOperations.GetByObjectId(tokenData.ObjectId.Value); | ||
if (await scalesets.AnyAsync()) { | ||
return true; | ||
} | ||
|
||
var principalId = _context.Creds.GetScalesetPrincipalId(); | ||
return principalId == tokenData.ObjectId; | ||
} | ||
|
||
if (!tokenData.ApplicationId.HasValue) { | ||
return false; | ||
} | ||
|
||
var pools = _context.PoolOperations.GetByClientId(tokenData.ApplicationId.Value); | ||
if (await pools.AnyAsync()) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.