|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Security.Cryptography; |
| 5 | +using System.Text; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Azure.Sdk.Tools.PipelineWitness.Configuration; |
| 9 | +using Azure.Sdk.Tools.PipelineWitness.GitHubActions; |
| 10 | +using Azure.Storage.Queues; |
| 11 | +using Microsoft.AspNetCore.Mvc; |
| 12 | +using Microsoft.Extensions.Logging; |
| 13 | +using Microsoft.Extensions.Options; |
| 14 | + |
| 15 | +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 |
| 16 | + |
| 17 | +namespace Azure.Sdk.Tools.PipelineWitness.Controllers |
| 18 | +{ |
| 19 | + [Route("api/githubevents")] |
| 20 | + [ApiController] |
| 21 | + public class GitHubEventsController : ControllerBase |
| 22 | + { |
| 23 | + private readonly QueueClient queueClient; |
| 24 | + private readonly ILogger<GitHubEventsController> logger; |
| 25 | + private readonly PipelineWitnessSettings settings; |
| 26 | + |
| 27 | + public GitHubEventsController(ILogger<GitHubEventsController> logger, QueueServiceClient queueServiceClient, IOptions<PipelineWitnessSettings> options) |
| 28 | + { |
| 29 | + this.logger = logger; |
| 30 | + this.settings = options.Value; |
| 31 | + this.queueClient = queueServiceClient.GetQueueClient(this.settings.GitHubActionRunsQueueName); |
| 32 | + } |
| 33 | + |
| 34 | + // POST api/githubevents |
| 35 | + [HttpPost] |
| 36 | + public async Task<IActionResult> PostAsync() |
| 37 | + { |
| 38 | + var eventName = Request.Headers["X-GitHub-Event"].FirstOrDefault(); |
| 39 | + switch (eventName) |
| 40 | + { |
| 41 | + case "ping": |
| 42 | + return Ok(); |
| 43 | + case "workflow_run": |
| 44 | + return await ProcessWorkflowRunEventAsync(); |
| 45 | + default: |
| 46 | + this.logger.LogWarning("Received GitHub event {EventName} which is not supported", eventName); |
| 47 | + return BadRequest(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static bool VerifySignature(string text, string key, string signature) |
| 52 | + { |
| 53 | + Encoding encoding = Encoding.UTF8; |
| 54 | + |
| 55 | + byte[] textBytes = encoding.GetBytes(text); |
| 56 | + byte[] keyBytes = encoding.GetBytes(key); |
| 57 | + |
| 58 | + using HMACSHA256 hasher = new(keyBytes); |
| 59 | + byte[] hashBytes = hasher.ComputeHash(textBytes); |
| 60 | + |
| 61 | + var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); |
| 62 | + var expectedSignature = $"sha256={hash}"; |
| 63 | + return signature == expectedSignature; |
| 64 | + } |
| 65 | + |
| 66 | + private async Task<IActionResult> ProcessWorkflowRunEventAsync() |
| 67 | + { |
| 68 | + using var reader = new StreamReader(Request.Body); |
| 69 | + var body = await reader.ReadToEndAsync(); |
| 70 | + var signature = Request.Headers["X-Hub-Signature-256"].FirstOrDefault(); |
| 71 | + |
| 72 | + if (!VerifySignature(body, this.settings.GitHubWebhookSecret, signature)) |
| 73 | + { |
| 74 | + this.logger.LogWarning("Received GitHub event {Event} with invalid signature", "workflow_run"); |
| 75 | + return Unauthorized(); |
| 76 | + } |
| 77 | + |
| 78 | + var eventMessage = JsonDocument.Parse(body).RootElement; |
| 79 | + |
| 80 | + string action = eventMessage.GetProperty("action").GetString(); |
| 81 | + |
| 82 | + this.logger.LogInformation("Received GitHub event {Event}.{Action}", "workflow_run", action); |
| 83 | + |
| 84 | + if (action == "completed") |
| 85 | + { |
| 86 | + var queueMessage = new GitHubRunCompleteMessage |
| 87 | + { |
| 88 | + Owner = eventMessage.GetProperty("repository").GetProperty("owner").GetProperty("login").GetString(), |
| 89 | + Repository = eventMessage.GetProperty("repository").GetProperty("name").GetString(), |
| 90 | + RunId = eventMessage.GetProperty("workflow_run").GetProperty("id").GetInt64(), |
| 91 | + }; |
| 92 | + |
| 93 | + this.logger.LogInformation("Enqueuing GitHubRunCompleteMessage for {Owner}/{Repository} run {RunId}", queueMessage.Owner, queueMessage.Repository, queueMessage.RunId); |
| 94 | + |
| 95 | + await this.queueClient.SendMessageAsync(JsonSerializer.Serialize(queueMessage)); |
| 96 | + } |
| 97 | + |
| 98 | + return Ok(); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments