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 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: stas <statis@microsoft.com>
- Loading branch information
Showing
8 changed files
with
246 additions
and
24 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,35 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
|
||
using Microsoft.OneFuzz.Service; | ||
|
||
|
||
namespace ApiService.TestHooks { | ||
public class ContainerTestHooks { | ||
|
||
private readonly ILogTracer _log; | ||
private readonly IConfigOperations _configOps; | ||
private readonly IContainers _containers; | ||
|
||
public ContainerTestHooks(ILogTracer log, IConfigOperations configOps, IContainers containers) { | ||
_log = log.WithTag("TestHooks", "ContainerTestHooks"); | ||
_configOps = configOps; | ||
_containers = containers; | ||
} | ||
|
||
[Function("GetInstanceId")] | ||
public async Task<HttpResponseData> GetInstanceId([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/containers/instanceId")] HttpRequestData req) { | ||
_log.Info("Get instance ID"); | ||
var instanceId = await _containers.GetInstanceId(); | ||
|
||
var resp = req.CreateResponse(HttpStatusCode.OK); | ||
await resp.WriteStringAsync(instanceId.ToString()); | ||
return resp; | ||
} | ||
|
||
|
||
|
||
} | ||
} |
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,65 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
|
||
using Microsoft.OneFuzz.Service; | ||
|
||
|
||
|
||
namespace ApiService.TestHooks { | ||
public class CredsTestHookks { | ||
private readonly ILogTracer _log; | ||
private readonly IConfigOperations _configOps; | ||
private readonly ICreds _creds; | ||
|
||
public CredsTestHookks(ILogTracer log, IConfigOperations configOps, ICreds creds) { | ||
_log = log.WithTag("TestHooks", "ContainerTestHooks"); | ||
_configOps = configOps; | ||
_creds = creds; | ||
} | ||
|
||
[Function("GetSubscription")] | ||
public async Task<HttpResponseData> GetSubscription([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/creds/subscription")] HttpRequestData req) { | ||
_log.Info("Get subscription"); | ||
var resp = req.CreateResponse(HttpStatusCode.OK); | ||
await resp.WriteStringAsync(_creds.GetSubscription().ToString()); | ||
return resp; | ||
} | ||
|
||
|
||
[Function("GetBaseResourceGroup")] | ||
public async Task<HttpResponseData> GetBaseResourceGroup([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/creds/baseResourceGroup")] HttpRequestData req) { | ||
_log.Info("Get base resource group"); | ||
var resp = req.CreateResponse(HttpStatusCode.OK); | ||
await resp.WriteStringAsync(_creds.GetBaseResourceGroup().ToString()); | ||
return resp; | ||
} | ||
|
||
[Function("GetInstanceName")] | ||
public async Task<HttpResponseData> GetInstanceName([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/creds/instanceName")] HttpRequestData req) { | ||
_log.Info("Get instance name"); | ||
var resp = req.CreateResponse(HttpStatusCode.OK); | ||
await resp.WriteStringAsync(_creds.GetInstanceName().ToString()); | ||
return resp; | ||
} | ||
|
||
[Function("GetBaseRegion")] | ||
public async Task<HttpResponseData> GetBaseRegion([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/creds/baseRegion")] HttpRequestData req) { | ||
_log.Info("Get base region"); | ||
var resp = req.CreateResponse(HttpStatusCode.OK); | ||
var region = await _creds.GetBaseRegion(); | ||
await resp.WriteStringAsync(region); | ||
return resp; | ||
} | ||
|
||
[Function("GetInstanceUrl")] | ||
public async Task<HttpResponseData> GetInstanceUrl([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/creds/instanceUrl")] HttpRequestData req) { | ||
_log.Info("Get instance url"); | ||
var resp = req.CreateResponse(HttpStatusCode.OK); | ||
await resp.WriteStringAsync(_creds.GetInstanceUrl().ToString()); | ||
return resp; | ||
} | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/ApiService/ApiService/TestHooks/DiskOperationsTestHooks.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,36 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
|
||
using Microsoft.OneFuzz.Service; | ||
|
||
|
||
|
||
namespace ApiService.TestHooks { | ||
public class DiskOperationsTestHooks { | ||
private readonly ILogTracer _log; | ||
private readonly IConfigOperations _configOps; | ||
private readonly IDiskOperations _diskOps; | ||
private readonly ICreds _creds; | ||
|
||
public DiskOperationsTestHooks(ILogTracer log, IConfigOperations configOps, IDiskOperations diskOps, ICreds creds) { | ||
_log = log.WithTag("TestHooks", "ContainerTestHooks"); | ||
_configOps = configOps; | ||
_diskOps = diskOps; | ||
_creds = creds; | ||
} | ||
|
||
[Function("GetDiskNames")] | ||
public async Task<HttpResponseData> GetSubscription([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/disks")] HttpRequestData req) { | ||
_log.Info("Get disk names"); | ||
var resp = req.CreateResponse(HttpStatusCode.OK); | ||
var diskNames = _diskOps.ListDisks(_creds.GetBaseResourceGroup()).ToList().Select(x => x.Data.Name); | ||
await resp.WriteAsJsonAsync(diskNames); | ||
return resp; | ||
} | ||
|
||
|
||
|
||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/ApiService/ApiService/TestHooks/InstanceConfigTestHooks.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,72 @@ | ||
using System.Net; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using Microsoft.OneFuzz.Service; | ||
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm; | ||
|
||
namespace ApiService.TestHooks { | ||
public class InstanceConfigTestHooks { | ||
|
||
private readonly ILogTracer _log; | ||
private readonly IConfigOperations _configOps; | ||
|
||
public InstanceConfigTestHooks(ILogTracer log, IConfigOperations configOps) { | ||
_log = log.WithTag("TestHooks", "InstanceConfigTestHooks"); | ||
_configOps = configOps; | ||
} | ||
|
||
[Function("GetInstanceConfig")] | ||
public async Task<HttpResponseData> Get([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/instance-config")] HttpRequestData req) { | ||
_log.Info("Fetching instance config"); | ||
var config = await _configOps.Fetch(); | ||
|
||
if (config is null) { | ||
_log.Error("Instance config is null"); | ||
Error err = new(ErrorCode.INVALID_REQUEST, new[] { "Instance config is null" }); | ||
var resp = req.CreateResponse(HttpStatusCode.InternalServerError); | ||
await resp.WriteAsJsonAsync(err); | ||
return resp; | ||
} else { | ||
var str = (new EntityConverter()).ToJsonString(config); | ||
|
||
var resp = req.CreateResponse(HttpStatusCode.OK); | ||
await resp.WriteStringAsync(str); | ||
return resp; | ||
} | ||
} | ||
|
||
[Function("PatchInstanceConfig")] | ||
public async Task<HttpResponseData> Patch([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "testhooks/instance-config")] HttpRequestData req) { | ||
_log.Info("Patch instance config"); | ||
|
||
var s = await req.ReadAsStringAsync(); | ||
var newInstanceConfig = JsonSerializer.Deserialize<InstanceConfig>(s!, EntityConverter.GetJsonSerializerOptions()); | ||
|
||
if (newInstanceConfig is null) { | ||
var resp = req.CreateResponse(); | ||
resp.StatusCode = HttpStatusCode.BadRequest; | ||
await resp.WriteAsJsonAsync(new { Error = "Instance config is not set" }); | ||
return resp; | ||
} else { | ||
|
||
var query = UriExtension.GetQueryComponents(req.Url); | ||
bool isNew = UriExtension.GetBoolValue("isNew", query, false); | ||
//requireEtag wont' work since our current schema does not return etag to the client when getting data form the table, so | ||
// there is no way to know which etag to use | ||
bool requireEtag = UriExtension.GetBoolValue("requireEtag", query, false); | ||
|
||
await _configOps.Save(newInstanceConfig, isNew, requireEtag); | ||
|
||
var resp = req.CreateResponse(); | ||
resp.StatusCode = HttpStatusCode.OK; | ||
return resp; | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
} |
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,30 @@ | ||
namespace ApiService.TestHooks { | ||
public class UriExtension { | ||
|
||
public static IDictionary<string, string> GetQueryComponents(System.Uri uri) { | ||
var queryComponents = uri.GetComponents(UriComponents.Query, UriFormat.UriEscaped).Split("&"); | ||
|
||
var q = | ||
from cs in queryComponents | ||
where !string.IsNullOrEmpty(cs) | ||
let i = cs.IndexOf('=') | ||
select new KeyValuePair<string, string>(Uri.UnescapeDataString(cs.Substring(0, i)), Uri.UnescapeDataString(cs.Substring(i + 1))); | ||
|
||
return new Dictionary<string, string>(q); | ||
} | ||
|
||
public static bool GetBoolValue(string key, IDictionary<string, string> query, bool defaultValue = false) { | ||
bool v; | ||
if (query.ContainsKey(key)) { | ||
if (!bool.TryParse(query[key], out v)) { | ||
v = defaultValue; | ||
} | ||
} else { | ||
v = defaultValue; | ||
} | ||
return v; | ||
} | ||
|
||
|
||
} | ||
} |
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