-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.tf
84 lines (74 loc) · 3.04 KB
/
test.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# MIT License
#
# Copyright (c) 2024 Matheus Pimenta
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Reference:
# https://cloud.google.com/iam/docs/workload-identity-federation-with-kubernetes
locals {
test_bucket = "gke-metadata-server-test"
principal_prefix = "principal://iam.googleapis.com/${google_iam_workload_identity_pool.test_kind_cluster.name}/subject/system:serviceaccount"
}
resource "google_iam_workload_identity_pool" "test_kind_cluster" {
workload_identity_pool_id = "test-kind-cluster"
}
resource "google_service_account" "test" {
account_id = "test-sa"
}
resource "google_service_account_iam_binding" "test_workload_identity_users" {
service_account_id = google_service_account.test.name
role = local.wi_user_role
members = [
"${local.principal_prefix}:kube-system:gke-metadata-server",
"${local.principal_prefix}:default:test-impersonated",
]
}
# this allows an OAuth 2.0 Access Token for the Google Service Account to be exchanged
# for an OpenID Connect ID Token for the Google Service Account. this is necessary
# for the GET /computeMetadata/v1/instance/service-accounts/*/identity API to work
resource "google_service_account_iam_member" "openid_token_creator" {
service_account_id = google_service_account.test.name
role = "roles/iam.serviceAccountOpenIdTokenCreator"
member = google_service_account.test.member
}
resource "google_storage_bucket" "test" {
name = local.test_bucket
location = "us"
public_access_prevention = "enforced"
uniform_bucket_level_access = true # required for direct resource access
soft_delete_policy {
retention_duration_seconds = 0
}
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 1
}
}
}
resource "google_storage_bucket_iam_binding" "test_bucket_object_admins" {
bucket = google_storage_bucket.test.name
role = "roles/storage.objectAdmin"
members = [
google_service_account.test.member,
"${local.principal_prefix}:default:test",
]
}