From a5e4460553e1f4345f2040b1009ad2faebab5c17 Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Mon, 29 Jan 2024 23:49:49 -0500 Subject: [PATCH] Add user-agent to all http clients using RawClientHttpRequestSettings. --- src/Runner.Sdk/Util/VssUtil.cs | 25 ++++++++++++++++++- src/Runner.Worker/JobRunner.cs | 3 +++ src/Sdk/Common/Common/RawHttpHeaders.cs | 3 +-- .../Common/Common/RawHttpMessageHandler.cs | 2 ++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Runner.Sdk/Util/VssUtil.cs b/src/Runner.Sdk/Util/VssUtil.cs index bf74fa16cac..894e29864ae 100644 --- a/src/Runner.Sdk/Util/VssUtil.cs +++ b/src/Runner.Sdk/Util/VssUtil.cs @@ -23,7 +23,13 @@ public static void InitializeVssClientSettings(List addi if (VssClientHttpRequestSettings.Default.UserAgent != null && VssClientHttpRequestSettings.Default.UserAgent.Count > 0) { - headerValues.AddRange(VssClientHttpRequestSettings.Default.UserAgent); + foreach (var headerVal in VssClientHttpRequestSettings.Default.UserAgent) + { + if (!headerValues.Contains(headerVal)) + { + headerValues.Add(headerVal); + } + } } VssClientHttpRequestSettings.Default.UserAgent = headerValues; @@ -33,6 +39,23 @@ public static void InitializeVssClientSettings(List addi { VssClientHttpRequestSettings.Default.ServerCertificateValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; } + + var rawHeaderValues = new List(); + rawHeaderValues.AddRange(additionalUserAgents); + rawHeaderValues.Add(new ProductInfoHeaderValue($"({StringUtil.SanitizeUserAgentHeader(RuntimeInformation.OSDescription)})")); + + if (RawClientHttpRequestSettings.Default.UserAgent != null && RawClientHttpRequestSettings.Default.UserAgent.Count > 0) + { + foreach (var headerVal in RawClientHttpRequestSettings.Default.UserAgent) + { + if (!rawHeaderValues.Contains(headerVal)) + { + rawHeaderValues.Add(headerVal); + } + } + } + + RawClientHttpRequestSettings.Default.UserAgent = rawHeaderValues; } public static VssConnection CreateConnection( diff --git a/src/Runner.Worker/JobRunner.cs b/src/Runner.Worker/JobRunner.cs index 17915231e47..14328c4523a 100644 --- a/src/Runner.Worker/JobRunner.cs +++ b/src/Runner.Worker/JobRunner.cs @@ -49,6 +49,9 @@ public async Task RunAsync(AgentJobRequestMessage message, Cancellat !string.IsNullOrEmpty(orchestrationId.Value)) { HostContext.UserAgents.Add(new ProductInfoHeaderValue("OrchestrationId", orchestrationId.Value)); + + // make sure orchestration id is in the user-agent header. + VssUtil.InitializeVssClientSettings(HostContext.UserAgents, HostContext.WebProxy); } var jobServerQueueTelemetry = false; diff --git a/src/Sdk/Common/Common/RawHttpHeaders.cs b/src/Sdk/Common/Common/RawHttpHeaders.cs index f4f6beabe1c..55f0caaf9b1 100644 --- a/src/Sdk/Common/Common/RawHttpHeaders.cs +++ b/src/Sdk/Common/Common/RawHttpHeaders.cs @@ -1,12 +1,11 @@ using System; using System.ComponentModel; -using System.Diagnostics.CodeAnalysis; namespace GitHub.Services.Common.Internal { [EditorBrowsable(EditorBrowsableState.Never)] public static class RawHttpHeaders { - public const String SessionHeader = "X-Runner-Session"; + public const String SessionHeader = "X-Actions-Session"; } } diff --git a/src/Sdk/Common/Common/RawHttpMessageHandler.cs b/src/Sdk/Common/Common/RawHttpMessageHandler.cs index 09cd2d59a60..316bcd576ff 100644 --- a/src/Sdk/Common/Common/RawHttpMessageHandler.cs +++ b/src/Sdk/Common/Common/RawHttpMessageHandler.cs @@ -138,6 +138,8 @@ protected override async Task SendAsync( response.Dispose(); } + this.Settings.ApplyTo(request); + // Let's start with sending a token IssuedToken token = null; if (m_tokenProvider != null)