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

Commit

Permalink
scaleset extensions (#1861)
Browse files Browse the repository at this point in the history
Co-authored-by: stas <statis@microsoft.com>
  • Loading branch information
stishkin and stas authored Apr 28, 2022
1 parent b28e1c7 commit 26665f6
Show file tree
Hide file tree
Showing 9 changed files with 628 additions and 37 deletions.
4 changes: 3 additions & 1 deletion src/ApiService/ApiService/OneFuzzTypes/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ public record AzureMonitorExtensionConfig(

public record AzureVmExtensionConfig(
KeyvaultExtensionConfig? Keyvault,
AzureMonitorExtensionConfig AzureMonitor
AzureMonitorExtensionConfig? AzureMonitor,
AzureSecurityExtensionConfig? AzureSecurity,
GenevaExtensionConfig? Geneva
);

public record NetworkConfig(
Expand Down
25 changes: 25 additions & 0 deletions src/ApiService/ApiService/OneFuzzTypes/ReturnTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,32 @@ public struct OneFuzzResult<T_Ok> {

public static OneFuzzResult<T_Ok> Ok(T_Ok ok) => new(ok);
public static OneFuzzResult<T_Ok> Error(ErrorCode errorCode, string[] errors) => new(errorCode, errors);
public static OneFuzzResult<T_Ok> Error(ErrorCode errorCode, string error) => new(errorCode, new[] { error });

public static OneFuzzResult<T_Ok> Error(Error err) => new(err);
}


public struct OneFuzzResultVoid {
static Error NoError = new(0);

readonly Error error;
readonly bool isOk;

public bool IsOk => isOk;

public Error ErrorV => error;

private OneFuzzResultVoid(ErrorCode errorCode, string[] errors) => (error, isOk) = (new Error(errorCode, errors), false);

private OneFuzzResultVoid(Error err) => (error, isOk) = (err, false);

public static OneFuzzResultVoid Ok() => new();
public static OneFuzzResultVoid Error(ErrorCode errorCode, string[] errors) => new(errorCode, errors);
public static OneFuzzResultVoid Error(ErrorCode errorCode, string error) => new(errorCode, new[] { error });
public static OneFuzzResultVoid Error(Error err) => new(err);
}



}
2 changes: 2 additions & 0 deletions src/ApiService/ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public static void Main() {
.AddScoped<IScheduler, Scheduler>()
.AddScoped<IConfig, Config>()
.AddScoped<ILogAnalytics, LogAnalytics>()
.AddScoped<IExtensions, Extensions>()
.AddScoped<IVmssOperations, VmssOperations>()

//Move out expensive resources into separate class, and add those as Singleton
// ArmClient, Table Client(s), Queue Client(s), HttpClient, etc.\
Expand Down
71 changes: 70 additions & 1 deletion src/ApiService/ApiService/TestHooks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
Expand All @@ -13,12 +14,16 @@ public class TestHooks {
private readonly IConfigOperations _configOps;
private readonly IEvents _events;
private readonly IServiceConfig _config;
private readonly ISecretsOperations _secretOps;
private readonly ILogAnalytics _logAnalytics;

public TestHooks(ILogTracer log, IConfigOperations configOps, IEvents events, IServiceConfig config) {
public TestHooks(ILogTracer log, IConfigOperations configOps, IEvents events, IServiceConfig config, ISecretsOperations secretOps, ILogAnalytics logAnalytics) {
_log = log;
_configOps = configOps;
_events = events;
_config = config;
_secretOps = secretOps;
_logAnalytics = logAnalytics;
}

[Function("Info")]
Expand Down Expand Up @@ -57,4 +62,68 @@ public async Task<HttpResponseData> InstanceConfig([HttpTrigger(AuthorizationLev
return resp;
}
}

[Function("GetKeyvaultAddress")]
public async Task<HttpResponseData> GetKeyVaultAddress([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/secrets/keyvaultaddress")] HttpRequestData req) {
_log.Info("Getting keyvault address");
var addr = _secretOps.GetKeyvaultAddress();
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteAsJsonAsync(addr);
return resp;
}

[Function("SaveToKeyvault")]
public async Task<HttpResponseData> SaveToKeyvault([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "testhooks/secrets/keyvault")] HttpRequestData req) {
var s = await req.ReadAsStringAsync();
var secretData = JsonSerializer.Deserialize<SecretData<string>>(s!, EntityConverter.GetJsonSerializerOptions());
if (secretData is null) {
_log.Error("Secret data is null");
return req.CreateResponse(HttpStatusCode.BadRequest);
} else {
_log.Info($"Saving secret data in the keyvault");
var r = await _secretOps.SaveToKeyvault(secretData);
var addr = _secretOps.GetKeyvaultAddress();
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteAsJsonAsync(addr);
return resp;
}
}

[Function("GetSecretStringValue")]
public async Task<HttpResponseData> GetSecretStringValue([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/secrets/keyvault")] HttpRequestData req) {
var queryComponents = req.Url.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)));

var qs = new Dictionary<string, string>(q);
var d = await _secretOps.GetSecretStringValue(new SecretData<string>(qs["SecretName"]));

var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteAsJsonAsync(d);
return resp;
}


[Function("GetWorkspaceId")]
public async Task<HttpResponseData> GetWorkspaceId([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/logAnalytics/workspaceId")] HttpRequestData req) {
var id = _logAnalytics.GetWorkspaceId();
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteAsJsonAsync(id);
return resp;
}



[Function("GetMonitorSettings")]
public async Task<HttpResponseData> GetMonitorSettings([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/logAnalytics/monitorSettings")] HttpRequestData req) {
var settings = await _logAnalytics.GetMonitorSettings();
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteAsJsonAsync(settings);
return resp;
}

}
64 changes: 32 additions & 32 deletions src/ApiService/ApiService/onefuzzlib/Containers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,35 +142,35 @@ public async Async.Task<Guid> GetInstanceId() {
}
return System.Guid.Parse(blob.ToString());
}

public Uri? GetContainerSasUrlService(
BlobContainerClient client,
BlobSasPermissions permissions,
bool tag = false,
TimeSpan? timeSpan = null) {
var (start, expiry) = SasTimeWindow(timeSpan ?? TimeSpan.FromDays(30.0));
var sasBuilder = new BlobSasBuilder(permissions, expiry) { StartsOn = start };
var sas = client.GenerateSasUri(sasBuilder);
return sas;
}


//TODO: instead of returning null when container not found, convert to return to "Result" type and set appropriate error
public async Async.Task<Uri?> GetContainerSasUrl(Container container, StorageType storageType, BlobSasPermissions permissions) {
var client = await FindContainer(container, storageType);

if (client is null) {
return null;
}

var uri = GetContainerSasUrlService(client, permissions);

if (uri is null) {
//TODO: return result error
return uri;
} else {
return uri;
}
}
}


public Uri? GetContainerSasUrlService(
BlobContainerClient client,
BlobSasPermissions permissions,
bool tag = false,
TimeSpan? timeSpan = null) {
var (start, expiry) = SasTimeWindow(timeSpan ?? TimeSpan.FromDays(30.0));
var sasBuilder = new BlobSasBuilder(permissions, expiry) { StartsOn = start };
var sas = client.GenerateSasUri(sasBuilder);
return sas;
}


//TODO: instead of returning null when container not found, convert to return to "Result" type and set appropriate error
public async Async.Task<Uri?> GetContainerSasUrl(Container container, StorageType storageType, BlobSasPermissions permissions) {
var client = await FindContainer(container, storageType);

if (client is null) {
return null;
}

var uri = GetContainerSasUrlService(client, permissions);

if (uri is null) {
//TODO: return result error
return uri;
} else {
return uri;
}
}
}
Loading

0 comments on commit 26665f6

Please sign in to comment.