Exemplo n.º 1
0
        /// <summary>
        /// Creates a new instance that will connect to the database server (<paramref name="target"/>) and perform redactions using the <paramref name="updater"/>
        /// </summary>
        /// <param name="opts">CLI options for the process</param>
        /// <param name="target">DBMS to connect to for redacting</param>
        /// <param name="ignorer">Rules base for detecting false positives</param>
        /// <param name="updater">Rules base for redacting true positives</param>
        public UnattendedReviewer(IsIdentifiableReviewerOptions opts, Target target, IgnoreRuleGenerator ignorer, RowUpdater updater)
        {
            _log = LogManager.GetCurrentClassLogger();

            if (string.IsNullOrWhiteSpace(opts.FailuresCsv))
            {
                throw new Exception("Unattended requires a file of errors to process");
            }

            var fi = new FileInfo(opts.FailuresCsv);

            if (!fi.Exists)
            {
                throw new FileNotFoundException($"Could not find Failures file '{fi.FullName}'");
            }

            if (!opts.OnlyRules)
            {
                _target = target ?? throw new Exception("A single Target must be supplied for database updates");
            }

            _reportReader = new ReportReader(fi);

            if (string.IsNullOrWhiteSpace(opts.UnattendedOutputPath))
            {
                throw new Exception("An output path must be specified for Failures that could not be resolved");
            }

            _outputFile = new FileInfo(opts.UnattendedOutputPath);

            _ignorer = ignorer;
            _updater = updater;
        }
Exemplo n.º 2
0
        public RulesView(ReportReader currentReport, IgnoreRuleGenerator ignorer, RowUpdater updater)
        {
            CurrentReport = currentReport;
            Ignorer       = ignorer;
            Updater       = updater;
            Modal         = true;

            var lblInitialSummary = new Label($"There are {ignorer.Rules.Count} ignore rules and {updater.Rules.Count} update rules.  Current report contains {CurrentReport.Failures.Length:N0} Failures");

            Add(lblInitialSummary);

            var lblEvaluate = new Label($"Evaluate:")
            {
                Y = Pos.Bottom(lblInitialSummary) + 1
            };

            Add(lblEvaluate);

            var ruleCollisions = new Button("Rule Coverage")
            {
                Y = Pos.Bottom(lblEvaluate)
            };

            ruleCollisions.Clicked += () => EvaluateRuleCoverage();
            Add(ruleCollisions);

            _treeView = new TreeView()
            {
                Y      = Pos.Bottom(ruleCollisions) + 1,
                Width  = Dim.Fill(),
                Height = Dim.Fill(1)
            };
            _treeView.KeyPress += _treeView_KeyPress;

            Add(_treeView);

            var close = new Button("Close", true)
            {
                Y = Pos.Bottom(_treeView)
            };

            close.Clicked += () => Quit();

            Add(close);
        }
Exemplo n.º 3
0
        public void TestRepeatedIgnoring()
        {
            var failure = new Failure(
                new FailurePart[]
            {
                new FailurePart("Kansas", FailureClassification.Location, 13),
                new FailurePart("Toto", FailureClassification.Location, 28)
            })
            {
                ProblemValue       = "We aren't in Kansas anymore Toto",
                ProblemField       = "Narrative",
                ResourcePrimaryKey = "1.2.3.4"
            };

            var newRules = new FileInfo(Path.Combine(TestContext.CurrentContext.WorkDirectory, "IgnoreList.yaml"));

            //make sure repeat test runs work properly
            if (File.Exists(newRules.FullName))
            {
                File.Delete(newRules.FullName);
            }

            IgnoreRuleGenerator ignorer = new IgnoreRuleGenerator(newRules);

            //it should be novel i.e. require user decision
            Assert.IsTrue(ignorer.OnLoad(failure, out _));

            //we tell it to ignore this value
            ignorer.Add(failure);

            TestHelpers.Contains(
                @"- Action: Ignore
  IfColumn: Narrative
  IfPattern: ^We\ aren't\ in\ Kansas\ anymore\ Toto$
", File.ReadAllText(newRules.FullName)); //btw slash space is a 'literal space' so legit

            //it should be no longer be novel
            Assert.IsFalse(ignorer.OnLoad(failure, out _));
        }
Exemplo n.º 4
0
        private static int OnParse(GlobalOptions globals, IsIdentifiableReviewerOptions opts)
        {
            FansiImplementations.Load();

            Deserializer  d = new Deserializer();
            List <Target> targets;

            try
            {
                var file = new FileInfo(opts.TargetsFile);

                if (!file.Exists)
                {
                    Console.Write($"Could not find '{file.FullName}'");
                    return(1);
                }

                var contents = File.ReadAllText(file.FullName);

                if (string.IsNullOrWhiteSpace(contents))
                {
                    Console.Write($"Targets file is empty '{file.FullName}'");
                    return(2);
                }

                targets = d.Deserialize <List <Target> >(contents);

                if (!targets.Any())
                {
                    Console.Write($"Targets file did not contain any valid targets '{file.FullName}'");
                    return(3);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error deserializing '{opts.TargetsFile}'");
                Console.WriteLine(e.Message);
                return(4);
            }

            if (opts.OnlyRules)
            {
                Console.WriteLine("Skipping Connection Tests");
            }
            else
            {
                Console.WriteLine("Running Connection Tests");

                try
                {
                    foreach (Target t in targets)
                    {
                        Console.WriteLine(t.Discover().Exists()
                            ? $"Successfully connected to {t.Name}"
                            : $"Failed to connect to {t.Name}");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error Validating Targets");
                    Console.WriteLine(e.ToString());
                    return(10);
                }
            }

            //for updater try to match the ProblemValue words
            var updater = new RowUpdater(new FileInfo(opts.RedList))
            {
                RulesOnly    = opts.OnlyRules,
                RulesFactory = new MatchProblemValuesPatternFactory()
            };

            //for Ignorer match the whole string
            var ignorer = new IgnoreRuleGenerator(new FileInfo(opts.IgnoreList));

            try
            {
                if (!string.IsNullOrWhiteSpace(opts.UnattendedOutputPath))
                {
                    //run unattended
                    if (targets.Count != 1)
                    {
                        throw new Exception("Unattended requires a single entry in Targets");
                    }

                    var unattended = new UnattendedReviewer(opts, targets.Single(), ignorer, updater);
                    return(unattended.Run());
                }
                else
                {
                    Console.WriteLine("Press any key to launch GUI");
                    Console.ReadKey();

                    //run interactive
                    Application.Init();
                    var mainWindow = new MainWindow(opts, ignorer, updater);
                    Application.Top.Add(mainWindow);
                    Application.Run();
                    return(0);
                }
            }
            catch (Exception e)
            {
                Console.Write(e.Message);

                int tries = 5;
                while (Application.Top != null && tries-- > 0)
                {
                    try
                    {
                        Application.RequestStop();
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Failed to terminate GUI on crash");
                    }
                }

                return(99);
            }
        }
Exemplo n.º 5
0
        public void TestUndo()
        {
            var failure = new Failure(
                new FailurePart[]
            {
                new FailurePart("Kansas", FailureClassification.Location, 13),
                new FailurePart("Toto", FailureClassification.Location, 28)
            })
            {
                ProblemValue       = "We aren't in Kansas anymore Toto",
                ProblemField       = "Narrative",
                ResourcePrimaryKey = "1.2.3.4"
            };

            var newRules = new FileInfo(Path.Combine(TestContext.CurrentContext.WorkDirectory, "IgnoreList.yaml"));

            //make sure repeat test runs work properly
            if (File.Exists(newRules.FullName))
            {
                File.Delete(newRules.FullName);
            }

            //create an existing rule to check that Undo doesn't just nuke the entire file
            File.WriteAllText(newRules.FullName, @"- Action: Ignore
  IfColumn: Narrative
  IfPattern: ^Joker Wuz Ere$
");

            IgnoreRuleGenerator ignorer = new IgnoreRuleGenerator(newRules);

            //it should be novel i.e. require user decision
            Assert.IsTrue(ignorer.OnLoad(failure, out _));

            //we tell it to ignore this value
            ignorer.Add(failure);

            TestHelpers.Contains(
                @"- Action: Ignore
  IfColumn: Narrative
  IfPattern: ^We\ aren't\ in\ Kansas\ anymore\ Toto$
", File.ReadAllText(newRules.FullName)); //btw slash space is a 'literal space' so legit

            //it should be no longer be novel
            Assert.IsFalse(ignorer.OnLoad(failure, out _));

            //Undo
            Assert.AreEqual(1, ignorer.History.Count);
            Assert.AreEqual(2, ignorer.Rules.Count);
            ignorer.Undo();

            Assert.AreEqual(0, ignorer.History.Count);
            Assert.AreEqual(1, ignorer.Rules.Count);

            //only the original one should be there
            Assert.AreEqual(@"- Action: Ignore
  IfColumn: Narrative
  IfPattern: ^Joker Wuz Ere$
", File.ReadAllText(newRules.FullName));

            //repeated undo calls do nothing
            ignorer.Undo();
            ignorer.Undo();
            ignorer.Undo();
        }
Exemplo n.º 6
0
        public MainWindow(IsIdentifiableReviewerOptions opts, IgnoreRuleGenerator ignorer, RowUpdater updater)
        {
            Ignorer = ignorer;
            Updater = updater;
            _origUpdaterRulesFactory = updater.RulesFactory;
            _origIgnorerRulesFactory = ignorer.RulesFactory;

            X      = 0;
            Y      = 1;
            Width  = Dim.Fill();
            Height = Dim.Fill();

            var top = Application.Top;

            var menu = new MenuBar(new MenuBarItem [] {
                new MenuBarItem("_File (F9)", new MenuItem [] {
                    new MenuItem("_Open Report", null, OpenReport),
                    new MenuItem("_Quit", null, () => { top.Running = false; })
                }),
                new MenuBarItem("_Options", new MenuItem [] {
                    miCustomPatterns = new MenuItem("_Custom Patterns", null, ToggleCustomPatterns)
                    {
                        CheckType = MenuItemCheckStyle.Checked, Checked = false
                    }
                }),
                new MenuBarItem("_View", new MenuItem [] {
                    new MenuItem("_Rules", null, ViewRules),
                })
            });

            _info = new Label("Info")
            {
                X      = 0,
                Y      = 0,
                Width  = Dim.Fill(),
                Height = 1
            };

            _info.ColorScheme = _greyOnBlack;

            _valuePane = new FailureView()
            {
                X      = 0,
                Y      = 1,
                Width  = Dim.Fill(),
                Height = 10,
            };

            var frame = new FrameView("Options")
            {
                X      = 0,
                Y      = 11,
                Width  = Dim.Fill(),
                Height = Dim.Fill()
            };

            var ignoreButton = new Button("Ignore")
            {
                X = 0
            };

            ignoreButton.Clicked += Ignore;
            frame.Add(ignoreButton);

            var updateButton = new Button("Update")
            {
                X = 11
            };

            updateButton.Clicked += Update;
            frame.Add(updateButton);

            _gotoTextField = new TextField("1")
            {
                X     = 28,
                Width = 5
            };
            _gotoTextField.TextChanged += (s) => GoTo();
            frame.Add(_gotoTextField);
            frame.Add(new Label(23, 0, "GoTo:"));

            var prevButton = new Button("Prev")
            {
                X = 0,
                Y = 1
            };

            prevButton.Clicked += () => GoToRelative(-1);
            frame.Add(prevButton);

            var nextButton = new Button("Next")
            {
                X = 11,
                Y = 1
            };

            nextButton.Clicked += () => GoToRelative(1);
            frame.Add(nextButton);

            var undoButton = new Button("unDo")
            {
                X = 11,
                Y = 2
            };

            undoButton.Clicked += () => Undo();
            frame.Add(undoButton);

            frame.Add(new Label(0, 4, "Default Patterns"));

            _ignoreRuleLabel = new Label(0, 5, "Ignore:");
            _updateRuleLabel = new Label(0, 6, "Update:");;
            frame.Add(_ignoreRuleLabel);
            frame.Add(_updateRuleLabel);

            // always run rules only mode for the manual gui
            Updater.RulesOnly = true;

            top.Add(menu);
            Add(_info);
            Add(_valuePane);
            Add(frame);

            if (!string.IsNullOrWhiteSpace(opts.FailuresCsv))
            {
                OpenReport(opts.FailuresCsv, (e) => throw e);
            }
        }