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

Commit 2c7109d

Browse files
nharper285chkeita
authored andcommitted
[C# Port] Adding new Proxy Update Queue Function. (#1757)
* Adding QueueProxyUpdate. * Setting to serializer. * Updates. * Updating with new ORM model and [model]Operation. * Fixing return type. * Working on changes. * Tested and ready for review. * Formatting. * Removing test code. * Update src/ApiService/Tests/OrmTest.cs Co-authored-by: Cheick Keita <chkeita@microsoft.com> * Fixing tests. * Fixing tests again. * Asserting null in tests. * Adding null param. * Removing whitespace. * syntax error. Co-authored-by: Cheick Keita <chkeita@microsoft.com>
1 parent 8239675 commit 2c7109d

File tree

5 files changed

+38
-40
lines changed

5 files changed

+38
-40
lines changed

src/ApiService/ApiService/OneFuzzTypes/Model.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
22
using System;
33
using System.Collections.Generic;
4-
using System.Text.Json.Serialization;
5-
6-
using Container = System.String;
7-
using Region = System.String;
84
using PoolName = System.String;
95
using Endpoint = System.String;
106
using GroupId = System.Guid;
@@ -21,6 +17,12 @@ namespace Microsoft.OneFuzz.Service;
2117
/// the "partion key" and "row key" are identified by the [PartitionKey] and [RowKey] attributes
2218
/// Guids are mapped to string in the db
2319

20+
public record Authentication
21+
(
22+
string Password,
23+
string PublicKey,
24+
string PrivateKey
25+
);
2426

2527
public record Authentication
2628
(
@@ -92,7 +94,7 @@ public enum NodeState
9294

9395
public record ProxyHeartbeat
9496
(
95-
Region Region,
97+
string Region,
9698
Guid ProxyId,
9799
List<ProxyForward> Forwards,
98100
DateTimeOffset TimeStamp
@@ -116,7 +118,7 @@ bool DebugKeepNode
116118

117119
public partial record ProxyForward
118120
(
119-
[PartitionKey] Region Region,
121+
[PartitionKey] string Region,
120122
[RowKey] int DstPort,
121123
int SrcPort,
122124
string DstIp
@@ -126,7 +128,7 @@ public partial record ProxyConfig
126128
(
127129
Uri Url,
128130
string Notification,
129-
Region Region,
131+
string Region,
130132
Guid? ProxyId,
131133
List<ProxyForward> Forwards,
132134
string InstanceTelemetryKey,
@@ -136,7 +138,7 @@ string MicrosoftTelemetryKey
136138

137139
public partial record Proxy
138140
(
139-
[PartitionKey] Region Region,
141+
[PartitionKey] string Region,
140142
[RowKey] Guid ProxyId,
141143
DateTimeOffset? CreatedTimestamp,
142144
VmState State,

src/ApiService/ApiService/Program.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,16 @@ public static void Main()
6565
)
6666
.ConfigureServices((context, services) =>
6767
services
68-
.AddScoped<ILogTracer>(s => new LogTracerFactory(GetLoggers()).CreateLogTracer(Guid.Empty, severityLevel: EnvironmentVariables.LogSeverityLevel()))
69-
.AddScoped<INodeOperations, NodeOperations>()
70-
.AddScoped<IEvents, Events>()
71-
.AddScoped<IWebhookOperations, WebhookOperations>()
72-
.AddScoped<IWebhookMessageLogOperations, WebhookMessageLogOperations>()
73-
.AddScoped<ITaskOperations, TaskOperations>()
74-
.AddScoped<IQueue, Queue>()
75-
.AddScoped<ICreds, Creds>()
76-
.AddScoped<IStorage, Storage>()
77-
.AddScoped<IProxyOperations, ProxyOperations>()
78-
.AddScoped<IConfigOperations, ConfigOperations>()
79-
80-
//TODO: move out expensive resources into separate class, and add those as Singleton
81-
// ArmClient, Table Client(s), Queue Client(s), HttpClient, etc.
82-
68+
.AddSingleton<ILogTracerFactory>(_ => new LogTracerFactory(GetLoggers()))
69+
.AddSingleton<IStorageProvider>(_ => new StorageProvider(EnvironmentVariables.OneFuzz.FuncStorage ?? throw new InvalidOperationException("Missing account id")))
70+
.AddSingleton<INodeOperations, NodeOperations>()
71+
.AddSingleton<IEvents, Events>()
72+
.AddSingleton<IWebhookOperations, WebhookOperations>()
73+
.AddSingleton<IWebhookMessageLogOperations, WebhookMessageLogOperations>()
74+
.AddSingleton<IQueue, Queue>()
75+
.AddSingleton<ICreds>(_ => new Creds())
76+
.AddSingleton<IStorage, Storage>()
77+
.AddSingleton<IProxyOperations, ProxyOperations>()
8378
)
8479
.Build();
8580

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
11
using System;
22
using Microsoft.Azure.Functions.Worker;
3+
using Microsoft.Extensions.Logging;
34
using System.Text.Json;
5+
using System.Threading.Tasks;
46
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
57

68
namespace Microsoft.OneFuzz.Service;
79

810
public class QueueProxyHearbeat
911
{
10-
private readonly ILogTracer _log;
12+
private readonly ILogger _logger;
1113

1214
private readonly IProxyOperations _proxy;
1315

14-
public QueueProxyHearbeat(ILogTracer log, IProxyOperations proxy)
16+
public QueueProxyHearbeat(ILoggerFactory loggerFactory, IProxyOperations proxy)
1517
{
16-
_log = log;
18+
_logger = loggerFactory.CreateLogger<QueueProxyHearbeat>();
1719
_proxy = proxy;
1820
}
1921

2022
[Function("QueueProxyHearbeat")]
21-
public async Async.Task Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string msg)
23+
public async Task Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string msg)
2224
{
23-
_log.Info($"heartbeat: {msg}");
25+
_logger.LogInformation($"heartbeat: {msg}");
2426

2527
var hb = JsonSerializer.Deserialize<ProxyHeartbeat>(msg, EntityConverter.GetJsonSerializerOptions()).EnsureNotNull($"wrong data {msg}"); ;
2628
var newHb = hb with { TimeStamp = DateTimeOffset.UtcNow };
2729

2830
var proxy = await _proxy.GetByProxyId(newHb.ProxyId);
2931

30-
var log = _log.WithTag("ProxyId", newHb.ProxyId.ToString());
31-
3232
if (proxy == null)
3333
{
34-
log.Warning($"invalid proxy id: {newHb.ProxyId}");
34+
_logger.LogWarning($"invalid proxy id: {newHb.ProxyId}");
3535
return;
3636
}
3737
var newProxy = proxy with { heartbeat = newHb };
3838

39-
var r = await _proxy.Replace(newProxy);
40-
if (!r.IsOk)
41-
{
42-
var (status, reason) = r.ErrorV;
43-
log.Error($"Failed to replace proxy heartbeat record due to [{status}] {reason}");
44-
}
39+
await _proxy.Replace(newProxy);
40+
4541
}
4642
}

src/ApiService/ApiService/onefuzzlib/ProxyOperations.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using Microsoft.Extensions.Logging;
56

67
namespace Microsoft.OneFuzz.Service;
78

@@ -11,12 +12,12 @@ public interface IProxyOperations : IOrm<Proxy>
1112
}
1213
public class ProxyOperations : Orm<Proxy>, IProxyOperations
1314
{
14-
private readonly ILogTracer _log;
15+
private readonly ILogger _logger;
1516

16-
public ProxyOperations(ILogTracer log, IStorage storage)
17+
public ProxyOperations(ILoggerFactory loggerFactory, IStorage storage)
1718
: base(storage)
1819
{
19-
_log = log;
20+
_logger = loggerFactory.CreateLogger<QueueProxyHearbeat>();
2021
}
2122

2223
public async Task<Proxy?> GetByProxyId(Guid proxyId)

src/ApiService/ApiService/onefuzzlib/orm/EntityConverter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ public T ToRecord<T>(TableEntity entity) where T : EntityBase
257257
else
258258
{
259259
var value = entity.GetString(fieldName);
260+
if (value == null)
261+
{
262+
return null;
263+
}
260264
return JsonSerializer.Deserialize(value, ef.type, options: _options); ;
261265
}
262266
}

0 commit comments

Comments
 (0)