Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: tunnel reduced supported tfms #1140

Merged
merged 2 commits into from
Jul 25, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- New package Sentry.Tunnel to proxy Sentry events ([#1133](https://github.com/getsentry/sentry-dotnet/pull/1133))

### Fixes

- Avoid serializing dangerous types ([#1134](https://github.com/getsentry/sentry-dotnet/pull/1134))
Expand Down
17 changes: 3 additions & 14 deletions src/Sentry.Tunnel/Sentry.Tunnel.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.0;netstandard2.0</TargetFrameworks>
<PackageTags>$(PackageTags);AspNetCore;MVC</PackageTags>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
<PackageTags>$(PackageTags);Sentry Proxy;Envelopes</PackageTags>
<PackageId>Sentry.Tunnel</PackageId>
<AssemblyName>Sentry.Tunnel</AssemblyName>
<RootNamespace>Sentry.Tunnel</RootNamespace>
<Description>Official Tunnel middleware for Sentry.</Description>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="2.1.0" />
<PackageReference Include="System.Text.Json" Version="4.6.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

Expand Down
62 changes: 30 additions & 32 deletions src/Sentry.Tunnel/SentryTunnelMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,48 +49,46 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
var client = httpClientFactory.CreateClient("SentryTunnel");
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Sentry.NET_Tunnel", Version));
var ms = new MemoryStream();
await context.Request.Body.CopyToAsync(ms);
await context.Request.Body.CopyToAsync(ms).ConfigureAwait(false);
ms.Position = 0;
using (var reader = new StreamReader(ms))
using var reader = new StreamReader(ms);
var header = await reader.ReadLineAsync().ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(header))
{
var header = await reader.ReadLineAsync().ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(header))
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
return;
}
context.Response.StatusCode = StatusCodes.Status400BadRequest;
return;
}

try
{
var headerJson = JsonSerializer.Deserialize<Dictionary<string, object>>(header);
if (headerJson == null)
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Invalid DSN JSON supplied").ConfigureAwait(false);
return;
}
if (headerJson.TryGetValue("dsn", out var dsnString) && Uri.TryCreate(dsnString.ToString(), UriKind.Absolute, out var dsn) && _allowedHosts.Contains(dsn.Host))
{
var projectId = dsn.AbsolutePath.Trim('/');
ms.Position = 0;
var responseMessage = await client.PostAsync($"https://{dsn.Host}/api/{projectId}/envelope/",
new StreamContent(ms)).ConfigureAwait(false);
context.Response.Headers["content-type"] = "application/json";
context.Response.StatusCode = StatusCodes.Status200OK;
await responseMessage.Content.CopyToAsync(context.Response.Body).ConfigureAwait(false);
}
}
catch(JsonException)
try
{
var headerJson = JsonSerializer.Deserialize<Dictionary<string, object>>(header);
if (headerJson == null)
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Invalid DSN JSON supplied").ConfigureAwait(false);
return;
}
catch(ArgumentNullException)
if (headerJson.TryGetValue("dsn", out var dsnString) && Uri.TryCreate(dsnString.ToString(), UriKind.Absolute, out var dsn) && _allowedHosts.Contains(dsn.Host))
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Received empty body").ConfigureAwait(false);
var projectId = dsn.AbsolutePath.Trim('/');
ms.Position = 0;
var responseMessage = await client.PostAsync($"https://{dsn.Host}/api/{projectId}/envelope/",
new StreamContent(ms)).ConfigureAwait(false);
context.Response.Headers["content-type"] = "application/json";
context.Response.StatusCode = StatusCodes.Status200OK;
await responseMessage.Content.CopyToAsync(context.Response.Body).ConfigureAwait(false);
}
}
catch (JsonException)
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Invalid DSN JSON supplied").ConfigureAwait(false);
}
catch (ArgumentNullException)
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Received empty body").ConfigureAwait(false);
}
}
}
}
26 changes: 14 additions & 12 deletions src/Sentry.Tunnel/SentryTunnelingApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@

namespace Sentry.Tunnel
{
/// <summary>
/// Extension methods to add Sentry ingestion tunnel.
/// </summary>
public static class SentryTunnelingApplicationBuilderExtensions
{
/// <summary>
/// Adds and configures the Sentry tunneling middleware
/// Adds and configures the Sentry tunneling middleware.
/// </summary>
/// <param name="services"></param>
/// <param name="hostnames">The extra hostnames to be allowed for the tunneling. sentry.io is allowed by default; add your own Sentry domain if you use a self-hosted Sentry or Relay.</param>
public static void AddSentryTunneling(this IServiceCollection services, params string[] hostnames)
{
services.AddScoped<SentryTunnelMiddleware>((s) => new SentryTunnelMiddleware(hostnames));
}

public static void UseSentryTunneling(this IApplicationBuilder builder, string path = "/tunnel")
{
public static void AddSentryTunneling(this IServiceCollection services, params string[] hostnames) =>
services.AddScoped(_ => new SentryTunnelMiddleware(hostnames));

/// <summary>
/// Adds the <see cref="SentryTunnelMiddleware"/> to the pipeline.
/// </summary>
/// <param name="builder">The app builder.</param>
/// <param name="path">The path to listen for Sentry envelopes.</param>
public static void UseSentryTunneling(this IApplicationBuilder builder, string path = "/tunnel") =>
builder.Map(path, applicationBuilder =>
{
applicationBuilder.UseMiddleware<SentryTunnelMiddleware>();
});
}
applicationBuilder.UseMiddleware<SentryTunnelMiddleware>());
}
}
4 changes: 2 additions & 2 deletions test/Sentry.EntityFramework.Tests/ErrorProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ public async Task EntityValidationExceptions_Extra_EntityValidationErrorsNotNull
[Fact]
public async Task Integration_DbEntityValidationExceptionProcessorAsync()
{
// We use an actual Entity Framework instane since manually generating any EF related data is highly inaccurate
// We use an actual Entity Framework instance since manually generating any EF related data is highly inaccurate
_fixture.DbContext.TestTable.Add(new TestDbContext.TestData());
try
{
// This will throw a validation exception since TestData has a Required column which we didn't set
await _fixture.DbContext.SaveChangesAsync();
}
catch(DbEntityValidationException e)
catch (DbEntityValidationException e)
{
Exception assertError = null;
// SaveChanges will throw an exception
Expand Down
40 changes: 3 additions & 37 deletions test/Sentry.Tunnel.Tests/Sentry.Tunnel.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,47 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.7" />
</ItemGroup>

<!-- Run netcoreapp3.0 against netstandard2.1 target of Sentry -->
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<ProjectReference Update="../../src/Sentry/Sentry.csproj" SetTargetFramework="TargetFramework=netstandard2.1" />
<!-- NET Core's built-in STJ is lower version which causes conflicts, so we have to explicitly reference it -->
<PackageReference Include="System.Text.Json" Version="5.0.2" />
<!-- Ben.Demystifier uses S.R.M v5 and also requires it via package reference when on nca3.x -->
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'netcoreapp2.1'">
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.0" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'net5.0'">
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Sentry.Tunnel\Sentry.Tunnel.csproj" />
<ProjectReference Include="../../src/Sentry.Tunnel/Sentry.Tunnel.csproj" />
<ProjectReference Include="../Sentry.Testing/Sentry.Testing.csproj" />
</ItemGroup>

</Project>