Use TF_HTTP_USERNAME and TF_HTTP_PASSWORD in delete-state

What does this MR do?

gitlab-tofu delete-state always authenticated with gitlab-ci-token:$CI_JOB_TOKEN and ignored TF_HTTP_USERNAME and TF_HTTP_PASSWORD. Every other state operation (init, plan, apply, destroy) uses these variables.

This affected the delete-state job and the destroy job with auto_delete_state: true. For example, with a Developer pipeline and a Maintainer project access token set in TF_HTTP_PASSWORD, the deletion got 403 Forbidden. The job still passed, because curl exits with 0 on an HTTP error.

This MR:

  • uses TF_HTTP_ADDRESS, TF_HTTP_USERNAME and TF_HTTP_PASSWORD for the DELETE request, so it targets the same state with the same credentials as init, plan and apply. They are exported by configure_variables_for_tofu and default to the GitLab-managed state address, gitlab-ci-token and $CI_JOB_TOKEN, so nothing changes when they are not overridden.
  • adds --fail-with-body, so the job fails when the state is not deleted and the response body still shows in the job log.

How to test

New unit tests in tests/unit/gitlab-tofu.bats replace curl with a stub and check that:

  • the CI job token is used by default
  • TF_HTTP_USERNAME and TF_HTTP_PASSWORD are used when set
  • --fail-with-body is passed and the command fails when the request fails
  • TF_HTTP_ADDRESS is used as the deletion URL when set

Closes #544 (closed)

Edited by Corentin Hervaud

Merge request reports

Loading
Loading