Use the GitHubAccessToken when downloading the ServerUpdatePackage

Workaround for https://github.com/actions/runner-images/issues/7007
This commit is contained in:
Dominion
2023-05-17 20:10:29 -04:00
parent d8aa40011b
commit d86b4c8e33
6 changed files with 27 additions and 6 deletions
@@ -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);
}
}
}
@@ -43,6 +43,11 @@ namespace Tgstation.Server.Host.Core
/// </summary>
readonly ILogger<ServerUpdater> logger;
/// <summary>
/// The <see cref="GeneralConfiguration"/> for the <see cref="ServerUpdater"/>.
/// </summary>
readonly GeneralConfiguration generalConfiguration;
/// <summary>
/// The <see cref="UpdatesConfiguration"/> for the <see cref="ServerUpdater"/>.
/// </summary>
@@ -61,6 +66,7 @@ namespace Tgstation.Server.Host.Core
/// <param name="fileDownloader">The value of <see cref="fileDownloader"/>.</param>
/// <param name="serverControl">The value of <see cref="serverControl"/>.</param>
/// <param name="logger">The value of <see cref="logger"/>.</param>
/// <param name="generalConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="generalConfiguration"/>.</param>
/// <param name="updatesConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="updatesConfiguration"/>.</param>
public ServerUpdater(
IGitHubClientFactory gitHubClientFactory,
@@ -68,6 +74,7 @@ namespace Tgstation.Server.Host.Core
IFileDownloader fileDownloader,
IServerControl serverControl,
ILogger<ServerUpdater> logger,
IOptions<GeneralConfiguration> generalConfigurationOptions,
IOptions<UpdatesConfiguration> 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)
{
@@ -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
}
/// <inheritdoc />
public async Task<MemoryStream> DownloadFile(Uri url, CancellationToken cancellationToken)
public async Task<MemoryStream> 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();
@@ -14,8 +14,9 @@ namespace Tgstation.Server.Host.IO
/// Downloads a file from <paramref name="url"/>.
/// </summary>
/// <param name="url">The URL to download.</param>
/// <param name="bearerToken">Optional <see cref="string"/> to use as the "Bearer" value in the optional "Authorization" header for the request.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in a <see cref="MemoryStream"/> of the downloaded file.</returns>
Task<MemoryStream> DownloadFile(Uri url, CancellationToken cancellationToken);
Task<MemoryStream> DownloadFile(Uri url, string bearerToken, CancellationToken cancellationToken);
}
}
@@ -51,7 +51,7 @@ namespace Tgstation.Server.Host.Components.Byond.Tests
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => installer.DownloadVersion(null, default));
var ourArray = Array.Empty<byte>();
mockFileDownloader.Setup(x => x.DownloadFile(It.Is<Uri>(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 => 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);
+1 -1
View File
@@ -386,7 +386,7 @@ try
using (var loggerFactory = LoggerFactory.Create(builder => { }))
{
var fileDownloader = new FileDownloader(httpClientFactory, loggerFactory.CreateLogger<FileDownloader>());
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);
}