Ejemplo n.º 1
0
        private async Task <DumpArtifact> AddDumpArtifactToDbAsync(DumplingDb dumplingDb, string dumpId, string localPath, string hash, CancellationToken cancelToken)
        {
            using (var opTracker = new TrackedOperation("AddDumpArtifactToDbAsync"))
            {
                //if the specified dumpId is not valid throw an exception
                if (await dumplingDb.Dumps.FindAsync(cancelToken, dumpId) == null)
                {
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The specified dumpling id is invalid."));
                }

                var dumpArtifact = new DumpArtifact()
                {
                    DumpId          = dumpId,
                    LocalPath       = localPath,
                    DebugCritical   = true,
                    ExecutableImage = false,
                    Hash            = hash
                };

                dumplingDb.DumpArtifacts.AddOrUpdate(dumpArtifact);

                await dumplingDb.SaveChangesAsync(cancelToken);

                return(dumpArtifact);
            }
        }
Ejemplo n.º 2
0
        private async Task StoreArtifactContentAsync(HttpContent content, string hash, string dumpId, string localPath, CancellationToken cancelToken)
        {
            //if the specified hash is not formatted properly throw an exception
            if (!ValidateHashFormat(hash))
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The specified hash is improperly formatted"));
            }

            using (DumplingDb dumplingDb = new DumplingDb())
            {
                var artifact = await AddArtifactToDbAsync(dumplingDb, hash, localPath, cancelToken);

                //if the artifact didn't already exist and we added it to the db upload the file
                if (artifact != null)
                {
                    using (var uploaded = await UploadContentValidateHashAsync(content, hash, cancelToken))
                    {
                        artifact.CompressedSize = uploaded.Length;

                        uploaded.Position = 0;

                        artifact.Url = await DumplingStorageClient.StoreArtifactAsync(uploaded, hash, artifact.FileName + ".gz", cancelToken);

                        await dumplingDb.SaveChangesAsync(cancelToken);
                    }
                }

                //if a dumpId was specified add the dumpartifact entry
                if (dumpId != null)
                {
                    await AddDumpArtifactToDbAsync(dumplingDb, dumpId, localPath, hash, cancelToken);
                }
            }
        }
Ejemplo n.º 3
0
        protected virtual async Task <Artifact> CreateArtifactAsync()
        {
            FileName = Path.GetFileName(_path).ToLowerInvariant();

            CompressedSize = new FileInfo(_path).Length;

            var artifact = new Artifact
            {
                Hash           = ExpectedHash,
                FileName       = FileName,
                Format         = ArtifactFormat.Unknown,
                CompressedSize = CompressedSize,
                UploadTime     = DateTime.UtcNow
            };

            bool newArtifact = await _dumplingDb.TryAddAsync(artifact) == artifact;

            DumpArtifact dumpArtifact = null;


            if (DumpId != null)
            {
                dumpArtifact = new DumpArtifact()
                {
                    DumpId        = DumpId,
                    LocalPath     = LocalPath,
                    DebugCritical = DebugCritical
                };

                _dumplingDb.DumpArtifacts.AddOrUpdate(dumpArtifact);
            }

            await _dumplingDb.SaveChangesAsync();

            if (newArtifact)
            {
                //upload the artifact to blob storage
                await StoreArtifactBlobAsync(artifact);
            }

            return(newArtifact ? artifact : null);
        }
Ejemplo n.º 4
0
        public async Task <string> CreateDump([FromUri] string hash, [FromUri] string user, [FromUri] string displayName, CancellationToken cancelToken)
        {
            //if the specified hash is not formatted properly throw an exception
            if (!ValidateHashFormat(hash))
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The specified hash is improperly formatted"));
            }

            using (DumplingDb dumplingDb = new DumplingDb())
            {
                var dumpling = await dumplingDb.Dumps.FindAsync(cancelToken, hash);

                if (dumpling != null)
                {
                    return(hash);
                }

                dumpling = new Dump()
                {
                    DumpId = hash, User = user, DisplayName = displayName, DumpTime = DateTime.UtcNow, Os = OS.Unknown
                };

                dumplingDb.Dumps.Add(dumpling);

                try
                {
                    await dumplingDb.SaveChangesAsync();
                }
                catch (DbEntityValidationException)
                {
                    dumpling = await dumplingDb.Dumps.FindAsync(cancelToken, hash);

                    //if the specified dump was not found throw an exception
                    if (dumpling != null)
                    {
                        return(hash);
                    }

                    throw;
                }

                return(hash);
            }
        }
Ejemplo n.º 5
0
        public async Task <HttpResponseMessage> UpdateDumpProperties(string dumplingid, [FromBody] JToken properties, CancellationToken cancelToken)
        {
            using (DumplingDb dumplingDb = new DumplingDb())
            {
                var dumpling = await dumplingDb.Dumps.FindAsync(cancelToken, dumplingid);

                //if the specified dump was not found throw an exception
                if (dumpling == null)
                {
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The given dumplingId is invalid"));
                }

                var propDict = JsonConvert.DeserializeObject <Dictionary <string, string> >(properties.ToString());

                string failureHash = null;

                //if the properties contain FAILURE_HASH get the failure
                if (propDict.TryGetValue("FAILURE_HASH", out failureHash))
                {
                    //if the failure is not in the db yet try to add it
                    if (await dumplingDb.Failures.FindAsync(cancelToken, failureHash) == null)
                    {
                        try
                        {
                            dumplingDb.Failures.Add(new Failure()
                            {
                                FailureHash = failureHash
                            });

                            await dumplingDb.SaveChangesAsync();
                        }
                        //swallow the validation exception if the failure was inserted by another request since we checked
                        catch (DbEntityValidationException e)
                        {
                        }
                    }

                    dumpling.FailureHash = failureHash;
                }

                //update any properties which were pre-existing with the new value
                foreach (var existingProp in dumpling.Properties)
                {
                    string val = null;

                    if (propDict.TryGetValue(existingProp.Name, out val))
                    {
                        existingProp.Value = val;

                        propDict.Remove(existingProp.Name);
                    }
                }

                //add any properties which were not previously existsing
                //(the existing keys have been removed in previous loop)
                foreach (var newProp in propDict)
                {
                    dumpling.Properties.Add(new Property()
                    {
                        Name = newProp.Key, Value = newProp.Value
                    });
                }

                await dumplingDb.SaveChangesAsync();

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
        }