diff --git a/addons/logging-destination-splunk/.header.md b/addons/logging-destination-splunk/.header.md new file mode 100644 index 0000000..22faea6 --- /dev/null +++ b/addons/logging-destination-splunk/.header.md @@ -0,0 +1,91 @@ +# Logging Destination: Splunk + +This addon configures AWS Kinesis Firehose to send Fleet's osquery logs to Splunk. 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 "splunk-logging" { + source = "github.com/fleetdm/fleet-terraform//addons/logging-destination-splunk?depth=1&ref=tf-mod-addon-logging-destination-splunk-v1.0.0" + + s3_bucket_config = { + name_prefix = "fleet-splunk-failure" + expires_days = 5 + } + + log_destinations = { + results = { + name = "fleet-osquery-results-splunk" + hec_endpont = "https://http-inputs-mydomain.splunkcloud.com:443" + hec_token = "51D4DA16-C61B-4F5F-8EC7-ED4301342A4A" + hec_acknowledgement_timeout = 600 + hec_endpoint_type = "Raw" + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_error_output_prefix = "results/" + }, + status = { + name = "fleet-osquery-status-splunk" + hec_endpont = "https://http-inputs-mydomain.splunkcloud.com:443" + hec_token = "51D4DA16-C61B-4F5F-8EC7-ED4301342A4A" + hec_acknowledgement_timeout = 600 + hec_endpoint_type = "Raw" + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_error_output_prefix = "status/" + }, + audit = { + name = "fleet-audit-splunk" + hec_endpont = "https://http-inputs-mydomain.splunkcloud.com:443" + hec_token = "51D4DA16-C61B-4F5F-8EC7-ED4301342A4A" + hec_acknowledgement_timeout = 600 + hec_endpoint_type = "Raw" + 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 Splunk logging + module.splunk-logging.fleet_extra_environment_variables + ) + extra_iam_policies = concat( + # Uncomment to enable Splunk logging + module.splunk-logging.fleet_extra_iam_policies, + ) + } + + # ... other Fleet configuration ... +} +``` + diff --git a/addons/logging-destination-splunk/.terraform-docs.yml b/addons/logging-destination-splunk/.terraform-docs.yml new file mode 100644 index 0000000..e532fe1 --- /dev/null +++ b/addons/logging-destination-splunk/.terraform-docs.yml @@ -0,0 +1 @@ +header-from: .header.md \ No newline at end of file diff --git a/addons/logging-destination-splunk/README.md b/addons/logging-destination-splunk/README.md new file mode 100644 index 0000000..b26588e --- /dev/null +++ b/addons/logging-destination-splunk/README.md @@ -0,0 +1,139 @@ +# Logging Destination: Splunk + +This addon configures AWS Kinesis Firehose to send Fleet's osquery logs to Splunk. 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 "splunk-logging" { + source = "github.com/fleetdm/fleet-terraform//addons/logging-destination-splunk?depth=1&ref=tf-mod-addon-logging-destination-splunk-v1.0.0" + + s3_bucket_config = { + name_prefix = "fleet-splunk-failure" + expires_days = 5 + } + + log_destinations = { + results = { + name = "fleet-osquery-results-splunk" + hec_endpont = "https://http-inputs-mydomain.splunkcloud.com:443" + hec_token = "51D4DA16-C61B-4F5F-8EC7-ED4301342A4A" + hec_acknowledgement_timeout = 600 + hec_endpoint_type = "Raw" + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_error_output_prefix = "results/" + }, + status = { + name = "fleet-osquery-status-splunk" + hec_endpont = "https://http-inputs-mydomain.splunkcloud.com:443" + hec_token = "51D4DA16-C61B-4F5F-8EC7-ED4301342A4A" + hec_acknowledgement_timeout = 600 + hec_endpoint_type = "Raw" + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_error_output_prefix = "status/" + }, + audit = { + name = "fleet-audit-splunk" + hec_endpont = "https://http-inputs-mydomain.splunkcloud.com:443" + hec_token = "51D4DA16-C61B-4F5F-8EC7-ED4301342A4A" + hec_acknowledgement_timeout = 600 + hec_endpoint_type = "Raw" + 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 Splunk logging + module.splunk-logging.fleet_extra_environment_variables + ) + extra_iam_policies = concat( + # Uncomment to enable Splunk logging + module.splunk-logging.fleet_extra_iam_policies, + ) + } + + # ... other Fleet configuration ... +} +``` + +## Requirements + +No requirements. + +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | 6.25.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.splunk](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kinesis_firehose_delivery_stream) | resource | +| [aws_s3_bucket.splunk-failure](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource | +| [aws_s3_bucket_lifecycle_configuration.splunk-failure](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource | +| [aws_s3_bucket_public_access_block.splunk-failure](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource | +| [aws_s3_bucket_server_side_encryption_configuration.splunk-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 | +| [iam\_policy\_name](#input\_iam\_policy\_name) | n/a | `string` | `"splunk-firehose-policy"` | no | +| [log\_destinations](#input\_log\_destinations) | A map of configurations for Splunk Firehose delivery streams. |
map(object({
# hec endpoint/token are logically optional but validated to enforce
name = string
hec_endpoint = optional(string)
hec_token = optional(string)
hec_acknowledgement_timeout = optional(number, 600)
hec_endpoint_type = optional(string, "Raw")
s3_buffering_size = optional(number, 2)
s3_buffering_interval = optional(number, 400)
s3_error_output_prefix = optional(string, null)

}))
|
{
"audit": {
"hec_acknowledgement_timeout": 600,
"hec_endpoint_type": "Raw",
"name": "fleet-audit-splunk",
"s3_buffering_interval": 400,
"s3_buffering_size": 10,
"s3_error_output_prefix": "audit/"
},
"results": {
"hec_acknowledgement_timeout": 600,
"hec_endpoint_type": "Raw",
"name": "fleet-osquery-results-splunk",
"s3_buffering_interval": 400,
"s3_buffering_size": 10,
"s3_error_output_prefix": "results/"
},
"status": {
"hec_acknowledgement_timeout": 600,
"hec_endpoint_type": "Raw",
"name": "fleet-osquery-status-splunk",
"s3_buffering_interval": 400,
"s3_buffering_size": 10,
"s3_error_output_prefix": "status/"
}
}
| no | +| [s3\_bucket\_config](#input\_s3\_bucket\_config) | Configuration for the S3 bucket used to store failed Splunk delivery attempts |
object({
name_prefix = optional(string, "fleet-splunk-failure")
expires_days = optional(number, 1)
})
|
{
"expires_days": 1,
"name_prefix": "fleet-splunk-failure"
}
| no | + +## Outputs + +| Name | Description | +|------|-------------| +| [fleet\_extra\_environment\_variables](#output\_fleet\_extra\_environment\_variables) | Environment variables to configure Fleet to use Splunk logging via Firehose | +| [fleet\_extra\_iam\_policies](#output\_fleet\_extra\_iam\_policies) | IAM policies required for Fleet to log to Splunk via Firehose | +| [fleet\_s3\_splunk\_failure\_config](#output\_fleet\_s3\_splunk\_failure\_config) | S3 bucket details - splunk-failure | diff --git a/addons/logging-destination-splunk/main.tf b/addons/logging-destination-splunk/main.tf new file mode 100644 index 0000000..2506797 --- /dev/null +++ b/addons/logging-destination-splunk/main.tf @@ -0,0 +1,123 @@ +data "aws_region" "current" {} + +resource "aws_s3_bucket" "splunk-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" "splunk-failure" { + bucket = aws_s3_bucket.splunk-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" "splunk-failure" { + bucket = aws_s3_bucket.splunk-failure.bucket + rule { + apply_server_side_encryption_by_default { + sse_algorithm = "aws:kms" + } + } +} + +resource "aws_s3_bucket_public_access_block" "splunk-failure" { + bucket = aws_s3_bucket.splunk-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.splunk-failure.arn, + "${aws_s3_bucket.splunk-failure.arn}/*" + ] #tfsec:ignore:aws-iam-no-policy-wildcards + } +} + +resource "aws_iam_policy" "firehose" { + name = var.iam_policy_name + 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" "splunk" { + for_each = var.log_destinations + name = each.value.name + destination = "splunk" + + server_side_encryption { + enabled = true + } + + splunk_configuration { + hec_endpoint = each.value.hec_endpoint + hec_token = each.value.hec_token + hec_acknowledgment_timeout = each.value.hec_acknowledgement_timeout + hec_endpoint_type = each.value.hec_endpoint_type + s3_backup_mode = "FailedEventsOnly" + + s3_configuration { + error_output_prefix = each.value.s3_error_output_prefix + role_arn = aws_iam_role.firehose.arn + bucket_arn = aws_s3_bucket.splunk-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.splunk[stream].arn] + } +} + +resource "aws_iam_policy" "firehose-logging" { + description = "An IAM policy for fleet to log to Splunk via Firehose" + policy = data.aws_iam_policy_document.firehose-logging.json +} diff --git a/addons/logging-destination-splunk/outputs.tf b/addons/logging-destination-splunk/outputs.tf new file mode 100644 index 0000000..9058679 --- /dev/null +++ b/addons/logging-destination-splunk/outputs.tf @@ -0,0 +1,40 @@ +output "fleet_extra_environment_variables" { + value = merge( + { + FLEET_FIREHOSE_REGION = data.aws_region.current.region + }, + # Status: stream + plugin only if "status" exists + try({ + FLEET_FIREHOSE_STATUS_STREAM = aws_kinesis_firehose_delivery_stream.splunk["status"].name + FLEET_OSQUERY_STATUS_LOG_PLUGIN = "firehose" + }, {}), + # Results: stream + plugin only if "results" exists + try({ + FLEET_FIREHOSE_RESULT_STREAM = aws_kinesis_firehose_delivery_stream.splunk["results"].name + FLEET_OSQUERY_RESULT_LOG_PLUGIN = "firehose" + }, {}), + # Audit: stream + plugin + enable flag only if "audit" exists + try({ + FLEET_FIREHOSE_AUDIT_STREAM = aws_kinesis_firehose_delivery_stream.splunk["audit"].name + FLEET_ACTIVITY_AUDIT_LOG_PLUGIN = "firehose" + FLEET_ACTIVITY_ENABLE_AUDIT_LOG = "true" + }, {}) + ) + description = "Environment variables to configure Fleet to use Splunk logging via Firehose" +} + + +output "fleet_extra_iam_policies" { + value = [ + aws_iam_policy.firehose-logging.arn + ] + description = "IAM policies required for Fleet to log to Splunk via Firehose" +} + +output "fleet_s3_splunk_failure_config" { + value = { + bucket_name = aws_s3_bucket.splunk-failure.bucket + s3_object_prefix = var.s3_bucket_config.name_prefix + } + description = "S3 bucket details - splunk-failure" +} diff --git a/addons/logging-destination-splunk/variables.tf b/addons/logging-destination-splunk/variables.tf new file mode 100644 index 0000000..3ca3c3b --- /dev/null +++ b/addons/logging-destination-splunk/variables.tf @@ -0,0 +1,73 @@ +variable "iam_policy_name" { + type = string + default = "splunk-firehose-policy" +} + +variable "s3_bucket_config" { + type = object({ + name_prefix = optional(string, "fleet-splunk-failure") + expires_days = optional(number, 1) + }) + default = { + name_prefix = "fleet-splunk-failure" + expires_days = 1 + } + description = "Configuration for the S3 bucket used to store failed Splunk delivery attempts" +} + +variable "log_destinations" { + description = "A map of configurations for Splunk Firehose delivery streams." + type = map(object({ + # hec endpoint/token are logically optional but validated to enforce + name = string + hec_endpoint = optional(string) + hec_token = optional(string) + hec_acknowledgement_timeout = optional(number, 600) + hec_endpoint_type = optional(string, "Raw") + s3_buffering_size = optional(number, 2) + s3_buffering_interval = optional(number, 400) + s3_error_output_prefix = optional(string, null) + + })) + default = { + results = { + name = "fleet-osquery-results-splunk" + hec_acknowledgement_timeout = 600 + hec_endpoint_type = "Raw" + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_error_output_prefix = "results/" + }, + status = { + name = "fleet-osquery-status-splunk" + hec_acknowledgement_timeout = 600 + hec_endpoint_type = "Raw" + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_error_output_prefix = "status/" + }, + audit = { + name = "fleet-audit-splunk" + hec_acknowledgement_timeout = 600 + hec_endpoint_type = "Raw" + s3_buffering_size = 10 + s3_buffering_interval = 400 + s3_error_output_prefix = "audit/" + } + } + validation { + condition = alltrue([for _, ds in var.log_destinations : ds.hec_endpoint != null && ds.hec_endpoint != ""]) + error_message = "Each delivery stream must supply a non-empty `hec_endpoint`." + } + validation { + condition = alltrue([for _, ds in var.log_destinations : ds.hec_token != null && ds.hec_token != ""]) + error_message = "Each delivery stream must supply a non-empty `hec_token`." + } +} + + + +variable "compression_format" { + default = "UNCOMPRESSED" + description = "Compression format for the Firehose delivery stream" +}