Skip to content

Commit 50c65d7

Browse files
committed
feat(cloud_sql): improve the samples for customer-managed CAS CA instances.
1 parent b7e1274 commit 50c65d7

File tree

3 files changed

+198
-3
lines changed
  • cloud_sql
    • mysql_instance_customer_managed_cas_ca
    • postgres_instance_customer_managed_cas_ca
    • sqlserver_instance_customer_managed_cas_ca

3 files changed

+198
-3
lines changed

cloud_sql/mysql_instance_customer_managed_cas_ca/main.tf

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,71 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
# [START cloud_sql_instance_service_identity]
17+
resource "google_project_service_identity" "gcp_sa_cloud_sql" {
18+
provider = google-beta
19+
service = "sqladmin.googleapis.com"
20+
}
21+
# [END cloud_sql_instance_service_identity]
22+
23+
# [START cloud_sql_mysql_instance_ca_pool]
24+
resource "google_privateca_ca_pool" "customer_ca_pool" {
25+
name = "tf-test-cap"
26+
location = "asia-northeast1"
27+
tier = "DEVOPS"
28+
publishing_options {
29+
publish_ca_cert = false
30+
publish_crl = false
31+
}
32+
}
33+
# [END cloud_sql_mysql_instance_ca_pool]
34+
35+
# [START cloud_sql_mysql_instance_ca]
36+
resource "google_privateca_certificate_authority" "customer_ca" {
37+
pool = google_privateca_ca_pool.customer_ca_pool.name
38+
certificate_authority_id = "tf-test-ca"
39+
location = "asia-northeast1"
40+
lifetime = "86400s"
41+
type = "SELF_SIGNED"
42+
deletion_protection = false
43+
skip_grace_period = true
44+
ignore_active_certificates_on_deletion = true
45+
config {
46+
subject_config {
47+
subject {
48+
organization = "Test LLC"
49+
common_name = "my-ca"
50+
}
51+
}
52+
x509_config {
53+
ca_options {
54+
is_ca = true
55+
}
56+
key_usage {
57+
base_key_usage {
58+
cert_sign = true
59+
crl_sign = true
60+
}
61+
extended_key_usage {
62+
server_auth = false
63+
}
64+
}
65+
}
66+
}
67+
key_spec {
68+
algorithm = "RSA_PKCS1_4096_SHA256"
69+
}
70+
}
71+
# [END cloud_sql_mysql_instance_ca]
72+
73+
# [START cloud_sql_mysql_instance_iam_granting]
74+
resource "google_privateca_ca_pool_iam_member" "granting" {
75+
ca_pool = google_privateca_ca_pool.customer_ca_pool.id
76+
role = "roles/privateca.certificateRequester"
77+
78+
member = "serviceAccount:${google_project_service_identity.gcp_sa_cloud_sql.email}"
79+
}
80+
# [END cloud_sql_mysql_instance_iam_granting]
1681

1782
# [START cloud_sql_mysql_instance_customer_managed_cas_ca]
1883
resource "google_sql_database_instance" "mysql_instance" {
@@ -26,7 +91,7 @@ resource "google_sql_database_instance" "mysql_instance" {
2691
# https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/instances#ipconfiguration
2792
server_ca_mode = "CUSTOMER_MANAGED_CAS_CA"
2893
# This is the name of the customer-owned CAS CA pool.
29-
server_ca_pool = "projects/my-project/locations/asia-northeast1/caPools/my-pool"
94+
server_ca_pool = google_privateca_ca_pool.customer_ca_pool.id
3095
}
3196
}
3297
# set `deletion_protection` to true, will ensure that one cannot accidentally delete this instance by

cloud_sql/postgres_instance_customer_managed_cas_ca/main.tf

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,71 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
# [START cloud_sql_instance_service_identity]
17+
resource "google_project_service_identity" "gcp_sa_cloud_sql" {
18+
provider = google-beta
19+
service = "sqladmin.googleapis.com"
20+
}
21+
# [END cloud_sql_instance_service_identity]
22+
23+
# [START cloud_sql_postgres_instance_ca_pool]
24+
resource "google_privateca_ca_pool" "customer_ca_pool" {
25+
name = "tf-test-cap"
26+
location = "asia-northeast1"
27+
tier = "DEVOPS"
28+
publishing_options {
29+
publish_ca_cert = false
30+
publish_crl = false
31+
}
32+
}
33+
# [END cloud_sql_postgres_instance_ca_pool]
34+
35+
# [START cloud_sql_postgres_instance_ca]
36+
resource "google_privateca_certificate_authority" "customer_ca" {
37+
pool = google_privateca_ca_pool.customer_ca_pool.name
38+
certificate_authority_id = "tf-test-ca"
39+
location = "asia-northeast1"
40+
lifetime = "86400s"
41+
type = "SELF_SIGNED"
42+
deletion_protection = false
43+
skip_grace_period = true
44+
ignore_active_certificates_on_deletion = true
45+
config {
46+
subject_config {
47+
subject {
48+
organization = "Test LLC"
49+
common_name = "my-ca"
50+
}
51+
}
52+
x509_config {
53+
ca_options {
54+
is_ca = true
55+
}
56+
key_usage {
57+
base_key_usage {
58+
cert_sign = true
59+
crl_sign = true
60+
}
61+
extended_key_usage {
62+
server_auth = false
63+
}
64+
}
65+
}
66+
}
67+
key_spec {
68+
algorithm = "RSA_PKCS1_4096_SHA256"
69+
}
70+
}
71+
# [END cloud_sql_postgres_instance_ca]
72+
73+
# [START cloud_sql_postgres_instance_iam_granting]
74+
resource "google_privateca_ca_pool_iam_member" "granting" {
75+
ca_pool = google_privateca_ca_pool.customer_ca_pool.id
76+
role = "roles/privateca.certificateRequester"
77+
78+
member = "serviceAccount:${google_project_service_identity.gcp_sa_cloud_sql.email}"
79+
}
80+
# [END cloud_sql_postgres_instance_iam_granting]
1681

1782
# [START cloud_sql_postgres_instance_google_managed_cas_ca]
1883
resource "google_sql_database_instance" "postgres_instance" {
@@ -26,7 +91,7 @@ resource "google_sql_database_instance" "postgres_instance" {
2691
# https://cloud.google.com/sql/docs/postgres/admin-api/rest/v1beta4/instances#ipconfiguration
2792
server_ca_mode = "CUSTOMER_MANAGED_CAS_CA"
2893
# This is the name of the customer-owned CAS CA pool.
29-
server_ca_pool = "projects/my-project/locations/asia-northeast1/caPools/my-pool"
94+
server_ca_pool = google_privateca_ca_pool.customer_ca_pool.id
3095
}
3196
}
3297
# set `deletion_protection` to true, will ensure that one cannot accidentally delete this instance by

cloud_sql/sqlserver_instance_customer_managed_cas_ca/main.tf

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,71 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
# [START cloud_sql_instance_service_identity]
17+
resource "google_project_service_identity" "gcp_sa_cloud_sql" {
18+
provider = google-beta
19+
service = "sqladmin.googleapis.com"
20+
}
21+
# [END cloud_sql_instance_service_identity]
22+
23+
# [START cloud_sql_sqlserver_instance_ca_pool]
24+
resource "google_privateca_ca_pool" "customer_ca_pool" {
25+
name = "tf-test-cap"
26+
location = "asia-northeast1"
27+
tier = "DEVOPS"
28+
publishing_options {
29+
publish_ca_cert = false
30+
publish_crl = false
31+
}
32+
}
33+
# [END cloud_sql_sqlserver_instance_ca_pool]
34+
35+
# [START cloud_sql_sqlserver_instance_ca]
36+
resource "google_privateca_certificate_authority" "customer_ca" {
37+
pool = google_privateca_ca_pool.customer_ca_pool.name
38+
certificate_authority_id = "tf-test-ca"
39+
location = "asia-northeast1"
40+
lifetime = "86400s"
41+
type = "SELF_SIGNED"
42+
deletion_protection = false
43+
skip_grace_period = true
44+
ignore_active_certificates_on_deletion = true
45+
config {
46+
subject_config {
47+
subject {
48+
organization = "Test LLC"
49+
common_name = "my-ca"
50+
}
51+
}
52+
x509_config {
53+
ca_options {
54+
is_ca = true
55+
}
56+
key_usage {
57+
base_key_usage {
58+
cert_sign = true
59+
crl_sign = true
60+
}
61+
extended_key_usage {
62+
server_auth = false
63+
}
64+
}
65+
}
66+
}
67+
key_spec {
68+
algorithm = "RSA_PKCS1_4096_SHA256"
69+
}
70+
}
71+
# [END cloud_sql_sqlserver_instance_ca]
72+
73+
# [START cloud_sql_sqlserver_instance_iam_granting]
74+
resource "google_privateca_ca_pool_iam_member" "granting" {
75+
ca_pool = google_privateca_ca_pool.customer_ca_pool.id
76+
role = "roles/privateca.certificateRequester"
77+
78+
member = "serviceAccount:${google_project_service_identity.gcp_sa_cloud_sql.email}"
79+
}
80+
# [END cloud_sql_sqlserver_instance_iam_granting]
1681

1782
# [START cloud_sql_sqlserver_instance_require_ssl]
1883
resource "google_sql_database_instance" "sqlserver_instance" {
@@ -27,7 +92,7 @@ resource "google_sql_database_instance" "sqlserver_instance" {
2792
# https://cloud.google.com/sql/docs/sqlserver/admin-api/rest/v1beta4/instances#ipconfiguration
2893
server_ca_mode = "CUSTOMER_MANAGED_CAS_CA"
2994
# This is the name of the customer-owned CAS CA pool.
30-
server_ca_pool = "projects/my-project/locations/asia-northeast1/caPools/my-pool"
95+
server_ca_pool = google_privateca_ca_pool.customer_ca_pool.id
3196
}
3297
}
3398
# set `deletion_protection` to true, will ensure that one cannot accidentally delete this instance by

0 commit comments

Comments
 (0)