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

Starting work... #1736

Merged
merged 2 commits into from
Apr 1, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@

# vim
*.swp

/.ionide/symbolCache.db
21 changes: 15 additions & 6 deletions src/ApiService/ApiService.sln
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
# Visual Studio Version 17
VisualStudioVersion = 17.1.32328.378
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiService", "ApiService/ApiService.csproj", "{D9112E8C-A539-4B06-B78D-B01A7D0B0555}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiService", "ApiService\ApiService.csproj", "{D9112E8C-A539-4B06-B78D-B01A7D0B0555}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{06C9FE9B-6DDD-45EC-B716-CB074512DAA9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D9112E8C-A539-4B06-B78D-B01A7D0B0555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9112E8C-A539-4B06-B78D-B01A7D0B0555}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9112E8C-A539-4B06-B78D-B01A7D0B0555}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9112E8C-A539-4B06-B78D-B01A7D0B0555}.Release|Any CPU.Build.0 = Release|Any CPU
{06C9FE9B-6DDD-45EC-B716-CB074512DAA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06C9FE9B-6DDD-45EC-B716-CB074512DAA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06C9FE9B-6DDD-45EC-B716-CB074512DAA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06C9FE9B-6DDD-45EC-B716-CB074512DAA9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {390388A1-8F1C-4B89-93B4-68C69A329558}
EndGlobalSection
EndGlobal
18 changes: 17 additions & 1 deletion src/ApiService/ApiService/ApiService.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="4.0.4" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.EventGrid" Version="2.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.20.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.5.2" />
<PackageReference Include="Azure.Data.Tables" Version="12.5.0" />
<PackageReference Include="Azure.ResourceManager.Compute" Version="1.0.0-beta.6" />
<PackageReference Include="Azure.Core" Version="1.23.0" />
<PackageReference Include="Azure.Identity" Version="1.5.0" />
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.9.0" />
<PackageReference Include="Azure.ResourceManager" Version="1.0.0-beta.8" />
<PackageReference Include="Azure.ResourceManager.Network" Version="1.0.0-beta.5" />
<PackageReference Include="Azure.ResourceManager.Resources" Version="1.0.0-beta.6" />
<PackageReference Include="Azure.ResourceManager.Storage" Version="1.0.0-beta.6" />
<PackageReference Include="Microsoft.Graph" Version="4.20.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.42.0" />
<PackageReference Include="Microsoft.Identity.Web.TokenCache" Version="1.23.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.16.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand Down
39 changes: 39 additions & 0 deletions src/ApiService/ApiService/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
namespace Microsoft.OneFuzz.Service;

public static class EnvironmentVariables {

public static class AppInsights {
public static string? AppId { get => Environment.GetEnvironmentVariable("APPINSIGHTS_APPID"); }
public static string? InstrumentationKey { get => Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY"); }
}

public static class AzureSignalR {
public static string? ConnectionString { get => Environment.GetEnvironmentVariable("AzureSignalRConnectionString"); }
public static string? ServiceTransportType { get => Environment.GetEnvironmentVariable("AzureSignalRServiceTransportType"); }
}

public static class AzureWebJob {
public static string? DisableHomePage { get => Environment.GetEnvironmentVariable("AzureWebJobsDisableHomepage"); }
public static string? Storage { get => Environment.GetEnvironmentVariable("AzureWebJobsStorage"); }
}

public static class DiagnosticsAzureBlob {
public static string? ContainerSasUrl { get => Environment.GetEnvironmentVariable("DIAGNOSTICS_AZUREBLOBCONTAINERSASURL"); }
public static string? RetentionDays { get => Environment.GetEnvironmentVariable("DIAGNOSTICS_AZUREBLOBRETENTIONINDAYS"); }
}

public static string? MultiTenantDomain { get => Environment.GetEnvironmentVariable("MULTI_TENANT_DOMAIN"); }

public static class OneFuzz {
public static string? DataStorage { get => Environment.GetEnvironmentVariable("ONEFUZZ_DATA_STORAGE"); }
public static string? FuncStorage { get => Environment.GetEnvironmentVariable("ONEFUZZ_FUNC_STORAGE"); }
public static string? Instance { get => Environment.GetEnvironmentVariable("ONEFUZZ_INSTANCE"); }
public static string? InstanceName { get => Environment.GetEnvironmentVariable("ONEFUZZ_INSTANCE_NAME"); }
public static string? Keyvault { get => Environment.GetEnvironmentVariable("ONEFUZZ_KEYVAULT"); }
public static string? Monitor { get => Environment.GetEnvironmentVariable("ONEFUZZ_MONITOR"); }
public static string? Owner { get => Environment.GetEnvironmentVariable("ONEFUZZ_OWNER"); }
public static string? ResourceGroup { get => Environment.GetEnvironmentVariable("ONEFUZZ_RESOURCE_GROUP"); }
public static string? Telemetry { get => Environment.GetEnvironmentVariable("ONEFUZZ_TELEMETRY"); }
}
}
67 changes: 67 additions & 0 deletions src/ApiService/ApiService/HttpClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Net.Http.Headers;

namespace Microsoft.OneFuzz.Service;
using TokenType = String;
using AccessToken = String;

public class Request {
private static HttpClient httpClient = new HttpClient();

Func<Task<Tuple<TokenType, AccessToken>>>? auth;

public Request(Func<Task<Tuple<TokenType, AccessToken>>>? auth = null) {
this.auth = auth;
}

private async Task<HttpResponseMessage> Send(HttpMethod method, Uri url, HttpContent? content = null, IDictionary<string, string>? headers = null) {
var request = new HttpRequestMessage(method: method, requestUri: url);

if (auth is not null) {
Tuple<TokenType, AccessToken> token = await auth();
request.Headers.Authorization = new AuthenticationHeaderValue(token.Item1, token.Item2);
}

if (content is not null) {
request.Content = content;
}

if (headers is not null) {
foreach(var v in headers) {
request.Headers.Add(v.Key, v.Value);
}
}

return await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
}

public async Task<HttpResponseMessage> Get(Uri url) {
return await Send(method: HttpMethod.Get, url: url);
}
public async Task<HttpResponseMessage> Delete(Uri url)
{
return await Send(method: HttpMethod.Delete, url: url);
}

public async Task<HttpResponseMessage> Post(Uri url, String json, IDictionary<string, string>? headers = null) {
using var b = new StringContent(json);
b.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
return await Send(method: HttpMethod.Post, url: url, headers:headers);
}

public async Task<HttpResponseMessage> Put(Uri url, String json, IDictionary<string, string>? headers = null) {
using var b = new StringContent(json);
b.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
return await Send(method: HttpMethod.Put, url: url, headers: headers);
}

public async Task<HttpResponseMessage> Patch(Uri url, String json, IDictionary<string, string>? headers = null)
{
using var b = new StringContent(json);
b.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
return await Send(method: HttpMethod.Patch, url: url, headers: headers);
}
}
19 changes: 9 additions & 10 deletions src/ApiService/ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Azure.Functions.Worker.Configuration;

namespace ApiService
namespace Microsoft.OneFuzz.Service;

public class Program
{
public class Program
public static void Main()
{
public static void Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.Build();
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.Build();

host.Run();
}
host.Run();
}
}
}
72 changes: 34 additions & 38 deletions src/ApiService/ApiService/QueueNodeHearbeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,55 @@
using System.Threading.Tasks;
using Azure;

namespace Microsoft.OneFuzz.Service;

namespace ApiService
{


enum HeartbeatType
{
MachineAlive,
TaskAlive,
}

enum HeartbeatType
{
MachineAlive,
TaskAlive,
}
record NodeHeartbeatEntry(string NodeId, Dictionary<string, HeartbeatType>[] data);

record NodeHeartbeatEntry(string NodeId, Dictionary<string, HeartbeatType>[] data);

public class QueueNodeHearbeat
{

public class QueueNodeHearbeat
private (string, string) GetStorageAccountNameAndKey(string? accountId)
{
//TableEntity
return ("test", "test");
}

private (string, string) GetStorageAccountNameAndKey(string? accountId)
{
//TableEntity
return ("test", "test");
}

private async Task<TableServiceClient> GetStorageClient(string? table, string? accounId)
private async Task<TableServiceClient> GetStorageClient(string? table, string? accounId)
{
accounId ??= System.Environment.GetEnvironmentVariable("ONEFUZZ_FUNC_STORAGE");
if (accounId == null)
{
accounId ??= System.Environment.GetEnvironmentVariable("ONEFUZZ_FUNC_STORAGE");
if (accounId == null)
{
throw new Exception("ONEFUZZ_FUNC_STORAGE environment variable not set");
}
var (name, key) = GetStorageAccountNameAndKey(accounId);
var tableClient = new TableServiceClient(new Uri(accounId), new TableSharedKeyCredential(name, key));
await tableClient.CreateTableIfNotExistsAsync(table);
return tableClient;
throw new Exception("ONEFUZZ_FUNC_STORAGE environment variable not set");
}
var (name, key) = GetStorageAccountNameAndKey(accounId);
var tableClient = new TableServiceClient(new Uri(accounId), new TableSharedKeyCredential(name, key));
await tableClient.CreateTableIfNotExistsAsync(table);
return tableClient;
}

private readonly ILogger _logger;
private readonly ILogger _logger;

public QueueNodeHearbeat(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<QueueNodeHearbeat>();
}
public QueueNodeHearbeat(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<QueueNodeHearbeat>();
}

[Function("QueueNodeHearbeat")]
public void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string msg)
{
var hb = JsonSerializer.Deserialize<NodeHeartbeatEntry>(msg);
[Function("QueueNodeHearbeat")]
public void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string msg)
{
var hb = JsonSerializer.Deserialize<NodeHeartbeatEntry>(msg);



_logger.LogInformation($"heartbeat: {msg}");
}
_logger.LogInformation($"heartbeat: {msg}");
}
}

Expand Down
Loading