-
Notifications
You must be signed in to change notification settings - Fork 221
Add github action logs to pipeline witness #8459
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3d311da
Add github action logs
hallipr 898927c
Add hallipr as codeowner for pipeline-witness
hallipr 5f33a2d
Update .github/CODEOWNERS
hallipr c702571
Add deploy script
hallipr 0f1ee19
Revert net7 change
hallipr a55bdd5
Move to net8
hallipr ae00212
Update global.json
hallipr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| appsettings.local.json |
2 changes: 1 addition & 1 deletion
2
...itness/Azure.Sdk.Tools.PipelineWitness.Tests/Azure.Sdk.Tools.PipelineWitness.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...s/pipeline-witness/Azure.Sdk.Tools.PipelineWitness/Configuration/ISecretClientProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using System; | ||
| using Azure.Security.KeyVault.Secrets; | ||
|
|
||
| namespace Azure.Sdk.Tools.PipelineWitness.Configuration; | ||
|
|
||
| public interface ISecretClientProvider | ||
| { | ||
| SecretClient GetSecretClient(Uri vaultUri); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ne-witness/Azure.Sdk.Tools.PipelineWitness/Configuration/PostConfigureKeyVaultSettings.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Text.RegularExpressions; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace Azure.Sdk.Tools.PipelineWitness.Configuration; | ||
|
|
||
| public class PostConfigureKeyVaultSettings<T> : IPostConfigureOptions<T> where T : class | ||
| { | ||
| private static readonly Regex secretRegex = new Regex(@"(?<vault>https://.*?\.vault\.azure\.net)/secrets/(?<secret>.*)", RegexOptions.Compiled, TimeSpan.FromSeconds(5)); | ||
| private readonly ILogger logger; | ||
| private readonly ISecretClientProvider secretClientProvider; | ||
|
|
||
| public PostConfigureKeyVaultSettings(ILogger<PostConfigureKeyVaultSettings<T>> logger, ISecretClientProvider secretClientProvider) | ||
| { | ||
| this.logger = logger; | ||
| this.secretClientProvider = secretClientProvider; | ||
| } | ||
|
|
||
| public void PostConfigure(string name, T options) | ||
| { | ||
| var stringProperties = typeof(T) | ||
| .GetProperties() | ||
| .Where(x => x.PropertyType == typeof(string)); | ||
|
|
||
| foreach (var property in stringProperties) | ||
| { | ||
| var value = (string)property.GetValue(options); | ||
|
|
||
| if (value != null) | ||
| { | ||
| var match = secretRegex.Match(value); | ||
|
|
||
| if (match.Success) | ||
| { | ||
| var vaultUrl = match.Groups["vault"].Value; | ||
| var secretName = match.Groups["secret"].Value; | ||
|
|
||
| try | ||
| { | ||
| var secretClient = this.secretClientProvider.GetSecretClient(new Uri(vaultUrl)); | ||
| this.logger.LogInformation("Replacing setting property {PropertyName} with value from secret {SecretUrl}", property.Name, value); | ||
|
|
||
| var response = secretClient.GetSecret(secretName); | ||
| var secret = response.Value; | ||
|
|
||
| property.SetValue(options, secret.Value); | ||
| } | ||
| catch (Exception exception) | ||
| { | ||
| this.logger.LogError(exception, "Unable to read secret {SecretName} from vault {VaultUrl}", secretName, vaultUrl); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
tools/pipeline-witness/Azure.Sdk.Tools.PipelineWitness/Configuration/SecretClientProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using System; | ||
| using Azure.Core; | ||
| using Azure.Security.KeyVault.Secrets; | ||
|
|
||
| namespace Azure.Sdk.Tools.PipelineWitness.Configuration | ||
| { | ||
| public class SecretClientProvider : ISecretClientProvider | ||
| { | ||
| private readonly TokenCredential tokenCredential; | ||
|
|
||
| public SecretClientProvider(TokenCredential tokenCredential) | ||
| { | ||
| this.tokenCredential = tokenCredential; | ||
| } | ||
|
|
||
| public SecretClient GetSecretClient(Uri vaultUri) | ||
| { | ||
| return new SecretClient(vaultUri, this.tokenCredential); | ||
| } | ||
| } | ||
| } |
101 changes: 101 additions & 0 deletions
101
tools/pipeline-witness/Azure.Sdk.Tools.PipelineWitness/Controllers/GitHubEventsController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using Azure.Sdk.Tools.PipelineWitness.Configuration; | ||
| using Azure.Sdk.Tools.PipelineWitness.GitHubActions; | ||
| using Azure.Storage.Queues; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 | ||
|
|
||
| namespace Azure.Sdk.Tools.PipelineWitness.Controllers | ||
| { | ||
| [Route("api/githubevents")] | ||
| [ApiController] | ||
| public class GitHubEventsController : ControllerBase | ||
| { | ||
| private readonly QueueClient queueClient; | ||
| private readonly ILogger<GitHubEventsController> logger; | ||
| private readonly PipelineWitnessSettings settings; | ||
|
|
||
| public GitHubEventsController(ILogger<GitHubEventsController> logger, QueueServiceClient queueServiceClient, IOptions<PipelineWitnessSettings> options) | ||
| { | ||
| this.logger = logger; | ||
| this.settings = options.Value; | ||
| this.queueClient = queueServiceClient.GetQueueClient(this.settings.GitHubActionRunsQueueName); | ||
| } | ||
|
|
||
| // POST api/githubevents | ||
| [HttpPost] | ||
| public async Task<IActionResult> PostAsync() | ||
| { | ||
| var eventName = Request.Headers["X-GitHub-Event"].FirstOrDefault(); | ||
| switch (eventName) | ||
| { | ||
| case "ping": | ||
| return Ok(); | ||
| case "workflow_run": | ||
| return await ProcessWorkflowRunEventAsync(); | ||
| default: | ||
| this.logger.LogWarning("Received GitHub event {EventName} which is not supported", eventName); | ||
| return BadRequest(); | ||
| } | ||
| } | ||
|
|
||
| private static bool VerifySignature(string text, string key, string signature) | ||
| { | ||
| Encoding encoding = Encoding.UTF8; | ||
|
|
||
| byte[] textBytes = encoding.GetBytes(text); | ||
| byte[] keyBytes = encoding.GetBytes(key); | ||
|
|
||
| using HMACSHA256 hasher = new(keyBytes); | ||
| byte[] hashBytes = hasher.ComputeHash(textBytes); | ||
|
|
||
| var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); | ||
| var expectedSignature = $"sha256={hash}"; | ||
| return signature == expectedSignature; | ||
| } | ||
|
|
||
| private async Task<IActionResult> ProcessWorkflowRunEventAsync() | ||
| { | ||
| using var reader = new StreamReader(Request.Body); | ||
| var body = await reader.ReadToEndAsync(); | ||
| var signature = Request.Headers["X-Hub-Signature-256"].FirstOrDefault(); | ||
|
|
||
| if (!VerifySignature(body, this.settings.GitHubWebhookSecret, signature)) | ||
| { | ||
| this.logger.LogWarning("Received GitHub event {Event} with invalid signature", "workflow_run"); | ||
| return Unauthorized(); | ||
| } | ||
|
|
||
| var eventMessage = JsonDocument.Parse(body).RootElement; | ||
|
|
||
| string action = eventMessage.GetProperty("action").GetString(); | ||
|
|
||
| this.logger.LogInformation("Received GitHub event {Event}.{Action}", "workflow_run", action); | ||
|
|
||
| if (action == "completed") | ||
| { | ||
| var queueMessage = new GitHubRunCompleteMessage | ||
| { | ||
| Owner = eventMessage.GetProperty("repository").GetProperty("owner").GetProperty("login").GetString(), | ||
| Repository = eventMessage.GetProperty("repository").GetProperty("name").GetString(), | ||
| RunId = eventMessage.GetProperty("workflow_run").GetProperty("id").GetInt64(), | ||
| }; | ||
|
|
||
| this.logger.LogInformation("Enqueuing GitHubRunCompleteMessage for {Owner}/{Repository} run {RunId}", queueMessage.Owner, queueMessage.Repository, queueMessage.RunId); | ||
|
|
||
| await this.queueClient.SendMessageAsync(JsonSerializer.Serialize(queueMessage)); | ||
| } | ||
|
|
||
| return Ok(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.