<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.DirectoryServices.AccountManagement;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Mindscape.Raygun4Net;
using Newtonsoft.Json;
using SkillsWorkflow.Services.ADBlocker.Models;
using SkillsWorkflow.Services.ADBlocker.Utils;

namespace SkillsWorkflow.Services.ADBlocker
{
    internal class Program
    {
        private static WebRequestHandler _handler;
        private static HttpClient _client;
        private static readonly RaygunClient RaygunClient = new RaygunClient(ConfigurationManager.AppSettings["Raygun:AppKey"]);

        private static void Main(string[] args)
        {
            InitializeRaygunClient();
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            RunTaskAsync().Wait();
        }

        private static void InitializeRaygunClient()
        {
            var raygunJobName = ConfigurationManager.AppSettings["Raygun:JobName"];
            var raygunEnvironment = ConfigurationManager.AppSettings["Skills:Environment"];
            var raygunTags = new List<string> {raygunJobName, raygunEnvironment}.AsReadOnly();
            RaygunClient.SendingMessage += (sender, eventArgs) => { eventArgs.Message.Details.Tags = raygunTags; };
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            RaygunClient.Send(e.ExceptionObject as Exception);
        }

        private static async Task RunTaskAsync()
        {
            Trace.WriteLine("Start task", "ADBlocker");
            Trace.WriteLine($"Start Time: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");
            InitializeHttpClient();
            try
            {
                await RunBlockingTaskAsync();
                await RunScheduledTaskAsync();
                await UpdateTaskRuntimeAsync();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("ERROR", "ADBlocker");
                ex.TraceError();
                RaygunClient.Send(ex);
            }
            finally
            {
                Trace.WriteLine($"End Time: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");
                Trace.WriteLine("End Task", "ADBlocker");
                Trace.WriteLine("");
                Trace.Close();
                _client?.Dispose();
                _handler?.Dispose();
            }
        }

        private static void InitializeHttpClient()
        {
            _handler = new WebRequestHandler();
            if(!ConfigurationManager.AppSettings["Skills:Environment"].ToLower().Equals("local"))
                _handler.ServerCertificateValidationCallback = PinPublicKey;
            _client = new HttpClient(_handler)
            {
                BaseAddress = new Uri(ConfigurationManager.AppSettings["Skills:ApiUrl"])
            };
            _client.DefaultRequestHeaders.Add("X-AppId", ConfigurationManager.AppSettings["Skills:AppId"]);
            _client.DefaultRequestHeaders.Add("X-AppSecret", ConfigurationManager.AppSettings["Skills:AppSecret"]);
            _client.DefaultRequestHeaders.Accept.Clear();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        private static async Task UpdateTaskRuntimeAsync()
        {
            var response = await _client.PostAsync("api/blockedloginrequests/taskruntime", new StringContent(""));
            response.EnsureSuccessStatusCode();
        }

        private static async Task RunBlockingTaskAsync()
        {
            Trace.WriteLine($"Started running blocking task: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");
            var response = await _client.GetAsync("api/blockedloginrequests/userstoblock");
            response.EnsureSuccessStatusCode();
            var responseContent = await response.Content.ReadAsStringAsync();
            var usersToBlock = JsonConvert.DeserializeObject<List<User>>(responseContent);
            foreach (var user in usersToBlock)
                await BlockUserAsync(user);
            Trace.WriteLine($"Ended running blocking task: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");
        }

        private static async Task RunScheduledTaskAsync()
        {
            Trace.WriteLine($"Started running scheduled task: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");

            await ProcessBlockedLoginRequestsAsync();
            await ProcessUnblockUserRequestsAsync();

            Trace.WriteLine($"Ended running scheduled task: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");
        }

        private static async Task ProcessBlockedLoginRequestsAsync()
        {
            var response = await _client.GetAsync("api/blockedloginrequests");
            response.EnsureSuccessStatusCode();
            var resp = await response.Content.ReadAsStringAsync();
            var blockedLoginRequests = JsonConvert.DeserializeObject<List<BlockedLoginRequest>>(resp);

            foreach (var blockedLoginRequest in blockedLoginRequests)
                await UpdateBlockedLoginRequestAsync(ValidateLoginRequest(blockedLoginRequest));
        }

        private static async Task ProcessUnblockUserRequestsAsync()
        {
            var response = await _client.GetAsync("api/unblockuserrequests");
            response.EnsureSuccessStatusCode();
            var responseContent = await response.Content.ReadAsStringAsync();
            var unblockUserRequests = JsonConvert.DeserializeObject<List<UnblockUserRequest>>(responseContent);
            foreach (var unblockUserRequest in unblockUserRequests)
                await ProcessUnblockUserRequestAsync(unblockUserRequest);
        }

        private static async Task ProcessUnblockUserRequestAsync(UnblockUserRequest unblockUserRequest)
        {
            UnblockUserRequestResult result;
            using (var context = CreatePrincipalContext())
            {
                using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(context, unblockUserRequest.AdUserName))
                {
                    if (userPrincipal == null)
                        result = new UnblockUserRequestResult { Id = unblockUserRequest.Id, RequestResult = false, RequestResultMessage = "AD User not found." };
                    else
                    {
                        userPrincipal.AccountExpirationDate = unblockUserRequest.AccountExpirationDate;
                        userPrincipal.Save();
                        result = new UnblockUserRequestResult { Id = unblockUserRequest.Id, RequestResult = true, RequestResultMessage = "" };
                    }
                }
            }
            HttpContent putContent = new StringContent(JsonConvert.SerializeObject(result), Encoding.UTF8, "application/json");
            var response = await _client.PutAsync("api/unblockuserrequests", putContent);
            response.EnsureSuccessStatusCode();
        }
<commit_msg>Added trace to unblock user execution<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.DirectoryServices.AccountManagement;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Mindscape.Raygun4Net;
using Newtonsoft.Json;
using SkillsWorkflow.Services.ADBlocker.Models;
using SkillsWorkflow.Services.ADBlocker.Utils;

namespace SkillsWorkflow.Services.ADBlocker
{
    internal class Program
    {
        private static WebRequestHandler _handler;
        private static HttpClient _client;
        private static readonly RaygunClient RaygunClient = new RaygunClient(ConfigurationManager.AppSettings["Raygun:AppKey"]);

        private static void Main(string[] args)
        {
            InitializeRaygunClient();
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            RunTaskAsync().Wait();
        }

        private static void InitializeRaygunClient()
        {
            var raygunJobName = ConfigurationManager.AppSettings["Raygun:JobName"];
            var raygunEnvironment = ConfigurationManager.AppSettings["Skills:Environment"];
            var raygunTags = new List<string> {raygunJobName, raygunEnvironment}.AsReadOnly();
            RaygunClient.SendingMessage += (sender, eventArgs) => { eventArgs.Message.Details.Tags = raygunTags; };
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            RaygunClient.Send(e.ExceptionObject as Exception);
        }

        private static async Task RunTaskAsync()
        {
            Trace.WriteLine("Start task", "ADBlocker");
            Trace.WriteLine($"Start Time: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");
            InitializeHttpClient();
            try
            {
                await RunBlockingTaskAsync();
                await RunScheduledTaskAsync();
                await UpdateTaskRuntimeAsync();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("ERROR", "ADBlocker");
                ex.TraceError();
                RaygunClient.Send(ex);
            }
            finally
            {
                Trace.WriteLine($"End Time: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");
                Trace.WriteLine("End Task", "ADBlocker");
                Trace.WriteLine("");
                Trace.Close();
                _client?.Dispose();
                _handler?.Dispose();
            }
        }

        private static void InitializeHttpClient()
        {
            _handler = new WebRequestHandler();
            if(!ConfigurationManager.AppSettings["Skills:Environment"].ToLower().Equals("local"))
                _handler.ServerCertificateValidationCallback = PinPublicKey;
            _client = new HttpClient(_handler)
            {
                BaseAddress = new Uri(ConfigurationManager.AppSettings["Skills:ApiUrl"])
            };
            _client.DefaultRequestHeaders.Add("X-AppId", ConfigurationManager.AppSettings["Skills:AppId"]);
            _client.DefaultRequestHeaders.Add("X-AppSecret", ConfigurationManager.AppSettings["Skills:AppSecret"]);
            _client.DefaultRequestHeaders.Accept.Clear();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        private static async Task UpdateTaskRuntimeAsync()
        {
            var response = await _client.PostAsync("api/blockedloginrequests/taskruntime", new StringContent(""));
            response.EnsureSuccessStatusCode();
        }

        private static async Task RunBlockingTaskAsync()
        {
            Trace.WriteLine($"Started running blocking task: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");
            var response = await _client.GetAsync("api/blockedloginrequests/userstoblock");
            response.EnsureSuccessStatusCode();
            var responseContent = await response.Content.ReadAsStringAsync();
            var usersToBlock = JsonConvert.DeserializeObject<List<User>>(responseContent);
            foreach (var user in usersToBlock)
                await BlockUserAsync(user);
            Trace.WriteLine($"Ended running blocking task: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");
        }

        private static async Task RunScheduledTaskAsync()
        {
            Trace.WriteLine($"Started running scheduled task: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");

            await ProcessBlockedLoginRequestsAsync();
            await ProcessUnblockUserRequestsAsync();

            Trace.WriteLine($"Ended running scheduled task: {DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss.fff")}", "ADBlocker");
        }

        private static async Task ProcessBlockedLoginRequestsAsync()
        {
            var response = await _client.GetAsync("api/blockedloginrequests");
            response.EnsureSuccessStatusCode();
            var resp = await response.Content.ReadAsStringAsync();
            var blockedLoginRequests = JsonConvert.DeserializeObject<List<BlockedLoginRequest>>(resp);

            foreach (var blockedLoginRequest in blockedLoginRequests)
                await UpdateBlockedLoginRequestAsync(ValidateLoginRequest(blockedLoginRequest));
        }

        private static async Task ProcessUnblockUserRequestsAsync()
        {
            var response = await _client.GetAsync("api/unblockuserrequests");
            response.EnsureSuccessStatusCode();
            var responseContent = await response.Content.ReadAsStringAsync();
            var unblockUserRequests = JsonConvert.DeserializeObject<List<UnblockUserRequest>>(responseContent);
            foreach (var unblockUserRequest in unblockUserRequests)
                await ProcessUnblockUserRequestAsync(unblockUserRequest);
        }

        private static async Task ProcessUnblockUserRequestAsync(UnblockUserRequest unblockUserRequest)
        {
            UnblockUserRequestResult result;
            using (var context = CreatePrincipalContext())
            {
                using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(context, unblockUserRequest.AdUserName))
                {
                    if (userPrincipal == null)
                        result = new UnblockUserRequestResult { Id = unblockUserRequest.Id, RequestResult = false, RequestResultMessage = "AD User not found." };
                    else
                    {
                        userPrincipal.AccountExpirationDate = unblockUserRequest.AccountExpirationDate;
                        userPrincipal.Save();
                        result = new UnblockUserRequestResult { Id = unblockUserRequest.Id, RequestResult = true, RequestResultMessage = "" };
                        Trace.WriteLine($"Unblocked User {unblockUserRequest.AdUserName}", "ADBlocker");
                    }
                }
            }
            HttpContent putContent = new StringContent(JsonConvert.SerializeObject(result), Encoding.UTF8, "application/json");
            var response = await _client.PutAsync("api/unblockuserrequests", putContent);
            response.EnsureSuccessStatusCode();
        }

{'fraction_non_alphanumeric': 0.09278628787367568, 'fraction_numerical': 0.000674809366354005, 'mean_word_length': 2.547151747247487, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2391558', 'n_tokens_mistral': 3606, 'n_tokens_neox': 3536, 'n_words': 671}