Skip to content

Commit

Permalink
feat(filter): add shell exec filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 28, 2019
1 parent 630ce12 commit 6260946
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/controller/echo-controller.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ data:
kind: slack-listener
name: slack-isolex

filters:
- !include ../docs/filter/shell-filter-github.yml

transforms:
- metadata:
kind: template-transform
Expand Down
11 changes: 11 additions & 0 deletions docs/filter/shell-filter-github.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
metadata:
kind: shell-filter
name: test-shell-github
data:
filters: []
strict: true
# shell command
command: '/home/ssube/code/ssube/isolex/scripts/test-shell-filter.sh'
options:
cwd: ~
env: {}
2 changes: 1 addition & 1 deletion docs/isolex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data:
- !include ../docs/controller/command-controller.yml
- !include ../docs/controller/completion-controller.yml
- !include ../docs/controller/dice-controller.yml
# !include ../docs/controller/echo-controller.yml
- !include ../docs/controller/echo-controller.yml
- !include ../docs/controller/github/commit-controller.yml
- !include ../docs/controller/github/pull-controller.yml
- !include ../docs/controller/github/endpoint/pull-controller.yml
Expand Down
4 changes: 4 additions & 0 deletions scripts/test-shell-filter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cat - >> /tmp/test-filter.log

echo '{"test": "yes world, it is hello"}'
exit 0
65 changes: 65 additions & 0 deletions src/filter/ShellFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { exec } from 'child_process';
import { Writable } from 'stream';
import { promisify } from 'util';

import { FilterBehavior, FilterData, FilterValue } from '.';
import { doesExist, Optional } from '../utils';
import { NameValuePair, Dict } from '../utils/Map';
import { BaseFilter, BaseFilterOptions } from './BaseFilter';

const execPromise = promisify(exec);

export interface ShellFilterData extends FilterData {
command: string;
options: {
cwd: string;
env: Dict<string>; // TODO: should be an Array<NameValuePair<string>>;
timeout: number;
};
}

export class ShellFilter extends BaseFilter<ShellFilterData> {
constructor(options: BaseFilterOptions<ShellFilterData>) {
super(options, 'isolex#/definitions/service-filter-shell');
}

public async check(value: FilterValue): Promise<FilterBehavior> {
const child = execPromise(this.data.command, this.data.options);

if (doesExist(child.child.stdin)) {
await this.writeValue(child.child.stdin, value);
} else {
this.logger.warn('no stdin stream to write filter value');
}

const {
stderr,
stdout,
} = await child;

if (stderr.length > 0) {
this.logger.warn({
stderr,
stdout,
}, 'error from shell command');
return FilterBehavior.Drop;
} else {
return FilterBehavior.Allow;
}
}

public async writeValue(stream: Writable, value: FilterValue): Promise<boolean> {
return new Promise<boolean>((res, rej) => {
const data = JSON.stringify(value);
stream.write(`{"test": "hello world!", "data": ${data}}`, (err: Optional<Error>) => {
if (doesExist(err)) {
rej(err);
} else {
stream.end(() => {
res(true);
});
}
});
});
}
}
2 changes: 2 additions & 0 deletions src/module/FilterModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ModuleOptions } from 'noicejs';

import { CommandFilter } from '../filter/CommandFilter';
import { MessageFilter } from '../filter/MessageFilter';
import { ShellFilter } from '../filter/ShellFilter';
import { UserFilter } from '../filter/UserFilter';
import { BaseModule } from '../module/BaseModule';

Expand All @@ -12,6 +13,7 @@ export class FilterModule extends BaseModule {
// filters
this.bindService(CommandFilter);
this.bindService(MessageFilter);
this.bindService(ShellFilter);
this.bindService(UserFilter);
}
}
27 changes: 27 additions & 0 deletions src/schema/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,33 @@ definitions:
match:
$ref: "#/definitions/match-data"

service-filter-shell:
allOf:
- $ref: "#/definitions/service-filter"
- type: object
additionalProperties: true
required:
- command
- options
properties:
command:
type: string
options:
type: object
additionalProperties: true
properties:
cwd:
type: string
default: ~
env:
type: object
# type: array
# items:
# $ref: "#/definitions/name-value-pair"
timeout:
type: number
default: 0

service-filter-source:
allOf:
- $ref: "#/definitions/service-filter"
Expand Down

0 comments on commit 6260946

Please sign in to comment.