Skip to content

Feat: Allow setting custom headers or the cookie header for the gitlab client

  • Please check this box if this contribution uses AI-generated content (including content generated by GitLab Duo features) as outlined in the GitLab DCO & CLA

This merge request is not quite ready, but I would appreciate some help:

  • The headers are correctly used in my tests and I can connect to gitlab and request a data source, but the EarlyAuthFail requests fails: according to debug log it sends 2 requests if this setting is on, the first one uses the headers, the second one does not, receives a 401, and causes the execution to error.
Debug logs:
2024-10-01T15:26:38.200+0200 [INFO]  Terraform version: 1.9.6
2024-10-01T15:26:38.200+0200 [DEBUG] using github.com/hashicorp/go-tfe v1.58.0
2024-10-01T15:26:38.200+0200 [DEBUG] using github.com/hashicorp/hcl/v2 v2.20.0
2024-10-01T15:26:38.200+0200 [DEBUG] using github.com/hashicorp/terraform-svchost v0.1.1
2024-10-01T15:26:38.200+0200 [DEBUG] using github.com/zclconf/go-cty v1.14.4
2024-10-01T15:26:38.200+0200 [INFO]  Go runtime version: go1.22.7
2024-10-01T15:26:38.200+0200 [INFO]  CLI args: []string{"terraform", "plan"}
2024-10-01T15:26:38.200+0200 [DEBUG] Attempting to open CLI config file: .terraformrc
2024-10-01T15:26:38.200+0200 [INFO]  Loading CLI configuration from .terraformrc
2024-10-01T15:26:38.200+0200 [DEBUG] Not reading CLI config directory because config location is overridden by environment variable
2024-10-01T15:26:38.201+0200 [DEBUG] Explicit provider installation configuration is set
2024-10-01T15:26:38.201+0200 [INFO]  CLI command args: []string{"plan"}
2024-10-01T15:26:38.201+0200 [DEBUG] Provider registry.terraform.io/gitlabhq/gitlab is overridden by dev_overrides
2024-10-01T15:26:38.201+0200 [DEBUG] Provider registry.terraform.io/gitlabhq/gitlab is overridden to load from ../bin
2024-10-01T15:26:38.201+0200 [DEBUG] checking for provisioner in "."
2024-10-01T15:26:38.201+0200 [DEBUG] checking for provisioner in "/home/ruben/.local/share/mise/installs/terraform/1.9.6/bin"
2024-10-01T15:26:38.201+0200 [DEBUG] Provider registry.terraform.io/gitlabhq/gitlab is overridden by dev_overrides
2024-10-01T15:26:38.201+0200 [INFO]  backend/local: starting Plan operation
2024-10-01T15:26:38.202+0200 [DEBUG] Config.VerifyDependencySelections: skipping registry.terraform.io/gitlabhq/gitlab because it's overridden by a special configuration setting
2024-10-01T15:26:38.202+0200 [DEBUG] created provider logger: level=debug
2024-10-01T15:26:38.202+0200 [INFO]  provider: configuring client automatic mTLS
2024-10-01T15:26:38.209+0200 [DEBUG] provider: starting plugin: path=../bin/terraform-provider-gitlab args=["../bin/terraform-provider-gitlab"]
2024-10-01T15:26:38.210+0200 [DEBUG] provider: plugin started: path=../bin/terraform-provider-gitlab pid=809583
2024-10-01T15:26:38.210+0200 [DEBUG] provider: waiting for RPC address: plugin=../bin/terraform-provider-gitlab
2024-10-01T15:26:38.219+0200 [INFO]  provider.terraform-provider-gitlab: configuring server automatic mTLS: timestamp="2024-10-01T15:26:38.219+0200"
2024-10-01T15:26:38.233+0200 [DEBUG] provider: using plugin: version=6
2024-10-01T15:26:38.234+0200 [DEBUG] provider.terraform-provider-gitlab: plugin address: address=/tmp/plugin851604981 network=unix timestamp="2024-10-01T15:26:38.233+0200"
2024-10-01T15:26:38.274+0200 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF"
2024-10-01T15:26:38.276+0200 [INFO]  provider: plugin process exited: plugin=../bin/terraform-provider-gitlab id=809583
2024-10-01T15:26:38.276+0200 [DEBUG] provider: plugin exited
2024-10-01T15:26:38.276+0200 [DEBUG] Building and walking validate graph
2024-10-01T15:26:38.277+0200 [DEBUG] ProviderTransformer: "data.gitlab_current_user.this" (*terraform.NodeValidatableResource) needs provider["registry.terraform.io/gitlabhq/gitlab"]
2024-10-01T15:26:38.277+0200 [DEBUG] ReferenceTransformer: "data.gitlab_current_user.this" references: []
2024-10-01T15:26:38.277+0200 [DEBUG] ReferenceTransformer: "output.name (expand)" references: [data.gitlab_current_user.this]
2024-10-01T15:26:38.277+0200 [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/gitlabhq/gitlab\"]" references: []
2024-10-01T15:26:38.277+0200 [DEBUG] Starting graph walk: walkValidate
2024-10-01T15:26:38.278+0200 [DEBUG] created provider logger: level=debug
2024-10-01T15:26:38.278+0200 [INFO]  provider: configuring client automatic mTLS
2024-10-01T15:26:38.284+0200 [DEBUG] provider: starting plugin: path=../bin/terraform-provider-gitlab args=["../bin/terraform-provider-gitlab"]
2024-10-01T15:26:38.285+0200 [DEBUG] provider: plugin started: path=../bin/terraform-provider-gitlab pid=809607
2024-10-01T15:26:38.285+0200 [DEBUG] provider: waiting for RPC address: plugin=../bin/terraform-provider-gitlab
2024-10-01T15:26:38.294+0200 [INFO]  provider.terraform-provider-gitlab: configuring server automatic mTLS: timestamp="2024-10-01T15:26:38.293+0200"
2024-10-01T15:26:38.306+0200 [DEBUG] provider.terraform-provider-gitlab: plugin address: address=/tmp/plugin69374468 network=unix timestamp="2024-10-01T15:26:38.306+0200"
2024-10-01T15:26:38.306+0200 [DEBUG] provider: using plugin: version=6
2024-10-01T15:26:38.319+0200 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF"
2024-10-01T15:26:38.322+0200 [INFO]  provider: plugin process exited: plugin=../bin/terraform-provider-gitlab id=809607
2024-10-01T15:26:38.322+0200 [DEBUG] provider: plugin exited
2024-10-01T15:26:38.322+0200 [INFO]  backend/local: plan calling Plan
2024-10-01T15:26:38.322+0200 [DEBUG] Building and walking plan graph for NormalMode
2024-10-01T15:26:38.322+0200 [DEBUG] ProviderTransformer: "data.gitlab_current_user.this (expand)" (*terraform.nodeExpandPlannableResource) needs provider["registry.terraform.io/gitlabhq/gitlab"]
2024-10-01T15:26:38.322+0200 [DEBUG] ReferenceTransformer: "output.name (expand)" references: [data.gitlab_current_user.this (expand)]
2024-10-01T15:26:38.322+0200 [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/gitlabhq/gitlab\"]" references: []
2024-10-01T15:26:38.322+0200 [DEBUG] ReferenceTransformer: "data.gitlab_current_user.this (expand)" references: []
2024-10-01T15:26:38.323+0200 [DEBUG] Starting graph walk: walkPlan
2024-10-01T15:26:38.323+0200 [DEBUG] created provider logger: level=debug
2024-10-01T15:26:38.323+0200 [INFO]  provider: configuring client automatic mTLS
2024-10-01T15:26:38.329+0200 [DEBUG] provider: starting plugin: path=../bin/terraform-provider-gitlab args=["../bin/terraform-provider-gitlab"]
2024-10-01T15:26:38.329+0200 [DEBUG] provider: plugin started: path=../bin/terraform-provider-gitlab pid=809616
2024-10-01T15:26:38.329+0200 [DEBUG] provider: waiting for RPC address: plugin=../bin/terraform-provider-gitlab
2024-10-01T15:26:38.338+0200 [INFO]  provider.terraform-provider-gitlab: configuring server automatic mTLS: timestamp="2024-10-01T15:26:38.338+0200"
2024-10-01T15:26:38.349+0200 [DEBUG] provider: using plugin: version=6
2024-10-01T15:26:38.349+0200 [DEBUG] provider.terraform-provider-gitlab: plugin address: address=/tmp/plugin3064187612 network=unix timestamp="2024-10-01T15:26:38.349+0200"
2024-10-01T15:26:38.364+0200 [DEBUG] provider.terraform-provider-gitlab: Sending HTTP Request: Host=GITLAB_URL Cookie=COOKIE_NAME=COOKIE_VALUE User-Agent=go-gitlab tf_http_op_type=request tf_http_req_uri=/api/v4/user tf_http_req_version=HTTP/1.1 tf_http_trans_id=9de4bc3e-42e9-f98b-1cd4-bded2df13664 @module=gitlab.GitLab Authorization="Bearer CSC-***" tf_http_req_body="" tf_http_req_method=GET @caller=/home/ruben/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.34.0/helper/logging/logging_http_transport.go:160 Accept-Encoding=gzip Test=123 Accept=application/json timestamp="2024-10-01T15:26:38.364+0200"
2024-10-01T15:26:38.824+0200 [DEBUG] provider.terraform-provider-gitlab: Received HTTP Response: X-Runtime=0.245957 tf_http_trans_id=9de4bc3e-42e9-f98b-1cd4-bded2df13664 @module=gitlab.GitLab X-Request-Id=01J9445V35509RFEJ0BPYV5A34 tf_http_op_type=response tf_http_res_body="{\"id\":ID,\"username\":\"GITLAB_USERNAME\",\"name\":\"USERNAME ORGANIZATION\",\"state\":\"active\",\"locked\":false,\"avatar_url\":\"https://GITLAB_URL/uploads/-/system/user/avatar/ID/avatar.png\",\"web_url\":\"https://GITLAB_URL/GITLAB_USERNAME\",\"created_at\":\"2023-07-11T09:32:45.714+02:00\",\"bio\":\"\",\"location\":\"CITY, COUNTRY\",\"public_email\":\"GITLAB_USERNAME@COMPANY.COM\",\"skype\":\"\",\"linkedin\":\"\",\"twitter\":\"\",\"discord\":\"\",\"website_url\":\"\",\"organization\":\"ORGANIZATION\",\"job_title\":\"\",\"pronouns\":\"\",\"bot\":false,\"work_information\":\"ORGANIZATION\",\"local_time\":null,\"last_sign_in_at\":\"2024-10-01T13:04:06.740+02:00\",\"confirmed_at\":\"2023-07-11T09:32:45.667+02:00\",\"last_activity_on\":\"2024-10-01\",\"email\":\"GITLAB_USERNAME@COMPANY.COM\",\"theme_id\":3,\"color_scheme_id\":1,\"projects_limit\":42,\"current_sign_in_at\":\"2024-10-01T15:26:20.938+02:00\",\"identities\":[{\"provider\":\"ID_PROVIDER\",\"extern_uid\":\"EXTERN_ID\"}],\"can_create_group\":true,\"can_create_project\":true,\"two_factor_enabled\":false,\"external\":false,\"private_profile\":false,\"commit_email\":\"GITLAB_USERNAME@COMPANY.COM\"}" Connection=keep-alive tf_http_res_status_code=200 X-Gitlab-Meta="{\"correlation_id\":\"01J9445V35509RFEJ0BPYV5A34\",\"version\":\"1\"}" tf_http_res_version=HTTP/1.1 X-Content-Type-Options=nosniff Content-Type=application/json Etag="W/\"8f14f6414741fd7a8b33c9bcafa3bb83\"" Server=nginx Vary=[Accept-Encoding, Origin] tf_http_res_status_reason="200 OK" Content-Security-Policy="default-src 'none'" Referrer-Policy=strict-origin-when-cross-origin Date="Tue, 01 Oct 2024 13:26:38 GMT" Gap-Auth=GITLAB_USERNAME@COMPANY.COM Strict-Transport-Security=[max-age=63072000, max-age=31536000] X-Frame-Options=SAMEORIGIN @caller=/home/ruben/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.34.0/helper/logging/logging_http_transport.go:160 Cache-Control="max-age=0, private, must-revalidate" timestamp="2024-10-01T15:26:38.823+0200"
2024-10-01T15:26:38.825+0200 [DEBUG] provider.terraform-provider-gitlab: Sending HTTP Request: Authorization="Bearer CSC-***" tf_http_op_type=request tf_http_req_version=HTTP/1.1 @caller=/home/ruben/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.34.0/helper/logging/logging_http_transport.go:160 tf_http_req_method=GET tf_http_req_uri=/api/v4/user @module=gitlab.GitLab User-Agent=go-gitlab tf_http_trans_id=b9b11b2c-ac4b-e86c-f02e-d27ad6045f23 Accept=application/json Accept-Encoding=gzip tf_http_req_body="" Host=GITLAB_URL timestamp="2024-10-01T15:26:38.825+0200"
2024-10-01T15:26:38.899+0200 [DEBUG] provider.terraform-provider-gitlab: Received HTTP Response: @module=gitlab.GitLab Content-Type=application/json Server=nginx tf_http_res_version=HTTP/1.1 @caller=/home/ruben/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.34.0/helper/logging/logging_http_transport.go:160 tf_http_res_body={} Content-Length=2 Date="Tue, 01 Oct 2024 13:26:38 GMT" tf_http_res_status_code=401 tf_http_res_status_reason="401 Unauthorized" tf_http_trans_id=b9b11b2c-ac4b-e86c-f02e-d27ad6045f23 Connection=keep-alive Strict-Transport-Security=max-age=31536000 tf_http_op_type=response timestamp="2024-10-01T15:26:38.899+0200"
2024-10-01T15:26:38.900+0200 [DEBUG] Resource instance state not found for node "data.gitlab_current_user.this", instance data.gitlab_current_user.this
2024-10-01T15:26:38.900+0200 [DEBUG] ReferenceTransformer: "data.gitlab_current_user.this" references: []
2024-10-01T15:26:38.906+0200 [DEBUG] provider.terraform-provider-gitlab: [DEBUG] executing GraphQL Query query {currentUser {name, bot, groupCount, id, namespace{id}, publicEmail, username}} to retrieve current user: tf_data_source_type=gitlab_current_user tf_rpc=ReadDataSource @module=gitlab tf_mux_provider=tf5to6server.v5tov6Server tf_provider_addr=registry.terraform.io/gitlabhq/gitlab tf_req_id=a67baf81-432c-8606-9b4c-ad769f0b49fc @caller=/home/ruben/clients/gitlab/terraform-provider-gitlab/internal/provider/sdk/data_source_gitlab_current_user.go:77 timestamp="2024-10-01T15:26:38.906+0200"
2024-10-01T15:26:39.613+0200 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF"
2024-10-01T15:26:39.617+0200 [INFO]  provider: plugin process exited: plugin=../bin/terraform-provider-gitlab id=809616
2024-10-01T15:26:39.617+0200 [DEBUG] provider: plugin exited
2024-10-01T15:26:39.618+0200 [DEBUG] building apply graph to check for errors
2024-10-01T15:26:39.618+0200 [DEBUG] ProviderTransformer: "data.gitlab_current_user.this (expand)" (*terraform.nodeExpandApplyableResource) needs provider["registry.terraform.io/gitlabhq/gitlab"]
2024-10-01T15:26:39.618+0200 [DEBUG] ReferenceTransformer: "data.gitlab_current_user.this (expand)" references: []
2024-10-01T15:26:39.618+0200 [DEBUG] ReferenceTransformer: "output.name (expand)" references: [data.gitlab_current_user.this (expand)]
2024-10-01T15:26:39.619+0200 [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/gitlabhq/gitlab\"]" references: []
2024-10-01T15:26:39.619+0200 [DEBUG] pruneUnusedNodes: data.gitlab_current_user.this (expand) is no longer needed, removing
2024-10-01T15:26:39.619+0200 [DEBUG] pruneUnusedNodes: provider["registry.terraform.io/gitlabhq/gitlab"] is no longer needed, removing

2024-10-01T15:26:39.619+0200 [INFO]  backend/local: plan operation completed

Changes to Outputs:
  + name = {
      + bot                 = false
      + global_id           = "gid://gitlab/User/ID"
      + global_namespace_id = "gid://gitlab/Namespaces::UserNamespace/NAMESPACE_ID"
      + group_count         = 23
      + id                  = "ID"
      + name                = "NAME (ORGANIZATION)"
      + namespace_id        = "NAMESPACE_ID"
      + public_email        = "GITLAB_USERNAME@COMPANY.COM"
      + username            = "GITLAB_USERNAME"
    }

You can apply this plan to save these new output values to the Terraform
state, without changing any real infrastructure.

─────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.
  • I would appreciate some help for this section in the code, where I am not really sure how to add the terraform inputs. While simple types seem straight forward I am not sure how to best assign the map values, due to type mismatch a helper function seems to be necessary, or is there a simple way?

fixes #6361 (closed)

Edited by Ruben Aleman

Merge request reports

Loading