Skip to content

Commit

Permalink
Single signal reason for verification failure
Browse files Browse the repository at this point in the history
  • Loading branch information
moskyb committed Aug 4, 2023
1 parent 773277f commit aaaeaaf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
8 changes: 4 additions & 4 deletions agent/integration/job_verification_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestJobVerification(t *testing.T) {
job: jobWithInvalidSignature,
mockBootstrapExpectation: func(bt *bintest.Mock) { bt.Expect().NotCalled() },
expectedExitStatus: "-1",
expectedSignalReason: "job_verification_failed_invalid_signature",
expectedSignalReason: agent.SignalReasonVerificationFailed,
expectLogsContain: []string{"⚠️ ERROR"},
},
{
Expand All @@ -127,7 +127,7 @@ func TestJobVerification(t *testing.T) {
job: jobWithNoSignature,
mockBootstrapExpectation: func(bt *bintest.Mock) { bt.Expect().NotCalled() },
expectedExitStatus: "-1",
expectedSignalReason: "job_verification_failed_no_signature",
expectedSignalReason: agent.SignalReasonVerificationFailed,
expectLogsContain: []string{"⚠️ ERROR"},
},
{
Expand All @@ -144,7 +144,7 @@ func TestJobVerification(t *testing.T) {
job: jobWithMismatchedStepAndJob,
mockBootstrapExpectation: func(bt *bintest.Mock) { bt.Expect().NotCalled() },
expectedExitStatus: "-1",
expectedSignalReason: "job_verification_failed_invalid_signature",
expectedSignalReason: agent.SignalReasonVerificationFailed,
expectLogsContain: []string{
"⚠️ ERROR",
fmt.Sprintf(`the value of field "command" on the job (%q) does not match the value of the field on the step (%q)`,
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestWhenTheJobHasASignature_ButTheJobRunnerCantVerify_ItRefusesTheJob(t *te
}
}

if got, want := finish.SignalReason, "job_verification_failed_with_error"; got != want {
if got, want := finish.SignalReason, agent.SignalReasonVerificationFailed; got != want {
t.Errorf("job.SignalReason = %q, want %q", got, want)
}
}
24 changes: 16 additions & 8 deletions agent/run_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ import (
"github.com/buildkite/roko"
)

const (
SignalReasonAgentRefused = "agent_refused"
SignalReasonAgentStop = "agent_stop"
SignalReasonCancel = "cancel"
SignalReasonVerificationFailed = "verification_failed"
SignalReasonProcessRunError = "process_run_error"
)

// Runs the job
func (r *JobRunner) Run(ctx context.Context) error {
r.logger.Info("Starting job %s", r.conf.Job.ID)
Expand Down Expand Up @@ -87,7 +95,7 @@ func (r *JobRunner) Run(ctx context.Context) error {
VerificationBehaviourBlock,
)
exit.Status = -1
exit.SignalReason = "job_verification_failed_with_error"
exit.SignalReason = SignalReasonVerificationFailed
return nil
}

Expand All @@ -98,22 +106,22 @@ func (r *JobRunner) Run(ctx context.Context) error {
r.verificationFailureLogs(err, r.NoSignatureBehavior)
if r.NoSignatureBehavior == VerificationBehaviourBlock {
exit.Status = -1
exit.SignalReason = "job_verification_failed_no_signature"
exit.SignalReason = SignalReasonVerificationFailed
return nil
}

case errors.As(err, &ise):
r.verificationFailureLogs(err, r.InvalidSignatureBehavior)
if r.InvalidSignatureBehavior == VerificationBehaviourBlock {
exit.Status = -1
exit.SignalReason = "job_verification_failed_invalid_signature"
exit.SignalReason = SignalReasonVerificationFailed
return nil
}

case err != nil: // some other error
r.verificationFailureLogs(err, VerificationBehaviourBlock) // errors in verification are always fatal
exit.Status = -1
exit.SignalReason = "job_verification_failed_with_error"
exit.SignalReason = SignalReasonVerificationFailed
return nil

default: // no error, all good, keep going
Expand All @@ -134,7 +142,7 @@ func (r *JobRunner) Run(ctx context.Context) error {
r.logger.Error("pre-bootstrap hook rejected this job: %s", err)

exit.Status = -1
exit.SignalReason = "agent_refused"
exit.SignalReason = SignalReasonAgentRefused

return nil
}
Expand Down Expand Up @@ -166,7 +174,7 @@ func (r *JobRunner) runJob(ctx context.Context) processExit {
// The process did not run at all, so make sure it fails
return processExit{
Status: -1,
SignalReason: "process_run_error",
SignalReason: SignalReasonProcessRunError,
}
}
// Intended to capture situations where the job-exec (aka bootstrap) container did not
Expand All @@ -193,11 +201,11 @@ func (r *JobRunner) runJob(ctx context.Context) processExit {
case r.stopped:
// The agent is being gracefully stopped, and we signaled the job to end. Often due
// to pending host shutdown or EC2 spot instance termination
exit.SignalReason = "agent_stop"
exit.SignalReason = SignalReasonAgentStop

case r.cancelled:
// The job was signaled because it was cancelled via the buildkite web UI
exit.SignalReason = "cancel"
exit.SignalReason = SignalReasonCancel
}

return exit
Expand Down

0 comments on commit aaaeaaf

Please sign in to comment.