|
15 | 15 | using GitHub.DistributedTask.Logging; |
16 | 16 | using GitHub.Runner.Common.Util; |
17 | 17 | using GitHub.Runner.Sdk; |
| 18 | +using GitHub.Services.WebApi.Jwt; |
18 | 19 |
|
19 | 20 | namespace GitHub.Runner.Common |
20 | 21 | { |
@@ -306,6 +307,36 @@ public void LoadDefaultUserAgents() |
306 | 307 | { |
307 | 308 | _userAgents.Add(new ProductInfoHeaderValue("ClientId", clientId)); |
308 | 309 | } |
| 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 | + } |
309 | 340 | } |
310 | 341 |
|
311 | 342 | var runnerFile = GetConfigFile(WellKnownConfigFile.Runner); |
|
0 commit comments