Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user-agent to all http clients using RawClientHttpRequestSettings. #3115

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Runner.Sdk/Util/VssUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public static void InitializeVssClientSettings(List<ProductInfoHeaderValue> 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;
Expand All @@ -33,6 +39,23 @@ public static void InitializeVssClientSettings(List<ProductInfoHeaderValue> addi
{
VssClientHttpRequestSettings.Default.ServerCertificateValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
}

var rawHeaderValues = new List<ProductInfoHeaderValue>();
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(
Expand Down
3 changes: 3 additions & 0 deletions src/Runner.Worker/JobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public async Task<TaskResult> 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;
Expand Down
3 changes: 1 addition & 2 deletions src/Sdk/Common/Common/RawHttpHeaders.cs
Original file line number Diff line number Diff line change
@@ -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";
}
}
2 changes: 2 additions & 0 deletions src/Sdk/Common/Common/RawHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ protected override async Task<HttpResponseMessage> SendAsync(
response.Dispose();
}

this.Settings.ApplyTo(request);

// Let's start with sending a token
IssuedToken token = null;
if (m_tokenProvider != null)
Expand Down
Loading