Allow skipping the job token authentication to the module registry
What does this MR do?
Adds a GITLAB_TOFU_SKIP_MODULE_REGISTRY_AUTH variable. When it is true, tofu_authenticate_private_registry no longer falls back to setting TF_TOKEN_<host> to CI_JOB_TOKEN. A TF_TOKEN_<host> provided by the user is left untouched.
Closes #317 (closed)
Why
GitLab rejects a job-token-authenticated request to a private project that has not allowlisted the job's project, even when that project's package registry allows anyone to pull. It does not fall back to anonymous access:
Packages::Policies::ProjectPolicyenables:read_packagewhenpackage_registry_access_level == PUBLIC.ProjectPolicythen appliesrule { ~project_allowed_for_job_token }.prevent_allto job token users outside the allowlist of a private project, and the delegatedpreventwins.- Public and internal projects keep the anonymous permissions (
except(*::Authz::Role.get(:public_anonymous)...)), but private projects with a public package registry get no such exception.
So injecting CI_JOB_TOKEN means a module that is readable with no credential fails with 403 Forbidden when it lives in a namespace whose allowlist you cannot change.
I chose an explicit opt-in flag over treating an empty TF_TOKEN_<host> as "no auth": TF_TOKEN_gitlab_com: $SOME_TOKEN expands to empty when SOME_TOKEN is protected and the pipeline runs on an unprotected branch. Today that falls back to the job token, and changing it would silently break private module access for those users.
Limitation
OpenTofu credentials are per host, so the flag drops authentication for every module on the instance. Projects that also need private modules should set TF_TOKEN_<host> to an access token instead.
Note: this is a workaround for GitLab's behavior
Ideally the component would not need this flag. The better fix belongs in GitLab itself: a job token request to a private project with a public package registry should get the same anonymous permissions that public and internal projects already grant to job tokens outside the allowlist, instead of being denied outright. With that fixed upstream, the automatic CI_JOB_TOKEN fallback could never reduce what a job can reach, and this flag would become unnecessary.
How to test
variables:
GITLAB_TOFU_SKIP_MODULE_REGISTRY_AUTH: 'true'Then reference a module from a private project whose package registry allows anyone to pull, from a project that is not on its job token allowlist. tofu init succeeds instead of failing with 403 Forbidden.
Unit tests are added in tests/unit/gitlab-tofu.bats.