Terraform 条件 IAM GCP

huangapple go评论110阅读模式
英文:

Terraform Conditional IAM GCP

问题

尝试使用 Terraform 和基于时间的条件将用户添加到角色中。我的代码如下:

  1. resource "google_project_iam_binding" "cwx_readonly_users" {
  2. for_each = toset(local.grantees)
  3. project = "<project_id>"
  4. role = google_project_iam_custom_role.my-custom-role.id
  5. members = [
  6. "user:${each.value}"
  7. ]
  8. condition {
  9. title = "expires_after_30days"
  10. description = "Expires in 30 dates from now"
  11. expression = "request.time < timestamp(${local.expiry})"
  12. }
  13. }

我已经定义了一个本地变量 "expiry" 如下:

  1. expiry = timeadd(timestamp(), "720h")

我已经尝试了一切,但无法解决这个问题。

这是我收到的错误信息:

  1. Error: Request `Set IAM Binding for role "projects/xxxx/roles/xxx" on "project "xxxxl""` returned error: Batch request and retried single request "Set IAM Binding for role "projects/xxx/roles/xxxx"" both failed. Final error: Error applying IAM policy for project "xxx": Error setting IAM policy for project "xxx": googleapi: Error 400: Condition expression compilation failed. Debug message: ERROR : ERROR: <expression>:1:36: mismatched input 'T16' expecting {'==', '!=', 'in', '<', '<=', '>=', '>', '&&', '||', '[', ')', '.', ',', '-', '?', '+', '*', '/', '%%'}
  2. | request.time < timestamp(2023-09-03T16:05:22Z)
  3. | ...................................^ , badRequest
  4. with google_project_iam_binding.cwx_readonly_users["xxxx"],
  5. on main.tf line 37, in resource "google_project_iam_binding" "cwx_readonly_users":
  6. 37: resource "google_project_iam_binding" "cwx_readonly_users" {

如果我使用这行代码,它可以正常工作:

  1. expression = "request.time < timestamp('2023-04-12T00:00:00.00Z')"

但我希望使用一个变量时间而不是硬编码一个日期,以提高可用性。

英文:

Trying to add a user to a role using Terraform with a time based condition.

My code looks like below

  1. for_each = toset(local.grantees)
  2. project = &lt;project_id&gt;
  3. role = google_project_iam_custom_role.my-custom-role.id
  4. members = [
  5. &quot;user:${each.value}&quot;
  6. ]
  7. condition {
  8. title = &quot;expires_after_30days&quot;
  9. description = &quot;Expires in 30 dates from now&quot;
  10. #expression = &quot;request.time &lt; timestamp(&#39;2023-04-12T00:00:00.00Z&#39;)&quot;
  11. expression = &quot;request.time &lt; timestamp(${local.expiry})&quot;
  12. }
  13. }

I have defined a local variable "expiry" as below
expiry = timeadd(timestamp(), &quot;720h&quot;)

I have tried everything but unable to resolve this blocker.

Here is the error I get

  1. Error: Request `Set IAM Binding for role &quot;projects/xxxx/roles/xxx&quot; on &quot;project \&quot;xxxxl\&quot;&quot;` returned error: Batch request and retried single request &quot;Set IAM Binding for role \&quot;projects/xxx/roles/xxxx\&quot; on \&quot;project \\\&quot;xxx\\\&quot;\&quot;&quot; both failed. Final error: Error applying IAM policy for project &quot;xxx&quot;: Error setting IAM policy for project &quot;xxx&quot;: googleapi: Error 400: Condition expression compilation failed. Debug message: ERROR : ERROR: &lt;expression&gt;:1:36: mismatched input &#39;T16&#39; expecting {&#39;==&#39;, &#39;!=&#39;, &#39;in&#39;, &#39;&lt;&#39;, &#39;&lt;=&#39;, &#39;&gt;=&#39;, &#39;&gt;&#39;, &#39;&amp;&amp;&#39;, &#39;||&#39;, &#39;[&#39;, &#39;)&#39;, &#39;.&#39;, &#39;,&#39;, &#39;-&#39;, &#39;?&#39;, &#39;+&#39;, &#39;*&#39;, &#39;/&#39;, &#39;%%&#39;}
  2. | request.time &lt; timestamp(2023-09-03T16:05:22Z)
  3. | ...................................^ , badRequest
  4. with google_project_iam_binding.cwx_readonly_users[&quot;xxxx&quot;],
  5. on main.tf line 37, in resource &quot;google_project_iam_binding&quot; &quot;cwx_readonly_users&quot;:
  6. 37: resource &quot;google_project_iam_binding&quot; &quot;cwx_readonly_users&quot; {

If I use this line instead, it works just fine

  1. #expression = &quot;request.time &lt; timestamp(&#39;2023-04-12T00:00:00.00Z&#39;)&quot;

But, I am interested in using a variable time and not hard code a date for better usability

答案1

得分: 1

guillaume blaquiere 是对的,你漏了单引号:

  1. expression = "request.time < timestamp('${local.expiry}')"
英文:

guillaume blaquiere is right, you are missing single quotes:

  1. expression = &quot;request.time &lt; timestamp(&#39;${local.expiry}&#39;)&quot;

huangapple
  • 本文由 发表于 2023年8月5日 00:12:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定