我一直在努力为 Terraform 建立一个 monorepo 并运行。Terraform 在容器内执行。但是,当我尝试将init
http 指向我的自托管 Gitlab 的后端时,它返回为需要身份验证:
Successfully configured the backend "http"! Terraform will automatically
use this backend unless the backend configuration changes.
2021-04-15T13:02:00.815Z [TRACE] Meta.Backend: instantiated backend of type *http.Backend
2021-04-15T13:02:00.815Z [DEBUG] checking for provisioner in "."
2021-04-15T13:02:00.815Z [DEBUG] checking for provisioner in "/bin"
2021-04-15T13:02:00.815Z [INFO] Failed to read plugin lock file .terraform/plugins/linux_amd64/lock.json: open .terraform/plugins/linux_amd64/lock.json: no such file or directory
2021-04-15T13:02:00.815Z [TRACE] Meta.Backend: backend *http.Backend does not support operations, so wrapping it in a local backend
2021/04/15 13:02:00 [DEBUG] GET https://gitlab.my_domain.tld/api/v4/projects/2/terraform/state/infrastructure
Error refreshing state: HTTP remote state endpoint requires auth
我已经确认我的项目令牌确实可以通过对具有 API 访问权限的令牌的项目使用 curl 来工作:
bash-5.1# curl -k -I "https://gitlab.my_domain.tld/api/v4/projects?<my_token_id>"
HTTP/2 200
server: nginx
date: Wed, 14 Apr 2021 23:59:43 GMT
content-type: application/json
vary: Accept-Encoding
cache-control: no-cache
link: <https://gitlab.my_domain.tld/api/v4/projects?<my_token_id>=&membership=false&order_by=created_at&owned=false&page=1&per_page=20&repository_checksum_failed=false&simple=false&sort=desc&starred=false&statistics=false&wiki_checksum_failed=false&with_custom_attributes=false&with_issues_enabled=false&with_merge_requests_enabled=false>; rel="first", <https://gitlab.my_domain.tld/api/v4/projects?<my_project_token>=&membership=false&order_by=created_at&owned=false&page=1&per_page=20&repository_checksum_failed=false&simple=false&sort=desc&starred=false&statistics=false&wiki_checksum_failed=false&with_custom_attributes=false&with_issues_enabled=false&with_merge_requests_enabled=false>; rel="last"
vary: Origin
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-gitlab-feature-category: projects
x-next-page:
x-page: 1
x-per-page: 20
x-prev-page:
x-request-id: 01F39D73G8ZXPJ1E1ZAK0ZS860
x-runtime: 0.184197
x-total: 1
x-total-pages: 1
strict-transport-security: max-age=31536000
referrer-policy: strict-origin-when-cross-origin
bash-5.1#
凭据不会通过,-backend-config
也不会直接放入backend.tfvars
我在后端拥有的文件中:
后端.tf:
terraform {
backend "http" { }
}
后端.tfvars:
lock_method = "POST"
unlock_method = "DELETE"
retry_max = "3"
skip_cert_verification = true
我已经通过导出确认 和 的值TF_HTTP_USERNAME
是TF_HTTP_PASSWORD
在环境变量中设置的。见鬼,我什至将它们设置为全局变量:
declare -x TERRAFORM_VERSION="0.15.0"
declare -x TF_HTTP_PASSWORD="[MASKED]"
declare -x TF_HTTP_USERNAME="project_2_bot"
declare -x TF_LOG="trace"
declare -x TF_PASSWORD="[MASKED]"
declare -x TF_USERNAME="project_2_bot"
declare -x bot_2_token="[MASKED]"
我检查了 api_json.json 的 Gitlab 日志,我看到请求进来了,但是使用 INFO 他们并没有告诉我太多,只是尝试进来了:
{
"time": "2021-04-15T13:02:00.924Z",
"severity": "INFO",
"duration_s": 0.00915,
"db_duration_s": 0.00207,
"view_duration_s": 0.00708,
"status": 401,
"method": "GET",
"path": "/api/v4/projects/2/terraform/state/infrastructure",
"params": [],
"host": "gitlab.my_domain.tld",
"remote_ip": "10.4.6.95, 127.0.0.1",
"ua": "Go-http-client/1.1",
"route": "/api/:version/projects/:id/terraform/state/:name",
"queue_duration_s": 0.011228,
"db_count": 2,
"db_write_count": 0,
"db_cached_count": 1,
"cpu_s": 0.017418,
"mem_objects": 10346,
"mem_bytes": 423664,
"mem_mallocs": 1771,
"correlation_id": "01F3ASZH67FMVS0449NC289C47",
"meta.caller_id": "/api/:version/projects/:id/terraform/state/:name",
"meta.remote_ip": "10.4.6.95",
"meta.feature_category": "infrastructure_as_code",
"meta.client_id": "ip/10.4.6.95"
}
我正在尝试找出如何提高日志级别以捕获所有进入 API 的事件,但到目前为止还没有。
每次我尝试对后端运行命令时,它都会返回:
Error refreshing state: HTTP remote state endpoint requires auth
我试过 Terraform 版本 0.14.x(不记得最新的是什么)和 0.15
而且我已经没有办法去看了。任何人都经历过这种情况或就如何进一步排除故障提供想法?