Пример #1
0
        private static async Task ExecuteUsingTimelineLogs(
            WebJobsExecutionContext executionContext,
            ILogger log,
            string imageProvenance,
            string policy,
            Guid checkSuiteId,
            TaskProperties taskProperties,
            IDictionary <string, string> variables)
        {
            using (var taskClient = new TaskClient(taskProperties))
            {
                var taskLogger = new TaskLogger(taskProperties, taskClient);
                try
                {
                    // create timelinerecord if not provided
                    await taskLogger.CreateTaskTimelineRecordIfRequired(taskClient, default(CancellationToken)).ConfigureAwait(false);

                    // report task started
                    string taskStartedLog = string.Format("Initializing evaluation. Execution id - {0}", executionContext.InvocationId);
                    CommonUtilities.LogInformation(taskStartedLog, log, taskLogger, variables, null);

                    string outputLog;
                    var    violations = CommonUtilities.ExecutePolicyCheck(
                        executionContext,
                        log,
                        imageProvenance,
                        policy,
                        taskLogger,
                        variables,
                        null,
                        out ViolationType violationType,
                        out outputLog);

                    bool succeeded = !(violations?.Any() == true);
                    CommonUtilities.LogInformation($"Policy check succeeded: {succeeded}", log, taskLogger, variables, null);

                    var telemetryProperties = new Dictionary <string, object>();
                    telemetryProperties.Add("projectId", taskProperties.ProjectId);
                    telemetryProperties.Add("jobId", taskProperties.JobId);
                    telemetryProperties.Add("checkSuiteId", checkSuiteId);
                    telemetryProperties.Add("result", succeeded ? "succeeded" : "failed");
                    telemetryProperties.Add("layer", "Azure function");
                    telemetryProperties.Add(ArtifactPolicyTelemetryReasonKey, $"Found violations in evaluation. Violation type: {violationType}");

                    await UpdateCheckSuiteResult(
                        taskProperties.PlanUrl,
                        taskProperties.AuthToken,
                        taskProperties.ProjectId,
                        checkSuiteId,
                        succeeded,
                        outputLog,
                        log,
                        taskLogger,
                        variables);

                    await CustomerIntelligenceClient.GetClient(taskProperties.PlanUrl, taskProperties.AuthToken)
                    .PublishArtifactPolicyEventAsync(telemetryProperties).ConfigureAwait(false);

                    return;
                }
                catch (Exception e)
                {
                    if (taskLogger != null)
                    {
                        await taskLogger.Log(e.ToString()).ConfigureAwait(false);
                    }

                    throw;
                }
                finally
                {
                    if (taskLogger != null)
                    {
                        await taskLogger.End().ConfigureAwait(false);
                    }
                }
            }
        }