feat(autoflow): add autoflow workflow commands

Description

This adds glab autoflow workflow start, get, cancel and send, which talk to AutoFlow through Relay's public API. It's the client side of the short-term solution in gitlab!267945 (merged): GitLab mints a gitlab-autoflow token through the token exchange API for users with the token_exchange_autoflow_audience feature flag, and glab presents it to Relay. It's meant to replace the kas autoflow client (gitlab-org/cluster-integration/gitlab-agent!4876 (closed)).

All four commands:

  • exchange your GitLab credential for a one-minute gitlab-autoflow token, and exchange a fresh one when it's about to expire, so --wait can poll for longer than that,
  • call Relay over gRPC with TLS at --address. There is no discovery for the AutoFlow address yet, so you have to pass it. Runway staging is staging.autoflow-gke.svc.gitlab.net:443. --ca-cert adds a CA to trust, and --insecure connects without TLS for a local Relay (GDK, a test rig). That sends the token in cleartext, so it prints a warning,
  • default the namespace to your organization, which they take from the token's gitlab.origin_id claim, and send it in the request. --namespace-id overrides it. Since gitlab-org/cluster-integration/gitlab-agent!4854 (merged), Relay returns PermissionDenied unless the namespace is the token's organization. That's a stopgap authorization check, not where the namespace comes from,
  • never take a credential as a flag value, because flag values end up in the shell history and the process list. The workflow token, the channel token and the token binding come from --*-file (- reads stdin) or from GLAB_AUTOFLOW_WORKFLOW_TOKEN, GLAB_AUTOFLOW_CHANNEL_TOKEN and GLAB_AUTOFLOW_TOKEN_BINDING.

The commands:

  • workflow start takes the workflow definition from --script or --script-file. --kwarg name=<json> passes a keyword argument to main, and --channel name creates a channel and passes it as the keyword argument of the same name. It prints the workflow key, the workflow token, the idempotency key, the token binding and a token per channel, as text or with --output json. --idempotency-key plus --token-binding-file re-submit an earlier workflow to get fresh tokens. A key alone starts a new workflow under that key, and Relay answers a key an earlier workflow already uses with PermissionDenied. If the call fails with a code where Relay may have stored the workflow anyway (Unavailable, DeadlineExceeded, Canceled, Unknown, Internal, Aborted), the error tells you how to retry with the same key and binding, and saves a binding glab generated to a file only you can read. Any other code means Relay refused the request, so the error has no hint and nothing is saved.
  • workflow get <key> prints the state and, once the workflow has finished, its result or error. With --wait it polls until the workflow has finished, survives a few transient errors in a row, and exits with an error unless the workflow completed.
  • workflow cancel <key> requests the cancellation. With --wait it polls until the workflow is canceled.
  • workflow send sends a value to a channel. It takes the channel token, the workflow token, --value <json> and an --idempotency-key, which defaults to a random one and is named in the error if the call fails, so a retry doesn't deliver the value twice.

Values are JSON both ways: null is None, a number without a fraction or exponent is an integer, an array is a list and an object is a dict with string keys. get prints a result the same way, and falls back to the protobuf JSON form for kinds without a JSON equivalent (tuples, bytes, channels, ...). --value-format protojson prints the whole value in that form. I picked --value-format so it doesn't collide with glab's --output text|json, which picks the format of the whole output. Sensitive values (sensitive()) aren't supported yet, neither in --kwarg nor in send --value.

The autoflow command group is hidden, so there are no generated docs. The organization check is a stopgap until the Trigger becomes an IAM service account (gitlab-org/primitives/trigger!10).

The first commit moves the token exchange out of internal/api/artifactregistry into internal/api/tokenexchange and makes the audience a parameter. Artifact Registry behaves exactly as before. The shared test helpers move from artifactregistrytest to tokenexchangetest. The second commit adds cmdutils.AddSecretFileFlag, which reads a credential from a file, stdin or an environment variable, next to the other flag helpers, so other commands can use it too. The other commits add one command or flag each.

Dependencies

This adds google.golang.org/grpc v1.85.0 and gitlab.com/gitlab-org/protos/gen/go/gitlab/autoflow/rpc v0.1.10 (the version gitlab-agent pins), plus their transitive modules (protobuf, protovalidate, genproto RPC status and two more protos modules), and bumps go-logr/logr to v1.4.4. Relay only speaks gRPC: there is no gRPC-Web wrapper and no HTTP/JSON transcoding, so neither the standard library nor an existing dependency can reach it.

The stripped darwin/arm64 binary grows from 48.8 MB to 53.4 MB (+4.7 MB, +9.6%). Almost all of that is gRPC and protobuf: get, cancel and send add less than 100 KB on top of start. That's a lot, but I think it's fine for now: there is no lighter way to reach Relay, and the commands are hidden and experimental. If AutoFlow ever gets a REST API through Rails, we can drop gRPC again.

How has this been tested?

Unit tests run the commands against a fake token exchange endpoint and an in-process gRPC server, with and without TLS. They check the token exchange and its refresh, the Authorization metadata, every request, both output formats, the namespace taken from the token, reading credentials from files, stdin and the environment, re-submission, the retry hints, the JSON values both ways, --wait through transient errors, the 400 a user without the flag gets, Relay errors and an untrusted certificate.

On Runway staging, with gitlab!267945 (merged) deployed and the flag on for me, and without --namespace-id:

  • a return 1 workflow started and get --wait returned COMPLETED with 1,
  • a workflow started with --kwarg count=2 --channel inbox received "hello" through send, and get --wait returned COMPLETED with [2, "hello"] after about 1.4 seconds,
  • a workflow blocked on its channel ended canceled with cancel --wait.
Edited by Timo Furrer

Merge request reports

Loading
Loading