carlos / opentofu Public

Clone
git clone https://amadan.net/carlos/opentofu

Plain git — no account needed to clone.

Download

Download this file

1mock_provider "aws" {
2 mock_data "aws_ec2_instance_type_offerings" { defaults = { locations = ["eu-west-1a", "eu-west-1b"] } }
3 mock_data "aws_region" { defaults = { region = "eu-west-1" } }
4 mock_data "aws_availability_zones" { defaults = { names = ["eu-west-1a", "eu-west-1b"] } }
5 mock_data "aws_ami" { defaults = { id = "ami-0123456789abcdef0" } }
6 mock_resource "aws_s3_bucket" { defaults = { arn = "arn:aws:s3:::mock-test-bucket", id = "mock-test-bucket" } }
7 mock_resource "aws_iam_role" { defaults = { arn = "arn:aws:iam::123456789012:role/test" } }
8}
9
10variables {
11 name = "test-carlos-eu-west-1"
12 host_count = 2
13 vpc_cidr = "10.40.0.0/16"
14 deployment_bucket_name = "test-platform"
15 deployment_bucket_arn = "arn:aws:s3:::test-platform"
16 deployment_region = "eu-west-2"
17 data_bucket_name = "test-data"
18 data_bucket_arn = "arn:aws:s3:::test-data"
19 placement_region = "primary"
20 release_public_key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
21 apps_domain = "apps.example.com"
22}
23run "host_security" {
24 assert {
25 condition = alltrue([for h in aws_instance.host : h.disable_api_termination && !h.root_block_device[0].delete_on_termination]) && length(aws_cloudwatch_metric_alarm.recover) == 2
26 error_message = "Protect hosts from accidental termination and retain their disks."
27 }
28 assert {
29 condition = toset(jsondecode(aws_iam_role_policy.buckets.policy).Statement[1].Resource) == toset(["arn:aws:s3:::test-platform", "arn:aws:s3:::test-data"])
30 error_message = "Bucket listing must stay within this deployment and region."
31 }
32 command = plan
33 assert {
34 condition = alltrue([for h in aws_instance.host : h.metadata_options[0].http_tokens == "required" && h.root_block_device[0].encrypted])
35 error_message = "Hosts require IMDSv2 and encrypted disks."
36 }
37 assert {
38 condition = toset([for r in aws_security_group.host.ingress : r.from_port]) == toset([80, 443]) && alltrue([for r in aws_security_group.host.ingress : r.protocol == "tcp" && r.from_port == r.to_port])
39 error_message = "Only HTTP and HTTPS ingress is allowed."
40 }
41 assert {
42 condition = jsondecode(aws_iam_role_policy.deny_parameters.policy).Statement[0].Effect == "Deny" && jsondecode(aws_iam_role_policy.deny_parameters.policy).Statement[0].Resource == "*" && toset(jsondecode(aws_iam_role_policy.deny_parameters.policy).Statement[0].Action) == toset(["ssm:GetParameter", "ssm:GetParameters", "ssm:GetParameterHistory", "ssm:GetParametersByPath"])
43 error_message = "SSM must not grant hosts access to operator parameters."
44 }
45 assert {
46 condition = toset(jsondecode(aws_iam_role_policy.buckets.policy).Statement[0].Resource) == toset(["arn:aws:s3:::test-platform/*", "arn:aws:s3:::test-data/*"])
47 error_message = "Bucket writes must stay within control and local data buckets."
48 }
49 assert {
50 condition = aws_subnet.public[0].availability_zone != aws_subnet.public[1].availability_zone
51 error_message = "Hosts must spread across available zones."
52 }
53 assert {
54 condition = strcontains(aws_instance.host["host-0"].user_data, "AWS_REGION=eu-west-2") && strcontains(aws_instance.host["host-0"].user_data, "CARLOS_HIBERNATE_REGION=eu-west-1")
55 error_message = "Control and data bucket regions must remain distinct."
56 }
57 assert {
58 condition = strcontains(aws_instance.host["host-0"].user_data, "sha256sum -c -") && !strcontains(aws_instance.host["host-0"].user_data, "CARLOS_RELEASE_KEY=")
59 error_message = "Bootstrap must check downloads and must not carry a private signing key."
60 }
61}
62
63run "reject_module_injection" {
64 command = plan
65 variables { data_bucket_name = "test-data\nENVEOF\necho bad" }
66 expect_failures = [var.data_bucket_name]
67}
68