Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #63 from applandinc/refactor/rename-internal-code-…
Browse files Browse the repository at this point in the history
…objects

refactor: Rename Scanner concepts - Rule, Check, Scan
  • Loading branch information
dustinbyrne authored Dec 9, 2021
2 parents 40f0c5c + 51cabe7 commit 0f5b95e
Show file tree
Hide file tree
Showing 84 changed files with 1,300 additions and 1,461 deletions.
2 changes: 1 addition & 1 deletion bin/schema
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e
set -x

npx ts-json-schema-generator -i https://appland.com/schemas/scanner/configuration.json --path src/configuration/types/configuration.ts > src/configuration/schema/configuration.json
npx ts-json-schema-generator -i https://appland.com/schemas/scanner/options.json --path src/scanner/types.d.ts > src/configuration/schema/options.json
npx ts-json-schema-generator -i https://appland.com/schemas/scanner/options.json --path src/rules/types.d.ts > src/configuration/schema/options.json

fix_match_pattern_config() {
cd src/configuration/schema
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"typescript": "^4.4.2"
},
"dependencies": {
"@appland/models": "^1.7.1",
"@appland/models": "^1.8.0",
"@types/sinon": "^10.0.2",
"ajv": "^8.8.2",
"ansi-escapes": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/analyzer/recordSecrets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Event } from '@appland/models';
import { emptyValue, verbose } from '../scanner/util';
import { emptyValue, verbose } from '../rules/util';

export default function (secrets: Set<string>, e: Event): void {
if (!e.returnValue) {
Expand Down
92 changes: 0 additions & 92 deletions src/assertion.ts

This file was deleted.

55 changes: 55 additions & 0 deletions src/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { AppMap, Event } from '@appland/models';
import { verbose } from './rules/util';
import { EventFilter, Rule, ScopeName } from './types';

export default class Check {
public id: string;
public options: Record<string, any>;
public scope: ScopeName;
public includeScope: EventFilter[];
public excludeScope: EventFilter[];
public includeEvent: EventFilter[];
public excludeEvent: EventFilter[];

constructor(public rule: Rule, options?: Record<string, any>) {
function makeOptions() {
return rule.Options ? new rule.Options() : {};
}

this.id = rule.id;
this.options = options || makeOptions();
this.scope = rule.scope || 'root';
this.includeScope = [];
this.excludeScope = [];
this.includeEvent = [];
this.excludeEvent = [];
}

filterScope(event: Event, appMap?: AppMap): boolean {
if (this.includeScope.length > 0 && !this.includeScope.every((fn) => fn(event, appMap))) {
if (verbose()) {
console.warn(`\t'includeScope' clause is not satisifed.`);
}
return false;
}
if (this.excludeScope.some((fn) => fn(event, appMap))) {
if (verbose()) {
console.warn(`\t'excludeScope' clause is not satisifed.`);
}
return false;
}
return true;
}

toString(): string {
const tokens = [`[${this.rule.id}]`];
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self: any = this;
['includeScope', 'excludeScope', 'includeEvent', 'excludeEvent'].forEach((key) => {
if (self[key].length > 0) {
tokens.push(`(${key} ${self[key].join(' && ')})`);
}
});
return tokens.join(' ');
}
}
60 changes: 60 additions & 0 deletions src/checkInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { AppMap, Event } from '@appland/models';
import Check from './check';
import { verbose } from './rules/util';
import { RuleLogic, ScopeName } from './types';

export default class CheckInstance {
check: Check;
ruleLogic: RuleLogic;

constructor(check: Check) {
this.check = check;
this.ruleLogic = check.rule.build(check.options || {});
}

get checkId(): string {
return this.check.id;
}

get ruleId(): string {
return this.check.rule.id;
}

get title(): string {
return this.check.rule.title;
}

get scope(): ScopeName {
return this.check.scope;
}

get enumerateScope(): boolean {
return this.check.rule.enumerateScope;
}

filterEvent(event: Event, appMap?: AppMap): boolean {
if (this.ruleLogic.where && !this.ruleLogic.where(event, appMap)) {
if (verbose()) {
console.warn(`\t'where' clause is not satisifed.`);
}
return false;
}

if (
this.check.includeEvent.length > 0 &&
!this.check.includeEvent.every((fn) => fn(event, appMap))
) {
if (verbose()) {
console.warn(`\t'includeEvent' clause is not satisifed.`);
}
return false;
}
if (this.check.excludeEvent.some((fn) => fn(event, appMap))) {
if (verbose()) {
console.warn(`\t'excludeEvent' clause is not satisifed.`);
}
return false;
}
return true;
}
}
25 changes: 10 additions & 15 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { buildAppMap } from '@appland/models';
import { glob as globCallback } from 'glob';
import { promisify } from 'util';
import { promises as fs, constants as fsConstants, PathLike } from 'fs';
import AssertionChecker from './assertionChecker';
import ProgressFormatter from './formatter/progressFormatter';
import PrettyFormatter from './formatter/prettyFormatter';
import { ValidationError, AbortError } from './errors';
import { AssertionPrototype, Finding } from './types';
import { Finding } from './types';
import { Argv, Arguments } from 'yargs';
import chalk from 'chalk';
import { loadConfiguration } from './configuration';
import { verbose } from './scanner/util';
import { verbose } from './rules/util';
import { join } from 'path';
import postCommitStatus from './integration/github/commitStatus';
import postPullRequestComment from './integration/github/postPullRequestComment';
import Generator, { ReportFormat } from './report/generator';
import RuleChecker from './ruleChecker';

enum ExitCode {
ValidationError = 1,
Expand Down Expand Up @@ -140,12 +140,11 @@ export default {
files = [appmapFile];
}

const checker = new AssertionChecker();
const checker = new RuleChecker();
const formatter =
progressFormat === 'progress' ? new ProgressFormatter() : new PrettyFormatter();
const assertionPrototypes = await loadConfiguration(config);
const checks = await loadConfiguration(config);

let index = 0;
const findings: Finding[] = [];

await Promise.all(
Expand All @@ -162,14 +161,13 @@ export default {
process.stderr.write(formatter.appMap(appMap));

await Promise.all(
assertionPrototypes.map(async (assertionPrototype: AssertionPrototype) => {
index++;
checks.map(async (check) => {
const matchCount = findings.length;
await checker.check(appMap, assertionPrototype, findings);
await checker.check(appMap, check, findings);
const newMatches = findings.slice(matchCount, findings.length);
newMatches.forEach((match) => (match.appMapFile = file));

const message = formatter.result(assertionPrototype, newMatches, index);
const message = formatter.result(check, newMatches);
if (message) {
process.stderr.write(message);
}
Expand All @@ -179,7 +177,7 @@ export default {
);

const reportGenerator = new Generator(formatter, reportFormat, reportFile, ide);
const summary = reportGenerator.generate(findings, files.length * assertionPrototypes.length);
const summary = reportGenerator.generate(findings, files.length * checks.length);

if (pullRequestComment && findings.length > 0) {
try {
Expand All @@ -191,10 +189,7 @@ export default {

if (commitStatus) {
return findings.length === 0
? await postCommitStatus(
'success',
`${files.length * assertionPrototypes.length} checks passed`
)
? await postCommitStatus('success', `${files.length * checks.length} checks passed`)
: await postCommitStatus('failure', `${findings.length} findings`);
}

Expand Down
Loading

0 comments on commit 0f5b95e

Please sign in to comment.