From d86b4c8e331f1042093f3bf4a94216f8caa6e431 Mon Sep 17 00:00:00 2001 From: Dominion Date: Wed, 17 May 2023 18:57:10 -0400 Subject: [PATCH] Use the GitHubAccessToken when downloading the ServerUpdatePackage Workaround for https://github.com/actions/runner-images/issues/7007 --- .../Components/Byond/ByondInstallerBase.cs | 2 +- src/Tgstation.Server.Host/Core/ServerUpdater.cs | 14 +++++++++++++- src/Tgstation.Server.Host/IO/FileDownloader.cs | 10 +++++++++- src/Tgstation.Server.Host/IO/IFileDownloader.cs | 3 ++- .../Components/Byond/TestPosixByondInstaller.cs | 2 +- tools/Tgstation.Server.Migrator/Program.cs | 2 +- 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/Byond/ByondInstallerBase.cs b/src/Tgstation.Server.Host/Components/Byond/ByondInstallerBase.cs index 0b26625bc7..df32ce6e48 100644 --- a/src/Tgstation.Server.Host/Components/Byond/ByondInstallerBase.cs +++ b/src/Tgstation.Server.Host/Components/Byond/ByondInstallerBase.cs @@ -97,7 +97,7 @@ namespace Tgstation.Server.Host.Components.Byond Logger.LogTrace("Downloading BYOND version {major}.{minor}...", version.Major, version.Minor); var url = String.Format(CultureInfo.InvariantCulture, ByondRevisionsUrlTemplate, version.Major, version.Minor); - return fileDownloader.DownloadFile(new Uri(url), cancellationToken); + return fileDownloader.DownloadFile(new Uri(url), null, cancellationToken); } } } diff --git a/src/Tgstation.Server.Host/Core/ServerUpdater.cs b/src/Tgstation.Server.Host/Core/ServerUpdater.cs index bbada322dc..e552db745a 100644 --- a/src/Tgstation.Server.Host/Core/ServerUpdater.cs +++ b/src/Tgstation.Server.Host/Core/ServerUpdater.cs @@ -43,6 +43,11 @@ namespace Tgstation.Server.Host.Core /// readonly ILogger logger; + /// + /// The for the . + /// + readonly GeneralConfiguration generalConfiguration; + /// /// The for the . /// @@ -61,6 +66,7 @@ namespace Tgstation.Server.Host.Core /// The value of . /// The value of . /// The value of . + /// The containing the value of . /// The containing the value of . public ServerUpdater( IGitHubClientFactory gitHubClientFactory, @@ -68,6 +74,7 @@ namespace Tgstation.Server.Host.Core IFileDownloader fileDownloader, IServerControl serverControl, ILogger logger, + IOptions generalConfigurationOptions, IOptions updatesConfigurationOptions) { this.gitHubClientFactory = gitHubClientFactory ?? throw new ArgumentNullException(nameof(gitHubClientFactory)); @@ -75,6 +82,7 @@ namespace Tgstation.Server.Host.Core this.fileDownloader = fileDownloader ?? throw new ArgumentNullException(nameof(fileDownloader)); this.serverControl = serverControl ?? throw new ArgumentNullException(nameof(serverControl)); this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); + generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions)); updatesConfiguration = updatesConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(updatesConfigurationOptions)); } @@ -183,7 +191,11 @@ namespace Tgstation.Server.Host.Core try { logger.LogTrace("Downloading zip package..."); - updateZipData = await fileDownloader.DownloadFile(serverUpdateOperation.UpdateZipUrl, cancellationToken); + var bearerToken = generalConfiguration.GitHubAccessToken; + if (String.IsNullOrWhiteSpace(bearerToken)) + bearerToken = null; + + updateZipData = await fileDownloader.DownloadFile(serverUpdateOperation.UpdateZipUrl, bearerToken, cancellationToken); } catch (Exception ex) { diff --git a/src/Tgstation.Server.Host/IO/FileDownloader.cs b/src/Tgstation.Server.Host/IO/FileDownloader.cs index 5cd08b7635..46405a2c65 100644 --- a/src/Tgstation.Server.Host/IO/FileDownloader.cs +++ b/src/Tgstation.Server.Host/IO/FileDownloader.cs @@ -1,11 +1,13 @@ using System; using System.IO; using System.Net.Http; +using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Tgstation.Server.Api; using Tgstation.Server.Common; namespace Tgstation.Server.Host.IO @@ -35,14 +37,20 @@ namespace Tgstation.Server.Host.IO } /// - public async Task DownloadFile(Uri url, CancellationToken cancellationToken) + public async Task DownloadFile(Uri url, string bearerToken, CancellationToken cancellationToken) { + if (url == null) + throw new ArgumentNullException(nameof(url)); + logger.LogDebug("Starting download of {url}...", url); using var httpClient = httpClientFactory.CreateClient(); using var request = new HttpRequestMessage( HttpMethod.Get, url); + if (bearerToken != null) + request.Headers.Authorization = new AuthenticationHeaderValue(ApiHeaders.BearerAuthenticationScheme, bearerToken); + var webRequestTask = httpClient.SendAsync(request, cancellationToken); using var response = await webRequestTask; response.EnsureSuccessStatusCode(); diff --git a/src/Tgstation.Server.Host/IO/IFileDownloader.cs b/src/Tgstation.Server.Host/IO/IFileDownloader.cs index 199d904f06..7a31775e1f 100644 --- a/src/Tgstation.Server.Host/IO/IFileDownloader.cs +++ b/src/Tgstation.Server.Host/IO/IFileDownloader.cs @@ -14,8 +14,9 @@ namespace Tgstation.Server.Host.IO /// Downloads a file from . /// /// The URL to download. + /// Optional to use as the "Bearer" value in the optional "Authorization" header for the request. /// A for the operation. /// A resulting in a of the downloaded file. - Task DownloadFile(Uri url, CancellationToken cancellationToken); + Task DownloadFile(Uri url, string bearerToken, CancellationToken cancellationToken); } } diff --git a/tests/Tgstation.Server.Host.Tests/Components/Byond/TestPosixByondInstaller.cs b/tests/Tgstation.Server.Host.Tests/Components/Byond/TestPosixByondInstaller.cs index a78b19a86e..a2da857479 100644 --- a/tests/Tgstation.Server.Host.Tests/Components/Byond/TestPosixByondInstaller.cs +++ b/tests/Tgstation.Server.Host.Tests/Components/Byond/TestPosixByondInstaller.cs @@ -51,7 +51,7 @@ namespace Tgstation.Server.Host.Components.Byond.Tests await Assert.ThrowsExceptionAsync(() => installer.DownloadVersion(null, default)); var ourArray = Array.Empty(); - mockFileDownloader.Setup(x => x.DownloadFile(It.Is(uri => uri == new Uri("https://secure.byond.com/download/build/511/511.1385_byond_linux.zip")), default)).Returns(Task.FromResult(new MemoryStream(ourArray))).Verifiable(); + mockFileDownloader.Setup(x => x.DownloadFile(It.Is(uri => uri == new Uri("https://secure.byond.com/download/build/511/511.1385_byond_linux.zip")), null, default)).Returns(Task.FromResult(new MemoryStream(ourArray))).Verifiable(); var result = await installer.DownloadVersion(new Version(511, 1385), default); diff --git a/tools/Tgstation.Server.Migrator/Program.cs b/tools/Tgstation.Server.Migrator/Program.cs index 6d6406500c..9742ee38ce 100644 --- a/tools/Tgstation.Server.Migrator/Program.cs +++ b/tools/Tgstation.Server.Migrator/Program.cs @@ -386,7 +386,7 @@ try using (var loggerFactory = LoggerFactory.Create(builder => { })) { var fileDownloader = new FileDownloader(httpClientFactory, loggerFactory.CreateLogger()); - using var tgsFiveZipMemoryStream = await fileDownloader.DownloadFile(new Uri(serverServiceAsset.BrowserDownloadUrl), default); + using var tgsFiveZipMemoryStream = await fileDownloader.DownloadFile(new Uri(serverServiceAsset.BrowserDownloadUrl), null, default); Console.WriteLine("Unzipping TGS5..."); await serverFactory.IOManager.ZipToDirectory(tgsInstallPath, tgsFiveZipMemoryStream, default); }