|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Microsoft.AspNetCore.Authorization; |
| 3 | +using Microsoft.Extensions.Configuration; |
| 4 | +using Google.Protobuf; |
| 5 | +using System; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNetCore.Mvc.ModelBinding; |
| 8 | +using Microsoft.AspNetCore.Http; |
| 9 | +using System.IO; |
| 10 | +using System.Reflection; |
| 11 | +using Google.Protobuf.Reflection; |
| 12 | +using Microsoft.AspNetCore.Http.Extensions; |
| 13 | +using Runner.Server.Models; |
| 14 | +using System.Linq; |
| 15 | +using Microsoft.EntityFrameworkCore; |
| 16 | +using GitHub.Actions.Pipelines.WebApi; |
| 17 | +using System.Security.Cryptography; |
| 18 | +using Microsoft.IdentityModel.Tokens; |
| 19 | +using System.Text; |
| 20 | + |
| 21 | +namespace Runner.Server.Controllers |
| 22 | +{ |
| 23 | + |
| 24 | + [ApiController] |
| 25 | + [Route("twirp/github.actions.results.api.v1.ArtifactService")] |
| 26 | + [Authorize(AuthenticationSchemes = "Bearer", Policy = "AgentJob")] |
| 27 | + public class ArtifactControllerV2 : VssControllerBase{ |
| 28 | + private readonly SqLiteDb _context; |
| 29 | + private readonly JsonFormatter formatter; |
| 30 | + |
| 31 | + public ArtifactControllerV2(SqLiteDb _context, IConfiguration configuration) : base(configuration) |
| 32 | + { |
| 33 | + this._context = _context; |
| 34 | + formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithIndentation().WithPreserveProtoFieldNames(true).WithFormatDefaultValues(false)); |
| 35 | + } |
| 36 | + |
| 37 | + private string CreateSignature(int id) { |
| 38 | + using(var rsa = RSA.Create(Startup.AccessTokenParameter)) |
| 39 | + return Base64UrlEncoder.Encode(rsa.SignData(Encoding.UTF8.GetBytes(id.ToString()), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); |
| 40 | + } |
| 41 | + |
| 42 | + private bool VerifySignature(int id, string sig) { |
| 43 | + using(var rsa = RSA.Create(Startup.AccessTokenParameter)) |
| 44 | + return rsa.VerifyData(Encoding.UTF8.GetBytes(id.ToString()), Base64UrlEncoder.DecodeBytes(sig), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); |
| 45 | + } |
| 46 | + |
| 47 | + [HttpPost("CreateArtifact")] |
| 48 | + public async Task<string> CreateArtifact([FromBody, Protobuf] Github.Actions.Results.Api.V1.CreateArtifactRequest body) { |
| 49 | + var guid = Guid.Parse(body.WorkflowJobRunBackendId); |
| 50 | + var jobInfo = (from j in _context.Jobs where j.JobId == guid select new { j.runid, j.WorkflowRunAttempt.Attempt }).FirstOrDefault(); |
| 51 | + var artifacts = new ArtifactController(_context, Configuration); |
| 52 | + var fname = $"{body.Name}.zip"; |
| 53 | + var container = await artifacts.CreateContainer(jobInfo.runid, jobInfo.Attempt, new CreateActionsStorageArtifactParameters() { Name = body.Name }, jobInfo.Attempt); |
| 54 | + if(_context.Entry(container).Collection(c => c.Files).Query().Any()) { |
| 55 | + //var files = _context.Entry(container).Collection(c => c.Files).Query().ToList(); |
| 56 | + // Duplicated Artifact of the same name in the same Attempt => fail |
| 57 | + return formatter.Format(new Github.Actions.Results.Api.V1.CreateArtifactResponse() { |
| 58 | + Ok = false |
| 59 | + }); |
| 60 | + } |
| 61 | + var record = new ArtifactRecord() {FileName = fname, StoreName = Path.GetRandomFileName(), GZip = false, FileContainer = container} ; |
| 62 | + _context.ArtifactRecords.Add(record); |
| 63 | + await _context.SaveChangesAsync(); |
| 64 | + |
| 65 | + var resp = new Github.Actions.Results.Api.V1.CreateArtifactResponse |
| 66 | + { |
| 67 | + Ok = true, |
| 68 | + SignedUploadUrl = new Uri(new Uri(ServerUrl), $"twirp/github.actions.results.api.v1.ArtifactService/UploadArtifact?id={record.Id}&sig={CreateSignature(record.Id)}").ToString() |
| 69 | + }; |
| 70 | + return formatter.Format(resp); |
| 71 | + } |
| 72 | + |
| 73 | + [HttpPut("UploadArtifact")] |
| 74 | + [AllowAnonymous] |
| 75 | + public async Task<IActionResult> UploadArtifact(int id, string sig, string comp = null, bool seal = false) { |
| 76 | + if(string.IsNullOrEmpty(sig) || !VerifySignature(id, sig)) { |
| 77 | + return NotFound(); |
| 78 | + } |
| 79 | + if(comp == "block" || comp == "appendBlock") { |
| 80 | + var record = await _context.ArtifactRecords.FindAsync(id); |
| 81 | + var _targetFilePath = Path.Combine(GitHub.Runner.Sdk.GharunUtil.GetLocalStorage(), "artifacts"); |
| 82 | + using(var targetStream = new FileStream(Path.Combine(_targetFilePath, record.StoreName), FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write, FileShare.Write)) { |
| 83 | + await Request.Body.CopyToAsync(targetStream); |
| 84 | + } |
| 85 | + return Created(HttpContext.Request.GetEncodedUrl(), null); |
| 86 | + } |
| 87 | + if(comp == "blocklist") { |
| 88 | + return Created(HttpContext.Request.GetEncodedUrl(), null); |
| 89 | + } |
| 90 | + return Ok(); |
| 91 | + } |
| 92 | + |
| 93 | + [HttpPost("FinalizeArtifact")] |
| 94 | + public string FinalizeArtifact([FromBody, Protobuf] Github.Actions.Results.Api.V1.FinalizeArtifactRequest body) { |
| 95 | + var attempt = long.Parse(User.FindFirst("attempt")?.Value ?? "-1"); |
| 96 | + var artifactsMinAttempt = long.Parse(User.FindFirst("artifactsMinAttempt")?.Value ?? "-1"); |
| 97 | + var runid = long.Parse(body.WorkflowRunBackendId); |
| 98 | + |
| 99 | + var container = (from fileContainer in _context.ArtifactFileContainer where (fileContainer.Container.Attempt.Attempt >= artifactsMinAttempt || artifactsMinAttempt == -1) && (fileContainer.Container.Attempt.Attempt <= attempt || attempt == -1) && fileContainer.Container.Attempt.WorkflowRun.Id == runid && fileContainer.Files.Count == 1 && !fileContainer.Files.FirstOrDefault().FileName.Contains('/') && fileContainer.Files.FirstOrDefault().FileName.EndsWith(".zip") && body.Name.ToLower() == fileContainer.Name.ToLower() orderby fileContainer.Container.Attempt.Attempt descending select fileContainer).First(); |
| 100 | + container.Size = body.Size; |
| 101 | + var resp = new Github.Actions.Results.Api.V1.FinalizeArtifactResponse |
| 102 | + { |
| 103 | + Ok = true, |
| 104 | + ArtifactId = container.Id |
| 105 | + }; |
| 106 | + return formatter.Format(resp); |
| 107 | + } |
| 108 | + |
| 109 | + [HttpPost("ListArtifacts")] |
| 110 | + public string ListArtifacts([FromBody, Protobuf] Github.Actions.Results.Api.V1.ListArtifactsRequest body) { |
| 111 | + var resp = new Github.Actions.Results.Api.V1.ListArtifactsResponse(); |
| 112 | + |
| 113 | + var attempt = long.Parse(User.FindFirst("attempt")?.Value ?? "-1"); |
| 114 | + var artifactsMinAttempt = long.Parse(User.FindFirst("artifactsMinAttempt")?.Value ?? "-1"); |
| 115 | + var runid = long.Parse(body.WorkflowRunBackendId); |
| 116 | + resp.Artifacts.AddRange(from fileContainer in _context.ArtifactFileContainer where (fileContainer.Container.Attempt.Attempt >= artifactsMinAttempt || artifactsMinAttempt == -1) && (fileContainer.Container.Attempt.Attempt <= attempt || attempt == -1) && fileContainer.Container.Attempt.WorkflowRun.Id == runid && fileContainer.Files.Count == 1 && !fileContainer.Files.FirstOrDefault().FileName.Contains('/') && fileContainer.Files.FirstOrDefault().FileName.EndsWith(".zip") && (body.IdFilter == null || body.IdFilter == fileContainer.Id) && (body.NameFilter == null || body.NameFilter.ToLower() == fileContainer.Name.ToLower()) orderby fileContainer.Container.Attempt.Attempt descending select new Github.Actions.Results.Api.V1.ListArtifactsResponse_MonolithArtifact |
| 117 | + { |
| 118 | + CreatedAt = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTimeOffset(System.DateTimeOffset.UtcNow), |
| 119 | + DatabaseId = fileContainer.Id, |
| 120 | + Name = fileContainer.Name, |
| 121 | + Size = fileContainer.Size ?? 0, |
| 122 | + WorkflowRunBackendId = body.WorkflowRunBackendId, |
| 123 | + WorkflowJobRunBackendId = body.WorkflowJobRunBackendId |
| 124 | + }); |
| 125 | + return formatter.Format(resp); |
| 126 | + } |
| 127 | + |
| 128 | + [HttpPost("GetSignedArtifactURL")] |
| 129 | + public async Task<string> GetSignedArtifactURL([FromBody, Protobuf] Github.Actions.Results.Api.V1.GetSignedArtifactURLRequest body) { |
| 130 | + var attempt = long.Parse(User.FindFirst("attempt")?.Value ?? "-1"); |
| 131 | + var artifactsMinAttempt = long.Parse(User.FindFirst("artifactsMinAttempt")?.Value ?? "-1"); |
| 132 | + var runid = long.Parse(body.WorkflowRunBackendId); |
| 133 | + var file = await (from fileContainer in _context.ArtifactFileContainer where (fileContainer.Container.Attempt.Attempt >= artifactsMinAttempt || artifactsMinAttempt == -1) && (fileContainer.Container.Attempt.Attempt <= attempt || attempt == -1) && fileContainer.Container.Attempt.WorkflowRun.Id == runid && fileContainer.Files.Count == 1 && fileContainer.Files.FirstOrDefault().FileName.ToLower() == $"{body.Name}.zip".ToLower() orderby fileContainer.Container.Attempt.Attempt descending select fileContainer.Files.FirstOrDefault()).FirstAsync(); |
| 134 | + var resp = new Github.Actions.Results.Api.V1.GetSignedArtifactURLResponse |
| 135 | + { |
| 136 | + SignedUrl = new Uri(new Uri(ServerUrl), $"twirp/github.actions.results.api.v1.ArtifactService/DownloadArtifact?id={file.Id}&sig={CreateSignature(file.Id)}").ToString() |
| 137 | + }; |
| 138 | + return formatter.Format(resp); |
| 139 | + } |
| 140 | + |
| 141 | + [AllowAnonymous] |
| 142 | + [HttpGet("DownloadArtifact")] |
| 143 | + public IActionResult DownloadArtifact(int id, string sig) { |
| 144 | + if(string.IsNullOrEmpty(sig) || !VerifySignature(id, sig)) { |
| 145 | + return NotFound(); |
| 146 | + } |
| 147 | + var container = _context.ArtifactRecords.Find(id); |
| 148 | + var _targetFilePath = Path.Combine(GitHub.Runner.Sdk.GharunUtil.GetLocalStorage(), "artifacts"); |
| 149 | + return new FileStreamResult(System.IO.File.OpenRead(Path.Combine(_targetFilePath, container.StoreName)), "application/octet-stream") { EnableRangeProcessing = true }; |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + public class ProtobufBinder : IModelBinder |
| 154 | + { |
| 155 | + public async Task BindModelAsync(ModelBindingContext bindingContext) |
| 156 | + { |
| 157 | + if (!bindingContext.HttpContext.Request.HasJsonContentType()) |
| 158 | + { |
| 159 | + throw new BadHttpRequestException( |
| 160 | + "Request content type was not a recognized JSON content type.", |
| 161 | + StatusCodes.Status415UnsupportedMediaType); |
| 162 | + } |
| 163 | + |
| 164 | + using var sr = new StreamReader(bindingContext.HttpContext.Request.Body); |
| 165 | + var str = await sr.ReadToEndAsync(); |
| 166 | + |
| 167 | + var valueType = bindingContext.ModelType; |
| 168 | + var parser = new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true)); |
| 169 | + |
| 170 | + var descriptor = (MessageDescriptor)bindingContext.ModelType.GetProperty("Descriptor", BindingFlags.Public | BindingFlags.Static).GetValue(null, null); |
| 171 | + var obj = parser.Parse(str, descriptor); |
| 172 | + |
| 173 | + bindingContext.Result = ModelBindingResult.Success(obj); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + public class ProtobufAttribute : ModelBinderAttribute { |
| 178 | + public ProtobufAttribute() : base(typeof(ProtobufBinder)) { |
| 179 | + |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments