Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add PKCS11 automations #7

Merged
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2d7d9f0
Add PKCS11 automations
romanini-ciandt Apr 17, 2024
db156cd
Add implicit dependency
romanini-ciandt Apr 17, 2024
8df94ae
Add a service identity
romanini-ciandt Apr 17, 2024
e1fb57f
Attempt without impersonate resource
romanini-ciandt Apr 17, 2024
b874d5e
Attemp hardcoding CFT sa
romanini-ciandt Apr 17, 2024
ec0d19b
Attemp removing impersonate
romanini-ciandt Apr 17, 2024
c0ddbb6
Attempt with service agent
romanini-ciandt Apr 17, 2024
c1b749c
Add service identity again
romanini-ciandt Apr 17, 2024
134866c
Add a second impersonate permission
romanini-ciandt Apr 18, 2024
3445b0d
Isolate a single test
romanini-ciandt Apr 18, 2024
94ea860
Add project number output
romanini-ciandt Apr 18, 2024
2ca4c4e
add self impersonate
romanini-ciandt Apr 18, 2024
255606d
Attemp with sa names
romanini-ciandt Apr 18, 2024
300d2b2
Test self impersonate 2
romanini-ciandt Apr 18, 2024
84f5880
owner test
romanini-ciandt Apr 18, 2024
5ffe4ad
Enable debug in CB
romanini-ciandt Apr 18, 2024
8a9dfd6
Enable billing API
romanini-ciandt Apr 18, 2024
66cb616
Add billing account API
romanini-ciandt Apr 19, 2024
9d9df4f
Fix deppends on
romanini-ciandt Apr 19, 2024
2601b54
Add owner to cloudbuild SA
romanini-ciandt Apr 29, 2024
5028d20
Attemp with no sa in cloudbuild file
romanini-ciandt Apr 29, 2024
7e88d8d
Add imporsonate 2
romanini-ciandt Apr 29, 2024
eec3606
Attempt renabling CB
romanini-ciandt Apr 30, 2024
4dea9f2
Add owner
romanini-ciandt Apr 30, 2024
4db9fba
Removing examples for now
romanini-ciandt Apr 30, 2024
974d47a
Merge branch 'main' of github.com:romanini-ciandt/kms-solutions into …
romanini-ciandt Apr 30, 2024
186256f
Update oss-terraform-automation/README.md
romanini-ciandt May 3, 2024
7515e9e
Merge branch 'main' into feat/pkcs11-automations
romanini-ciandt May 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/pkcs11-apache-web-server/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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.
*/

module "pkcs11_apache_web_server" {
source = "../../pkcs-11-terraform-automation/1-apache-web-server"

project_id = var.project_id
keyring = "sample-keyring"
key = "sample-key"
artifact_image = "sample-image"
prevent_destroy = false
docker_file_path = "../../pkcs-11-terraform-automation/1-apache-web-server"
}
26 changes: 26 additions & 0 deletions examples/temp/pkcs11-cng-provider/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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.
*/

module "pkcs11_apache_web_server" {
source = "../../pkcs-11-terraform-automation/3-cng-provider"

project_id = var.project_id
keyring = "sample-keyring"
key = "sample-key"
artifact_image = "sample-image"
prevent_destroy = false
docker_file_path = "../../pkcs-11-terraform-automation/3-cng-provider"
}
20 changes: 20 additions & 0 deletions examples/temp/pkcs11-cng-provider/variables.tf
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.
*/

variable "project_id" {
description = "GCP project ID to use for the creation of resources."
type = string
}
26 changes: 26 additions & 0 deletions examples/temp/pkcs11-nginx-ssl-offloading/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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.
*/

module "pkcs11_apache_web_server" {
source = "../../pkcs-11-terraform-automation/2-nginx-ssl-offloading"

project_id = var.project_id
keyring = "sample-keyring"
key = "sample-key"
artifact_image = "sample-image"
prevent_destroy = false
docker_file_path = "../../pkcs-11-terraform-automation/2-nginx-ssl-offloading"
}
20 changes: 20 additions & 0 deletions examples/temp/pkcs11-nginx-ssl-offloading/variables.tf
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.
*/

variable "project_id" {
description = "GCP project ID to use for the creation of resources."
type = string
}
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.
*/

variable "project_id" {
description = "GCP project ID to use for the creation of resources."
type = string
}
90 changes: 90 additions & 0 deletions pkcs-11-terraform-automation/1-apache-web-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# 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
#
# https://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.

FROM ubuntu/apache2

# Install the OpenSSL PKCS #11 library
RUN apt-get update && apt-get install -y libengine-pkcs11-openssl wget curl

# Using ARGs so user can provide the custom variables required to create pkcs11 config file
ARG PROJECT_ID
ENV ENV_PROJECT_ID=$PROJECT_ID

ARG KEYRING_NAME
ENV ENV_KEYRING_NAME=$KEYRING_NAME

ARG KEY_NAME
ENV ENV_KEY_NAME=$KEY_NAME

ARG LOCATION
ENV ENV_LOCATION=$LOCATION

ARG PKCS11_LIB_VERSION
ENV ENV_PKCS11_LIB_VERSION=$PKCS11_LIB_VERSION

ARG CERTIFICATE_NAME
ENV ENV_CERTIFICATE_NAME=$CERTIFICATE_NAME

ARG CERTIFICATE_FILE
ENV ENV_CERTIFICATE_FILE=$CERTIFICATE_FILE

ARG DIGEST_FLAG
ENV ENV_DIGEST_FLAG=$DIGEST_FLAG

# Download the GCP pkcs11 library
RUN wget "https://github.com/GoogleCloudPlatform/kms-integrations/releases/download/pkcs11-v${ENV_PKCS11_LIB_VERSION}/libkmsp11-${ENV_PKCS11_LIB_VERSION}-linux-amd64.tar.gz"

# Extract the library to a specific directory
RUN mkdir -p /opt/libkmsp11 && \
tar -xf "libkmsp11-${ENV_PKCS11_LIB_VERSION}-linux-amd64.tar.gz" -C /opt/libkmsp11

RUN echo "---\ntokens:\n - key_ring: \"projects/${ENV_PROJECT_ID}/locations/${ENV_LOCATION}/keyRings/${ENV_KEYRING_NAME}\"\n" > /opt/libkmsp11/kms-pkcs11.conf

# Export PKCS #11 required env vars
ENV PKCS11_MODULE_PATH="/opt/libkmsp11/libkmsp11-${ENV_PKCS11_LIB_VERSION}-linux-amd64/libkmsp11.so"
ENV KMS_PKCS11_CONFIG="/opt/libkmsp11/kms-pkcs11.conf"
ENV GRPC_ENABLE_FORK_SUPPORT=1

# Create a self-signed certificate with the Cloud KMS-hosted signing key
RUN openssl req -new -x509 -days 3650 -subj '/CN='${ENV_CERTIFICATE_NAME}'/' \
-${ENV_DIGEST_FLAG} -engine pkcs11 -keyform engine \
-key pkcs11:object=${ENV_KEY_NAME} > /opt/ca.cert

# Override the self-signed certificate if user provided a certificate as an input
RUN [ -n "$ENV_CERTIFICATE_FILE" ] && echo "$ENV_CERTIFICATE_FILE" > /opt/ca.cert || true

RUN mkdir /etc/apache2/ssl
RUN mv /opt/ca.cert /etc/apache2/ssl

# Add the SSL conf to the Apache configuration file
RUN echo "<VirtualHost *:443>\n\
ServerAdmin webmaster@localhost\n\
DocumentRoot /var/www/html\n\
ErrorLog \${APACHE_LOG_DIR}/error.log\n\
CustomLog \${APACHE_LOG_DIR}/access.log combined\n\
SSLEngine on\n\
SSLCertificateFile /etc/apache2/ssl/ca.cert\n\
SSLCertificateKeyFile \"pkcs11:object=${ENV_KEY_NAME}\"\n\
</VirtualHost>" >> /etc/apache2/sites-available/000-default.conf

# Enable the Apache SSL module, enable the virtualhost configuration, and add a test web page in your DocumentRoot folder
RUN a2enmod ssl
RUN a2ensite 000-default.conf
RUN echo '<!doctype html><html><body><h1>Hello World!</h1></body></html>' | \
tee /var/www/html/index.html

EXPOSE 443

# Start Apache in the foreground
CMD ["apache2ctl", "-D", "FOREGROUND"]
89 changes: 89 additions & 0 deletions pkcs-11-terraform-automation/1-apache-web-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Use a Cloud HSM key for TLS offloading with NGINX

## Overview

This guide provides instructions for setting up a GCP infrastructure with Apache server using Cloud HSM key for TLS signing with Terraform.

## Prerequisites

- [Terraform](https://developer.hashicorp.com/terraform/downloads);
- [Google Cloud CLI (`gcloud`)](https://cloud.google.com/sdk/docs/install-sdk);
- You must be authenticated in your GCP account. If you're not you should run `gcloud auth login`;
- An existing [GCP project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project);
- Enable GCP services in the project created above:
- compute.googleapis.com
- iam.googleapis.com
- artifactregistry.googleapis.com
- cloudbuild.googleapis.com
- cloudkms.googleapis.com

**Note:** You can enable these services using `gcloud services enable <SERVICE>` command or terraform automation would auto-enable them for you.

- (Optional) An existing [GCP Organization](https://cloud.google.com/resource-manager/docs/creating-managing-organization);
- If you provide a `organization_id` variable in `terraform.tfvars`, the Terraform automation will configure the following organization policies: `constraints/compute.vmExternalIpAccess` and `constraints/iam.disableServiceAccountKeyCreation`;

**Note:** This automation won't work if you use `pkcs11_lib_version` variable lower than `1.3`

## Deploy infrastructure

1. Rename `terraform.example.tfvars` to `terraform.tfvars`:
```sh
mv terraform.example.tfvars terraform.tfvars
```

1. Update `terraform.tfvars` file with the required values.

1. Create the infrastructure.

```sh
terraform init
terraform plan
terraform apply
```

1. Connect into the Compute Engine VM using IAP and `gcloud` command:
```sh
gcloud compute ssh --zone "us-central1-a" "apache-hostname-example" --tunnel-through-iap --project "REPLACE-WITH-YOUR-EXISTING-PROJECT-ID"
```
**Note:** You can run the command above from Cloud Shell (recommended) or locally (additional permissions may be required)

1. Run the following command in the Compute Engine VM shell. You should see a succesful request output.
```sh
container_id=$(docker ps -q | head -n 1)
docker exec "$container_id" curl -v --insecure https://127.0.0.1
```
**Note:** The successful output should contain information about the certificate and a `HTTP/1.1 200 OK` string.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| artifact\_image | Image's name stored in Artifact Registry. | `string` | n/a | yes |
| artifact\_location | Location's name of the image stored in Artifact Registry. | `string` | `"us-central1"` | no |
| artifact\_repository | Respository's name of the image stored in Artifact Registry. | `string` | `"hsm-cloud-example"` | no |
| artifact\_version | Version of the image stored in Artifact Registry. | `string` | `"latest"` | no |
| certificate\_file\_path | Certificate file path to be used on sign process. This should be used when you have a certificate file signed by a Certificate Authority. If not provided, a self-signed certificate will be generated with OpenSSL. Use self-signed certificate for testing only. A self-signed certificate created this way is not appropriate for production use. | `string` | `null` | no |
| certificate\_name | A name for the certificate that you want to generate. This will be used on CN parameter for certificate signing requests or/and self-signed certificates. | `string` | `"TERRAFORM_CERT"` | no |
| digest\_flag | A flag indicating the type of digest. Use sha256, sha384, or sha512 depending on the algorithm of the key. | `string` | `"sha256"` | no |
| docker\_file\_path | The Dockerfile path. | `string` | `"./"` | no |
| hostname | Name of the GCE VM host. | `string` | `"apache-hostname-example"` | no |
| key | Name of the key to be created. | `string` | n/a | yes |
| keyring | Name of the keyring to be created. | `string` | n/a | yes |
| location | Location for the keyring. For available KMS locations see: https://cloud.google.com/kms/docs/locations. | `string` | `"us-central1"` | no |
| organization\_id | GCP organization ID that will used to apply desired Org Policies. If not provided, Org Policies won't be applied. | `string` | `""` | no |
| pkcs11\_lib\_version | Version of the PKCS #11 library version. This automation is not compatible with version lower than 1.3. To see more info about versions available: https://github.com/GoogleCloudPlatform/kms-integrations/releases?q=pkcs%2311&expanded=true | `string` | `"1.3"` | no |
| prevent\_destroy | Set the prevent\_destroy lifecycle attribute on keys. | `bool` | `true` | no |
| project\_id | GCP project ID to use for the creation of resources. | `string` | n/a | yes |
| suffix | A suffix to be used as an identifier for resources. (e.g., suffix for KMS Key, Keyring, SAs, etc.). If not provided, a 4 character random one will be generated. | `string` | `""` | no |

## Outputs

| Name | Description |
|------|-------------|
| key | Name of the key created. |
| keyring | Name of the keyring. |
| location | Location of the keyring created. |
| project\_id | ID of the GCP project being used. |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
37 changes: 37 additions & 0 deletions pkcs-11-terraform-automation/1-apache-web-server/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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.
*/

module "bootstrap-kms-hsm" {
source = "../common/modules/bootstrap-kms-hsm"

project_id = var.project_id
keyring = var.keyring
key = var.key
location = var.location
prevent_destroy = var.prevent_destroy
suffix = var.suffix
artifact_image = var.artifact_image
artifact_location = var.artifact_location
artifact_repository = var.artifact_repository
artifact_version = var.artifact_version
hostname = var.hostname
organization_id = var.organization_id
pkcs11_lib_version = var.pkcs11_lib_version
certificate_file_path = var.certificate_file_path
digest_flag = var.digest_flag
certificate_name = var.certificate_name
docker_file_path = var.docker_file_path
}
35 changes: 35 additions & 0 deletions pkcs-11-terraform-automation/1-apache-web-server/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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 "keyring" {
description = "Name of the keyring."
value = module.bootstrap-kms-hsm.keyring
}

output "location" {
description = "Location of the keyring created."
value = module.bootstrap-kms-hsm.location
}

output "key" {
description = "Name of the key created."
value = module.bootstrap-kms-hsm.key
}

output "project_id" {
description = "ID of the GCP project being used."
value = module.bootstrap-kms-hsm.project_id
}
Loading
Loading