From ed1406acc26065fe96705ec063fe1b5a3725648e Mon Sep 17 00:00:00 2001 From: Colton Hurst Date: Mon, 5 Sep 2022 11:19:04 -0400 Subject: [PATCH] [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 Co-authored-by: Oscar Hinton --- Directory.Build.props | 19 +++++++ .../src/Scim/Controllers/InfoController.cs | 2 +- .../src/Sso/Controllers/InfoController.cs | 2 +- .../src/Sso/Views/Shared/_Layout.cshtml | 2 +- src/Admin/Controllers/HomeController.cs | 2 +- src/Admin/Controllers/InfoController.cs | 2 +- src/Api/Api.csproj | 2 +- src/Api/Controllers/ConfigController.cs | 23 +++++++++ src/Api/Controllers/InfoController.cs | 2 +- .../Models/Response/ConfigResponseModel.cs | 51 +++++++++++++++++++ src/Billing/Controllers/InfoController.cs | 2 +- src/Core/Utilities/AssemblyHelpers.cs | 43 ++++++++++++++++ src/Core/Utilities/CoreHelpers.cs | 12 ----- src/Events/Controllers/InfoController.cs | 2 +- src/EventsProcessor/Startup.cs | 2 +- src/Icons/Controllers/InfoController.cs | 2 +- src/Identity/Controllers/InfoController.cs | 2 +- .../Controllers/InfoController.cs | 2 +- .../Utilities/ServiceCollectionExtensions.cs | 2 +- 19 files changed, 150 insertions(+), 26 deletions(-) create mode 100644 src/Api/Controllers/ConfigController.cs create mode 100644 src/Api/Models/Response/ConfigResponseModel.cs create mode 100644 src/Core/Utilities/AssemblyHelpers.cs diff --git a/Directory.Build.props b/Directory.Build.props index 3a158daa0f..d37501b6d1 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -7,6 +7,7 @@ Bit.$(MSBuildProjectName) true enable + false + + + + + + + + + <_Parameter1>GitHash + <_Parameter2>$(SourceRevisionId) + + + + \ No newline at end of file diff --git a/bitwarden_license/src/Scim/Controllers/InfoController.cs b/bitwarden_license/src/Scim/Controllers/InfoController.cs index aa08ce9bf7..9959bfb0a3 100644 --- a/bitwarden_license/src/Scim/Controllers/InfoController.cs +++ b/bitwarden_license/src/Scim/Controllers/InfoController.cs @@ -17,6 +17,6 @@ public class InfoController : Controller [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(AssemblyHelpers.GetVersion()); } } diff --git a/bitwarden_license/src/Sso/Controllers/InfoController.cs b/bitwarden_license/src/Sso/Controllers/InfoController.cs index c3641c4660..67794de750 100644 --- a/bitwarden_license/src/Sso/Controllers/InfoController.cs +++ b/bitwarden_license/src/Sso/Controllers/InfoController.cs @@ -15,6 +15,6 @@ public class InfoController : Controller [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(AssemblyHelpers.GetVersion()); } } diff --git a/bitwarden_license/src/Sso/Views/Shared/_Layout.cshtml b/bitwarden_license/src/Sso/Views/Shared/_Layout.cshtml index 4467f08f25..ae75119424 100644 --- a/bitwarden_license/src/Sso/Views/Shared/_Layout.cshtml +++ b/bitwarden_license/src/Sso/Views/Shared/_Layout.cshtml @@ -1,4 +1,4 @@ -@using static Bit.Core.Utilities.CoreHelpers; +@using static Bit.Core.Utilities.AssemblyHelpers; diff --git a/src/Admin/Controllers/HomeController.cs b/src/Admin/Controllers/HomeController.cs index 5e3b76ebb8..20c1be70d0 100644 --- a/src/Admin/Controllers/HomeController.cs +++ b/src/Admin/Controllers/HomeController.cs @@ -26,7 +26,7 @@ public class HomeController : Controller return View(new HomeModel { GlobalSettings = _globalSettings, - CurrentVersion = Core.Utilities.CoreHelpers.GetVersion() + CurrentVersion = Core.Utilities.AssemblyHelpers.GetVersion() }); } diff --git a/src/Admin/Controllers/InfoController.cs b/src/Admin/Controllers/InfoController.cs index 0c097fde7d..dc41721de4 100644 --- a/src/Admin/Controllers/InfoController.cs +++ b/src/Admin/Controllers/InfoController.cs @@ -15,6 +15,6 @@ public class InfoController : Controller [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(AssemblyHelpers.GetVersion()); } } diff --git a/src/Api/Api.csproj b/src/Api/Api.csproj index 30ef64658e..901036fcc0 100644 --- a/src/Api/Api.csproj +++ b/src/Api/Api.csproj @@ -32,5 +32,5 @@ - + diff --git a/src/Api/Controllers/ConfigController.cs b/src/Api/Controllers/ConfigController.cs new file mode 100644 index 0000000000..51d224bc6e --- /dev/null +++ b/src/Api/Controllers/ConfigController.cs @@ -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); + } +} diff --git a/src/Api/Controllers/InfoController.cs b/src/Api/Controllers/InfoController.cs index 739f9f4257..9c6b7b8866 100644 --- a/src/Api/Controllers/InfoController.cs +++ b/src/Api/Controllers/InfoController.cs @@ -15,7 +15,7 @@ public class InfoController : Controller [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(AssemblyHelpers.GetVersion()); } [HttpGet("~/ip")] diff --git a/src/Api/Models/Response/ConfigResponseModel.cs b/src/Api/Models/Response/ConfigResponseModel.cs new file mode 100644 index 0000000000..ad2d79a83c --- /dev/null +++ b/src/Api/Models/Response/ConfigResponseModel.cs @@ -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; } +} diff --git a/src/Billing/Controllers/InfoController.cs b/src/Billing/Controllers/InfoController.cs index 58b29f4c4f..16c0158442 100644 --- a/src/Billing/Controllers/InfoController.cs +++ b/src/Billing/Controllers/InfoController.cs @@ -15,6 +15,6 @@ public class InfoController : Controller [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(AssemblyHelpers.GetVersion()); } } diff --git a/src/Core/Utilities/AssemblyHelpers.cs b/src/Core/Utilities/AssemblyHelpers.cs new file mode 100644 index 0000000000..a00e108515 --- /dev/null +++ b/src/Core/Utilities/AssemblyHelpers.cs @@ -0,0 +1,43 @@ +using System.Reflection; + +namespace Bit.Core.Utilities; + +public static class AssemblyHelpers +{ + private static readonly IEnumerable _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(); + _assemblyInformationalVersionAttributes = Assembly.GetEntryAssembly().GetCustomAttribute(); + } + + 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; + } +} diff --git a/src/Core/Utilities/CoreHelpers.cs b/src/Core/Utilities/CoreHelpers.cs index ef6848cf15..0185281b85 100644 --- a/src/Core/Utilities/CoreHelpers.cs +++ b/src/Core/Utilities/CoreHelpers.cs @@ -459,18 +459,6 @@ public static class CoreHelpers return val.ToString(); } - public static string GetVersion() - { - if (string.IsNullOrWhiteSpace(_version)) - { - _version = Assembly.GetEntryAssembly() - .GetCustomAttribute() - .InformationalVersion; - } - - return _version; - } - public static string SanitizeForEmail(string value, bool htmlEncode = true) { var cleanedValue = value.Replace("@", "[at]"); diff --git a/src/Events/Controllers/InfoController.cs b/src/Events/Controllers/InfoController.cs index 6d42f67579..46f21a0f0c 100644 --- a/src/Events/Controllers/InfoController.cs +++ b/src/Events/Controllers/InfoController.cs @@ -15,6 +15,6 @@ public class InfoController : Controller [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(AssemblyHelpers.GetVersion()); } } diff --git a/src/EventsProcessor/Startup.cs b/src/EventsProcessor/Startup.cs index d0a624f737..2f64c0f926 100644 --- a/src/EventsProcessor/Startup.cs +++ b/src/EventsProcessor/Startup.cs @@ -48,7 +48,7 @@ public class Startup endpoints.MapGet("/now", async context => await context.Response.WriteAsJsonAsync(System.DateTime.UtcNow)); endpoints.MapGet("/version", - async context => await context.Response.WriteAsJsonAsync(CoreHelpers.GetVersion())); + async context => await context.Response.WriteAsJsonAsync(AssemblyHelpers.GetVersion())); }); } diff --git a/src/Icons/Controllers/InfoController.cs b/src/Icons/Controllers/InfoController.cs index 1ebbd473a1..253ee7cc78 100644 --- a/src/Icons/Controllers/InfoController.cs +++ b/src/Icons/Controllers/InfoController.cs @@ -15,6 +15,6 @@ public class InfoController : Controller [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(AssemblyHelpers.GetVersion()); } } diff --git a/src/Identity/Controllers/InfoController.cs b/src/Identity/Controllers/InfoController.cs index c06812cdf5..05cf3f2363 100644 --- a/src/Identity/Controllers/InfoController.cs +++ b/src/Identity/Controllers/InfoController.cs @@ -15,6 +15,6 @@ public class InfoController : Controller [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(AssemblyHelpers.GetVersion()); } } diff --git a/src/Notifications/Controllers/InfoController.cs b/src/Notifications/Controllers/InfoController.cs index 6a8eaf2827..afd6911a8d 100644 --- a/src/Notifications/Controllers/InfoController.cs +++ b/src/Notifications/Controllers/InfoController.cs @@ -15,6 +15,6 @@ public class InfoController : Controller [HttpGet("~/version")] public JsonResult GetVersion() { - return Json(CoreHelpers.GetVersion()); + return Json(AssemblyHelpers.GetVersion()); } } diff --git a/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs b/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs index 7fab4b2eed..a9a88b4fea 100644 --- a/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs +++ b/src/SharedWeb/Utilities/ServiceCollectionExtensions.cs @@ -511,7 +511,7 @@ public static class ServiceCollectionExtensions { httpContext.Response.OnStarting((state) => { - httpContext.Response.Headers.Append("Server-Version", CoreHelpers.GetVersion()); + httpContext.Response.Headers.Append("Server-Version", AssemblyHelpers.GetVersion()); return Task.FromResult(0); }, null); await next.Invoke();