66using Bit . Admin . Models ;
77using Bit . Core . Settings ;
88using Microsoft . AspNetCore . Authorization ;
9+ using Microsoft . AspNetCore . Http ;
910using Microsoft . AspNetCore . Mvc ;
11+ using Microsoft . Extensions . Logging ;
1012
1113namespace Bit . Admin . Controllers
1214{
1315 public class HomeController : Controller
1416 {
1517 private readonly GlobalSettings _globalSettings ;
16- private HttpClient _httpClient = new HttpClient ( ) ;
18+ private readonly HttpClient _httpClient = new HttpClient ( ) ;
19+ private readonly ILogger < HomeController > _logger ;
1720
18- public HomeController ( GlobalSettings globalSettings )
21+ public HomeController ( GlobalSettings globalSettings , ILogger < HomeController > logger )
1922 {
2023 _globalSettings = globalSettings ;
24+ _logger = logger ;
2125 }
2226
2327 [ Authorize ]
@@ -40,10 +44,10 @@ public IActionResult Error()
4044
4145 public async Task < IActionResult > GetLatestDockerHubVersion ( string repository , CancellationToken cancellationToken )
4246 {
47+ var requestUri = $ "https://hub.docker.com/v2/repositories/bitwarden/{ repository } /tags/";
4348 try
4449 {
45- var response = await _httpClient . GetAsync (
46- $ "https://hub.docker.com/v2/repositories/bitwarden/{ repository } /tags/", cancellationToken ) ;
50+ var response = await _httpClient . GetAsync ( requestUri , cancellationToken ) ;
4751 if ( response . IsSuccessStatusCode )
4852 {
4953 using var jsonDocument = await JsonDocument . ParseAsync ( await response . Content . ReadAsStreamAsync ( cancellationToken ) , cancellationToken : cancellationToken ) ;
@@ -60,25 +64,33 @@ public async Task<IActionResult> GetLatestDockerHubVersion(string repository, Ca
6064 }
6165 }
6266 }
63- catch ( HttpRequestException ) { }
67+ catch ( HttpRequestException e )
68+ {
69+ _logger . LogError ( e , $ "Error encountered while sending GET request to { requestUri } ") ;
70+ return new JsonResult ( "Unable to fetch latest version" ) { StatusCode = StatusCodes . Status500InternalServerError } ;
71+ }
6472
6573 return new JsonResult ( "-" ) ;
6674 }
6775
6876 public async Task < IActionResult > GetInstalledWebVersion ( CancellationToken cancellationToken )
6977 {
78+ var requestUri = $ "{ _globalSettings . BaseServiceUri . InternalVault } /version.json";
7079 try
7180 {
72- var response = await _httpClient . GetAsync (
73- $ "{ _globalSettings . BaseServiceUri . InternalVault } /version.json", cancellationToken ) ;
81+ var response = await _httpClient . GetAsync ( requestUri , cancellationToken ) ;
7482 if ( response . IsSuccessStatusCode )
7583 {
7684 using var jsonDocument = await JsonDocument . ParseAsync ( await response . Content . ReadAsStreamAsync ( cancellationToken ) , cancellationToken : cancellationToken ) ;
7785 var root = jsonDocument . RootElement ;
7886 return new JsonResult ( root . GetProperty ( "version" ) . GetString ( ) ) ;
7987 }
8088 }
81- catch ( HttpRequestException ) { }
89+ catch ( HttpRequestException e )
90+ {
91+ _logger . LogError ( e , $ "Error encountered while sending GET request to { requestUri } ") ;
92+ return new JsonResult ( "Unable to fetch installed version" ) { StatusCode = StatusCodes . Status500InternalServerError } ;
93+ }
8294
8395 return new JsonResult ( "-" ) ;
8496 }
0 commit comments