From 6cebbf2cff5e51476d5dbd434c6c7ff8190c89cb Mon Sep 17 00:00:00 2001 From: Robert Fairburn <8029478+rfairburn@users.noreply.github.com> Date: Thu, 31 Jul 2025 10:23:24 -0500 Subject: [PATCH] Initial snowflake module config (#72) --- .../logging-destination-snowflake/.header.md | 114 +++++++++++++ .../.terraform-docs.yml | 1 + .../logging-destination-snowflake/README.md | 161 ++++++++++++++++++ addons/logging-destination-snowflake/main.tf | 145 ++++++++++++++++ .../logging-destination-snowflake/outputs.tf | 20 +++ .../variables.tf | 86 ++++++++++ 6 files changed, 527 insertions(+) create mode 100644 addons/logging-destination-snowflake/.header.md create mode 100644 addons/logging-destination-snowflake/.terraform-docs.yml create mode 100644 addons/logging-destination-snowflake/README.md create mode 100644 addons/logging-destination-snowflake/main.tf create mode 100644 addons/logging-destination-snowflake/outputs.tf create mode 100644 addons/logging-destination-snowflake/variables.tf diff --git a/addons/logging-destination-snowflake/.header.md b/addons/logging-destination-snowflake/.header.md new file mode 100644 index 0000000..7ca4541 --- /dev/null +++ b/addons/logging-destination-snowflake/.header.md @@ -0,0 +1,114 @@ +# Logging Destination: Snowflake + +This addon configures AWS Kinesis Firehose to send Fleet's osquery logs to Snowflake. It creates: + +1. Kinesis Firehose delivery streams for each log type (results, status, and audit) +2. A single S3 bucket for storing all failed delivery attempts +3. IAM roles and policies for the Firehose streams to access the S3 bucket +4. An IAM policy for Fleet to access the Firehose streams + +## How to use + +```hcl +module "snowflake-logging" { + source = "github.com/fleetdm/fleet-terraform//addons/logging-destination-snowflake?ref=tf-mod-addon-logging-destination-snowflake-v1.0.0" + +module "snowflake-logging" { + source = "github.com/fleetdm/fleet-terraform/addons/logging-destination-snowflake?depth=1&ref=logging-destination-snowflake" + + s3_bucket_config = { + name_prefix = "fleet-snowflake-failure" + expires_days = 5 + } + snowflake_shared = { + account_url = "https://.snowflakecomputing.com" + private_key = "" + key_passphrase = "" + user = "fleet_user" + snowflake_role_configuration = { + enabled = true + snowflake_role = "fleet_cloud_rl" + } + } + + log_destinations = { + results = { + name = "fleet-osquery-results-snowflake" + database = "fleet_cloud_db" + schema = "fleet_cloud_schema" + table = "osquery_results" + buffering_size = 2 + buffering_interval = 60 + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_buffering_interval = 400 + s3_error_output_prefix = "results/" + data_loading_option = "VARIANT_CONTENT_MAPPING" + content_column_name = "results" + }, + status = { + name = "fleet-osquery-status-snowflake" + database = "fleet_cloud_db" + schema = "fleet_cloud_schema" + table = "osquery_status" + user = "fleet" + buffering_size = 2 + buffering_interval = 60 + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_buffering_interval = 400 + s3_error_output_prefix = "status/" + data_loading_option = "VARIANT_CONTENT_MAPPING" + content_column_name = "status" + }, + audit = { + name = "fleet-audit-snowflake" + database = "fleet_cloud_db" + schema = "fleet_cloud_schema" + table = "fleet_audit" + buffering_size = 2 + buffering_interval = 60 + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_error_output_prefix = "audit/" + } + } +} + +``` + +Then you can use the module's outputs in your Fleet configuration: + +```hcl +module "fleet" { + source = "github.com/fleetdm/fleet-terraform?depth=1&ref=tf-mod-root-v1.16.4" + certificate_arn = module.acm.acm_certificate_arn + + vpc = { + name = local.vpc_name + # azs = ["us-east-2a", "us-east-2b", "us-east-2c"] + } + + fleet_config = { + image = "fleetdm/fleet:v4.70.1" + autoscaling = { + min_capacity = 2 + max_capacity = 5 + } + mem = 4096 + cpu = 512 + extra_environment_variables = merge( + local.fleet_environment_variables, + # Uncomment to enable Snowflake logging + module.snowflake-logging.fleet_extra_environment_variables + ) + extra_iam_policies = concat( + # Uncomment to enable Snowflake logging + module.snowflake-logging.fleet_extra_iam_policies, + ) + } + + # ... other Fleet configuration ... +} +``` + diff --git a/addons/logging-destination-snowflake/.terraform-docs.yml b/addons/logging-destination-snowflake/.terraform-docs.yml new file mode 100644 index 0000000..e532fe1 --- /dev/null +++ b/addons/logging-destination-snowflake/.terraform-docs.yml @@ -0,0 +1 @@ +header-from: .header.md \ No newline at end of file diff --git a/addons/logging-destination-snowflake/README.md b/addons/logging-destination-snowflake/README.md new file mode 100644 index 0000000..429cfa5 --- /dev/null +++ b/addons/logging-destination-snowflake/README.md @@ -0,0 +1,161 @@ +# Logging Destination: Snowflake + +This addon configures AWS Kinesis Firehose to send Fleet's osquery logs to Snowflake. It creates: + +1. Kinesis Firehose delivery streams for each log type (results, status, and audit) +2. A single S3 bucket for storing all failed delivery attempts +3. IAM roles and policies for the Firehose streams to access the S3 bucket +4. An IAM policy for Fleet to access the Firehose streams + +## How to use + +```hcl +module "snowflake-logging" { + source = "github.com/fleetdm/fleet-terraform//addons/logging-destination-snowflake?ref=tf-mod-addon-logging-destination-snowflake-v1.0.0" + +module "snowflake-logging" { + source = "github.com/fleetdm/fleet-terraform/addons/logging-destination-snowflake?depth=1&ref=logging-destination-snowflake" + + s3_bucket_config = { + name_prefix = "fleet-snowflake-failure" + expires_days = 5 + } + snowflake_shared = { + account_url = "https://.snowflakecomputing.com" + private_key = "" + key_passphrase = "" + user = "fleet_user" + snowflake_role_configuration = { + enabled = true + snowflake_role = "fleet_cloud_rl" + } + } + + log_destinations = { + results = { + name = "fleet-osquery-results-snowflake" + database = "fleet_cloud_db" + schema = "fleet_cloud_schema" + table = "osquery_results" + buffering_size = 2 + buffering_interval = 60 + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_buffering_interval = 400 + s3_error_output_prefix = "results/" + data_loading_option = "VARIANT_CONTENT_MAPPING" + content_column_name = "results" + }, + status = { + name = "fleet-osquery-status-snowflake" + database = "fleet_cloud_db" + schema = "fleet_cloud_schema" + table = "osquery_status" + user = "fleet" + buffering_size = 2 + buffering_interval = 60 + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_buffering_interval = 400 + s3_error_output_prefix = "status/" + data_loading_option = "VARIANT_CONTENT_MAPPING" + content_column_name = "status" + }, + audit = { + name = "fleet-audit-snowflake" + database = "fleet_cloud_db" + schema = "fleet_cloud_schema" + table = "fleet_audit" + buffering_size = 2 + buffering_interval = 60 + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_error_output_prefix = "audit/" + } + } +} + +``` + +Then you can use the module's outputs in your Fleet configuration: + +```hcl +module "fleet" { + source = "github.com/fleetdm/fleet-terraform?depth=1&ref=tf-mod-root-v1.16.4" + certificate_arn = module.acm.acm_certificate_arn + + vpc = { + name = local.vpc_name + # azs = ["us-east-2a", "us-east-2b", "us-east-2c"] + } + + fleet_config = { + image = "fleetdm/fleet:v4.70.1" + autoscaling = { + min_capacity = 2 + max_capacity = 5 + } + mem = 4096 + cpu = 512 + extra_environment_variables = merge( + local.fleet_environment_variables, + # Uncomment to enable Snowflake logging + module.snowflake-logging.fleet_extra_environment_variables + ) + extra_iam_policies = concat( + # Uncomment to enable Snowflake logging + module.snowflake-logging.fleet_extra_iam_policies, + ) + } + + # ... other Fleet configuration ... +} +``` + +## Requirements + +No requirements. + +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | 6.4.0 | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [aws_iam_policy.firehose](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource | +| [aws_iam_policy.firehose-logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource | +| [aws_iam_role.firehose](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource | +| [aws_iam_role_policy_attachment.firehose](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource | +| [aws_kinesis_firehose_delivery_stream.snowflake](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kinesis_firehose_delivery_stream) | resource | +| [aws_s3_bucket.snowflake-failure](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource | +| [aws_s3_bucket_lifecycle_configuration.snowflake-failure](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource | +| [aws_s3_bucket_public_access_block.snowflake-failure](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource | +| [aws_s3_bucket_server_side_encryption_configuration.snowflake-failure](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource | +| [aws_iam_policy_document.firehose-logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_policy_document.firehose_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_policy_document.osquery_firehose_assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [compression\_format](#input\_compression\_format) | Compression format for the Firehose delivery stream | `string` | `"UNCOMPRESSED"` | no | +| [log\_destinations](#input\_log\_destinations) | A map of configurations for Snowflake Firehose delivery streams. |
map(object({
name = string
database = string
schema = string
table = string
buffering_size = number
buffering_interval = number
s3_buffering_size = number
s3_buffering_interval = number
s3_error_output_prefix = optional(string, null)
data_loading_option = optional(string, "JSON_MAPPING")
content_column_name = optional(string, null)
metadata_column_name = optional(string, null)
}))
|
{
"audit": {
"buffering_interval": 60,
"buffering_size": 2,
"database": "fleet",
"name": "fleet-audit-snowflake",
"s3_buffering_interval": 400,
"s3_buffering_size": 10,
"schema": "fleet_schema",
"table": "fleet_audit"
},
"results": {
"buffering_interval": 60,
"buffering_size": 2,
"database": "fleet",
"name": "fleet-osquery-results-snowflake",
"s3_buffering_interval": 400,
"s3_buffering_size": 10,
"schema": "fleet_schema",
"table": "osquery_results"
},
"status": {
"buffering_interval": 60,
"buffering_size": 2,
"database": "fleet",
"name": "fleet-osquery-status-snowflake",
"s3_buffering_interval": 400,
"s3_buffering_size": 10,
"schema": "fleet_schema",
"table": "osquery_status",
"user": "fleet"
}
}
| no | +| [s3\_bucket\_config](#input\_s3\_bucket\_config) | Configuration for the S3 bucket used to store failed Snowflake delivery attempts |
object({
name_prefix = optional(string, "fleet-snowflake-failure")
expires_days = optional(number, 1)
})
|
{
"expires_days": 1,
"name_prefix": "fleet-snowflake-failure"
}
| no | +| [snowflake\_shared](#input\_snowflake\_shared) | Shared configurations among each logging destination |
object({
account_url = string
private_key = string
key_passphrase = optional(string, null)
user = string
snowflake_role_configuration = object({
enabled = bool
snowflake_role = optional(string, null)
})
snowflake_vpc_configuration = optional(object({
private_link_vpce_id = string
}), {
private_link_vpce_id = null
})
})
| n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| [fleet\_extra\_environment\_variables](#output\_fleet\_extra\_environment\_variables) | Environment variables to configure Fleet to use Snowflake logging via Firehose | +| [fleet\_extra\_iam\_policies](#output\_fleet\_extra\_iam\_policies) | IAM policies required for Fleet to log to Snowflake via Firehose | diff --git a/addons/logging-destination-snowflake/main.tf b/addons/logging-destination-snowflake/main.tf new file mode 100644 index 0000000..11c9882 --- /dev/null +++ b/addons/logging-destination-snowflake/main.tf @@ -0,0 +1,145 @@ +data "aws_region" "current" {} + +resource "aws_s3_bucket" "snowflake-failure" { #tfsec:ignore:aws-s3-encryption-customer-key:exp:2022-07-01 #tfsec:ignore:aws-s3-enable-versioning #tfsec:ignore:aws-s3-enable-bucket-logging:exp:2022-06-15 + bucket_prefix = var.s3_bucket_config.name_prefix + force_destroy = true +} + +resource "aws_s3_bucket_lifecycle_configuration" "snowflake-failure" { + bucket = aws_s3_bucket.snowflake-failure.bucket + rule { + status = "Enabled" + id = "expire" + filter { + prefix = "" + } + expiration { + days = var.s3_bucket_config.expires_days + } + } +} + +resource "aws_s3_bucket_server_side_encryption_configuration" "snowflake-failure" { + bucket = aws_s3_bucket.snowflake-failure.bucket + rule { + apply_server_side_encryption_by_default { + sse_algorithm = "aws:kms" + } + } +} + +resource "aws_s3_bucket_public_access_block" "snowflake-failure" { + bucket = aws_s3_bucket.snowflake-failure.id + block_public_acls = true + block_public_policy = true + ignore_public_acls = true + restrict_public_buckets = true +} + +data "aws_iam_policy_document" "firehose_policy" { + statement { + effect = "Allow" + actions = [ + "s3:AbortMultipartUpload", + "s3:GetBucketLocation", + "s3:ListBucket", + "s3:ListBucketMultipartUploads", + "s3:PutObject" + ] + // This bucket is single-purpose and using a wildcard is not problematic + resources = [ + aws_s3_bucket.snowflake-failure.arn, + "${aws_s3_bucket.snowflake-failure.arn}/*" + ] #tfsec:ignore:aws-iam-no-policy-wildcards + } +} + +resource "aws_iam_policy" "firehose" { + name = "snowflake_firehose_policy" + policy = data.aws_iam_policy_document.firehose_policy.json +} + +resource "aws_iam_role" "firehose" { + assume_role_policy = data.aws_iam_policy_document.osquery_firehose_assume_role.json +} + +resource "aws_iam_role_policy_attachment" "firehose" { + policy_arn = aws_iam_policy.firehose.arn + role = aws_iam_role.firehose.name +} + +data "aws_iam_policy_document" "osquery_firehose_assume_role" { + statement { + effect = "Allow" + actions = ["sts:AssumeRole"] + principals { + identifiers = ["firehose.amazonaws.com"] + type = "Service" + } + } +} + +resource "aws_kinesis_firehose_delivery_stream" "snowflake" { + for_each = var.log_destinations + name = each.value.name + destination = "snowflake" + + server_side_encryption { + enabled = true + } + + snowflake_configuration { + account_url = var.snowflake_shared.account_url + database = each.value.database + private_key = var.snowflake_shared.private_key + key_passphrase = var.snowflake_shared.key_passphrase + schema = each.value.schema + table = each.value.table + user = var.snowflake_shared.user + buffering_size = each.value.buffering_size + buffering_interval = each.value.buffering_interval + role_arn = aws_iam_role.firehose.arn + s3_backup_mode = "FailedDataOnly" + data_loading_option = each.value.data_loading_option + content_column_name = each.value.content_column_name + metadata_column_name = each.value.metadata_column_name + + snowflake_role_configuration { + enabled = var.snowflake_shared.snowflake_role_configuration.enabled + snowflake_role = var.snowflake_shared.snowflake_role_configuration.snowflake_role + } + + dynamic "snowflake_vpc_configuration" { + for_each = var.snowflake_shared.snowflake_vpc_configuration.private_link_vpce_id == null ? [] : [1] + + content { + private_link_vpce_id = var.snowflake_shared.snowflake_vpc_configuration.private_link_vpce_id + } + } + + s3_configuration { + error_output_prefix = each.value.s3_error_output_prefix + role_arn = aws_iam_role.firehose.arn + bucket_arn = aws_s3_bucket.snowflake-failure.arn + buffering_size = each.value.s3_buffering_size + buffering_interval = each.value.s3_buffering_interval + compression_format = var.compression_format + } + } +} + +data "aws_iam_policy_document" "firehose-logging" { + statement { + actions = [ + "firehose:DescribeDeliveryStream", + "firehose:PutRecord", + "firehose:PutRecordBatch", + ] + resources = [for stream in keys(var.log_destinations) : aws_kinesis_firehose_delivery_stream.snowflake[stream].arn] + } +} + +resource "aws_iam_policy" "firehose-logging" { + description = "An IAM policy for fleet to log to Snowflake via Firehose" + policy = data.aws_iam_policy_document.firehose-logging.json +} diff --git a/addons/logging-destination-snowflake/outputs.tf b/addons/logging-destination-snowflake/outputs.tf new file mode 100644 index 0000000..ca8b827 --- /dev/null +++ b/addons/logging-destination-snowflake/outputs.tf @@ -0,0 +1,20 @@ +output "fleet_extra_environment_variables" { + value = { + FLEET_FIREHOSE_STATUS_STREAM = aws_kinesis_firehose_delivery_stream.snowflake["status"].name + FLEET_FIREHOSE_RESULT_STREAM = aws_kinesis_firehose_delivery_stream.snowflake["results"].name + FLEET_FIREHOSE_AUDIT_STREAM = aws_kinesis_firehose_delivery_stream.snowflake["audit"].name + FLEET_FIREHOSE_REGION = data.aws_region.current.region + FLEET_OSQUERY_STATUS_LOG_PLUGIN = "firehose" + FLEET_OSQUERY_RESULT_LOG_PLUGIN = "firehose" + FLEET_ACTIVITY_AUDIT_LOG_PLUGIN = "firehose" + FLEET_ACTIVITY_ENABLE_AUDIT_LOG = "true" + } + description = "Environment variables to configure Fleet to use Snowflake logging via Firehose" +} + +output "fleet_extra_iam_policies" { + value = [ + aws_iam_policy.firehose-logging.arn + ] + description = "IAM policies required for Fleet to log to Snowflake via Firehose" +} diff --git a/addons/logging-destination-snowflake/variables.tf b/addons/logging-destination-snowflake/variables.tf new file mode 100644 index 0000000..3efdb6d --- /dev/null +++ b/addons/logging-destination-snowflake/variables.tf @@ -0,0 +1,86 @@ +variable "s3_bucket_config" { + type = object({ + name_prefix = optional(string, "fleet-snowflake-failure") + expires_days = optional(number, 1) + }) + default = { + name_prefix = "fleet-snowflake-failure" + expires_days = 1 + } + description = "Configuration for the S3 bucket used to store failed Snowflake delivery attempts" +} + +variable "snowflake_shared" { + description = "Shared configurations among each logging destination" + type = object({ + account_url = string + private_key = string + key_passphrase = optional(string, null) + user = string + snowflake_role_configuration = object({ + enabled = bool + snowflake_role = optional(string, null) + }) + snowflake_vpc_configuration = optional(object({ + private_link_vpce_id = string + }), { + private_link_vpce_id = null + }) + }) +} + +variable "log_destinations" { + description = "A map of configurations for Snowflake Firehose delivery streams." + type = map(object({ + name = string + database = string + schema = string + table = string + buffering_size = number + buffering_interval = number + s3_buffering_size = number + s3_buffering_interval = number + s3_error_output_prefix = optional(string, null) + data_loading_option = optional(string, "JSON_MAPPING") + content_column_name = optional(string, null) + metadata_column_name = optional(string, null) + })) + default = { + results = { + name = "fleet-osquery-results-snowflake" + database = "fleet" + schema = "fleet_schema" + table = "osquery_results" + buffering_size = 2 + buffering_interval = 60 + s3_buffering_size = 10 + s3_buffering_interval = 400 + }, + status = { + name = "fleet-osquery-status-snowflake" + database = "fleet" + schema = "fleet_schema" + table = "osquery_status" + user = "fleet" + buffering_size = 2 + buffering_interval = 60 + s3_buffering_size = 10 + s3_buffering_interval = 400 + }, + audit = { + name = "fleet-audit-snowflake" + database = "fleet" + schema = "fleet_schema" + table = "fleet_audit" + buffering_size = 2 + buffering_interval = 60 + s3_buffering_size = 10 + s3_buffering_interval = 400 + } + } +} + +variable "compression_format" { + default = "UNCOMPRESSED" + description = "Compression format for the Firehose delivery stream" +}