Skip to content

Commit

Permalink
Merge pull request #14 from 0xcryptar/cry-23-cry-23-generating-salt-f…
Browse files Browse the repository at this point in the history
…or-value-0-in-json2hash-microservice

Cry 23 cry 23 generating salt for value 0 in json2hash microservice
  • Loading branch information
skolumban-ex authored Nov 11, 2024
2 parents 1bbd513 + 715ac02 commit 816c320
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/notarization-objecthash.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ env:
AZURE_FUNCTIONAPP_NAME: notarization-objecthash
AZURE_FUNCTIONAPP_PACKAGE_PATH: ObjectHashServer.API\.
CONFIGURATION: Release
DOTNET_CORE_VERSION: 6.0.x
DOTNET_CORE_VERSION: 8.0.x
WORKING_DIRECTORY: ObjectHashServer.API
jobs:
build-and-deploy:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x
- name: Install dependencies
run: dotnet restore
- name: Build master
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x
- name: Install dependencies
run: dotnet restore
- name: Build master
Expand Down
49 changes: 27 additions & 22 deletions ObjectHashServer.API/ObjectHashServer.API.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ObjectHashServer.BLL\ObjectHashServer.BLL.csproj" />
<ProjectReference Include="..\ObjectHashServer\src\ObjectHashServer.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.4.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.3.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.OpenApi" Version="1.5.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.18.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ObjectHashServer.BLL\ObjectHashServer.BLL.csproj" />
<ProjectReference Include="..\ObjectHashServer\src\ObjectHashServer.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
32 changes: 17 additions & 15 deletions ObjectHashServer.API/ObjectHasherFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.Functions.Worker;
using Microsoft.OpenApi.Models;
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Extensions;
using Microsoft.Azure.Functions.Worker.Http;


using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ObjectHashServer.BLL.Models;
using ObjectHashServer.BLL.Models.Api.Request;
using ObjectHashServer.BLL.Models.Api.Response;
using ObjectHashServer.BLL.Services.Implementations;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

namespace ObjectHashServer.API
{
Expand All @@ -26,12 +28,12 @@ public class ObjectHasherFunctions
/// <response code="200">Successful call</response>
/// <response code="404">Not Found</response>
/// <response code="500">Internal Server Error</response>
[FunctionName("HashObject")]
[Function("HashObject")]
[OpenApiOperation(operationId: "hash-object", Description = "Generates salts for the recieved json.")]
[OpenApiRequestBody(contentType: "application/json", bodyType: typeof(JObject), Description = "Json for which the salts should be generated.", Required = true)]
[OpenApiParameter(name: "generateSalts", In = ParameterLocation.Query, Required = true, Type = typeof(bool), Description = "Generate salts?")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(ObjectHashResponseModel), Description = "The generated/hashed result for the given json.")]
public async Task<ActionResult<ObjectHashResponseModel>> HashObject([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "hash-object")] HttpRequest req)
public async Task<IActionResult> HashObject([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "hash-object")] HttpRequest req)
{
try
{
Expand Down Expand Up @@ -62,7 +64,7 @@ public async Task<ActionResult<ObjectHashResponseModel>> HashObject([HttpTrigger
if (!generateSalts)
requestModel.Salts = null;

return new ObjectHashResponseModel(new ObjectHash(requestModel));
return new OkObjectResult(new ObjectHashResponseModel(new ObjectHash(requestModel)));
}
catch (Exception e)
{
Expand All @@ -79,11 +81,11 @@ public async Task<ActionResult<ObjectHashResponseModel>> HashObject([HttpTrigger
/// <response code="200">Successful call</response>
/// <response code="404">Not Found</response>
/// <response code="500">Internal Server Error</response>
[FunctionName("ReHashObject")]
[Function("ReHashObject")]
[OpenApiOperation(operationId: "rehash-object", Description = "Generates the hash for the recieved ObjectBaseRequestModel.")]
[OpenApiRequestBody(contentType: "application/json", bodyType: typeof(ObjectBaseRequestModel), Description = "ObjectBaseRequestModel for which the hash should be generated.", Required = true)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(ObjectHashResponseModel), Description = "The generated ObjectHashResponseModel containing the generated hash.")]
public async Task<ActionResult<ObjectHashResponseModel>> ReHashObject([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "rehash-object")] HttpRequest req)
public async Task<IActionResult> ReHashObject([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "rehash-object")] HttpRequest req)
{
try
{
Expand All @@ -101,7 +103,7 @@ public async Task<ActionResult<ObjectHashResponseModel>> ReHashObject([HttpTrigg
return result;
}

return new ObjectHashResponseModel(new ObjectHash(requestModel));
return new OkObjectResult(new ObjectHashResponseModel(new ObjectHash(requestModel)));
}
catch (Exception e)
{
Expand All @@ -118,11 +120,11 @@ public async Task<ActionResult<ObjectHashResponseModel>> ReHashObject([HttpTrigg
/// <response code="200">Successful call</response>
/// <response code="404">Not Found</response>
/// <response code="500">Internal Server Error</response>
[FunctionName("RedactObject")]
[Function("RedactObject")]
[OpenApiOperation(operationId: "redact-object", Description = "Calculates the redacted JSON given a settings file.")]
[OpenApiRequestBody(contentType: "application/json", bodyType: typeof(ObjectRedactionRequestModel), Description = "ObjectRedactionRequestModel for which the redacted JSON should be calculated.", Required = true)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(ObjectRedactionResponseModel), Description = "ObjectRedactionResponseModel containing the redacted JSON.")]
public async Task<ActionResult<ObjectRedactionResponseModel>> RedactObject([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "redact-object")] HttpRequest req)
public async Task<IActionResult> RedactObject([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "redact-object")] HttpRequest req)
{
try
{
Expand All @@ -141,7 +143,7 @@ public async Task<ActionResult<ObjectRedactionResponseModel>> RedactObject([Http
}

ObjectRedactionImplementation.RedactJToken(requestModel.Data, requestModel.RedactSettings, requestModel.Salts);
return new ObjectRedactionResponseModel(new ObjectRedaction(requestModel));
return new OkObjectResult(new ObjectRedactionResponseModel(new ObjectRedaction(requestModel)));
}
catch (Exception e)
{
Expand Down
29 changes: 29 additions & 0 deletions ObjectHashServer.API/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Azure.Core.Serialization;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;

var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureOpenApi() // Ensure OpenAPI is configured
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.Configure<WorkerOptions>(workerOptions =>
{
var settings = NewtonsoftJsonObjectSerializer.CreateJsonSerializerSettings();
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
settings.NullValueHandling = NullValueHandling.Ignore;

workerOptions.Serializer = new NewtonsoftJsonObjectSerializer(settings);
});

services.AddMvcCore().AddNewtonsoftJson();
})
.Build();

host.Run();
17 changes: 9 additions & 8 deletions ObjectHashServer.API/host.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
}
}
2 changes: 1 addition & 1 deletion ObjectHashServer.BLL/ObjectHashServer.BLL.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ private static string NormalizeDouble(double d)
e += 1;
}

while (d < 0.5)
while (d <= 0.5)
{
d *= 2;
e -= 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ObjectHashServer.BLL.Exceptions;
using ObjectHashServer.BLL.Models.Api.Response;
Expand Down Expand Up @@ -62,7 +63,7 @@ public override void OnException(ExceptionContext context)
logger.LogError(0, exception, exception.Message);
}

IHostingEnvironment env = context.HttpContext.RequestServices.GetRequiredService<IHostingEnvironment>();
IHostEnvironment env = context.HttpContext.RequestServices.GetRequiredService<IHostEnvironment>();
if (env.IsDevelopment())
{
errorModel.ExceptionMessage = exception.Message;
Expand Down
2 changes: 1 addition & 1 deletion ObjectHashServer/src/ObjectHashServer.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions ObjectHashServer/src/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace ObjectHashServer
public class Startup
{
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _environment;
private readonly IHostEnvironment _environment;

public Startup(IConfiguration configuration, IWebHostEnvironment environment)
public Startup(IConfiguration configuration, IHostEnvironment environment)
{
_configuration = configuration;
_environment = environment;
Expand All @@ -33,7 +33,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddMvc(config =>
{
config.Filters.Add(new ExceptionHandlerFilterAttribute());
}).SetCompatibilityVersion(CompatibilityVersion.Version_3_0).AddNewtonsoftJson();
}).AddNewtonsoftJson();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down

0 comments on commit 816c320

Please sign in to comment.