Skip to content

Commit

Permalink
feat: config application-level encryption for fl
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrarimarco committed Dec 20, 2024
1 parent a8e71cf commit f5af3f8
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 1 deletion.
10 changes: 9 additions & 1 deletion platforms/gke/base/use-cases/federated-learning/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ FEDERATED_LEARNING_SHARED_CONFIG_DIR="${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DI
# shellcheck disable=SC2034 # Variable is used in other scripts
FEDERATED_LEARNING_USE_CASE_INITIALIZE_SERVICE_DIR="${FEDERATED_LEARNING_USE_CASE_TERRAFORM_DIR}/initialize"

# Terraservices that are necessary for the core platform
federated_learning_core_platform_terraservices=(
"initialize"
"key_management_service"
)

# shellcheck disable=SC2034 # Variable is used in other scripts
federated_learning_terraservices=(
"initialize"
"${federated_learning_core_platform_terraservices[@]}"
"container_image_repository"
"private_google_access"
)
Expand All @@ -55,6 +61,8 @@ TERRAFORM_INIT_BACKEND_CONFIG_COMMAND=(

# shellcheck disable=SC2034 # Variable is used in other scripts
TERRAFORM_CLUSTER_CONFIGURATION=(
"cluster_database_encryption_state = \"ENCRYPTED\""
"cluster_database_encryption_key_name = \"cluster_database_encryption_key_name_placeholder\""
)

provision_terraservice() {
Expand Down
13 changes: 13 additions & 0 deletions platforms/gke/base/use-cases/federated-learning/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ for configuration_variable in "${TERRAFORM_CLUSTER_CONFIGURATION[@]}"; do
done
terraform fmt "${ACP_PLATFORM_SHARED_CONFIG_CLUSTER_AUTO_VARS_FILE}"

echo "Provision services that the core platform depends on"
# shellcheck disable=SC2154 # variable defined in common.sh
for terraservice in "${federated_learning_core_platform_terraservices[@]}"; do
provision_terraservice "${terraservice}"
done

if ! cluster_database_encryption_key_id="$(get_terraform_output "key_management_service" "cluster_database_encryption_key_id")"; then
echo "Error while getting cluster_database_encryption_key_id output"
exit 1
fi
# Use | as a separator in the sed command because substitution values might contain slashes
sed -i "s|cluster_database_encryption_key_name_placeholder|${cluster_database_encryption_key_id}|g" "${ACP_PLATFORM_SHARED_CONFIG_CLUSTER_AUTO_VARS_FILE}"

echo "Provisioning the core platform"
"${ACP_PLATFORM_CORE_DIR}/deploy.sh"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

locals {
gke_robot_service_account = "service-${data.google_project.default.number}@container-engine-robot.iam.gserviceaccount.com"
gke_robot_service_account_iam_email = "serviceAccount:${local.gke_robot_service_account}"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

terraform {
backend "gcs" {
bucket = ""
prefix = "terraform/federated-learning/key-management-service"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# KeyRings cannot be deleted; append a random suffix to the keyring name
resource "random_id" "keyring_suffix" {
byte_length = 4
}

resource "google_kms_key_ring" "key_ring" {
name = "${var.platform_name}-keyring-${random_id.keyring_suffix.hex}"
project = google_project_service.cloudkms_googleapis_com.project
location = var.cluster_region
}

resource "google_kms_crypto_key" "cluster_secrects_key" {
name = "clusterSecretsKey"
key_ring = google_kms_key_ring.key_ring.id
rotation_period = "7776000s"
purpose = "ENCRYPT_DECRYPT"
import_only = false
skip_initial_version_creation = false

lifecycle {
prevent_destroy = false
}

version_template {
# Ref: https://cloud.google.com/kms/docs/reference/rest/v1/CryptoKeyVersionAlgorithm
algorithm = "GOOGLE_SYMMETRIC_ENCRYPTION"

# Ref: https://cloud.google.com/kms/docs/reference/rest/v1/ProtectionLevel
protection_level = "SOFTWARE"
}
}

resource "google_kms_crypto_key_iam_binding" "cluster_secrets_decrypters" {
role = "roles/cloudkms.cryptoKeyDecrypter"
crypto_key_id = google_kms_crypto_key.cluster_secrects_key.id
members = [local.gke_robot_service_account_iam_email]
}

resource "google_kms_crypto_key_iam_binding" "cluster_secrets_encrypters" {
role = "roles/cloudkms.cryptoKeyEncrypter"
crypto_key_id = google_kms_crypto_key.cluster_secrects_key.id
members = [local.gke_robot_service_account_iam_email]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

output "cluster_database_encryption_key_id" {
description = "Id of the cluster database encryption key"
value = google_kms_crypto_key.cluster_secrects_key.id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

data "google_project" "default" {
project_id = var.cluster_project_id
}

resource "google_project_service" "cloudkms_googleapis_com" {
disable_dependent_services = false
disable_on_destroy = false
project = data.google_project.default.project_id
service = "cloudkms.googleapis.com"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

terraform {
required_version = ">= 1.5.7"

required_providers {
google = {
source = "hashicorp/google"
version = "6.12.0"
}
local = {
source = "hashicorp/local"
version = "2.5.2"
}
random = {
source = "hashicorp/random"
version = "3.6.3"
}
}

provider_meta "google" {
module_name = "cloud-solutions/acp_fl_kms_deploy-v1"
}
}

0 comments on commit f5af3f8

Please sign in to comment.