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

Functional tests #2333

Merged
merged 6 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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: 1 addition & 1 deletion src/ApiService/ApiService/ApiService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.6.0" />
<PackageReference Include="Azure.Data.Tables" Version="12.5.0" />
<PackageReference Include="Azure.ResourceManager.Compute" Version="1.0.0" />
<PackageReference Include="Azure.ResourceManager.Compute" Version="1.0.0-beta.8" />
<PackageReference Include="Azure.Core" Version="1.25.0" />
<PackageReference Include="Azure.Identity" Version="1.6.0" />
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.10.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/ApiService/ApiService/Functions/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private async Async.Task<HttpResponseData> Delete(HttpRequestData req) {
return await _context.RequestHandling.NotOk(
req,
request.ErrorV,
"debug_proxy delet");
"debug_proxy delete");
}

var regions = await _context.ProxyForwardOperations.RemoveForward(
Expand Down
14 changes: 8 additions & 6 deletions src/ApiService/ApiService/Functions/TimerProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ public async Async.Task Run([TimerTrigger("00:00:30")] TimerInfo myTimer) {
// since we do not support bring your own NSG

if (await nsgOpertions.GetNsg(region) != null) {
var network = await Network.Create(region, _context);
var network = await Network.Init(region, _context);

var subnet = await network.GetSubnet();
var vnet = await network.GetVnet();
if (subnet != null && vnet != null) {
var result = await nsgOpertions.AssociateSubnet(region, vnet, subnet);
if (!result.OkV) {
_logger.Error($"Failed to associate NSG and subnet due to {result.ErrorV} in region {region}");
if (subnet != null) {
var vnet = await network.GetVnet();
if (vnet != null) {
var result = await nsgOpertions.AssociateSubnet(region, vnet, subnet);
if (!result.OkV) {
_logger.Error($"Failed to associate NSG and subnet due to {result.ErrorV} in region {region}");
}
}
}
}
Expand Down
50 changes: 29 additions & 21 deletions src/ApiService/ApiService/ServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,55 @@ public ServiceConfiguration() {
#endif
}

private static string? GetEnv(string name) {
var v = Environment.GetEnvironmentVariable(name);
if (String.IsNullOrEmpty(v))
return null;

return v;
}

//TODO: Add environment variable to control where to write logs to
public LogDestination[] LogDestinations { get; set; }

//TODO: Get this from Environment variable
public ApplicationInsights.DataContracts.SeverityLevel LogSeverityLevel => ApplicationInsights.DataContracts.SeverityLevel.Verbose;

public string? ApplicationInsightsAppId => Environment.GetEnvironmentVariable("APPINSIGHTS_APPID");
public string? ApplicationInsightsInstrumentationKey => Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
public string? ApplicationInsightsAppId => GetEnv("APPINSIGHTS_APPID");
public string? ApplicationInsightsInstrumentationKey => GetEnv("APPINSIGHTS_INSTRUMENTATIONKEY");

public string? AzureSignalRConnectionString => Environment.GetEnvironmentVariable("AzureSignalRConnectionString");
public string? AzureSignalRServiceTransportType => Environment.GetEnvironmentVariable("AzureSignalRServiceTransportType");
public string? AzureSignalRConnectionString => GetEnv("AzureSignalRConnectionString");
public string? AzureSignalRServiceTransportType => GetEnv("AzureSignalRServiceTransportType");

public string? AzureWebJobDisableHomePage { get => Environment.GetEnvironmentVariable("AzureWebJobsDisableHomepage"); }
public string? AzureWebJobStorage { get => Environment.GetEnvironmentVariable("AzureWebJobsStorage"); }
public string? AzureWebJobDisableHomePage { get => GetEnv("AzureWebJobsDisableHomepage"); }
public string? AzureWebJobStorage { get => GetEnv("AzureWebJobsStorage"); }

public string? DiagnosticsAzureBlobContainerSasUrl { get => Environment.GetEnvironmentVariable("DIAGNOSTICS_AZUREBLOBCONTAINERSASURL"); }
public string? DiagnosticsAzureBlobRetentionDays { get => Environment.GetEnvironmentVariable("DIAGNOSTICS_AZUREBLOBRETENTIONINDAYS"); }
public string? DiagnosticsAzureBlobContainerSasUrl { get => GetEnv("DIAGNOSTICS_AZUREBLOBCONTAINERSASURL"); }
public string? DiagnosticsAzureBlobRetentionDays { get => GetEnv("DIAGNOSTICS_AZUREBLOBRETENTIONINDAYS"); }

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

public string? OneFuzzDataStorage { get => Environment.GetEnvironmentVariable("ONEFUZZ_DATA_STORAGE"); }
public string? OneFuzzFuncStorage { get => Environment.GetEnvironmentVariable("ONEFUZZ_FUNC_STORAGE"); }
public string? OneFuzzInstance { get => Environment.GetEnvironmentVariable("ONEFUZZ_INSTANCE"); }
public string? OneFuzzInstanceName { get => Environment.GetEnvironmentVariable("ONEFUZZ_INSTANCE_NAME"); }
public string? OneFuzzKeyvault { get => Environment.GetEnvironmentVariable("ONEFUZZ_KEYVAULT"); }
public string? OneFuzzMonitor { get => Environment.GetEnvironmentVariable("ONEFUZZ_MONITOR"); }
public string? OneFuzzOwner { get => Environment.GetEnvironmentVariable("ONEFUZZ_OWNER"); }
public string? OneFuzzResourceGroup { get => Environment.GetEnvironmentVariable("ONEFUZZ_RESOURCE_GROUP"); }
public string? OneFuzzTelemetry { get => Environment.GetEnvironmentVariable("ONEFUZZ_TELEMETRY"); }
public string? OneFuzzDataStorage { get => GetEnv("ONEFUZZ_DATA_STORAGE"); }
public string? OneFuzzFuncStorage { get => GetEnv("ONEFUZZ_FUNC_STORAGE"); }
public string? OneFuzzInstance { get => GetEnv("ONEFUZZ_INSTANCE"); }
public string? OneFuzzInstanceName { get => GetEnv("ONEFUZZ_INSTANCE_NAME"); }
public string? OneFuzzKeyvault { get => GetEnv("ONEFUZZ_KEYVAULT"); }
public string? OneFuzzMonitor { get => GetEnv("ONEFUZZ_MONITOR"); }
public string? OneFuzzOwner { get => GetEnv("ONEFUZZ_OWNER"); }
public string? OneFuzzResourceGroup { get => GetEnv("ONEFUZZ_RESOURCE_GROUP"); }
public string? OneFuzzTelemetry { get => GetEnv("ONEFUZZ_TELEMETRY"); }

public string OneFuzzVersion {
get {
// version can be overridden by config:
return Environment.GetEnvironmentVariable("ONEFUZZ_VERSION")
return GetEnv("ONEFUZZ_VERSION")
?? _oneFuzzVersion
?? throw new InvalidOperationException("Unable to read OneFuzz version from assembly");
}
}

public string? OneFuzzAllowOutdatedAgent => Environment.GetEnvironmentVariable("ONEFUZZ_ALLOW_OUTDATED_AGENT");
public string? OneFuzzAllowOutdatedAgent => GetEnv("ONEFUZZ_ALLOW_OUTDATED_AGENT");

public string OneFuzzNodeDisposalStrategy { get => Environment.GetEnvironmentVariable("ONEFUZZ_NODE_DISPOSAL_STRATEGY") ?? "scale_in"; }
public string OneFuzzNodeDisposalStrategy { get => GetEnv("ONEFUZZ_NODE_DISPOSAL_STRATEGY") ?? "scale_in"; }
public string OneFuzzStoragePrefix => ""; // in production we never prefix the tables
}
2 changes: 1 addition & 1 deletion src/ApiService/ApiService/onefuzzlib/Creds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Async.Task<Guid> GetScalesetPrincipalId() {

var resource = await uid.GetAsync();
var principalId = resource.Value.Data.Properties.ToObjectFromJson<ScaleSetIdentity>().principalId;
return new Guid(principalId);
return Guid.Parse(principalId);
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/ApiService/ApiService/onefuzzlib/DiskOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Microsoft.OneFuzz.Service;

public interface IDiskOperations {
DiskImageCollection ListDisks(string resourceGroup);
DiskCollection ListDisks(string resourceGroup);

Async.Task<bool> DeleteDisk(string resourceGroup, string name);
}
Expand All @@ -23,7 +23,7 @@ public DiskOperations(ILogTracer log, ICreds creds) {
public async Task<bool> DeleteDisk(string resourceGroup, string name) {
try {
_logTracer.Info($"deleting disks {resourceGroup} : {name}");
var disk = await _creds.GetResourceGroupResource().GetDiskImageAsync(name);
var disk = await _creds.GetResourceGroupResource().GetDiskAsync(name);
if (disk != null) {
await disk.Value.DeleteAsync(WaitUntil.Started);
return true;
Expand All @@ -35,8 +35,8 @@ public async Task<bool> DeleteDisk(string resourceGroup, string name) {
return false;
}

public DiskImageCollection ListDisks(string resourceGroup) {
public DiskCollection ListDisks(string resourceGroup) {
_logTracer.Info($"listing disks {resourceGroup}");
return _creds.GetResourceGroupResource().GetDiskImages();
return _creds.GetResourceGroupResource().GetDisks();
}
}
9 changes: 5 additions & 4 deletions src/ApiService/ApiService/onefuzzlib/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ public static VMExtensionWrapper GenevaExtension(AzureLocation region) {
);

var fileName = $"{pool.Name}/config.json";
await _context.Containers.SaveBlob(new Container("vm-scripts"), fileName, (JsonSerializer.Serialize(config, EntityConverter.GetJsonSerializerOptions())), StorageType.Config);
var configJson = JsonSerializer.Serialize(config, EntityConverter.GetJsonSerializerOptions());
await _context.Containers.SaveBlob(new Container("vm-scripts"), fileName, configJson, StorageType.Config);
return await ConfigUrl(new Container("vm-scripts"), fileName, false);
}

Expand Down Expand Up @@ -264,6 +265,7 @@ public async Async.Task<VMExtensionWrapper> AgentConfig(AzureLocation region, Os
await UpdateManagedScripts();
var urlsUpdated = urls ?? new();

var managedIdentity = JsonSerializer.Serialize(new { ManagedIdentity = new Dictionary<string, string>() }, _extensionSerializerOptions);
if (vmOs == Os.Windows) {
var vmScripts = await ConfigUrl(new Container("vm-scripts"), "managed.ps1", withSas) ?? throw new Exception("failed to get VmScripts config url");
var toolsAzCopy = await ConfigUrl(new Container("tools"), "win64/azcopy.exe", withSas) ?? throw new Exception("failed to get toolsAzCopy config url");
Expand All @@ -286,7 +288,7 @@ public async Async.Task<VMExtensionWrapper> AgentConfig(AzureLocation region, Os
TypeHandlerVersion = "1.9",
AutoUpgradeMinorVersion = true,
Settings = new BinaryData(JsonSerializer.Serialize(new { commandToExecute = toExecuteCmd, fileUris = urlsUpdated }, _extensionSerializerOptions)),
ProtectedSettings = new BinaryData(JsonSerializer.Serialize(new { managedIdentity = new Dictionary<string, string>() }, _extensionSerializerOptions))
ProtectedSettings = new BinaryData(managedIdentity)
};
return extension;
} else if (vmOs == Os.Linux) {
Expand All @@ -301,7 +303,6 @@ public async Async.Task<VMExtensionWrapper> AgentConfig(AzureLocation region, Os

var toExecuteCmd = $"sh setup.sh {mode.ToString().ToLowerInvariant()}";
var extensionSettings = JsonSerializer.Serialize(new { CommandToExecute = toExecuteCmd, FileUris = urlsUpdated }, _extensionSerializerOptions);
var protectedExtensionSettings = JsonSerializer.Serialize(new { ManagedIdentity = new Dictionary<string, string>() }, _extensionSerializerOptions);

var extension = new VMExtensionWrapper {
Name = "CustomScript",
Expand All @@ -312,7 +313,7 @@ public async Async.Task<VMExtensionWrapper> AgentConfig(AzureLocation region, Os
ForceUpdateTag = Guid.NewGuid().ToString(),
AutoUpgradeMinorVersion = true,
Settings = new BinaryData(extensionSettings),
ProtectedSettings = new BinaryData(protectedExtensionSettings)
ProtectedSettings = new BinaryData(managedIdentity)
};
return extension;
}
Expand Down
18 changes: 8 additions & 10 deletions src/ApiService/ApiService/onefuzzlib/ImageOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ex is NullReferenceException
}
} else {
try {
name = (await _context.Creds.GetResourceGroupResource().GetDiskImages().GetAsync(
name = (await _context.Creds.GetResourceGroupResource().GetImages().GetAsync(
parsed.Data.Name
)).Value.Data.StorageProfile.OSDisk.OSType.ToString().ToLowerInvariant();
} catch (Exception ex) when (
Expand Down Expand Up @@ -96,15 +96,13 @@ ex is NullReferenceException
version = imageInfo.Version;
}

var vmImage = await subscription.GetVirtualMachineImageAsync(
region,
imageInfo.Publisher,
imageInfo.Offer,
imageInfo.Sku
, version
);

name = vmImage.Value.OSDiskImageOperatingSystem!.Value.ToString().ToLower();
name = (await subscription.GetVirtualMachineImageAsync(
region,
imageInfo.Publisher,
imageInfo.Offer,
imageInfo.Sku
, version
)).Value.OSDiskImageOperatingSystem.ToString().ToLower();
} catch (RequestFailedException ex) {
return OneFuzzResult<Os>.Error(
ErrorCode.INVALID_IMAGE,
Expand Down
14 changes: 11 additions & 3 deletions src/ApiService/ApiService/onefuzzlib/IpOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface IIpOperations {

public Async.Task<string?> GetPublicIp(ResourceIdentifier resourceId);

public Async.Task<string?> GetPublicIp(string resourceId);

public Async.Task<PublicIPAddressResource?> GetIp(string resourceGroup, string name);

public Async.Task DeleteNic(string resourceGroup, string name);
Expand Down Expand Up @@ -86,6 +88,9 @@ public async System.Threading.Tasks.Task DeleteIp(string resourceGroup, string n
var ips = await _networkInterfaceQuery.ListInstancePrivateIps(scalesetId, instance.OkV);
return ips.FirstOrDefault();
}
public async Task<string?> GetPublicIp(string resourceId) {
return await GetPublicIp(new ResourceIdentifier(resourceId));
}

public async Task<string?> GetPublicIp(ResourceIdentifier resourceId) {
// TODO: Parts of this function seem redundant, but I'm mirroring
Expand Down Expand Up @@ -117,12 +122,15 @@ public async System.Threading.Tasks.Task DeleteIp(string resourceGroup, string n
public async Task<OneFuzzResultVoid> CreatePublicNic(string resourceGroup, string name, string region, Nsg? nsg) {
_logTracer.Info($"creating nic for {resourceGroup}:{name} in {region}");

var network = await Network.Create(region, _context);
var network = await Network.Init(region, _context);
var subnetId = await network.GetId();

if (subnetId is null) {
await network.Create();
return OneFuzzResultVoid.Ok;
var r = await network.Create();
if (!r.IsOk) {
_logTracer.Error($"failed to create network in region {region} due to {r.ErrorV}");
}
return r;
}

if (nsg != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/ApiService/ApiService/onefuzzlib/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Network(string region, string group, string name, IOnefuzzContext context
_networkConfig = networkConfig;
}

public static async Async.Task<Network> Create(string region, IOnefuzzContext context) {
public static async Async.Task<Network> Init(string region, IOnefuzzContext context) {
var group = context.Creds.GetBaseResourceGroup();
var instanceConfig = await context.ConfigOperations.Fetch();
var networkConfig = instanceConfig.NetworkConfig;
Expand Down
2 changes: 1 addition & 1 deletion src/ApiService/ApiService/onefuzzlib/ProxyOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private static IEnumerable<string> GetErrors(Proxy proxy, VirtualMachineData vmD
}

foreach (var status in instanceView.Statuses) {
if (status.Level == ComputeStatusLevelType.Error) {
if (status.Level == StatusLevelTypes.Error) {
yield return $"code:{status.Code} status:{status.DisplayStatus} message:{status.Message}";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ApiService/ApiService/onefuzzlib/ScalesetOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public async Async.Task<Scaleset> Setup(Scaleset scaleset) {
//# This was done as part of the generated per-task setup script.
_logTracer.Info($"{SCALESET_LOG_PREFIX} setup. scalset_id: {scaleset.ScalesetId}");

var network = await Network.Create(scaleset.Region, _context);
var network = await Network.Init(scaleset.Region, _context);
var networkId = await network.GetId();
if (networkId is null) {
_logTracer.Info($"{SCALESET_LOG_PREFIX} creating network. region: {scaleset.Region} scaleset_id:{scaleset.ScalesetId}");
Expand Down
13 changes: 9 additions & 4 deletions src/ApiService/ApiService/onefuzzlib/Subnet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,17 @@ await _creds.GetResourceGroupResource().GetVirtualNetworks().CreateOrUpdateAsync
}

public async Async.Task<SubnetResource?> GetSubnet(string vnetName, string subnetName) {
var vnet = await this.GetVnet(vnetName);
try {
var vnet = await this.GetVnet(vnetName);

if (vnet != null) {
return await vnet.GetSubnetAsync(subnetName);
if (vnet != null) {
return await vnet.GetSubnetAsync(subnetName);
}
return null;
} catch (RequestFailedException ex) when (ex.Status == 404) {
return null;
}
return null;

}

public async Task<ResourceIdentifier?> GetSubnetId(string name, string subnetName) {
Expand Down
7 changes: 5 additions & 2 deletions src/ApiService/ApiService/onefuzzlib/VmExtensionWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Azure.Core;
using Azure.ResourceManager.Compute;


namespace Microsoft.OneFuzz.Service {
public class VMExtensionWrapper {
public AzureLocation? Location { get; init; }
Expand All @@ -27,7 +28,7 @@ public class VMExtensionWrapper {
var protectedSettings = ProtectedSettings ?? new BinaryData(new Dictionary<string, string>());

return (Name!, new VirtualMachineExtensionData(Location.Value) {
ExtensionType = TypePropertiesType,
TypePropertiesType = TypePropertiesType,
Publisher = Publisher,
TypeHandlerVersion = TypeHandlerVersion,
AutoUpgradeMinorVersion = AutoUpgradeMinorVersion,
Expand All @@ -49,7 +50,8 @@ public VirtualMachineScaleSetExtensionData GetAsVirtualMachineScaleSetExtension(
var protectedSettings = ProtectedSettings ?? new BinaryData(new Dictionary<string, string>());

return new VirtualMachineScaleSetExtensionData() {
ExtensionType = TypePropertiesType,
Name = Name,
TypePropertiesType = TypePropertiesType,
Publisher = Publisher,
TypeHandlerVersion = TypeHandlerVersion,
AutoUpgradeMinorVersion = AutoUpgradeMinorVersion,
Expand All @@ -62,3 +64,4 @@ public VirtualMachineScaleSetExtensionData GetAsVirtualMachineScaleSetExtension(
}

}

Loading