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

Commit 8706bae

Browse files
stishkinstas
authored andcommitted
instance config (#1791)
* instance config * address PR comments * make logs scoped * make logs scoped Co-authored-by: stas <statis@microsoft.com>
1 parent 95dc487 commit 8706bae

File tree

14 files changed

+261
-128
lines changed

14 files changed

+261
-128
lines changed

src/ApiService/ApiService/Info.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/ApiService/ApiService/Log.cs

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,34 @@ public interface ILogTracer
132132
void ForceFlush();
133133
void Info(string message);
134134
void Warning(string message);
135+
void Verbose(string message);
135136

136-
ILogTracer AddTags((string, string)[]? tags);
137+
ILogTracer WithTag(string k, string v);
138+
ILogTracer WithTags((string, string)[]? tags);
137139
}
138140

139141
internal interface ILogTracerInternal : ILogTracer
142+
{
143+
void ReplaceCorrelationId(Guid newCorrelationId);
144+
void AddTags((string, string)[] tags);
145+
}
146+
147+
148+
149+
public class LogTracer : ILogTracerInternal
140150
{
141151
private string? GetCaller()
142152
{
143153
return new StackTrace()?.GetFrame(2)?.GetMethod()?.DeclaringType?.FullName;
144154
}
145155

156+
private Guid _correlationId;
146157
private List<ILog> _loggers;
158+
private Dictionary<string, string> _tags;
159+
private SeverityLevel _logSeverityLevel;
147160

148-
public Guid CorrelationId { get; }
149-
public IReadOnlyDictionary<string, string> Tags { get; }
150-
161+
public Guid CorrelationId => _correlationId;
162+
public IReadOnlyDictionary<string, string> Tags => _tags;
151163

152164
private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]? tags)
153165
{
@@ -166,17 +178,43 @@ private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]
166178
}
167179
}
168180

169-
public LogTracer(Guid correlationId, (string, string)[]? tags, List<ILog> loggers) : this(correlationId, new Dictionary<string, string>(ConvertTags(tags)), loggers) { }
181+
public LogTracer(Guid correlationId, (string, string)[]? tags, List<ILog> loggers, SeverityLevel logSeverityLevel) :
182+
this(correlationId, new Dictionary<string, string>(ConvertTags(tags)), loggers, logSeverityLevel)
183+
{ }
170184

171185

172-
public LogTracer(Guid correlationId, IReadOnlyDictionary<string, string> tags, List<ILog> loggers)
186+
public LogTracer(Guid correlationId, IReadOnlyDictionary<string, string> tags, List<ILog> loggers, SeverityLevel logSeverityLevel)
173187
{
174-
CorrelationId = correlationId;
175-
Tags = tags;
188+
_correlationId = correlationId;
189+
_tags = new(tags);
176190
_loggers = loggers;
191+
_logSeverityLevel = logSeverityLevel;
192+
}
193+
194+
//Single threaded only
195+
public void ReplaceCorrelationId(Guid newCorrelationId)
196+
{
197+
_correlationId = newCorrelationId;
198+
}
199+
200+
//single threaded only
201+
public void AddTags((string, string)[] tags)
202+
{
203+
if (tags is not null)
204+
{
205+
foreach (var (k, v) in tags)
206+
{
207+
_tags[k] = v;
208+
}
209+
}
177210
}
178211

179-
public ILogTracer AddTags((string, string)[]? tags)
212+
public ILogTracer WithTag(string k, string v)
213+
{
214+
return WithTags(new[] { (k, v) });
215+
}
216+
217+
public ILogTracer WithTags((string, string)[]? tags)
180218
{
181219
var newTags = new Dictionary<string, string>(Tags);
182220
if (tags is not null)
@@ -186,42 +224,66 @@ public ILogTracer AddTags((string, string)[]? tags)
186224
newTags[k] = v;
187225
}
188226
}
189-
return new LogTracer(CorrelationId, newTags, _loggers);
227+
return new LogTracer(CorrelationId, newTags, _loggers, _logSeverityLevel);
228+
}
229+
230+
public void Verbose(string message)
231+
{
232+
if (_logSeverityLevel >= SeverityLevel.Verbose)
233+
{
234+
var caller = GetCaller();
235+
foreach (var logger in _loggers)
236+
{
237+
logger.Log(CorrelationId, message, SeverityLevel.Verbose, Tags, caller);
238+
}
239+
}
190240
}
191241

192242
public void Info(string message)
193243
{
194-
var caller = GetCaller();
195-
foreach (var logger in _loggers)
244+
if (_logSeverityLevel >= SeverityLevel.Information)
196245
{
197-
logger.Log(CorrelationId, message, SeverityLevel.Information, Tags, caller);
246+
var caller = GetCaller();
247+
foreach (var logger in _loggers)
248+
{
249+
logger.Log(CorrelationId, message, SeverityLevel.Information, Tags, caller);
250+
}
198251
}
199252
}
200253

201254
public void Warning(string message)
202255
{
203-
var caller = GetCaller();
204-
foreach (var logger in _loggers)
256+
if (_logSeverityLevel >= SeverityLevel.Warning)
205257
{
206-
logger.Log(CorrelationId, message, SeverityLevel.Warning, Tags, caller);
258+
var caller = GetCaller();
259+
foreach (var logger in _loggers)
260+
{
261+
logger.Log(CorrelationId, message, SeverityLevel.Warning, Tags, caller);
262+
}
207263
}
208264
}
209265

210266
public void Error(string message)
211267
{
212-
var caller = GetCaller();
213-
foreach (var logger in _loggers)
268+
if (_logSeverityLevel >= SeverityLevel.Error)
214269
{
215-
logger.Log(CorrelationId, message, SeverityLevel.Error, Tags, caller);
270+
var caller = GetCaller();
271+
foreach (var logger in _loggers)
272+
{
273+
logger.Log(CorrelationId, message, SeverityLevel.Error, Tags, caller);
274+
}
216275
}
217276
}
218277

219278
public void Critical(string message)
220279
{
221-
var caller = GetCaller();
222-
foreach (var logger in _loggers)
280+
if (_logSeverityLevel >= SeverityLevel.Critical)
223281
{
224-
logger.Log(CorrelationId, message, SeverityLevel.Critical, Tags, caller);
282+
var caller = GetCaller();
283+
foreach (var logger in _loggers)
284+
{
285+
logger.Log(CorrelationId, message, SeverityLevel.Critical, Tags, caller);
286+
}
225287
}
226288
}
227289

@@ -254,7 +316,7 @@ public void ForceFlush()
254316

255317
public interface ILogTracerFactory
256318
{
257-
LogTracer MakeLogTracer(Guid correlationId, (string, string)[]? tags = null);
319+
LogTracer CreateLogTracer(Guid correlationId, (string, string)[]? tags = null, SeverityLevel severityLevel = SeverityLevel.Verbose);
258320
}
259321

260322
public class LogTracerFactory : ILogTracerFactory
@@ -266,9 +328,9 @@ public LogTracerFactory(List<ILog> loggers)
266328
_loggers = loggers;
267329
}
268330

269-
public LogTracer MakeLogTracer(Guid correlationId, (string, string)[]? tags = null)
331+
public LogTracer CreateLogTracer(Guid correlationId, (string, string)[]? tags = null, SeverityLevel severityLevel = SeverityLevel.Verbose)
270332
{
271-
return new(correlationId, tags, _loggers);
333+
return new(correlationId, tags, _loggers, severityLevel);
272334
}
273335

274336
}

src/ApiService/ApiService/OneFuzzTypes/Model.cs

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
22
using System;
33
using System.Collections.Generic;
4-
using PoolName = System.String;
5-
using Region = System.String;
4+
using System.Text.Json.Serialization;
5+
66
using Container = System.String;
7+
using Region = System.String;
8+
using PoolName = System.String;
9+
using Endpoint = System.String;
10+
using GroupId = System.Guid;
11+
using PrincipalId = System.Guid;
712

813
namespace Microsoft.OneFuzz.Service;
914

@@ -16,6 +21,7 @@ namespace Microsoft.OneFuzz.Service;
1621
/// the "partion key" and "row key" are identified by the [PartitionKey] and [RowKey] attributes
1722
/// Guids are mapped to string in the db
1823

24+
1925
public record Authentication
2026
(
2127
string Password,
@@ -259,5 +265,118 @@ public record Task(
259265
{
260266
List<TaskEventSummary> Events { get; set; } = new List<TaskEventSummary>();
261267
List<NodeAssignment> Nodes { get; set; } = new List<NodeAssignment>();
268+
}
269+
public record AzureSecurityExtensionConfig();
270+
public record GenevaExtensionConfig();
271+
272+
273+
public record KeyvaultExtensionConfig(
274+
string KeyVaultName,
275+
string CertName,
276+
string CertPath,
277+
string ExtensionStore
278+
);
279+
280+
public record AzureMonitorExtensionConfig(
281+
string ConfigVersion,
282+
string Moniker,
283+
string Namespace,
284+
[property: JsonPropertyName("monitoringGSEnvironment")] string MonitoringGSEnvironment,
285+
[property: JsonPropertyName("monitoringGCSAccount")] string MonitoringGCSAccount,
286+
[property: JsonPropertyName("monitoringGCSAuthId")] string MonitoringGCSAuthId,
287+
[property: JsonPropertyName("monitoringGCSAuthIdType")] string MonitoringGCSAuthIdType
288+
);
262289

290+
public record AzureVmExtensionConfig(
291+
KeyvaultExtensionConfig? Keyvault,
292+
AzureMonitorExtensionConfig AzureMonitor
293+
);
294+
295+
public record NetworkConfig(
296+
string AddressSpace,
297+
string Subnet
298+
)
299+
{
300+
public NetworkConfig() : this("10.0.0.0/8", "10.0.0.0/16") { }
301+
}
302+
303+
public record NetworkSecurityGroupConfig(
304+
string[] AllowedServiceTags,
305+
string[] AllowedIps
306+
)
307+
{
308+
public NetworkSecurityGroupConfig() : this(Array.Empty<string>(), Array.Empty<string>()) { }
309+
}
310+
311+
public record ApiAccessRule(
312+
string[] Methods,
313+
Guid[] AllowedGroups
314+
);
315+
316+
public record InstanceConfig
317+
(
318+
[PartitionKey, RowKey] string InstanceName,
319+
//# initial set of admins can only be set during deployment.
320+
//# if admins are set, only admins can update instance configs.
321+
Guid[]? Admins,
322+
//# if set, only admins can manage pools or scalesets
323+
bool AllowPoolManagement,
324+
string[] AllowedAadTenants,
325+
NetworkConfig NetworkConfig,
326+
NetworkSecurityGroupConfig ProxyNsgConfig,
327+
AzureVmExtensionConfig? Extensions,
328+
string ProxyVmSku,
329+
IDictionary<Endpoint, ApiAccessRule>? ApiAccessRules,
330+
IDictionary<PrincipalId, GroupId[]>? GroupMembership,
331+
332+
IDictionary<string, string>? VmTags,
333+
IDictionary<string, string>? VmssTags
334+
) : EntityBase()
335+
{
336+
public InstanceConfig(string instanceName) : this(
337+
instanceName,
338+
null,
339+
true,
340+
Array.Empty<string>(),
341+
new NetworkConfig(),
342+
new NetworkSecurityGroupConfig(),
343+
null,
344+
"Standard_B2s",
345+
null,
346+
null,
347+
null,
348+
null)
349+
{ }
350+
351+
public List<Guid>? CheckAdmins(List<Guid>? value)
352+
{
353+
if (value is not null && value.Count == 0)
354+
{
355+
throw new ArgumentException("admins must be null or contain at least one UUID");
356+
}
357+
else
358+
{
359+
return value;
360+
}
361+
}
362+
363+
364+
//# At the moment, this only checks allowed_aad_tenants, however adding
365+
//# support for 3rd party JWT validation is anticipated in a future release.
366+
public ResultOk<List<string>> CheckInstanceConfig()
367+
{
368+
List<string> errors = new();
369+
if (AllowedAadTenants.Length == 0)
370+
{
371+
errors.Add("allowed_aad_tenants must not be empty");
372+
}
373+
if (errors.Count == 0)
374+
{
375+
return ResultOk<List<string>>.Ok();
376+
}
377+
else
378+
{
379+
return ResultOk<List<string>>.Error(errors);
380+
}
381+
}
263382
}

0 commit comments

Comments
 (0)