-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters