1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-28 13:15:12 +01:00

[SM-90] Add Config Endpoint Phase 1 (#2130)

* Add config endpoint with version and gitHash in response

* Remove gitHash from version, formatting and other improvements

* change name of variable in ConfigController

* Update to properly get gitHash

* SM-94: Add global settings for api url

* SM-94: ConfigController cleanup

* SM-94: Make version and gitHash available for all projects, using AssemblyHelper

* Update ConfigResponseModel GetVersion() call

* Change AssemblyHelpers.cs to use the UTF-8 charset

* SM-94: Use AssemblyHelpers.GetVersion and deprecate CoreHelpers.GetVersion

* SM-90: Add other BaseServiceUriSettings urls

* SM-94: Fix dotnet format issue

* remove old GetVersion method

* Add back the linebreak

* Fix typo in Directory.Build.props

Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com>

Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com>
This commit is contained in:
Colton Hurst 2022-09-05 11:19:04 -04:00 committed by GitHub
parent 9a12992b59
commit ed1406acc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 150 additions and 26 deletions

View File

@ -7,6 +7,7 @@
<RootNamespace>Bit.$(MSBuildProjectName)</RootNamespace> <RootNamespace>Bit.$(MSBuildProjectName)</RootNamespace>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile> <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
</PropertyGroup> </PropertyGroup>
<!-- <!--
@ -49,4 +50,22 @@
<AutoFixtureAutoNSubstituteVersion>4.17.0</AutoFixtureAutoNSubstituteVersion> <AutoFixtureAutoNSubstituteVersion>4.17.0</AutoFixtureAutoNSubstituteVersion>
</PropertyGroup> </PropertyGroup>
<!--
This section is for getting & setting the gitHash value, which can easily be accessed
via the Core.Utilities.AssemblyHelpers class.
-->
<Target Name="SetSourceRevisionId" BeforeTargets="CoreGenerateAssemblyInfo">
<Exec Command="git describe --long --always --dirty --exclude=* --abbrev=8" ConsoleToMSBuild="True" IgnoreExitCode="False">
<Output PropertyName="SourceRevisionId" TaskParameter="ConsoleOutput" />
</Exec>
</Target>
<Target Name="WriteRevision" AfterTargets="SetSourceRevisionId">
<ItemGroup>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>GitHash</_Parameter1>
<_Parameter2>$(SourceRevisionId)</_Parameter2>
</AssemblyAttribute>
</ItemGroup>
</Target>
</Project> </Project>

View File

@ -17,6 +17,6 @@ public class InfoController : Controller
[HttpGet("~/version")] [HttpGet("~/version")]
public JsonResult GetVersion() public JsonResult GetVersion()
{ {
return Json(CoreHelpers.GetVersion()); return Json(AssemblyHelpers.GetVersion());
} }
} }

View File

@ -15,6 +15,6 @@ public class InfoController : Controller
[HttpGet("~/version")] [HttpGet("~/version")]
public JsonResult GetVersion() public JsonResult GetVersion()
{ {
return Json(CoreHelpers.GetVersion()); return Json(AssemblyHelpers.GetVersion());
} }
} }

View File

@ -1,4 +1,4 @@
@using static Bit.Core.Utilities.CoreHelpers; @using static Bit.Core.Utilities.AssemblyHelpers;
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">

View File

@ -26,7 +26,7 @@ public class HomeController : Controller
return View(new HomeModel return View(new HomeModel
{ {
GlobalSettings = _globalSettings, GlobalSettings = _globalSettings,
CurrentVersion = Core.Utilities.CoreHelpers.GetVersion() CurrentVersion = Core.Utilities.AssemblyHelpers.GetVersion()
}); });
} }

View File

@ -15,6 +15,6 @@ public class InfoController : Controller
[HttpGet("~/version")] [HttpGet("~/version")]
public JsonResult GetVersion() public JsonResult GetVersion()
{ {
return Json(CoreHelpers.GetVersion()); return Json(AssemblyHelpers.GetVersion());
} }
} }

View File

@ -0,0 +1,23 @@
using Bit.Api.Models.Response;
using Bit.Core.Settings;
using Microsoft.AspNetCore.Mvc;
namespace Bit.Api.Controllers;
[Route("config")]
public class ConfigController : Controller
{
private readonly IGlobalSettings _globalSettings;
public ConfigController(IGlobalSettings globalSettings)
{
_globalSettings = globalSettings;
}
[HttpGet("")]
public ConfigResponseModel GetConfigs()
{
return new ConfigResponseModel(_globalSettings);
}
}

View File

@ -15,7 +15,7 @@ public class InfoController : Controller
[HttpGet("~/version")] [HttpGet("~/version")]
public JsonResult GetVersion() public JsonResult GetVersion()
{ {
return Json(CoreHelpers.GetVersion()); return Json(AssemblyHelpers.GetVersion());
} }
[HttpGet("~/ip")] [HttpGet("~/ip")]

View File

@ -0,0 +1,51 @@
using Bit.Core.Models.Api;
using Bit.Core.Settings;
using Bit.Core.Utilities;
namespace Bit.Api.Models.Response;
public class ConfigResponseModel : ResponseModel
{
public string Version { get; set; }
public string GitHash { get; set; }
public ServerConfigResponseModel Server { get; set; }
public EnvironmentConfigResponseModel Environment { get; set; }
public ConfigResponseModel(string obj = "config") : base(obj)
{
Version = AssemblyHelpers.GetVersion();
GitHash = AssemblyHelpers.GetGitHash();
Environment = new EnvironmentConfigResponseModel();
}
public ConfigResponseModel(IGlobalSettings globalSettings, string obj = "config") : base(obj)
{
Version = AssemblyHelpers.GetVersion();
GitHash = AssemblyHelpers.GetGitHash();
Environment = new EnvironmentConfigResponseModel
{
Vault = globalSettings.BaseServiceUri.Vault,
Api = globalSettings.BaseServiceUri.Api,
Identity = globalSettings.BaseServiceUri.Identity,
Admin = globalSettings.BaseServiceUri.Admin,
Notifications = globalSettings.BaseServiceUri.Notifications,
Sso = globalSettings.BaseServiceUri.Sso
};
}
}
public class ServerConfigResponseModel
{
public string Name { get; set; }
public string Url { get; set; }
}
public class EnvironmentConfigResponseModel
{
public string Vault { get; set; }
public string Api { get; set; }
public string Identity { get; set; }
public string Admin { get; set; }
public string Notifications { get; set; }
public string Sso { get; set; }
}

View File

@ -15,6 +15,6 @@ public class InfoController : Controller
[HttpGet("~/version")] [HttpGet("~/version")]
public JsonResult GetVersion() public JsonResult GetVersion()
{ {
return Json(CoreHelpers.GetVersion()); return Json(AssemblyHelpers.GetVersion());
} }
} }

View File

@ -0,0 +1,43 @@
using System.Reflection;
namespace Bit.Core.Utilities;
public static class AssemblyHelpers
{
private static readonly IEnumerable<AssemblyMetadataAttribute> _assemblyMetadataAttributes;
private static readonly AssemblyInformationalVersionAttribute _assemblyInformationalVersionAttributes;
private const string GIT_HASH_ASSEMBLY_KEY = "GitHash";
private static string _version;
private static string _gitHash;
static AssemblyHelpers()
{
_assemblyMetadataAttributes = Assembly.GetEntryAssembly().GetCustomAttributes<AssemblyMetadataAttribute>();
_assemblyInformationalVersionAttributes = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>();
}
public static string GetVersion()
{
if (string.IsNullOrWhiteSpace(_version))
{
_version = _assemblyInformationalVersionAttributes.InformationalVersion;
}
return _version;
}
public static string GetGitHash()
{
if (string.IsNullOrWhiteSpace(_gitHash))
{
try
{
_gitHash = _assemblyMetadataAttributes.Where(i => i.Key == GIT_HASH_ASSEMBLY_KEY).First().Value;
}
catch (Exception)
{ }
}
return _gitHash;
}
}

View File

@ -459,18 +459,6 @@ public static class CoreHelpers
return val.ToString(); return val.ToString();
} }
public static string GetVersion()
{
if (string.IsNullOrWhiteSpace(_version))
{
_version = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
}
return _version;
}
public static string SanitizeForEmail(string value, bool htmlEncode = true) public static string SanitizeForEmail(string value, bool htmlEncode = true)
{ {
var cleanedValue = value.Replace("@", "[at]"); var cleanedValue = value.Replace("@", "[at]");

View File

@ -15,6 +15,6 @@ public class InfoController : Controller
[HttpGet("~/version")] [HttpGet("~/version")]
public JsonResult GetVersion() public JsonResult GetVersion()
{ {
return Json(CoreHelpers.GetVersion()); return Json(AssemblyHelpers.GetVersion());
} }
} }

View File

@ -48,7 +48,7 @@ public class Startup
endpoints.MapGet("/now", endpoints.MapGet("/now",
async context => await context.Response.WriteAsJsonAsync(System.DateTime.UtcNow)); async context => await context.Response.WriteAsJsonAsync(System.DateTime.UtcNow));
endpoints.MapGet("/version", endpoints.MapGet("/version",
async context => await context.Response.WriteAsJsonAsync(CoreHelpers.GetVersion())); async context => await context.Response.WriteAsJsonAsync(AssemblyHelpers.GetVersion()));
}); });
} }

View File

@ -15,6 +15,6 @@ public class InfoController : Controller
[HttpGet("~/version")] [HttpGet("~/version")]
public JsonResult GetVersion() public JsonResult GetVersion()
{ {
return Json(CoreHelpers.GetVersion()); return Json(AssemblyHelpers.GetVersion());
} }
} }

View File

@ -15,6 +15,6 @@ public class InfoController : Controller
[HttpGet("~/version")] [HttpGet("~/version")]
public JsonResult GetVersion() public JsonResult GetVersion()
{ {
return Json(CoreHelpers.GetVersion()); return Json(AssemblyHelpers.GetVersion());
} }
} }

View File

@ -15,6 +15,6 @@ public class InfoController : Controller
[HttpGet("~/version")] [HttpGet("~/version")]
public JsonResult GetVersion() public JsonResult GetVersion()
{ {
return Json(CoreHelpers.GetVersion()); return Json(AssemblyHelpers.GetVersion());
} }
} }

View File

@ -511,7 +511,7 @@ public static class ServiceCollectionExtensions
{ {
httpContext.Response.OnStarting((state) => httpContext.Response.OnStarting((state) =>
{ {
httpContext.Response.Headers.Append("Server-Version", CoreHelpers.GetVersion()); httpContext.Response.Headers.Append("Server-Version", AssemblyHelpers.GetVersion());
return Task.FromResult(0); return Task.FromResult(0);
}, null); }, null);
await next.Invoke(); await next.Invoke();