Skip to content

Commit 42a323f

Browse files
committed
fix: Merge 'n fix.
2 parents e52345b + 2ee1c80 commit 42a323f

File tree

93 files changed

+2677
-523
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+2677
-523
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,5 @@ MigrationBackup/
349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351351
nuget.exe
352+
353+
.vshistory/

README.md

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,45 +58,45 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5858
}
5959
```
6060

61-
## Authorization: configuration
61+
## Options
62+
Options can be found in the [UIOptions](src/Serilog.Ui.Web/Extensions/UiOptions.cs) class.
63+
`internal` properties can generally be set via extension methods, see [SerilogUiOptionBuilderExtensions](src/Serilog.Ui.Web/Extensions/SerilogUiOptionBuilderExtensions.cs)
64+
65+
### Authorization
6266

63-
By default serilog-ui allows access to the log page only for local requests. In order to give appropriate rights for production use, you need to configure authorization. You can secure the log page by allowing specific users or roles to view logs:
67+
By default serilog-ui allows access to the log page only for local requests. In order to give appropriate rights for production use, you need to configure authorization. You can add your own implementations of the `IUiAuthorizationFilter` interface, whose Authorize method is used to allow or prohibit a request. The first step is to provide your own implementation.:
6468

6569
```csharp
66-
public void ConfigureServices(IServiceCollection services)
70+
public void Configure(IApplicationBuilder appBuilder)
6771
{
68-
services.AddSerilogUi(options => options
69-
.EnableAuthorization(authOptions =>
72+
appBuilder.UseSerilogUi(options =>
73+
{
74+
options.Authorization.AuthenticationType = AuthenticationType.Jwt;
75+
options.Authorization.Filters = new[]
7076
{
71-
authOption.AuthenticationType = AuthenticationType.Jwt; // or AuthenticationType.Cookie
72-
authOptions.Usernames = new[] { "User1", "User2" };
73-
authOptions.Roles = new[] { "AdminRole" };
74-
})
75-
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), "LogTableName"));
77+
new CustomAuthorizeFilter()
78+
};
79+
});
7680
// ...
7781
}
7882
```
79-
Only `User1` and `User2` or users with `AdminRole` role can view logs.
8083

8184
If you set `AuthenticationType` to `Jwt`, you can set a jwt token and an `Authorization` header will be added to the request and for `Cookie` just login into you website and no extra step is required.
8285

83-
To disable anonymous access for local requests, (e.g. for testing authentication locally) set `AlwaysAllowLocalRequests` to `false`.
84-
85-
To disable authorization on production, set `Enabled` to false.
86+
Here is an example of how you can implement your own authentication and authorization:
8687

8788
``` csharp
88-
services.AddSerilogUi(options => options
89-
.EnableAuthorization(authOption =>
89+
public class CustomAuthorizeFilter : IUiAuthorizationFilter
90+
{
91+
public bool Authorize(DashboardContext context)
9092
{
91-
authOption.AlwaysAllowLocalRequests = false; // disable anonymous access on local
92-
authOption.Enabled = false; // disable authorization access check on production
93-
})
94-
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), "Logs"));
95-
```
93+
var httpContext = context.GetHttpContext();
9694

97-
## Options
98-
Options can be found in the [UIOptions](src/Serilog.Ui.Web/Extensions/UiOptions.cs) class.
99-
`internal` properties can generally be set via extension methods, see [SerilogUiOptionBuilderExtensions](src/Serilog.Ui.Web/Extensions/SerilogUiOptionBuilderExtensions.cs)
95+
// Allow all authenticated users to see the Dashboard (potentially dangerous).
96+
return httpContext.User.Identity?.IsAuthenticated ?? false;
97+
}
98+
}
99+
```
100100

101101
### Log page URL
102102

@@ -232,6 +232,9 @@ To run the tests, use Test Explorer in Visual Studio or run from the root folder
232232
dotnet test
233233
```
234234

235+
Please notice: to run the tests, you'll need a running Docker instance on your sistem.
236+
Integration tests (identified by traits) on MS Sql, MySql, Postgres, use Docker to spin the integration database.
237+
235238
## JS UI assets
236239

237240
Tests are located inside src/Serilog.Ui.Web/assets/__tests__

Serilog.Ui.sln

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.MongoDbProvider.
2929
EndProject
3030
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.Common.Tests", "tests\Serilog.Ui.Common.Tests\Serilog.Ui.Common.Tests.csproj", "{96689D04-8B2F-4C06-AE1D-3483B1508D59}"
3131
EndProject
32-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Ui.PostgreSqlProvider.Tests", "tests\Serilog.Ui.PostgreSqlProvider.Tests\Serilog.Ui.PostgreSqlProvider.Tests.csproj", "{D1688095-7E1F-42B2-BC3F-8CB29975CAD5}"
32+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.PostgreSqlProvider.Tests", "tests\Serilog.Ui.PostgreSqlProvider.Tests\Serilog.Ui.PostgreSqlProvider.Tests.csproj", "{D1688095-7E1F-42B2-BC3F-8CB29975CAD5}"
3333
EndProject
3434
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.Web.Tests", "tests\Serilog.Ui.Web.Tests\Serilog.Ui.Web.Tests.csproj", "{9AD9BF6A-0CB0-467C-BB84-BE1C701C0113}"
3535
EndProject
36-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Ui.MySqlProvider.Tests", "tests\Serilog.Ui.MySqlProvider.Tests\Serilog.Ui.MySqlProvider.Tests.csproj", "{6986AD9C-3B9E-4DAF-A45F-643D964CEBD3}"
36+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.MySqlProvider.Tests", "tests\Serilog.Ui.MySqlProvider.Tests\Serilog.Ui.MySqlProvider.Tests.csproj", "{6986AD9C-3B9E-4DAF-A45F-643D964CEBD3}"
3737
EndProject
38-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Ui.MsSqlServerProvider.Tests", "tests\Serilog.Ui.MsSqlServerProvider.Tests\Serilog.Ui.MsSqlServerProvider.Tests.csproj", "{1F6675BC-32FC-47DE-96AB-422632AB2730}"
38+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.MsSqlServerProvider.Tests", "tests\Serilog.Ui.MsSqlServerProvider.Tests\Serilog.Ui.MsSqlServerProvider.Tests.csproj", "{1F6675BC-32FC-47DE-96AB-422632AB2730}"
3939
EndProject
40-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Ui.ElastichSearchProvider.Tests", "tests\Serilog.Ui.ElastichSearchProvider.Tests\Serilog.Ui.ElastichSearchProvider.Tests.csproj", "{1AB759E4-61CD-4195-9CA9-E70B63AF28B9}"
40+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Ui.ElasticSearchProvider.Tests", "tests\Serilog.Ui.ElastichSearchProvider.Tests\Serilog.Ui.ElasticSearchProvider.Tests.csproj", "{1AB759E4-61CD-4195-9CA9-E70B63AF28B9}"
4141
EndProject
4242
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{83E91BE7-19B3-4AE0-992C-9DFF30FC409E}"
4343
ProjectSection(SolutionItems) = preProject
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Serilog.Ui.Web.Authorization;
3+
4+
namespace SampleWebApp.Authentication;
5+
6+
public class SerilogUiCustomAuthFilter : IUiAuthorizationFilter
7+
{
8+
public bool Authorize(HttpContext httpContext)
9+
{
10+
return httpContext.User.Identity is { IsAuthenticated: true };
11+
}
12+
}

samples/SampleWebApp/SampleWebApp.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
</ItemGroup>
2626

2727
<ItemGroup>
28-
<ProjectReference Include="..\..\src\Serilog.Ui.ElasticSearchProvider\Serilog.Ui.ElasticSearchProvider.csproj" />
29-
<ProjectReference Include="..\..\src\Serilog.Ui.MongoDbProvider\Serilog.Ui.MongoDbProvider.csproj" />
3028
<ProjectReference Include="..\..\src\Serilog.Ui.MsSqlServerProvider\Serilog.Ui.MsSqlServerProvider.csproj" />
31-
<ProjectReference Include="..\..\src\Serilog.Ui.PostgreSqlProvider\Serilog.Ui.PostgreSqlProvider.csproj" />
3229
<ProjectReference Include="..\..\src\Serilog.Ui.Web\Serilog.Ui.Web.csproj" />
3330
</ItemGroup>
3431

samples/SampleWebApp/Startup.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.Extensions.Hosting;
99
using Microsoft.IdentityModel.Tokens;
10+
using SampleWebApp.Authentication;
1011
using SampleWebApp.Authentication.Jwt;
1112
using SampleWebApp.Data;
1213
using SampleWebApp.Services.HostedServices;
@@ -40,11 +41,6 @@ public void ConfigureServices(IServiceCollection services)
4041
services.AddRazorPages();
4142

4243
services.AddSerilogUi(options => options
43-
.EnableAuthorization(authOption =>
44-
{
45-
authOption.AuthenticationType = AuthenticationType.Jwt;
46-
authOption.Usernames = new[] { "[email protected]" };
47-
})
4844
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), "Logs")
4945
.UseSqlServer(Configuration.GetConnectionString("SecondLogConnection"), "Logs2")
5046
);
@@ -66,16 +62,17 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6662
else
6763
{
6864
app.UseExceptionHandler("/Home/Error");
69-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
65+
// The default HSTS value is 30 days. You may want to change this for production
66+
// scenarios, see https://aka.ms/aspnetcore-hsts.
7067
app.UseHsts();
7168
}
7269
app.UseHttpsRedirection();
7370
app.UseStaticFiles();
7471

7572
app.UseSwagger();
7673

77-
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
78-
// specifying the Swagger JSON endpoint.
74+
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger
75+
// JSON endpoint.
7976
app.UseSwaggerUI(c =>
8077
{
8178
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
@@ -85,11 +82,16 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
8582

8683
app.UseAuthentication();
8784
app.UseAuthorization();
88-
app.UseSerilogUi(x =>
85+
app.UseSerilogUi(options =>
8986
{
90-
x.RoutePrefix = "serilog-ui";
91-
x.HomeUrl = "/#Test";
92-
x.InjectJavascript("/js/serilog-ui/custom.js");
87+
options.RoutePrefix = "serilog-ui";
88+
options.HomeUrl = "/#Test";
89+
options.InjectJavascript("/js/serilog-ui/custom.js");
90+
options.Authorization = new AuthorizationOptions
91+
{
92+
AuthenticationType = AuthenticationType.Jwt,
93+
Filters = new[] { new SerilogUiCustomAuthFilter() }
94+
};
9395
});
9496

9597
app.UseEndpoints(endpoints =>

samples/SampleWebApp/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"Jwt": {
1010
"Audience": "SampleClient",
1111
"Issuer": "http://localhost:5000",
12-
"SecretKey": "Th!$P@ssw0rd",
12+
"SecretKey": "Th!$AlongP@ssw0rdForJwt",
1313
"ExpireDays": "30"
1414
},
1515

src/Serilog.Ui.ElasticSearchProvider/Extensions/SerilogUiOptionBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static void UseElasticSearchDb(this SerilogUiOptionsBuilder optionsBuilde
2525
if (endpoint == null)
2626
throw new ArgumentNullException(nameof(endpoint));
2727

28-
if (string.IsNullOrEmpty(indexName))
28+
if (string.IsNullOrWhiteSpace(indexName))
2929
throw new ArgumentNullException(nameof(indexName));
3030

3131
var options = new ElasticSearchDbOptions

src/Serilog.Ui.ElasticSearchProvider/Serilog.Ui.ElasticSearchProvider.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.3" />
910
<PackageReference Include="NEST" Version="7.11.1" />
1011
<PackageReference Include="NEST.JsonNetSerializer" Version="7.11.1" />
1112
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />

src/Serilog.Ui.MongoDbProvider/Extensions/SerilogUiOptionBuilderExtensions.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,20 @@ public static void UseMongoDb(
2626
string collectionName
2727
)
2828
{
29-
if (string.IsNullOrEmpty(connectionString))
29+
if (string.IsNullOrWhiteSpace(connectionString))
3030
throw new ArgumentNullException(nameof(connectionString));
3131

32-
if (string.IsNullOrEmpty(collectionName))
32+
if (string.IsNullOrWhiteSpace(collectionName))
3333
throw new ArgumentNullException(nameof(collectionName));
3434

35+
var databaseName = MongoUrl.Create(connectionString).DatabaseName;
36+
37+
if (string.IsNullOrWhiteSpace(databaseName)) throw new ArgumentException(nameof(MongoUrl.DatabaseName));
38+
3539
var mongoProvider = new MongoDbOptions
3640
{
3741
ConnectionString = connectionString,
38-
DatabaseName = MongoUrl.Create(connectionString).DatabaseName,
42+
DatabaseName = databaseName,
3943
CollectionName = collectionName
4044
};
4145

@@ -68,13 +72,13 @@ public static void UseMongoDb(
6872
string collectionName
6973
)
7074
{
71-
if (string.IsNullOrEmpty(connectionString))
75+
if (string.IsNullOrWhiteSpace(connectionString))
7276
throw new ArgumentNullException(nameof(connectionString));
7377

74-
if (string.IsNullOrEmpty(databaseName))
78+
if (string.IsNullOrWhiteSpace(databaseName))
7579
throw new ArgumentNullException(nameof(databaseName));
7680

77-
if (string.IsNullOrEmpty(collectionName))
81+
if (string.IsNullOrWhiteSpace(collectionName))
7882
throw new ArgumentNullException(nameof(collectionName));
7983

8084
var mongoProvider = new MongoDbOptions
@@ -92,7 +96,7 @@ string collectionName
9296
throw new NotSupportedException($"Adding multiple registrations of '{typeof(MongoDbDataProvider).FullName}' is not (yet) supported.");
9397

9498
((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddSingleton(mongoProvider);
95-
((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddSingleton<IMongoClient>(o => new MongoClient(connectionString));
99+
((ISerilogUiOptionsBuilder)optionsBuilder).Services.TryAddSingleton<IMongoClient>(o => new MongoClient(connectionString));
96100
((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddScoped<IDataProvider, MongoDbDataProvider>();
97101
}
98102
}

0 commit comments

Comments
 (0)