Skip to content

Commit

Permalink
fix(forwarder): improve variable checks
Browse files Browse the repository at this point in the history
I wasted 20 minutes debugging what turned out to be a bad input variable
to the forwarder module. This commit adds stricter validation.
  • Loading branch information
jta committed Jun 7, 2024
1 parent a79d8d8 commit 6bcdd39
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
82 changes: 82 additions & 0 deletions modules/forwarder/tests/validation.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

# The below variables are all valid. We will validate them in first test, and
# then successively override them with bad inputs.
variables {
name = "test"
destination = {
bucket = "bucket_name"
}
source_bucket_names = ["*", "a-b-c-", "a31asf"]
source_topic_arns = ["arn:aws:sns:us-west-2:123456789012:my-topic-name"]
source_kms_key_arns = ["arn:aws:kms:us-west-2:123456789012:key/abcd1234-ab12-cd34-ef56-abcdef123456"]

content_type_overrides = [
{
pattern = ".*"
content_type = "application/json"
}
]
}

run "valid_canned_variables" {
command = plan
}

run "invalid_uri_scheme" {
command = plan
expect_failures = [var.destination]

variables {
destination = {
uri = "ftp://foo"
}
}
}

run "mutually_exclusive_uri_and_bucket" {
command = plan
expect_failures = [var.destination]

variables {
destination = {
uri = "https://foo.com"
bucket = "foo.com"
}
}
}

run "invalid_source_bucket_name" {
command = plan
expect_failures = [var.source_bucket_names]

variables {
source_bucket_names = ["a*c"]
}
}

run "source_bucket_arn" {
command = plan
expect_failures = [var.source_bucket_names]

variables {
source_bucket_names = ["arn:s3:::hello-world"]
}
}

run "invalid_source_topic_arn" {
command = plan
expect_failures = [var.source_topic_arns]

variables {
source_topic_arns = ["foo"]
}
}

run "invalid_source_kms_key_arn" {
command = plan
expect_failures = [var.source_kms_key_arns]

variables {
source_kms_key_arns = ["foo"]
}
}
18 changes: 18 additions & 0 deletions modules/forwarder/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ variable "source_bucket_names" {
type = list(string)
nullable = false
default = []

validation {
# this check is not exhaustive, but it ensures wildcard can only be used as
# a suffix, and filters out most common misconfiguration of providing an
# ARN.
condition = alltrue([for name in var.source_bucket_names : can(regex("^[a-z0-9-.]*(\\*)?$", name))])
error_message = "Invalid S3 bucket name"
}
}

variable "source_topic_arns" {
Expand All @@ -63,6 +71,11 @@ variable "source_topic_arns" {
type = list(string)
nullable = false
default = []

validation {
condition = alltrue([for arn in var.source_topic_arns : can(regex("^arn:[^:]+:sns:", arn))])
error_message = "Invalid SNS ARN"
}
}

variable "source_kms_key_arns" {
Expand All @@ -72,6 +85,11 @@ variable "source_kms_key_arns" {
type = list(string)
nullable = false
default = []

validation {
condition = alltrue([for arn in var.source_kms_key_arns : can(regex("^arn:[^:]+:kms:", arn))])
error_message = "Invalid KMS ARN"
}
}

variable "max_file_size" {
Expand Down

0 comments on commit 6bcdd39

Please sign in to comment.