Skip to content

Commit ac752ff

Browse files
TingluoHuangLouisHaftmann
authored andcommitted
Try add orchestrationid into user-agent using token claim. (actions#3945)
1 parent 5ba17b0 commit ac752ff

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/Runner.Common/HostContext.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using GitHub.DistributedTask.Logging;
1616
using GitHub.Runner.Common.Util;
1717
using GitHub.Runner.Sdk;
18+
using GitHub.Services.WebApi.Jwt;
1819

1920
namespace GitHub.Runner.Common
2021
{
@@ -306,6 +307,36 @@ public void LoadDefaultUserAgents()
306307
{
307308
_userAgents.Add(new ProductInfoHeaderValue("ClientId", clientId));
308309
}
310+
311+
// for Hosted runner, we can pull orchestrationId from JWT claims of the runner listening token.
312+
if (credData != null &&
313+
credData.Scheme == Constants.Configuration.OAuthAccessToken &&
314+
credData.Data.TryGetValue(Constants.Runner.CommandLine.Args.Token, out var accessToken) &&
315+
!string.IsNullOrEmpty(accessToken))
316+
{
317+
try
318+
{
319+
var jwt = JsonWebToken.Create(accessToken);
320+
var claims = jwt.ExtractClaims();
321+
var orchestrationId = claims.FirstOrDefault(x => string.Equals(x.Type, "orch_id", StringComparison.OrdinalIgnoreCase))?.Value;
322+
if (string.IsNullOrEmpty(orchestrationId))
323+
{
324+
// fallback to orchid for C# actions-service
325+
orchestrationId = claims.FirstOrDefault(x => string.Equals(x.Type, "orchid", StringComparison.OrdinalIgnoreCase))?.Value;
326+
}
327+
328+
if (!string.IsNullOrEmpty(orchestrationId))
329+
{
330+
_trace.Info($"Pull OrchestrationId {orchestrationId} from runner JWT claims");
331+
_userAgents.Insert(0, new ProductInfoHeaderValue("OrchestrationId", orchestrationId));
332+
}
333+
}
334+
catch (Exception ex)
335+
{
336+
_trace.Error("Fail to extract OrchestrationId from runner JWT claims");
337+
_trace.Error(ex);
338+
}
339+
}
309340
}
310341

311342
var runnerFile = GetConfigFile(WellKnownConfigFile.Runner);

src/Runner.Listener/JobDispatcher.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ public void Run(Pipelines.AgentJobRequestMessage jobRequestMessage, bool runOnce
110110
{
111111
var jwt = JsonWebToken.Create(accessToken);
112112
var claims = jwt.ExtractClaims();
113-
orchestrationId = claims.FirstOrDefault(x => string.Equals(x.Type, "orchid", StringComparison.OrdinalIgnoreCase))?.Value;
113+
orchestrationId = claims.FirstOrDefault(x => string.Equals(x.Type, "orch_id", StringComparison.OrdinalIgnoreCase))?.Value;
114+
if (string.IsNullOrEmpty(orchestrationId))
115+
{
116+
orchestrationId = claims.FirstOrDefault(x => string.Equals(x.Type, "orchid", StringComparison.OrdinalIgnoreCase))?.Value;
117+
}
118+
114119
if (!string.IsNullOrEmpty(orchestrationId))
115120
{
116121
Trace.Info($"Pull OrchestrationId {orchestrationId} from JWT claims");

src/Runner.Worker/JobRunner.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ public async Task<TaskResult> RunAsync(AgentJobRequestMessage message, Cancellat
5050
if (message.Variables.TryGetValue(Constants.Variables.System.OrchestrationId, out VariableValue orchestrationId) &&
5151
!string.IsNullOrEmpty(orchestrationId.Value))
5252
{
53-
// make the orchestration id the first item in the user-agent header to avoid get truncated in server log.
54-
HostContext.UserAgents.Insert(0, new ProductInfoHeaderValue("OrchestrationId", orchestrationId.Value));
53+
if (!HostContext.UserAgents.Any(x => string.Equals(x.Product.Name, "OrchestrationId", StringComparison.OrdinalIgnoreCase)))
54+
{
55+
// make the orchestration id the first item in the user-agent header to avoid get truncated in server log.
56+
HostContext.UserAgents.Insert(0, new ProductInfoHeaderValue("OrchestrationId", orchestrationId.Value));
57+
}
5558

5659
// make sure orchestration id is in the user-agent header.
5760
VssUtil.InitializeVssClientSettings(HostContext.UserAgents, HostContext.WebProxy);

0 commit comments

Comments
 (0)