|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +# [START cloud_sql_instance_service_identity] |
| 17 | +resource "google_project_service_identity" "default" { |
| 18 | + provider = google-beta |
| 19 | + service = "sqladmin.googleapis.com" |
| 20 | +} |
| 21 | +# [END cloud_sql_instance_service_identity] |
| 22 | + |
| 23 | +# [START privateca_ca_pool_suffix] |
| 24 | +resource "random_string" "default" { |
| 25 | + length = 10 |
| 26 | + special = false |
| 27 | + upper = false |
| 28 | +} |
| 29 | +# [END privateca_ca_pool_suffix] |
| 30 | + |
| 31 | +# [START cloud_sql_mysql_instance_ca_pool] |
| 32 | +resource "google_privateca_ca_pool" "default" { |
| 33 | + name = "customer-ca-pool-${random_string.default.result}" |
| 34 | + location = "asia-northeast1" |
| 35 | + tier = "DEVOPS" |
| 36 | + publishing_options { |
| 37 | + publish_ca_cert = false |
| 38 | + publish_crl = false |
| 39 | + } |
| 40 | +} |
| 41 | +# [END cloud_sql_mysql_instance_ca_pool] |
| 42 | + |
| 43 | +# [START cloud_sql_mysql_instance_ca] |
| 44 | +resource "google_privateca_certificate_authority" "default" { |
| 45 | + pool = google_privateca_ca_pool.default.name |
| 46 | + certificate_authority_id = "my-certificate-authority" |
| 47 | + location = "asia-northeast1" |
| 48 | + lifetime = "86400s" |
| 49 | + type = "SELF_SIGNED" |
| 50 | + deletion_protection = false # set to "true" in production |
| 51 | + skip_grace_period = true |
| 52 | + ignore_active_certificates_on_deletion = true |
| 53 | + config { |
| 54 | + subject_config { |
| 55 | + subject { |
| 56 | + organization = "my organization" |
| 57 | + common_name = "my certificate authority name" |
| 58 | + } |
| 59 | + } |
| 60 | + x509_config { |
| 61 | + ca_options { |
| 62 | + is_ca = true |
| 63 | + } |
| 64 | + key_usage { |
| 65 | + base_key_usage { |
| 66 | + cert_sign = true |
| 67 | + crl_sign = true |
| 68 | + } |
| 69 | + extended_key_usage { |
| 70 | + server_auth = false |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + key_spec { |
| 76 | + algorithm = "RSA_PKCS1_4096_SHA256" |
| 77 | + } |
| 78 | +} |
| 79 | +# [END cloud_sql_mysql_instance_ca] |
| 80 | + |
| 81 | +# [START cloud_sql_mysql_instance_iam_granting] |
| 82 | +resource "google_privateca_ca_pool_iam_member" "default" { |
| 83 | + ca_pool = google_privateca_ca_pool.default.id |
| 84 | + role = "roles/privateca.certificateRequester" |
| 85 | + |
| 86 | + member = "serviceAccount:${google_project_service_identity.default.email}" |
| 87 | +} |
| 88 | +# [END cloud_sql_mysql_instance_iam_granting] |
| 89 | + |
| 90 | +# [START cloud_sql_mysql_instance_custom_subject_alternative_names] |
| 91 | +resource "google_sql_database_instance" "default" { |
| 92 | + name = "mysql-instance" |
| 93 | + region = "asia-northeast1" |
| 94 | + database_version = "MYSQL_8_4" |
| 95 | + settings { |
| 96 | + edition = "ENTERPRISE" |
| 97 | + tier = "db-f1-micro" |
| 98 | + ip_configuration { |
| 99 | + # The following server CA mode lets the instance use customer-managed CAS CA to issue server certificates. |
| 100 | + # https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/instances#ipconfiguration |
| 101 | + server_ca_mode = "CUSTOMER_MANAGED_CAS_CA" |
| 102 | + server_ca_pool = google_privateca_ca_pool.default.id |
| 103 | + custom_subject_alternative_names = ["customSan.test.com"] |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | +# [END cloud_sql_mysql_instance_custom_subject_alternative_names] |
0 commit comments