Skip to content

Commit

Permalink
notify stdout to slack
Browse files Browse the repository at this point in the history
Signed-off-by: smallkirby <[email protected]>
  • Loading branch information
smallkirby committed Oct 29, 2023
1 parent 0f1c7f6 commit a0c3574
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func run_test(executer Executer, ch chan<- asyncTestResult, conf CheckerConfig)
killer_chan := make(chan bool)
go executer.ExecuteDockerTest(res_chan, killer_chan, conf)

res := TestResultMessage{ResultRunning, ""}
res := TestResultMessage{ResultRunning, "", ""}

for res.Result == ResultRunning {
select {
Expand Down Expand Up @@ -208,7 +208,7 @@ func RunRecordTests(logger *zap.SugaredLogger, conf CheckerConfig, db *sqlx.DB)
}

if conf.NotifySlack && result.result.Result != ResultSuccess {
slack_notifier.NotifyError(result.executer.chall, result.result.Result, result.result.Errlog)
slack_notifier.NotifyError(result.executer.chall, result.result.Result, result.result.Stdout, result.result.Errlog)
}
}

Expand Down
15 changes: 9 additions & 6 deletions checker/executer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Executer struct {

type TestResultMessage struct {
Result TestResult
Stdout string
Errlog string
}

Expand Down Expand Up @@ -105,7 +106,9 @@ func (e *Executer) ExecuteDockerTest(res_chan chan TestResultMessage, killer_cha
cmd := exec.Command("bash", "-c", fmt.Sprintf("docker run %s --name container_%s --rm $(docker build -qt %s %s) %s %d", conf.ExtraDockerArg, container_name, image_name, chall.SolverDir, chall.target.Host, chall.target.Port))

var errbuf bytes.Buffer
var outbuf bytes.Buffer
cmd.Stderr = &errbuf
cmd.Stdout = &outbuf

// termination signal hook
signal_chan := make(chan os.Signal, 1)
Expand All @@ -115,10 +118,10 @@ func (e *Executer) ExecuteDockerTest(res_chan chan TestResultMessage, killer_cha
res_chan_internal := make(chan error)
if err := cmd.Start(); err != nil {
e.logger.Warnf("[%s] Failed to start test: \n%w", chall.Name, err)
res_chan <- TestResultMessage{ResultFailure, err.Error()}
res_chan <- TestResultMessage{ResultFailure, outbuf.String(), err.Error()}
return
}
res_chan <- TestResultMessage{ResultRunning, ""}
res_chan <- TestResultMessage{ResultRunning, outbuf.String(), errbuf.String()}
e.logger.Infof("[%s] Test started as pid %d in %s.", chall.Name, cmd.Process.Pid, container_name)
go func() {
res_chan_internal <- cmd.Wait()
Expand All @@ -144,13 +147,13 @@ func (e *Executer) ExecuteDockerTest(res_chan chan TestResultMessage, killer_cha
case <-signal_chan:
e.logger.Infof("[%s] Checker process interrupted, cleaning up docker container...", chall.Name)
cleanup_container()
res_chan <- TestResultMessage{ResultTestInterrupted, "Interrupted by signal."}
res_chan <- TestResultMessage{ResultTestInterrupted, "", "Interrupted by signal."}
break
// timeout
case <-killer_chan:
cleanup_container()
e.logger.Infof("[%s] Test timed out.", chall.Name)
res_chan <- TestResultMessage{ResultTimeout, "Timeout."}
res_chan <- TestResultMessage{ResultTimeout, "", "Timeout."}
break
// test finished
case err := <-res_chan_internal:
Expand All @@ -160,12 +163,12 @@ func (e *Executer) ExecuteDockerTest(res_chan chan TestResultMessage, killer_cha
if conf.Vervose {
e.logger.Infof("[%s] stderr: %s", chall.Name, errbuf.String())
}
res_chan <- TestResultMessage{ResultFailure, errbuf.String()}
res_chan <- TestResultMessage{ResultFailure, outbuf.String(), errbuf.String()}
}
} else {
// test ends without any failure
e.logger.Infof("[%s] exits with status code 0.", chall.Name)
res_chan <- TestResultMessage{ResultSuccess, ""}
res_chan <- TestResultMessage{ResultSuccess, "", ""}
}
break
}
Expand Down
5 changes: 3 additions & 2 deletions checker/slacknotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ func NewSlackNotifier(token string, channel string, logger *zap.SugaredLogger) *
}
}

func (s *SlackNotifier) NotifyError(chall Challenge, result TestResult, errlog string) error {
func (s *SlackNotifier) NotifyError(chall Challenge, result TestResult, stdout string, errlog string) error {
args := slack.PostMessageParameters{
Username: "TSGCTF Status",
IconEmoji: ":fire:",
Markdown: true,
}
stdout = fmt.Sprintf("```\n%s\n```", stdout)
errlog = fmt.Sprintf("```\n%s\n```", errlog)
msg := fmt.Sprintf("Status check failed for `%s`\n"+"Result: `%s`\n"+"Asignee: <@%s>\n%s", chall.Name, result.ToMessage(), chall.Assignee, errlog)
msg := fmt.Sprintf("Status check failed for `%s`\n"+"Result: `%s`\n"+"Asignee: <@%s>\nSTDOUT:\n%sSTDERR:\n%s", chall.Name, result.ToMessage(), chall.Assignee, stdout, errlog)

_, _, err := s.api.PostMessage(s.channel, slack.MsgOptionText(msg, false), slack.MsgOptionPostMessageParameters(args))
if err != nil {
Expand Down

0 comments on commit a0c3574

Please sign in to comment.