Cloudfront installers (#13)

This commit is contained in:
Robert Fairburn authored and GitHub committed 2025-03-04 18:09:33 -06:00
1 parent d2ba0fec3f
commit e8154d7bde
18 files changed
+676 -18

No files matched your search

@@ -0,0 +1,159 @@
# Cloudfront Software Installers
This module allows for Fleet software installers to be served via AWS Cloudfront instead of directly from Fleet.
This should improve the performance of software installer delivery. For general information about Fleet and Cloudfront see the following:
https://fleetdm.com/guides/cdn-signed-urls
https://victoronsoftware.com/posts/cloudfront-signed-urls/
The second link includes a script that can be used to test and see if signed URLs are working outside of Fleet for troubleshooting purposes.
## Other module requirements
These are the minimum versions of modules required if used:
- tf-mod-root-v1.13.0
- tf-mod-byo-vpc-v1.14.0
- tf-mod-byo-db-v1.10.0
- tf-mod-byo-ecs-v1.9.0
- tf-mod-addon-logging-alb-v1.3.0
Previous versions do not allow for proper interaction with both the software installers and logging s3 buckets.
## Configuration considerations for other modules
### tf-mod-root/tf-mod-byo-vpc/tf-mod-byo-db/tf-mod-byo-ecs
For any of these modules, the software installers configuration will require a KMS key created in order to be able to set a key policy.
This is the relevant configuration starting at the software installers configuration block:
```
software_installers = {
bucket_prefix = "fleet-software-installers-"
create_kms_key = true
kms_alias = "fleet-software-installers"
}
```
The new configuration items are `create_kms_key` and `kms_alias`.
### tf-mod-addon-logging-alb
No changes required if using at least version `tf-mod-addon-logging-alb-v1.3.0`. Bucket ACLs are changed to allow for the alb logging bucket to accept Cloudfront logs via ACLs.
## Configuration Example
This example assumes that you used the following commands to create your public and private key for consumption by the module:
```
openssl genrsa -out cloudfront.key 2048
openssl rsa -pubout -in cloudfront.pem -out cloudfront.pem
```
To be able to store these in source control in a sane manner, the objects will be KMS encrypted for storage at rest. This can happen by having a KMS key as follows:
```
resource "aws_kms_key" "customer_data_key" {
description = "key used to encrypt sensitive data stored in terraform"
}
resource "aws_kms_alias" "alias" {
name = "alias/fleet-terraform-encrypted"
target_key_id = aws_kms_key.customer_data_key.id
}
output "kms_key_id" {
value = aws_kms_key.customer_data_key.id
}
```
Then with the key, the following `encrypt.sh` script encrypt the objects:
```
#!/bin/bash
set -e
function usage() {
cat <<-EOUSAGE
Usage: $(basename ${0}) <KMS_KEY_ID> <SOURCE> <DESTINATION> [AWS_PROFILE]
This script encrypts an plaintext file from SOURCE into an
AWS KMS encrypted DESTINATION file. Optionally you
may provide the AWS_PROFILE you wish to use to run the aws kms
commands.
EOUSAGE
exit 1
}
[ $# -lt 3 ] && usage
if [ -n "${4}" ]; then
export AWS_PROFILE=${4}
fi
aws kms encrypt --key-id "${1:?}" --plaintext fileb://<(cat "${2:?}") --output text --query CiphertextBlob > "${3:?}"
```
We can do the following with that script to encrypt the objects:
```
./encrypt.sh <KMS_KEY_ID> cloudfront.key cloudfront.key.encrypted
./encrypt.sh <KMS_KEY_ID> cloudfront.pem cloudfront.pem.encrypted
```
Now with those encrypted we could setup the module with something like the following to populate the module (assuming we add the files to a /resources folder):
```
module "cloudfront-software-installers" {
source = "github.com/fleetdm/fleet-terraform/addons/cloudfront-software-installers?ref=tf-mod-addon-cloudfront-software-installers-v1.0.0"
customer = "fleet"
s3_bucket = module.main.byo-vpc.byo-db.byo-ecs.fleet_s3_software_installers_config.bucket_name
s3_kms_key_id = module.main.byo-vpc.byo-db.byo-ecs.fleet_s3_software_installers_config.kms_key_id
public_key = data.aws_kms_secrets.cloudfront.plaintext["public_key"]
private_key = data.aws_kms_secrets.cloudfront.plaintext["private_key"]
enable_logging = true
logging_s3_bucket = module.logging_alb.log_s3_bucket_id
}
data "aws_kms_secrets" "cloudfront" {
secret {
name = "public_key"
key_id = aws_kms_key.customer_data_key.id
payload = file("${path.module}/resources/cloudfront.pem.encrypted")
}
secret {
name = "private_key"
key_id = aws_kms_key.customer_data_key.id
payload = file("${path.module}/resources/cloudfront.key.encrypted")
}
}
```
Then we need to include outputs from this module once applied back into the main fleet-config under the `extra_secrets` and `extra_execution_roles`:
Under the `fleet_config` section. If not using the mdm module, that could be omitted but was included to show how to include multiple extra items:
```
fleet_config = {
...
extra_execution_iam_policies = concat(
module.mdm.extra_execution_iam_policies,
module.cloudfront-software-installers.extra_execution_iam_policies,
)
extra_secrets = merge(
module.mdm.extra_secrets,
module.cloudfront-software-installers.extra_secrets
)
}
```
@@ -0,0 +1 @@
header-from: .header.md
@@ -0,0 +1,212 @@
# Cloudfront Software Installers
This module allows for Fleet software installers to be served via AWS Cloudfront instead of directly from Fleet.
This should improve the performance of software installer delivery. For general information about Fleet and Cloudfront see the following:
https://fleetdm.com/guides/cdn-signed-urls
https://victoronsoftware.com/posts/cloudfront-signed-urls/
The second link includes a script that can be used to test and see if signed URLs are working outside of Fleet for troubleshooting purposes.
## Other module requirements
These are the minimum versions of modules required if used:
- tf-mod-root-v1.13.0
- tf-mod-byo-vpc-v1.14.0
- tf-mod-byo-db-v1.10.0
- tf-mod-byo-ecs-v1.9.0
- tf-mod-addon-logging-alb-v1.3.0
Previous versions do not allow for proper interaction with both the software installers and logging s3 buckets.
## Configuration considerations for other modules
### tf-mod-root/tf-mod-byo-vpc/tf-mod-byo-db/tf-mod-byo-ecs
For any of these modules, the software installers configuration will require a KMS key created in order to be able to set a key policy.
This is the relevant configuration starting at the software installers configuration block:
```
software_installers = {
bucket_prefix = "fleet-software-installers-"
create_kms_key = true
kms_alias = "fleet-software-installers"
}
```
The new configuration items are `create_kms_key` and `kms_alias`.
### tf-mod-addon-logging-alb
No changes required if using at least version `tf-mod-addon-logging-alb-v1.3.0`. Bucket ACLs are changed to allow for the alb logging bucket to accept Cloudfront logs via ACLs.
## Configuration Example
This example assumes that you used the following commands to create your public and private key for consumption by the module:
```
openssl genrsa -out cloudfront.key 2048
openssl rsa -pubout -in cloudfront.pem -out cloudfront.pem
```
To be able to store these in source control in a sane manner, the objects will be KMS encrypted for storage at rest. This can happen by having a KMS key as follows:
```
resource "aws_kms_key" "customer_data_key" {
description = "key used to encrypt sensitive data stored in terraform"
}
resource "aws_kms_alias" "alias" {
name = "alias/fleet-terraform-encrypted"
target_key_id = aws_kms_key.customer_data_key.id
}
output "kms_key_id" {
value = aws_kms_key.customer_data_key.id
}
```
Then with the key, the following `encrypt.sh` script encrypt the objects:
```
#!/bin/bash
set -e
function usage() {
cat <<-EOUSAGE
Usage: $(basename ${0}) <KMS_KEY_ID> <SOURCE> <DESTINATION> [AWS_PROFILE]
This script encrypts an plaintext file from SOURCE into an
AWS KMS encrypted DESTINATION file. Optionally you
may provide the AWS_PROFILE you wish to use to run the aws kms
commands.
EOUSAGE
exit 1
}
[ $# -lt 3 ] && usage
if [ -n "${4}" ]; then
export AWS_PROFILE=${4}
fi
aws kms encrypt --key-id "${1:?}" --plaintext fileb://<(cat "${2:?}") --output text --query CiphertextBlob > "${3:?}"
```
We can do the following with that script to encrypt the objects:
```
./encrypt.sh <KMS_KEY_ID> cloudfront.key cloudfront.key.encrypted
./encrypt.sh <KMS_KEY_ID> cloudfront.pem cloudfront.pem.encrypted
```
Now with those encrypted we could setup the module with something like the following to populate the module (assuming we add the files to a /resources folder):
```
module "cloudfront-software-installers" {
source = "github.com/fleetdm/fleet-terraform/addons/cloudfront-software-installers?ref=tf-mod-addon-cloudfront-software-installers-v1.0.0"
customer = "fleet"
s3_bucket = module.main.byo-vpc.byo-db.byo-ecs.fleet_s3_software_installers_config.bucket_name
s3_kms_key_id = module.main.byo-vpc.byo-db.byo-ecs.fleet_s3_software_installers_config.kms_key_id
public_key = data.aws_kms_secrets.cloudfront.plaintext["public_key"]
private_key = data.aws_kms_secrets.cloudfront.plaintext["private_key"]
enable_logging = true
logging_s3_bucket = module.logging_alb.log_s3_bucket_id
}
data "aws_kms_secrets" "cloudfront" {
secret {
name = "public_key"
key_id = aws_kms_key.customer_data_key.id
payload = file("${path.module}/resources/cloudfront.pem.encrypted")
}
secret {
name = "private_key"
key_id = aws_kms_key.customer_data_key.id
payload = file("${path.module}/resources/cloudfront.key.encrypted")
}
}
```
Then we need to include outputs from this module once applied back into the main fleet-config under the `extra_secrets` and `extra_execution_roles`:
Under the `fleet_config` section. If not using the mdm module, that could be omitted but was included to show how to include multiple extra items:
```
fleet_config = {
...
extra_execution_iam_policies = concat(
module.mdm.extra_execution_iam_policies,
module.cloudfront-software-installers.extra_execution_iam_policies,
)
extra_secrets = merge(
module.mdm.extra_secrets,
module.cloudfront-software-installers.extra_secrets
)
}
```
## Requirements
No requirements.
## Providers
| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.88.0 |
## Modules
| Name | Source | Version |
|------|--------|---------|
| <a name="module_cloudfront_software_installers"></a> [cloudfront\_software\_installers](#module\_cloudfront\_software\_installers) | terraform-aws-modules/cloudfront/aws | n/a |
## Resources
| Name | Type |
|------|------|
| [aws_cloudfront_key_group.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_key_group) | resource |
| [aws_cloudfront_public_key.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_public_key) | resource |
| [aws_iam_policy.software_installers_secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_kms_key_policy.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key_policy) | resource |
| [aws_s3_bucket_policy.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
| [aws_secretsmanager_secret.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret) | resource |
| [aws_secretsmanager_secret_version.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret_version) | resource |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.software_installers_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.software_installers_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.software_installers_secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_s3_bucket.logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_bucket) | data source |
| [aws_s3_bucket.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_bucket) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_customer"></a> [customer](#input\_customer) | Customer name for the cloudfront instance | `string` | `"fleet"` | no |
| <a name="input_enable_logging"></a> [enable\_logging](#input\_enable\_logging) | Enable optional logging to s3 | `bool` | `false` | no |
| <a name="input_logging_s3_bucket"></a> [logging\_s3\_bucket](#input\_logging\_s3\_bucket) | s3 bucket to log to | `string` | `null` | no |
| <a name="input_logging_s3_prefix"></a> [logging\_s3\_prefix](#input\_logging\_s3\_prefix) | logging s3 bucket prefix | `string` | `"cloudfront"` | no |
| <a name="input_private_key"></a> [private\_key](#input\_private\_key) | Private key used for signed URLs | `string` | n/a | yes |
| <a name="input_public_key"></a> [public\_key](#input\_public\_key) | Public key used for signed URLs | `string` | n/a | yes |
| <a name="input_s3_bucket"></a> [s3\_bucket](#input\_s3\_bucket) | Name of the S3 bucket that Cloudfront will point to | `string` | n/a | yes |
| <a name="input_s3_kms_key_id"></a> [s3\_kms\_key\_id](#input\_s3\_kms\_key\_id) | KMS key id used to encrypt the s3 bucket | `string` | n/a | yes |
## Outputs
| Name | Description |
|------|-------------|
| <a name="output_cloudfront_arn"></a> [cloudfront\_arn](#output\_cloudfront\_arn) | n/a |
| <a name="output_extra_execution_iam_policies"></a> [extra\_execution\_iam\_policies](#output\_extra\_execution\_iam\_policies) | n/a |
| <a name="output_extra_secrets"></a> [extra\_secrets](#output\_extra\_secrets) | n/a |
@@ -0,0 +1,154 @@
data "aws_caller_identity" "current" {}
data "aws_iam_policy_document" "software_installers_bucket" {
statement {
actions = ["s3:GetObject"]
resources = ["${data.aws_s3_bucket.software_installers.arn}/*"]
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudfront.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = [module.cloudfront_software_installers.cloudfront_distribution_arn]
}
}
}
data "aws_s3_bucket" "software_installers" {
bucket = var.s3_bucket
}
resource "aws_s3_bucket_policy" "software_installers" {
bucket = data.aws_s3_bucket.software_installers.bucket
policy = data.aws_iam_policy_document.software_installers_bucket.json
}
data "aws_iam_policy_document" "software_installers_kms" {
statement {
actions = ["kms:*"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
}
resources = ["*"]
}
statement {
sid = "AllowOriginAccessIdentity"
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudfront.amazonaws.com"]
}
actions = ["kms:Decrypt"]
resources = ["*"]
condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = [module.cloudfront_software_installers.cloudfront_distribution_arn]
}
}
}
resource "aws_kms_key_policy" "software_installers" {
key_id = var.s3_kms_key_id
policy = data.aws_iam_policy_document.software_installers_kms.json
}
data "aws_iam_policy_document" "software_installers_secret" {
statement {
effect = "Allow"
actions = ["secretsmanager:GetSecretValue"]
resources = [aws_secretsmanager_secret.software_installers.arn]
}
}
resource "aws_iam_policy" "software_installers_secret" {
policy = data.aws_iam_policy_document.software_installers_secret.json
}
resource "aws_cloudfront_public_key" "software_installers" {
comment = "${var.customer} software installers public key"
encoded_key = var.public_key
name = "${var.customer}-software-installers"
}
resource "aws_cloudfront_key_group" "software_installers" {
comment = "${var.customer} software installers key group"
items = [aws_cloudfront_public_key.software_installers.id]
name = "${var.customer}-software-installers-group"
}
resource "aws_secretsmanager_secret" "software_installers" {
name = "${var.customer}-software-installers"
}
resource "aws_secretsmanager_secret_version" "software_installers" {
secret_id = aws_secretsmanager_secret.software_installers.id
secret_string = jsonencode({
FLEET_S3_SOFTWARE_INSTALLERS_CLOUDFRONT_URL_SIGNING_PRIVATE_KEY = var.private_key
FLEET_S3_SOFTWARE_INSTALLERS_CLOUDFRONT_URL_SIGNING_PUBLC_KEY = var.public_key
FLEET_S3_SOFTWARE_INSTALLERS_CLOUDFRONT_URL = "https://${module.cloudfront_software_installers.cloudfront_distribution_domain_name}"
FLEET_S3_SOFTWARE_INSTALLERS_CLOUDFRONT_URL_SIGNING_PUBLIC_KEY_ID = aws_cloudfront_public_key.software_installers.id
})
}
data "aws_s3_bucket" "logging" {
bucket = var.logging_s3_bucket
}
module "cloudfront_software_installers" {
source = "terraform-aws-modules/cloudfront/aws"
comment = "${var.customer} software installers"
enabled = true
# We're not using IPV6 elsewhere. Turn it on across the board when we want it.
is_ipv6_enabled = false
price_class = "PriceClass_All"
retain_on_delete = false
wait_for_deployment = false
create_origin_access_identity = false
create_origin_access_control = true
origin_access_control = {
"${var.customer}-software-installers" = {
description = "${var.customer}-software-installers"
origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}
}
# setup a logging bucket
logging_config = var.enable_logging == true ? {
bucket = data.aws_s3_bucket.logging.bucket_domain_name
prefix = var.logging_s3_prefix
} : {
bucket = null
prefix = null
}
origin = {
s3_one = {
domain_name = data.aws_s3_bucket.software_installers.bucket_domain_name
origin_access_control = "${var.customer}-software-installers"
}
}
default_cache_behavior = {
target_origin_id = "s3_one"
viewer_protocol_policy = "redirect-to-https"
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
compress = true
query_string = true
trusted_key_groups = [aws_cloudfront_key_group.software_installers.id]
}
ordered_cache_behavior = []
}
@@ -0,0 +1,15 @@
output "extra_secrets" {
value = {
FLEET_S3_SOFTWARE_INSTALLERS_CLOUDFRONT_URL_SIGNING_PRIVATE_KEY = "${aws_secretsmanager_secret.software_installers.arn}:FLEET_S3_SOFTWARE_INSTALLERS_CLOUDFRONT_URL_SIGNING_PRIVATE_KEY::"
FLEET_S3_SOFTWARE_INSTALLERS_CLOUDFRONT_URL_SIGNING_PUBLIC_KEY_ID = "${aws_secretsmanager_secret.software_installers.arn}:FLEET_S3_SOFTWARE_INSTALLERS_CLOUDFRONT_URL_SIGNING_PUBLIC_KEY_ID::"
FLEET_S3_SOFTWARE_INSTALLERS_CLOUDFRONT_URL = "${aws_secretsmanager_secret.software_installers.arn}:FLEET_S3_SOFTWARE_INSTALLERS_CLOUDFRONT_URL::"
}
}
output "extra_execution_iam_policies" {
value = [aws_iam_policy.software_installers_secret.arn]
}
output "cloudfront_arn" {
value = module.cloudfront_software_installers.cloudfront_distribution_arn
}
@@ -0,0 +1,43 @@
variable "customer" {
description = "Customer name for the cloudfront instance"
type = string
default = "fleet"
}
variable "private_key" {
description = "Private key used for signed URLs"
type = string
}
variable "public_key" {
description = "Public key used for signed URLs"
type = string
}
variable "s3_bucket" {
description = "Name of the S3 bucket that Cloudfront will point to"
type = string
}
variable "s3_kms_key_id" {
description = "KMS key id used to encrypt the s3 bucket"
type = string
}
variable "enable_logging" {
description = "Enable optional logging to s3"
type = bool
default = false
}
variable "logging_s3_bucket" {
description = "s3 bucket to log to"
type = string
default = null
}
variable "logging_s3_prefix" {
description = "logging s3 bucket prefix"
type = string
default = "cloudfront"
}