public async Task <IHttpActionResult> Post(string name, string serverName) { try { var getAppTask = applicationRepository.GetApplicationAsync(name); var getDeploymentTask = deploymentRepository.GetActiveDeploymentsAsync(name); await Task.WhenAll(getAppTask, getDeploymentTask); var application = getAppTask.Result; var activeDeployments = getDeploymentTask.Result; if (application == null) { return(BadRequest("Unknown application name: `" + name + "`.")); } if (activeDeployments == null || activeDeployments.Count == 0 || activeDeployments.All(d => d.ServerName != serverName)) { return(BadRequest("No deployment found for application `" + name + "` with server `" + serverName + "`.")); } var activeDeployment = activeDeployments.FirstOrDefault(d => d.ServerName == serverName); var deploymentAudit = await applicationHashingService.HashDeployment(activeDeployment, application.GetRegularExpressions(), application.HashHiddenFiles, false); await auditRepository.CreateAuditAsync(deploymentAudit); return(Ok(deploymentAudit)); } catch (Exception ex) { Log.Error("Error in audit controller:", ex); return(InternalServerError()); } }
public async Task <IHttpActionResult> Post(string name, string serverName, [FromBody] NewDeployment payload) { try { if (string.IsNullOrWhiteSpace(payload?.NetworkPath)) { return(BadRequest("You must include the field `NetworkPath` in the body.")); } var application = await applicationRepository.GetApplicationAsync(name); if (application == null) { return(BadRequest("The application " + name + " does not exist.")); } if (!Directory.Exists(payload.NetworkPath)) { return(BadRequest("The path `" + payload.NetworkPath + "` is invalid or inaccessible.")); } var deployment = new Deployment { ApplicationName = name, ServerName = serverName, NetworkPath = payload.NetworkPath }; var deploymentAudit = await applicationHashingService.HashDeployment(deployment, application.GetRegularExpressions(), application.HashHiddenFiles, true); await deploymentRepository.InsertDeploymentAsync(deployment); await auditRepository.CreateAuditAsync(deploymentAudit); deployment.FileHashes = null; return(Ok(deployment)); } catch (Exception ex) { Log.Error("Error in deployment controller:", ex); return(InternalServerError()); } }
private async Task AuditHashApplication(Application application, bool sendAuditEmail = true) { var activeDeployments = await deploymentRepository.GetActiveDeploymentsAsync(application.Name); var failedAudits = new Dictionary <Deployment, DeploymentAudit>(); foreach (var deployment in activeDeployments) { var audit = await hashingService.HashDeployment(deployment, application.GetRegularExpressions(), application.HashHiddenFiles, false); await auditRepository.CreateAuditAsync(audit); if (!audit.ValidHash) { failedAudits.Add(deployment, audit); } } if (failedAudits.Count > 0) { var sb = new StringBuilder(); sb.AppendFormat("The application {0} has a failed audit on the following servers: \r\n", application.Name); foreach (var failedAudit in failedAudits) { sb.AppendFormat("Audit UTC:{0}, Server {1}:\r\n", failedAudit.Value.AuditDateTime.ToLongTimeString(), failedAudit.Key.ServerName); foreach (var fileHashMismatch in failedAudit.Value.FileHashMismatches) { sb.AppendFormat("Deployment Hash:{0}, Audit Hash:{1} \r\n", fileHashMismatch.OriginalHash, fileHashMismatch.AuditHash); } } Log.Error(sb.ToString()); await mailService.SendAuditEmail("Audit failure for " + application.Name, sb.ToString()); } else { Log.InfoFormat("All audits passed for application {0}.", application.Name); } }