1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-29 13:25:17 +01:00

Update code to log to Azure Cosmos DB (#1624)

* Update code to log to Azure Cosmos DB using latest SDK.
This commit is contained in:
Vince Grassia 2021-10-11 10:16:19 -04:00 committed by GitHub
parent 8a5a371a8f
commit fd6cdd019e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 25 deletions

View File

@ -18,6 +18,7 @@
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.21.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
<PackageReference Include="NewRelic.Agent" Version="8.41.0" /> <PackageReference Include="NewRelic.Agent" Version="8.41.0" />
</ItemGroup> </ItemGroup>

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bit.Admin.Models; using Bit.Admin.Models;
@ -6,8 +7,8 @@ using Bit.Core.Settings;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Documents.Client; using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Documents.Linq; using Microsoft.Azure.Cosmos.Linq;
using Serilog.Events; using Serilog.Events;
namespace Bit.Admin.Controllers namespace Bit.Admin.Controllers
@ -17,7 +18,7 @@ namespace Bit.Admin.Controllers
public class LogsController : Controller public class LogsController : Controller
{ {
private const string Database = "Diagnostics"; private const string Database = "Diagnostics";
private const string Collection = "Logs"; private const string Container = "Logs";
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
@ -29,17 +30,18 @@ namespace Bit.Admin.Controllers
public async Task<IActionResult> Index(string cursor = null, int count = 50, public async Task<IActionResult> Index(string cursor = null, int count = 50,
LogEventLevel? level = null, string project = null, DateTime? start = null, DateTime? end = null) LogEventLevel? level = null, string project = null, DateTime? start = null, DateTime? end = null)
{ {
var collectionLink = UriFactory.CreateDocumentCollectionUri(Database, Collection); using (var client = new CosmosClient(_globalSettings.DocumentDb.Uri,
using (var client = new DocumentClient(new Uri(_globalSettings.DocumentDb.Uri),
_globalSettings.DocumentDb.Key)) _globalSettings.DocumentDb.Key))
{ {
var options = new FeedOptions var cosmosContainer = client.GetContainer(Database, Container);
{ var query = cosmosContainer.GetItemLinqQueryable<LogModel>(
MaxItemCount = count, requestOptions: new QueryRequestOptions()
RequestContinuation = cursor {
}; MaxItemCount = count
},
var query = client.CreateDocumentQuery<LogModel>(collectionLink, options).AsQueryable(); continuationToken: cursor
).AsQueryable();
if (level.HasValue) if (level.HasValue)
{ {
query = query.Where(l => l.Level == level.Value.ToString()); query = query.Where(l => l.Level == level.Value.ToString());
@ -56,9 +58,8 @@ namespace Bit.Admin.Controllers
{ {
query = query.Where(l => l.Timestamp <= end.Value); query = query.Where(l => l.Timestamp <= end.Value);
} }
var feedIterator = query.OrderByDescending(l => l.Timestamp).ToFeedIterator();
var docQuery = query.OrderByDescending(l => l.Timestamp).AsDocumentQuery(); var response = await feedIterator.ReadNextAsync();
var response = await docQuery.ExecuteNextAsync<LogModel>();
return View(new LogsModel return View(new LogsModel
{ {
@ -69,24 +70,27 @@ namespace Bit.Admin.Controllers
Items = response.ToList(), Items = response.ToList(),
Count = count, Count = count,
Cursor = cursor, Cursor = cursor,
NextCursor = response.ResponseContinuation NextCursor = response.ContinuationToken
}); });
} }
} }
public async Task<IActionResult> View(Guid id) public async Task<IActionResult> View(Guid id)
{ {
using (var client = new DocumentClient(new Uri(_globalSettings.DocumentDb.Uri), using (var client = new CosmosClient(_globalSettings.DocumentDb.Uri,
_globalSettings.DocumentDb.Key)) _globalSettings.DocumentDb.Key))
{ {
var uri = UriFactory.CreateDocumentUri(Database, Collection, id.ToString()); var cosmosContainer = client.GetContainer(Database, Container);
var response = await client.ReadDocumentAsync<LogDetailsModel>(uri); var query = cosmosContainer.GetItemLinqQueryable<LogDetailsModel>()
if (response?.Document == null) .AsQueryable()
.Where(l => l.Id == id.ToString());
var response = await query.ToFeedIterator().ReadNextAsync();
if (response == null || response.Count == 0)
{ {
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
return View(response.First());
return View(response.Document);
} }
} }
} }

View File

@ -45,11 +45,11 @@
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" /> <PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" /> <PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" /> <PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.AzureDocumentDB" Version="3.8.0" />
<PackageReference Include="Sentry.Serilog" Version="2.1.5" /> <PackageReference Include="Sentry.Serilog" Version="2.1.5" />
<PackageReference Include="IdentityServer4" Version="4.0.4" /> <PackageReference Include="IdentityServer4" Version="4.0.4" />
<PackageReference Include="Dapper" Version="2.0.35" /> <PackageReference Include="Dapper" Version="2.0.35" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Serilog.Sinks.AzureCosmosDB" Version="1.0.0" />
<PackageReference Include="Serilog.Sinks.SyslogMessages" Version="1.0.5" /> <PackageReference Include="Serilog.Sinks.SyslogMessages" Version="1.0.5" />
<PackageReference Include="System.Text.Json" Version="4.7.2" /> <PackageReference Include="System.Text.Json" Version="4.7.2" />
<PackageReference Include="AspNetCoreRateLimit" Version="2.1.0" /> <PackageReference Include="AspNetCoreRateLimit" Version="2.1.0" />

View File

@ -63,8 +63,9 @@ namespace Bit.Core.Utilities
if (CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Uri) && if (CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Uri) &&
CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Key)) CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Key))
{ {
config.WriteTo.AzureDocumentDB(new Uri(globalSettings.DocumentDb.Uri), config.WriteTo.AzureCosmosDB(new Uri(globalSettings.DocumentDb.Uri),
globalSettings.DocumentDb.Key, timeToLive: TimeSpan.FromDays(7)) globalSettings.DocumentDb.Key, timeToLive: TimeSpan.FromDays(7),
partitionKey: "_partitionKey")
.Enrich.FromLogContext() .Enrich.FromLogContext()
.Enrich.WithProperty("Project", globalSettings.ProjectName); .Enrich.WithProperty("Project", globalSettings.ProjectName);
} }