From 3240190746917f439e9d0e58f2cc95749e8b102d Mon Sep 17 00:00:00 2001 From: Miguel Martinez Trivino Date: Wed, 2 Dec 2026 23:57:33 +0100 Subject: [PATCH] fix(cli): validate attestation event input like the other commands chainloop attestation event now fails on a wrong call: --step and --status are required flags, positional arguments are rejected, and an invalid step or status is an error. A failure to record the event returns a graceful error, as in the other attestation commands, so --graceful-exit turns it into exit 0. The --current and --total progress flags follow the same rule: they are integer flags that cobra requires together, and an invalid progress fails before the event is sent. The step and the progress are checked with the proto rules of the control plane, so the CLI keeps no copy of them. Supersedes R-013 with R-016 and R-015 with R-017, and records D-016 in the spec of #3825. Assisted-by: Claude Code Signed-off-by: Miguel Martinez Trivino Chainloop-Trace-Sessions: d7a81b32-8204-49c1-ac44-d405416d90a0 --- app/cli/cmd/attestation_event.go | 88 +++++++------------ app/cli/cmd/attestation_event_test.go | 74 +++++++++------- app/cli/documentation/cli-reference.md | 6 +- app/cli/pkg/action/attestation_event.go | 4 +- app/cli/pkg/action/runevents.go | 19 ++-- .../issue-3825-attestation-progress-events.md | 34 +++++-- 6 files changed, 119 insertions(+), 106 deletions(-) diff --git a/app/cli/cmd/attestation_event.go b/app/cli/cmd/attestation_event.go index 6a15d90f9..ec2ef50b8 100644 --- a/app/cli/cmd/attestation_event.go +++ b/app/cli/cmd/attestation_event.go @@ -16,9 +16,7 @@ package cmd import ( - "errors" "fmt" - "strconv" "strings" "github.com/chainloop-dev/chainloop/app/cli/pkg/action" @@ -27,7 +25,10 @@ import ( ) func newAttestationEventCmd() *cobra.Command { - var step, status, message, current, total string + var ( + step, status, message string + current, total int64 + ) cmd := &cobra.Command{ Use: "event", @@ -39,7 +40,7 @@ Events are progress information only. They are not part of the signed attestatio The step uses letters, digits, "_" and "-", up to 64 characters. Events with the same step are one step of the timeline. To show a material in the step that adds it, use the same step in "chainloop attestation add --step". A long step can report its progress with --current and --total, for example 240 of 500 commits, in repeated started events. -If the event cannot be recorded, the command shows a warning and exits with success, so it never breaks the pipeline.`, +If the event cannot be recorded, the command fails like the other attestation commands. Use --graceful-exit to exit with success instead.`, Example: ` # Record the start and the end of a step chainloop attestation event --step sast-scan --status started --message "Semgrep, ruleset p/ci" chainloop attestation add --name sast-report --value report.sarif --step sast-scan @@ -51,49 +52,45 @@ If the event cannot be recorded, the command shows a warning and exits with succ # Record a step that did not run chainloop attestation event --step dast-scan --status skipped --message "no web target"`, - // the arguments are checked in RunE, so that a wrong call is a warning too - Args: cobra.ArbitraryArgs, + Args: cobra.NoArgs, Annotations: map[string]string{ useAPIToken: trueString, supportsFederatedAuthAnnotation: trueString, }, - RunE: func(cmd *cobra.Command, args []string) error { - // a progress signal never breaks the pipeline (spec issue-3825 R-013), so a failure to - // record the event, and a wrong argument or flag value, is a warning and the command - // exits with success. An unknown flag, as the removed --name, fails in cobra before - // RunE (spec issue-3878 R-003). - if len(args) > 0 { - logger.Warn().Msgf("the event was not recorded: unexpected arguments %q, set the step with --step", args) - return nil - } - if step == "" { - logger.Warn().Msg("the event was not recorded: --step is required") - return nil + RunE: func(cmd *cobra.Command, _ []string) error { + // the values are checked here and not in PreRunE: cobra checks the + // required flags after PreRunE, and a missing flag must show its own error + if err := action.ValidateRunEventStep(step); err != nil { + return err } st, err := action.ParseRunEventStatus(status) if err != nil { - logger.Warn().Err(err).Msg("the event was not recorded") - return nil + return err } - progress, err := parseRunEventProgress(cmd.Flags().Changed("current"), cmd.Flags().Changed("total"), current, total) - if err != nil { - logger.Warn().Err(err).Msg("the event was not recorded") - return nil + // cobra already requires --current and --total together + var progress *pb.WorkflowRunEvent_Progress + if cmd.Flags().Changed("current") { + progress = &pb.WorkflowRunEvent_Progress{Current: current, Total: total} + if err := action.ValidateRunEventProgress(progress); err != nil { + return err + } } a := action.NewAttestationEvent(&action.AttestationEventOpts{ActionsOpts: ActionOpts, LocalStatePath: attestationLocalStatePath}) recorded, err := a.Run(cmd.Context(), attestationID, step, st, message, progress) - switch { - case err != nil: - logger.Warn().Err(err).Msg("the event was not recorded") - case !recorded: + if err != nil { + // as in the other attestation commands, --graceful-exit turns the failure into exit 0 + return newGracefulError(err) + } + + if !recorded { logger.Info().Msg("dry run, the event was not recorded") - default: - logger.Info().Msg("event recorded") + return nil } + logger.Info().Msg("event recorded") return nil }, } @@ -101,33 +98,12 @@ If the event cannot be recorded, the command shows a warning and exits with succ cmd.Flags().StringVar(&step, "step", "", "key of the step (required), with letters, digits, _ and -, up to 64 characters") cmd.Flags().StringVar(&status, "status", "", fmt.Sprintf("status of the step (required), valid options are %s", strings.Join(action.RunEventStatuses, ", "))) cmd.Flags().StringVar(&message, "message", "", "message of the event, up to 1 KB") - // string flags, so that a value that is not a number is a warning and not a failure - cmd.Flags().StringVar(¤t, "current", "", "progress of the step: the work done so far, from 0 to --total. Requires --total") - cmd.Flags().StringVar(&total, "total", "", "progress of the step: the total work, more than 0. Requires --current") + cmd.Flags().Int64Var(¤t, "current", 0, "progress of the step: the work done so far, from 0 to --total. Requires --total") + cmd.Flags().Int64Var(&total, "total", 0, "progress of the step: the total work, more than 0. Requires --current") + cobra.CheckErr(cmd.MarkFlagRequired("step")) + cobra.CheckErr(cmd.MarkFlagRequired("status")) + cmd.MarkFlagsRequiredTogether("current", "total") flagAttestationID(cmd) return cmd } - -// parseRunEventProgress returns the progress of the flags, or nil when neither is set. A flag -// that is set with an empty value is set, so its value is wrong. The action checks the values -// with the rules of the control plane. -func parseRunEventProgress(currentSet, totalSet bool, current, total string) (*pb.WorkflowRunEvent_Progress, error) { - if !currentSet && !totalSet { - return nil, nil - } - if !currentSet || !totalSet { - return nil, errors.New("set both --current and --total, or neither") - } - - c, err := strconv.ParseInt(current, 10, 64) - if err != nil { - return nil, fmt.Errorf("invalid --current %q: %w", current, err) - } - t, err := strconv.ParseInt(total, 10, 64) - if err != nil { - return nil, fmt.Errorf("invalid --total %q: %w", total, err) - } - - return &pb.WorkflowRunEvent_Progress{Current: c, Total: t}, nil -} diff --git a/app/cli/cmd/attestation_event_test.go b/app/cli/cmd/attestation_event_test.go index 2d4883e46..b23de5753 100644 --- a/app/cli/cmd/attestation_event_test.go +++ b/app/cli/cmd/attestation_event_test.go @@ -18,9 +18,11 @@ package cmd import ( "bytes" "context" + "errors" "io" "net" "path/filepath" + "strings" "sync" "testing" @@ -60,19 +62,22 @@ func (f *eventsControlPlane) received() []*pb.AttestationServiceRecordEventReque return append([]*pb.AttestationServiceRecordEventRequest(nil), f.events...) } -// The failures of chainloop attestation event are warnings, and the command exits with success. -// The command has no --name flag, so a call with it fails as an unknown flag (spec issue-3878 R-003). +// chainloop attestation event fails like the other attestation commands (spec issue-3825 R-014). +// A wrong call is a usage error. A failure to record the event is a graceful error, so +// --graceful-exit can turn it into exit 0. The command has no --name flag, so a call with it +// fails as an unknown flag (spec issue-3878 R-003). func TestAttestationEventCmd(t *testing.T) { const ( runID = "4f8d2b7e-8c1b-4b1e-9f3a-2a6f1b2c3d4e" - invalidStatus = "invalid status" stepFlag = "--step" statusFlag = "--status" started = "started" sastScan = "sast-scan" + invalidKey = "it must contain only letters, digits, _ and -, up to 64 characters" currentFlag = "--current" totalFlag = "--total" - bothFlags = "set both --current and --total, or neither" + eventRecorded = "event recorded" + bothFlags = "if any flags in the group [current total] are set they must all be set" ) // remote selects remote state, so no local state is needed remote := func(args ...string) []string { return append(args, "--attestation-id", runID) } @@ -82,47 +87,58 @@ func TestAttestationEventCmd(t *testing.T) { args []string cpErr error wantSent bool - wantKey string // wantProgress is the current and total of the sent event, nil for no progress wantProgress []int64 - wantWarn string wantInfo string wantErr string + wantGraceful bool }{ { name: "recorded", args: remote(stepFlag, sastScan, statusFlag, started, "--message", "Semgrep"), - wantSent: true, wantKey: sastScan, wantInfo: "event recorded", + wantSent: true, wantInfo: eventRecorded, }, + {name: "skipped", args: remote(stepFlag, sastScan, statusFlag, "skipped"), wantSent: true, wantInfo: eventRecorded}, + // a failure to record the event is a graceful error, as in the other attestation commands { - name: "rejected by the control plane", args: remote(stepFlag, "sast.scan", statusFlag, started), - cpErr: status.Error(codes.InvalidArgument, "custom event key"), wantSent: true, wantKey: "sast.scan", wantWarn: "custom event key", + name: "rejected by the control plane", args: remote(stepFlag, sastScan, statusFlag, started), + cpErr: status.Error(codes.InvalidArgument, "too many events"), wantSent: true, + wantErr: "too many events", wantGraceful: true, }, { name: "control plane without custom events", args: remote(stepFlag, sastScan, statusFlag, started), - cpErr: status.Error(codes.Unimplemented, "unknown method"), wantSent: true, wantKey: sastScan, wantWarn: "unknown method", + cpErr: status.Error(codes.Unimplemented, "unknown method"), wantSent: true, + wantErr: "unknown method", wantGraceful: true, }, - {name: "no attestation in progress", args: []string{stepFlag, sastScan, statusFlag, started}, wantWarn: "not yet initialized"}, - {name: "invalid status", args: remote(stepFlag, sastScan, statusFlag, "done"), wantWarn: invalidStatus}, - {name: "missing status", args: remote(stepFlag, sastScan), wantWarn: invalidStatus}, - {name: "missing step", args: remote(statusFlag, started), wantWarn: "--step is required"}, + { + name: "no attestation in progress", args: []string{stepFlag, sastScan, statusFlag, started}, + wantErr: "not yet initialized", wantGraceful: true, + }, + // a wrong call is a usage error, as in the other commands + {name: "invalid status", args: remote(stepFlag, sastScan, statusFlag, "done"), wantErr: `invalid status "done", valid options are`}, + {name: "missing status", args: remote(stepFlag, sastScan), wantErr: `required flag(s) "status" not set`}, + {name: "missing step", args: remote(statusFlag, started), wantErr: `required flag(s) "step" not set`}, + {name: "missing step and status", args: remote(), wantErr: `required flag(s) "status", "step" not set`}, + {name: "invalid step", args: remote(stepFlag, "sast.scan", statusFlag, started), wantErr: `invalid step "sast.scan", ` + invalidKey}, + {name: "step too long", args: remote(stepFlag, strings.Repeat("a", 65), statusFlag, started), wantErr: invalidKey}, {name: "the removed --name flag", args: remote("--name", sastScan, statusFlag, started), wantErr: "unknown flag: --name"}, - {name: "positional argument", args: remote(sastScan, statusFlag, started), wantWarn: "unexpected arguments"}, + {name: "positional argument", args: remote(sastScan, stepFlag, sastScan, statusFlag, started), wantErr: `unknown command "sast-scan"`}, { name: "progress", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "240", totalFlag, "500"), - wantSent: true, wantKey: sastScan, wantProgress: []int64{240, 500}, wantInfo: "event recorded", + wantSent: true, wantProgress: []int64{240, 500}, wantInfo: eventRecorded, }, { name: "progress at zero", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "0", totalFlag, "500"), - wantSent: true, wantKey: sastScan, wantProgress: []int64{0, 500}, wantInfo: "event recorded", + wantSent: true, wantProgress: []int64{0, 500}, wantInfo: eventRecorded, }, - {name: "empty current and total", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag+"=", totalFlag+"="), wantWarn: "invalid --current"}, - {name: "current with no total", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "240"), wantWarn: bothFlags}, - {name: "total with no current", args: remote(stepFlag, sastScan, statusFlag, started, totalFlag, "500"), wantWarn: bothFlags}, - {name: "current that is not a number", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "many", totalFlag, "500"), wantWarn: "invalid --current"}, - {name: "total that is not a number", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "1", totalFlag, "1.5"), wantWarn: "invalid --total"}, - {name: "current over total", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "501", totalFlag, "500"), wantWarn: "current must not be greater than total"}, - {name: "zero total", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "0", totalFlag, "0"), wantWarn: "invalid progress"}, - {name: "negative current", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "-1", totalFlag, "500"), wantWarn: "invalid progress"}, + // a wrong progress is a usage error too + {name: "empty current and total", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag+"=", totalFlag+"="), wantErr: `invalid argument "" for "--current"`}, + {name: "current with no total", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "240"), wantErr: bothFlags}, + {name: "total with no current", args: remote(stepFlag, sastScan, statusFlag, started, totalFlag, "500"), wantErr: bothFlags}, + {name: "current that is not a number", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "many", totalFlag, "500"), wantErr: `invalid argument "many" for "--current"`}, + {name: "total that is not a number", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "1", totalFlag, "1.5"), wantErr: `invalid argument "1.5" for "--total"`}, + {name: "current over total", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "501", totalFlag, "500"), wantErr: "current must not be greater than total"}, + {name: "zero total", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "0", totalFlag, "0"), wantErr: "invalid progress"}, + {name: "negative current", args: remote(stepFlag, sastScan, statusFlag, started, currentFlag, "-1", totalFlag, "500"), wantErr: "invalid progress"}, } for _, tc := range testCases { @@ -138,6 +154,7 @@ func TestAttestationEventCmd(t *testing.T) { err := cmd.Execute() if tc.wantErr != "" { require.ErrorContains(t, err, tc.wantErr) + assert.Equal(t, tc.wantGraceful, errors.As(err, &GracefulError{}), "graceful error") } else { require.NoError(t, err) } @@ -149,7 +166,7 @@ func TestAttestationEventCmd(t *testing.T) { require.Len(t, got, 1) assert.Equal(t, runID, got[0].GetWorkflowRunId()) assert.Equal(t, pb.WorkflowRunEvent_TYPE_CUSTOM, got[0].GetEvent().GetType()) - assert.Equal(t, tc.wantKey, got[0].GetEvent().GetKey()) + assert.Equal(t, sastScan, got[0].GetEvent().GetKey()) assert.Nil(t, got[0].GetEvent().GetAttributes()) if tc.wantProgress == nil { assert.Nil(t, got[0].GetEvent().GetProgress()) @@ -160,12 +177,7 @@ func TestAttestationEventCmd(t *testing.T) { } } - if tc.wantWarn != "" { - assert.Contains(t, out.String(), `"level":"warn"`) - assert.Contains(t, out.String(), tc.wantWarn) - } if tc.wantInfo != "" { - assert.NotContains(t, out.String(), `"level":"warn"`) assert.Contains(t, out.String(), tc.wantInfo) } }) diff --git a/app/cli/documentation/cli-reference.md b/app/cli/documentation/cli-reference.md index a1673edf0..8589e52c9 100644 --- a/app/cli/documentation/cli-reference.md +++ b/app/cli/documentation/cli-reference.md @@ -320,7 +320,7 @@ Events are progress information only. They are not part of the signed attestatio The step uses letters, digits, "_" and "-", up to 64 characters. Events with the same step are one step of the timeline. To show a material in the step that adds it, use the same step in "chainloop attestation add --step". A long step can report its progress with --current and --total, for example 240 of 500 commits, in repeated started events. -If the event cannot be recorded, the command shows a warning and exits with success, so it never breaks the pipeline. +If the event cannot be recorded, the command fails like the other attestation commands. Use --graceful-exit to exit with success instead. ``` chainloop attestation event [flags] @@ -346,12 +346,12 @@ Options ``` --attestation-id string Unique identifier of the in-progress attestation ---current string progress of the step: the work done so far, from 0 to --total. Requires --total +--current int progress of the step: the work done so far, from 0 to --total. Requires --total -h, --help[=true|false] help for event --message string message of the event, up to 1 KB --status string status of the step (required), valid options are started, succeeded, failed, canceled, skipped --step string key of the step (required), with letters, digits, _ and -, up to 64 characters ---total string progress of the step: the total work, more than 0. Requires --current +--total int progress of the step: the total work, more than 0. Requires --current ``` Options inherited from parent commands diff --git a/app/cli/pkg/action/attestation_event.go b/app/cli/pkg/action/attestation_event.go index 6ff6ab20b..5793adde3 100644 --- a/app/cli/pkg/action/attestation_event.go +++ b/app/cli/pkg/action/attestation_event.go @@ -77,7 +77,7 @@ func NewAttestationEvent(cfg *AttestationEventOpts) *AttestationEvent { // Run records the event, with the progress of the step when it is not nil. It returns false, // with no error, when the attestation is a dry run, which has no run in the control plane. -// The event is informational only: the caller must not fail the pipeline on its error. +// The command returns the error to the user, see spec issue-3825 R-016. func (action *AttestationEvent) Run(ctx context.Context, attestationID, key string, st pb.WorkflowRunEvent_Status, message string, progress *pb.WorkflowRunEvent_Progress) (bool, error) { event := &pb.WorkflowRunEvent{ Type: pb.WorkflowRunEvent_TYPE_CUSTOM, @@ -87,7 +87,7 @@ func (action *AttestationEvent) Run(ctx context.Context, attestationID, key stri } if progress != nil { // check the values before anything else, with the rules of the control plane - if err := validateRunEventProgress(progress); err != nil { + if err := ValidateRunEventProgress(progress); err != nil { return false, err } event.Progress = progress diff --git a/app/cli/pkg/action/runevents.go b/app/cli/pkg/action/runevents.go index 6c5487c60..aa9aacf63 100644 --- a/app/cli/pkg/action/runevents.go +++ b/app/cli/pkg/action/runevents.go @@ -64,14 +64,19 @@ const ( runEventKeyMaterialPrefix = runEventKeyMaterial + "." ) -// validateRunEventStepKey checks a step with the rules of the control plane for the step -// of a material event: the format and the limit of a custom key -func validateRunEventStepKey(step string) error { - return protovalidate.Validate(&pb.WorkflowRunEvent_MaterialAttributes{Step: step}) +// ValidateRunEventStep checks a step with the rules of the control plane for the step +// of a material event: the format and the limit of a custom key. The rule is the one of +// the API, so the CLI does not keep its own copy. +func ValidateRunEventStep(step string) error { + if err := protovalidate.Validate(&pb.WorkflowRunEvent_MaterialAttributes{Step: step}); err != nil { + return fmt.Errorf("invalid step %q, it must contain only letters, digits, _ and -, up to 64 characters", step) + } + + return nil } -// validateRunEventProgress checks the progress of a custom event with the rules of the control plane -func validateRunEventProgress(p *pb.WorkflowRunEvent_Progress) error { +// ValidateRunEventProgress checks the progress of a custom event with the rules of the control plane +func ValidateRunEventProgress(p *pb.WorkflowRunEvent_Progress) error { if err := protovalidate.Validate(p); err != nil { return fmt.Errorf("invalid progress: %w", err) } @@ -99,7 +104,7 @@ func (r *runEventRecorder) withStep(step string) *runEventRecorder { if step == "" { return r } - if err := validateRunEventStepKey(step); err != nil { + if err := ValidateRunEventStep(step); err != nil { r.logger.Warn().Err(err).Str("step", step).Msg("the material is added with no step") return r } diff --git a/docs/specs/issue-3825-attestation-progress-events.md b/docs/specs/issue-3825-attestation-progress-events.md index c69102750..37fd4c8d4 100644 --- a/docs/specs/issue-3825-attestation-progress-events.md +++ b/docs/specs/issue-3825-attestation-progress-events.md @@ -121,17 +121,29 @@ Superseded by R-013. ~~Supersedes R-009. `chainloop attestation event --name --status --message ` MUST record a custom event on the run of the attestation in progress. The command takes the key with the `--name` flag, not as a positional argument, as `chainloop attestation add --name` does. The status is started, succeeded, failed, or canceled. The command finds the run in the same way as the other attestation commands. A custom key MUST use the format of annotation names: letters, digits, `_`, and `-`. A custom key cannot hold a dot, so it never matches an automatic `chainloop.` key. When the command cannot record the event, it shows a warning and exits with success. This also applies to a wrong call, for example a missing `--name`, and to an older control plane. A progress signal never breaks a pipeline.~~ - ~~Done when: `chainloop attestation event --name sast-scan --status started` between `init` and `push` shows the step in `describe`. Each failure shows a warning, and the command exits with 0.~~ -### R-013: Custom events, with a skipped status -Supersedes R-012. `chainloop attestation event --name --status --message ` MUST record a custom event on the run of the attestation in progress. The command takes the key with the `--name` flag, not as a positional argument, as `chainloop attestation add --name` does. The status is started, succeeded, failed, canceled, or skipped. Skipped is a step that did not run, for example a scan with no target. Only custom events accept the skipped status (D-013). The command finds the run in the same way as the other attestation commands. A custom key MUST use the format of annotation names: letters, digits, `_`, and `-`. A custom key cannot hold a dot, so it never matches an automatic `chainloop.` key. When the command cannot record the event, it shows a warning and exits with success. This also applies to a wrong call, for example a missing `--name`, and to an older control plane. A progress signal never breaks a pipeline. -- Done when: `chainloop attestation event --name sast-scan --status started` between `init` and `push` shows the step in `describe`. `chainloop attestation event --name dast-scan --status skipped` shows the step as skipped in `describe`. Each failure shows a warning, and the command exits with 0. +### ~~R-013: Custom events, with a skipped status~~ +Superseded by R-016. + +~~Supersedes R-012. `chainloop attestation event --name --status --message ` MUST record a custom event on the run of the attestation in progress. The command takes the key with the `--name` flag, not as a positional argument, as `chainloop attestation add --name` does. The status is started, succeeded, failed, canceled, or skipped. Skipped is a step that did not run, for example a scan with no target. Only custom events accept the skipped status (D-013). The command finds the run in the same way as the other attestation commands. A custom key MUST use the format of annotation names: letters, digits, `_`, and `-`. A custom key cannot hold a dot, so it never matches an automatic `chainloop.` key. When the command cannot record the event, it shows a warning and exits with success. This also applies to a wrong call, for example a missing `--name`, and to an older control plane. A progress signal never breaks a pipeline.~~ +- ~~Done when: `chainloop attestation event --name sast-scan --status started` between `init` and `push` shows the step in `describe`. `chainloop attestation event --name dast-scan --status skipped` shows the step as skipped in `describe`. Each failure shows a warning, and the command exits with 0.~~ ### R-014: Run events are kept for the life of the run Supersedes R-007. The control plane MUST keep the events of a run for as long as it keeps the run. It deletes the events only when it deletes the run. No periodic job deletes events, and the control plane configuration has no retention setting (D-014). A configuration that still sets the removed `workflow_run_events_retention` MUST load, so that an upgrade does not break the control plane. - Done when: the events of a run that finished more than 90 days ago are still returned by the read call (R-011). A control plane with `workflow_run_events_retention` in its configuration starts. -### R-015: Progress values on custom events -A custom event MAY have progress values: a current value and a total, for example 240 of 500 commits. `chainloop attestation event --step --current --total ` MUST record them in the progress of the event (D-015). The command takes the key with `--step`, which replaced `--name` (R-003 of spec issue-3878). The command needs both flags or neither. A flag with an empty value is a wrong value. The control plane and the CLI MUST reject progress values unless the current value is from 0 to the total, and the total is more than 0. Only custom events accept progress values. A long step reports its progress with repeated `started` events of its key. There is no new status. A wrong value is a wrong call: the command shows a warning, sends nothing, and exits with success (R-013). When the last event of a custom step has progress values, `chainloop workflow run describe` MUST show them on the row of the step as `/`. Below the step, it shows only the last of consecutive progress updates. A progress update is a `started` event with progress values that is not the first event of the step. -- Done when: `chainloop attestation event --step classify-commits --status started --current 240 --total 500` shows the step row ``step `classify-commits` running for 1m20s · 240/500`` in `describe`. `--current 501 --total 500`, or `--current 1` with no `--total`, shows a warning, records nothing, and exits with 0. +### ~~R-015: Progress values on custom events~~ +Superseded by R-017. + +~~A custom event MAY have progress values: a current value and a total, for example 240 of 500 commits. `chainloop attestation event --step --current --total ` MUST record them in the progress of the event (D-015). The command takes the key with `--step`, which replaced `--name` (R-003 of spec issue-3878). The command needs both flags or neither. A flag with an empty value is a wrong value. The control plane and the CLI MUST reject progress values unless the current value is from 0 to the total, and the total is more than 0. Only custom events accept progress values. A long step reports its progress with repeated `started` events of its key. There is no new status. A wrong value is a wrong call: the command shows a warning, sends nothing, and exits with success (R-013). When the last event of a custom step has progress values, `chainloop workflow run describe` MUST show them on the row of the step as `/`. Below the step, it shows only the last of consecutive progress updates. A progress update is a `started` event with progress values that is not the first event of the step.~~ +- ~~Done when: `chainloop attestation event --step classify-commits --status started --current 240 --total 500` shows the step row ``step `classify-commits` running for 1m20s · 240/500`` in `describe`. `--current 501 --total 500`, or `--current 1` with no `--total`, shows a warning, records nothing, and exits with 0.~~ + +### R-016: Custom events fail like the other attestation commands +Supersedes R-013. `chainloop attestation event --step --status --message ` MUST record a custom event on the run of the attestation in progress. The command takes the key with the `--step` flag (R-003 of spec issue-3878). The status is started, succeeded, failed, canceled, or skipped. Skipped is a step that did not run, for example a scan with no target. Only custom events accept the skipped status (D-013). The command finds the run in the same way as the other attestation commands. A custom key MUST use the format of annotation names: letters, digits, `_`, and `-`, with at most 64 characters. A custom key cannot hold a dot, so it never matches an automatic `chainloop.` key. The command fails in the same way as the other attestation commands (D-016). The CLI MUST check the call before it sends the event: `--step` and `--status` are required flags, a positional argument is an error, and an invalid key or status is an error. When the command cannot record the event, for example with no attestation in progress or against an older control plane, it fails. With `--graceful-exit`, it exits with success, as `init` and `push` do. The best-effort rule of R-002 applies only to the automatic events. +- Done when: `chainloop attestation event --step sast-scan --status started` between `init` and `push` shows the step in `describe`. `chainloop attestation event --step dast-scan --status skipped` shows the step as skipped in `describe`. A call with no `--status`, or with an invalid key, fails before it sends the event. A call that the control plane cannot record fails, and exits with 0 with `--graceful-exit`. + +### R-017: Progress values on custom events, checked as a wrong call +Supersedes R-015. A custom event MAY have progress values: a current value and a total, for example 240 of 500 commits. `chainloop attestation event --step --current --total ` MUST record them in the progress of the event (D-015). The command takes the key with `--step`, which replaced `--name` (R-003 of spec issue-3878). The command needs both flags or neither. The flags are integers, and a flag with an empty value or a value that is not an integer is a wrong value. The control plane and the CLI MUST reject progress values unless the current value is from 0 to the total, and the total is more than 0. Only custom events accept progress values. A long step reports its progress with repeated `started` events of its key. There is no new status. A wrong value is a wrong call: the command fails before it sends the event, as for an invalid key or status (R-016). When the last event of a custom step has progress values, `chainloop workflow run describe` MUST show them on the row of the step as `/`. Below the step, it shows only the last of consecutive progress updates. A progress update is a `started` event with progress values that is not the first event of the step. +- Done when: `chainloop attestation event --step classify-commits --status started --current 240 --total 500` shows the step row ``step `classify-commits` running for 1m20s · 240/500`` in `describe`. `--current 501 --total 500`, or `--current 1` with no `--total`, fails before it sends the event. ## Constraints @@ -302,6 +314,14 @@ Supersedes the Non-goal "custom attributes on custom events". A run event has an - A free key-value map on custom events: no check of the content (D-006). - Source: PFM-8434 +### D-016: The custom event command fails like the other attestation commands +The custom event command checks its flags as the other commands do, and returns its errors as the other attestation commands do (R-016, R-017). A missing flag, a positional argument, or an invalid key, status, or progress fails the command before it sends the event. A failure to record the event also fails the command, and `--graceful-exit` turns it into exit 0. The best-effort rule of R-002 stays for the automatic events of `init` and `add`. +- Why: the user calls the command on purpose, so it is not a side signal of another action. As a warning, a wrong call showed as a log field (`WRN the event was not recorded error="invalid status \"\", ..."`), and the pipeline passed with no step in the timeline. The mistake stayed hidden. An error shows the problem the first time that the pipeline runs, in the same form as the other commands. A pipeline that must never fail on the command can set `--graceful-exit`, as for the other attestation commands. +- Alternatives considered: + - Keep every failure as a warning (R-013, R-015): the pipeline never breaks, but a mistake that is always wrong stays hidden, and the command behaves unlike the other attestation commands. + - Fail on a wrong call, and warn when a valid event cannot be recorded: still unlike the other attestation commands, and `--graceful-exit` already gives the best-effort behavior. +- Source: implementation review of #3825 + ## Open Questions None. @@ -309,7 +329,7 @@ None. ## Milestones 1. **Automatic progress.** Automatic events (R-001, R-002, R-005, R-006), the read call (R-003), `describe` for runs in progress (R-004), retention (R-007), and limits (R-008). R-010 supersedes R-001, R-011 supersedes R-003, and R-014 supersedes R-007. -2. **Custom events.** `chainloop attestation event` (R-009). R-012 supersedes R-009. R-015 adds progress values. +2. **Custom events.** `chainloop attestation event` (R-009). R-012 supersedes R-009. R-013 supersedes R-012, and R-016 supersedes R-013. R-015 adds progress values, and R-017 supersedes R-015. ## Risks