public void ConfigureChannel(string hostName, string username, string password, int retryCount, string queueName) { _logger.LogInformation("RabbitMQ is trying to connect"); var policy = PolicyHelper.CreateRabbitMqPolicy(_logger, retryCount); policy.Execute(() => { _connectionFactory = new ConnectionFactory() { HostName = hostName , UserName = username , Password = password }; _connection = _connectionFactory.CreateConnection(); _channel = _connection.CreateModel(); _channel.QueueDeclare(queueName , false , false , false , null); _consumer = new EventingBasicConsumer(_channel); _consumer.Received += OnConsumerOnReceived; _channel.BasicConsume(queueName, true, _consumer); _logger.LogInformation("RabbitMQ Client is ready to receive messages"); }); }
public void GetPoliciesTest_AddDynamicStoredProcParamPolicies() { EnterpriseConfiguration cfg = new EnterpriseConfiguration(); ScriptPolicyArgument arg = new ScriptPolicyArgument(); arg.Name = "Schema"; arg.Value = "dbo"; ScriptPolicy pol1 = new ScriptPolicy(); pol1.PolicyId = PolicyIdKey.StoredProcParameterPolicy; pol1.Enforce = true; pol1.Argument = new ScriptPolicyArgument[] { arg }; ScriptPolicy pol2 = new ScriptPolicy(); pol2.PolicyId = PolicyIdKey.StoredProcParameterPolicy; pol2.Enforce = true; pol2.Argument = new ScriptPolicyArgument[] { arg }; cfg.ScriptPolicy = new ScriptPolicy[] { pol1, pol2 }; EnterpriseConfigHelper.EnterpriseConfig = cfg; List <IScriptPolicy> actual; actual = PolicyHelper.GetPolicies(); Assert.IsTrue(actual.Count == 2, "Expected 2 policies but got " + actual.Count.ToString()); Assert.IsTrue(actual[0] is StoredProcParameterPolicy); Assert.IsTrue(actual[1] is StoredProcParameterPolicy); }
/// <summary> /// Method validating the model /// </summary> /// <param name="validationContext">The context to validate for</param> /// <returns></returns> public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { List <ValidationResult> validationResult = new List <ValidationResult>(); if (this.Count < 1) { validationResult.Add(new ValidationResult("Missing rulesToDelete in body")); return(validationResult); } if (this.Any(r => r.RuleIds == null || r.RuleIds.Count == 0)) { validationResult.Add(new ValidationResult("Not all RequestToDelete has RuleId")); return(validationResult); } try { if (this.GroupBy(x => PolicyHelper.GetAltinnAppDelegationPolicyPath(x.PolicyMatch)).Any(g => g.Count() > 1)) { validationResult.Add(new ValidationResult("Input should not contain any duplicate policies")); return(validationResult); } } catch { validationResult.Add(new ValidationResult("Not all requests to delete contains valid policy paths")); return(validationResult); } return(validationResult); }
public async Task <Shared.Store> AddAsync(Uri uri) { var absoluteUrl = uri.AbsoluteUri; var storedStore = storesRepository.Find(store => store.Url == absoluteUrl).FirstOrDefault(); if (storedStore == null) { var dateTime = DateTime.Now; var store = new Store { Name = "Unknown Store", IsEnabled = true, Url = UriHelper.EnsureStoreUrl(absoluteUrl), Added = dateTime, Updated = dateTime, Read = dateTime }; var transaction = PolicyHelper.WaitAndRetry().Execute( () => storesRepository.BeginTransaction(IsolationLevel.Serializable)); storesRepository.Add(store); await storesRepository.SaveChangesAsync(); await transaction.CommitAsync(); return(store.ToDataTransferObject(true)); } return(storedStore?.ToDataTransferObject()); }
public static async Task <(bool, Product)> TryRegisterProductAsync(this IRepository <Product, int> productRepository, string absoluteUrl) { Log.Information("Try to register product with Url '{Url}'", absoluteUrl); var product = productRepository.Find(p => p.Url == absoluteUrl).FirstOrDefault(); var registered = false; if (product == null) { var dateTime = DateTime.Now; product = new Product(); { product.Name = "Unknown Product"; product.Url = UriHelper.EnsureProductUrl(absoluteUrl); product.Added = dateTime; product.Updated = dateTime; product.Read = dateTime; product.IsEnabled = true; var transaction = PolicyHelper.WaitAndRetry().Execute( () => productRepository.BeginTransaction(IsolationLevel.Serializable)); productRepository.Add(product); await productRepository.SaveChangesAsync(); await transaction.CommitAsync(); registered = true; } } Log.Information("Product with Url '{Url}' is registered as {Name}", absoluteUrl, product.Name); return(registered, product); }
public void ValidateFilesAgainstPolicies_NoViolations() { EnterpriseConfigHelper.EnterpriseConfig = null; string script = Properties.Resources.PolicyHelper_NoViolations; script = script.Replace("<<date>>", DateTime.Now.ToString("MM/dd/yyyy")); string fileName = string.Empty; Package actual; try { fileName = Path.GetTempFileName(); File.WriteAllText(fileName, script); List <string> files = new List <string>(); files.Add(fileName); actual = new PolicyHelper().ValidateFilesAgainstPolicies(files); Assert.IsNull(actual, "ScriptViolations object should be null for scripts with no violations."); } finally { if (File.Exists(fileName)) { File.Delete(fileName); } } }
public void ValidateFilesAgainstPolicies_WithViolations() { PolicyHelper.activePolicies = (from p in PolicyHelper.allPolicies select p.Value).ToList(); string script = Properties.Resources.PolicyHelper_WithViolations; string fileName = string.Empty; Package actual; try { fileName = Path.GetTempFileName(); File.WriteAllText(fileName, script); List <string> files = new List <string>(); files.Add(fileName); actual = new PolicyHelper().ValidateFilesAgainstPolicies(files); Assert.IsNotNull(actual, "ScriptViolations object should not be null for scripts with violations"); Assert.AreEqual(1, actual.Count); Assert.AreEqual(Path.GetFileName(fileName), actual[0].ScriptName); Assert.AreEqual(6, actual[0].Count); Assert.AreEqual("Check for Comments", actual[0][4].Name); } finally { if (File.Exists(fileName)) { File.Delete(fileName); } } }
static void Main(string[] args) { PolicyHelper policyHelper = new PolicyHelper(); List <Policy> validPolicies = new List <Policy>(); Console.WriteLine("Hello World!"); try { validPolicies = new DataLoadFromCSVFile(@"E:\MaturityData.csv").GetValidPolicies(); Console.WriteLine("Load Complete..."); } catch (Exception e) { PrintExceptionMessage(e); } try { var result = new OutputXMLFile(validPolicies, policyHelper); result.CreateOutputFile(new XmlSerializer(typeof(List <PolicyDTO>), new XmlRootAttribute("MaturedPolicies") { IsNullable = false })); Console.WriteLine("Creating XML File Complete..."); } catch (Exception e) { PrintExceptionMessage(e); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
public void SendMessage <T>(string exchangeName, T msg, string exchangeType, string qName = "", string routingKey = "", SerializeType type = SerializeType.Json) where T : class { if (string.IsNullOrEmpty(exchangeName) || msg == null) { return; } var body = SerializeObject(type, msg); Action a1 = () => { using (var channel = connHelper.GetConnection().CreateModel()) { //设置交换器的类型 channel.ExchangeDeclare(exchangeName, exchangeType); if (!string.IsNullOrWhiteSpace(qName)) { //声明一个队列,设置队列是否持久化,排他性,与自动删除 channel.QueueDeclare(qName, durable: true, false, false, null); //绑定消息队列,交换器,routingkey channel.QueueBind(qName, exchangeName, routingKey); } var properties = channel.CreateBasicProperties(); //队列持久化 properties.Persistent = true; //发送信息 channel.BasicPublish(exchangeName, routingKey, properties, body); } }; PolicyHelper.GetRetryTimesPolicy(3, ex => { var task = new Task(a1); TasksCache.Enqueue(task); }).Execute(() => { a1.Invoke(); }); }
public async Task <Stock> GetStock(long id, string jwtToken) { return(await PolicyHelper.ThreeRetriesAsync().ExecuteAsync(() => _publicShareOwnerControl.BaseAddress .AppendPathSegments(_publicShareOwnerControl.PublicSharePath.Stock, id) .WithOAuthBearerToken(jwtToken).GetJsonAsync <Stock>())); }
public async Task <List <SellRequestModel> > GetSellRequestsForSpecificStock(long stockId, string jwtToken) { return(await PolicyHelper.ThreeRetriesAsync().ExecuteAsync(() => _stockTraderBroker.BaseAddress .AppendPathSegment(_stockTraderBroker.StockTraderBrokerPath.SellRequest).SetQueryParam("stockId", stockId) .WithOAuthBearerToken(jwtToken).GetJsonAsync <List <SellRequestModel> >())); }
public async Task <Shared.Store> AddAsync(Uri uri) { var absoluteUrl = uri.AbsoluteUri; var storedStore = this.storesRepository.Find(store => store.Url == absoluteUrl).FirstOrDefault(); if (storedStore == null) { var dateTime = DateTime.Now; var store = await this.entityScrapper.GetAsync(absoluteUrl); if (store != null) { store.Added = dateTime; store.Updated = dateTime; store.Read = dateTime; var transaction = PolicyHelper.WaitAndRetry().Execute( () => this.storesRepository.BeginTransaction(IsolationLevel.Serializable)); this.storesRepository.Add(store); await this.storesRepository.SaveChangesAsync(); await transaction.CommitAsync(); return(store.ToDataTransferObject(true)); } } return(storedStore?.ToDataTransferObject()); }
public async Task <ActionResult <Product> > Add( [FromServices] IRepository <Models.Product, int> productRepository, [FromServices] IEntityScrapper <Models.Product> entityScrapper, [FromBody] Uri uri) { var absoluteUrl = uri.AbsoluteUri; var storedProduct = productRepository.Find(product => product.Url == absoluteUrl).FirstOrDefault(); if (storedProduct == null) { var dateTime = DateTime.Now; var product = await entityScrapper.GetAsync(absoluteUrl); if (product != null) { product.Added = dateTime; product.Updated = dateTime; product.Read = dateTime; var transaction = PolicyHelper.WaitAndRetry().Execute( () => productRepository.BeginTransaction(IsolationLevel.Serializable)); productRepository.Add(product); await productRepository.SaveChangesAsync(); await transaction.CommitAsync(); return(product.ToDataTransferObject(true)); } } return(storedProduct?.ToDataTransferObject()); }
public async Task <List <BuyRequestModel> > GetBuyRequests(Guid id, string jwtToken) { return(await PolicyHelper.ThreeRetriesAsync().ExecuteAsync(() => _stockTraderBroker.BaseAddress .AppendPathSegment(_stockTraderBroker.StockTraderBrokerPath.BuyRequest).SetQueryParam("ownerId", id) .WithOAuthBearerToken(jwtToken).GetJsonAsync <List <BuyRequestModel> >())); }
public void SendMessage <T>(string qName, T msg, SerializeType type = SerializeType.Json) where T : class { if (string.IsNullOrEmpty(qName) || msg == null) { return; } var body = SerializeObject(type, msg); Action a1 = () => { using (var channel = connHelper.GetConnection().CreateModel()) { channel.QueueDeclare(qName, durable: true, false, false, null); var basicProperties = new BasicProperties { Persistent = true }; //RabbitMQ有一个默认的exchange="",此时qName即为routingKey channel.BasicPublish("", qName, basicProperties, body); } }; PolicyHelper.GetRetryTimesPolicy(3, ex => { var task = new Task(a1); TasksCache.Enqueue(task); }).Execute(() => { a1.Invoke(); }); }
public async Task <ActionResult <ValidationResult> > RemoveBuyRequest(long id, string jwtToken) { return(await PolicyHelper.ThreeRetriesAsync().ExecuteAsync(() => _stockTraderBroker.BaseAddress .AppendPathSegments(_stockTraderBroker.StockTraderBrokerPath.BuyRequest, id) .WithOAuthBearerToken(jwtToken).DeleteAsync().ReceiveJson <ValidationResult>())); }
public async Task <ActionResult <Department> > Add( [FromServices] IRepository <Models.Department, int> departmentRepository, [FromServices] IEntityScraper <Models.Department> departmentScraper, [FromBody] Uri uri) { var absoluteUrl = uri.AbsoluteUri; var storedDepartment = departmentRepository.Find(department => department.Url == absoluteUrl).FirstOrDefault(); if (storedDepartment == null) { var dateTime = DateTime.Now; var department = new Models.Department { Name = "Unknown Department", IsEnabled = true, Url = UriHelper.EnsureDepartmentUrl(absoluteUrl), Added = dateTime, Updated = dateTime, Read = dateTime }; var transaction = PolicyHelper.WaitAndRetry().Execute( () => departmentRepository.BeginTransaction(IsolationLevel.Serializable)); departmentRepository.Add(department); await departmentRepository.SaveChangesAsync(); await transaction.CommitAsync(); return(department.ToDataTransferObject(true)); } return(storedDepartment?.ToDataTransferObject()); }
public async Task <ValidationResult> PostBuyRequest(BuyRequestInput request, string jwtToken) { return(await PolicyHelper.ThreeRetriesAsync().ExecuteAsync(() => _stockTraderBroker.BaseAddress .AppendPathSegment(_stockTraderBroker.StockTraderBrokerPath.BuyRequest) .WithOAuthBearerToken(jwtToken).PostJsonAsync(request).ReceiveJson <ValidationResult>())); }
private async Task <List <Rule> > DeleteRulesInPolicy(RequestToDelete rulesToDelete) { string coveredBy = DelegationHelper.GetCoveredByFromMatch(rulesToDelete.PolicyMatch.CoveredBy, out int?coveredByUserId, out int?coveredByPartyId); DelegationHelper.TryGetResourceFromAttributeMatch(rulesToDelete.PolicyMatch.Resource, out string org, out string app); string policyPath; try { policyPath = PolicyHelper.GetAltinnAppDelegationPolicyPath(org, app, rulesToDelete.PolicyMatch.OfferedByPartyId.ToString(), coveredByUserId, coveredByPartyId); } catch (Exception ex) { string rulesToDeleteString = string.Join(", ", rulesToDelete.RuleIds); _logger.LogError(ex, "Not possible to build policy path App: {org}/{app} CoveredBy: {coveredBy} OfferedBy: {policyToDelete.PolicyMatch.OfferedByPartyId} RuleIds: {rulesToDeleteString}", org, app, coveredBy, rulesToDelete.PolicyMatch.OfferedByPartyId, rulesToDeleteString); return(null); } if (!await _policyRepository.PolicyExistsAsync(policyPath)) { _logger.LogWarning("No blob was found for the expected path: {policyPath} this must be removed without upading the database", policyPath); return(null); } List <Rule> currentRules = await ProcessPolicyFile(policyPath, org, app, rulesToDelete); return(currentRules); }
public void GetPoliciesTest_AddDynamicIScriptPolicies() { EnterpriseConfiguration cfg = new EnterpriseConfiguration(); ScriptPolicy pol1 = new ScriptPolicy(); pol1.PolicyId = PolicyIdKey.WithNoLockPolicy; pol1.Enforce = true; ScriptPolicy pol2 = new ScriptPolicy(); pol2.PolicyId = PolicyIdKey.GrantExecutePolicy; pol2.Enforce = true; cfg.ScriptPolicy = new ScriptPolicy[] { pol1, pol2 }; EnterpriseConfigHelper.EnterpriseConfig = cfg; List <IScriptPolicy> actual; actual = PolicyHelper.GetPolicies(); Assert.IsTrue(actual.Count == 2, "Expected 2 policies but got " + actual.Count.ToString()); Assert.IsTrue(actual[0] is WithNoLockPolicy); Assert.IsTrue(actual[1] is GrantExecutePolicy); }
public void ValidateFileAgainstPolicies_WithViolations() { PolicyHelper.activePolicies = (from p in PolicyHelper.allPolicies select p.Value).ToList(); string script = Properties.Resources.PolicyHelper_WithViolations; string fileName = string.Empty; Script actual; try { fileName = Path.GetTempFileName(); File.WriteAllText(fileName, script); actual = new PolicyHelper().ValidateFileAgainstPolicies(fileName); Assert.AreEqual(6, actual.Count); Assert.AreEqual("Check for GRANT .. TO [public]", actual[0].Name); Assert.AreEqual("Check for Comments", actual[4].Name); } finally { if (File.Exists(fileName)) { File.Delete(fileName); } } }
public async Task Delete([FromServices] IRepository <Models.Department, int> departmentRepository, int id) { var transaction = PolicyHelper.WaitAndRetry() .Execute(() => departmentRepository.BeginTransaction(IsolationLevel.Serializable)); departmentRepository.Delete(department => department.Id == id); await departmentRepository.SaveChangesAsync(); await transaction.CommitAsync(); }
public async Task Delete([FromServices] IRepository <Product, int> productRepository, int id) { var transaction = PolicyHelper.WaitAndRetry() .Execute(() => productRepository.BeginTransaction(IsolationLevel.Serializable)); productRepository.Delete(product => product.Id == id); await productRepository.SaveChangesAsync(); await transaction.CommitAsync(); }
public void ValidateScriptAgainstPolicyTest_WithIScriptPolicy() { string script = Properties.Resources.PolicyHelper_WithViolations; string targetDatabase = "TestDb"; IScriptPolicy policy = new CommentHeaderPolicy(); Violation actual; actual = PolicyHelper.ValidateScriptAgainstPolicy(script, targetDatabase, policy); Assert.IsNotNull(actual); }
public async Task ExecuteAsync( IRepository <User, int> userRepository, ITelegramBotClient telegramBotClient = null) { if (telegramBotClient == null) { Log.Information("TelegramBotClient is not registered."); return; } Log.Information("Synchronizing users from Telegram."); Update[] updates = null; try { updates = await telegramBotClient.GetUpdatesAsync(-1); } catch (Exception e) { Log.Error(e, "Error retrieving updates from telegram"); } var users = updates ?.Select( update => new User { ChatId = update?.Message?.Chat?.Id ?? -1, Name = update?.Message?.Chat?.Username ?? string.Empty }).Where(user => !string.IsNullOrWhiteSpace(user.Name)) /*.Distinct(new UserEqualityComparer())*/.ToList(); if (users != null) { foreach (var user in users) { var storedUser = userRepository.Find(u => u.Name == user.Name).FirstOrDefault(); if (storedUser != null) { user.Id = storedUser.Id; } var transaction = PolicyHelper.WaitAndRetry().Execute( () => userRepository.BeginTransaction(IsolationLevel.Serializable)); userRepository.TryAddOrUpdate(user, nameof(User.IsEnable)); await userRepository.SaveChangesAsync(); await transaction.CommitAsync(); } } Log.Information("User synchronization from telegram completed"); }
public void ValidateScriptAgainstPolicyTest_NoViolation() { string script = Properties.Resources.PolicyHelper_NoViolations; string targetDatabase = "TestDb"; IScriptPolicy policy = new SelectStarPolicy(); Violation actual; actual = PolicyHelper.ValidateScriptAgainstPolicy(script, targetDatabase, policy); Assert.IsNull(actual); }
public void ValidateScriptAgainstPoliciesTest_NoViolations() { EnterpriseConfigHelper.EnterpriseConfig = null; string script = Properties.Resources.PolicyHelper_NoViolations; script = script.Replace("<<date>>", DateTime.Now.ToString("MM/dd/yyyy")); Script actual; actual = new PolicyHelper().ValidateScriptAgainstPolicies(script); Assert.IsNull(actual, "ScriptViolations object should be null for scripts with no violations."); }
/// <inheritdoc/> public async Task <bool> WritePolicyAsync(string org, string app, Stream fileStream) { if (fileStream == null) { throw new ArgumentException("The policy file can not be null"); } string filePath = PolicyHelper.GetAltinnAppsPolicyPath(org, app); Response <BlobContentInfo> response = await _policyRepository.WritePolicyAsync(filePath, fileStream); return(response?.GetRawResponse()?.Status == (int)HttpStatusCode.Created); }
private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { if (!(sender is BackgroundWorker)) { return; } BackgroundWorker bg = (BackgroundWorker)sender; if (!(e.Argument is List <IScriptPolicy>)) { bg.ReportProgress(0, "Error running. Policies not selected"); return; } List <IScriptPolicy> selectedPolices = (List <IScriptPolicy>)e.Argument; string message, fileName, script, targetDatabase; bool passed; foreach (SqlSyncBuildData.ScriptRow row in this.buildData.Script) { fileName = Path.Combine(this.projectFilePath + row.FileName); if (File.Exists(fileName)) { script = File.ReadAllText(fileName); targetDatabase = row.Database; for (int i = 0; i < selectedPolices.Count; i++) { passed = false; message = string.Empty; if (selectedPolices[i] is CommentHeaderPolicy) { ((CommentHeaderPolicy)selectedPolices[i]).DayThreshold = 40; } Violation tmp = PolicyHelper.ValidateScriptAgainstPolicy(script, targetDatabase, selectedPolices[i]); if (tmp == null) { passed = true; } else { message = tmp.Message; } PolicyMessage msg = new PolicyMessage(row.FileName, selectedPolices[i].ShortDescription, passed, message, row); bg.ReportProgress(0, msg); } } } }
private void BindDropDownList() { Group group = this.Domain; // 绑定已拥有的角色列表 this.RoleUnids.DataSource = group.RoleLists; this.RoleUnids.DataTextField = "Name"; this.RoleUnids.DataValueField = "Unid"; this.RoleUnids.DataBind(); // 绑定已包含的人员列表 this.UserInfoUnids.DataSource = group.UserInfoLists; this.UserInfoUnids.DataTextField = "Name"; this.UserInfoUnids.DataValueField = "Unid"; this.UserInfoUnids.DataBind(); // 绑定所有角色和OU的信息 if (!string.IsNullOrEmpty(group.OUUnid)) { IList roleList = ListUtils.WeedList(roleService.FindByOU(group.OUUnid), group.RoleLists); this.AllRoles.DataSource = roleList; this.AllRoles.DataTextField = "Name"; this.AllRoles.DataValueField = "Unid"; this.AllRoles.DataBind(); string policyValue = PolicyHelper.GetPolicyValue("9001"); if (string.IsNullOrEmpty(policyValue)) { policyValue = TSLibWeb.Constants.YESNO_YES; } IList ouList; if (policyValue.Equals(TSLibWeb.Constants.YESNO_YES, StringComparison.OrdinalIgnoreCase)) { ouList = this.ouInfoService.FindOUInfoByParentUnid(group.OUUnid, true, true); } else { ouList = this.ouInfoService.FindOUInfoByParentUnid(group.OUUnid, true, false); } this.OUInfos.DataSource = ouList; this.OUInfos.DataTextField = "FullName"; this.OUInfos.DataValueField = "Unid"; this.OUInfos.DataBind(); this.OUInfos.Text = group.OUUnid; // 获取该OU中的人员信息 IList userInfoList = ListUtils.WeedList(this.userInfoService.FindByOU(group.OUUnid), group.UserInfoLists); this.AllUserInfos.DataSource = userInfoList; this.AllUserInfos.DataTextField = "Name"; this.AllUserInfos.DataValueField = "Unid"; this.AllUserInfos.DataBind(); } }