2024-12-06 16:08:48 +00:00
|
|
|
from datetime import datetime, timezone
|
2022-05-16 11:44:03 +01:00
|
|
|
from typing import Any
|
2022-03-10 14:26:22 +00:00
|
|
|
|
|
|
|
from app.extensions import db
|
2022-04-22 14:01:16 +01:00
|
|
|
from app.models.mirrors import Proxy
|
2022-05-24 19:51:38 +01:00
|
|
|
from app.terraform.proxy import ProxyAutomation, update_smart_proxy_instance
|
2022-03-10 14:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProxyCloudfrontAutomation(ProxyAutomation):
|
|
|
|
short_name = "proxy_cloudfront"
|
2022-05-08 17:20:04 +01:00
|
|
|
description = "Deploy proxies to AWS CloudFront"
|
2022-03-10 14:26:22 +00:00
|
|
|
provider = "cloudfront"
|
2022-05-24 19:51:38 +01:00
|
|
|
smart_proxies = True
|
2022-10-23 12:14:29 +01:00
|
|
|
cloud_name = "aws"
|
2022-03-10 14:26:22 +00:00
|
|
|
|
|
|
|
template_parameters = [
|
2023-04-26 15:41:43 +01:00
|
|
|
"admin_email",
|
2022-03-10 14:26:22 +00:00
|
|
|
"aws_access_key",
|
2022-05-24 19:51:38 +01:00
|
|
|
"aws_secret_key",
|
2022-11-28 19:03:24 +00:00
|
|
|
"smart_zone"
|
2022-03-10 14:26:22 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
template = """
|
|
|
|
terraform {
|
2022-08-30 10:05:12 +01:00
|
|
|
{{ backend_config }}
|
2022-03-10 14:26:22 +00:00
|
|
|
required_providers {
|
2022-05-24 19:51:38 +01:00
|
|
|
acme = {
|
|
|
|
source = "vancluever/acme"
|
2022-11-28 19:03:24 +00:00
|
|
|
version = "~> 2.11.0"
|
2022-05-24 19:51:38 +01:00
|
|
|
}
|
2022-03-10 14:26:22 +00:00
|
|
|
aws = {
|
2022-11-28 19:03:24 +00:00
|
|
|
version = "~> 4.41.0"
|
2022-03-10 14:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-16 13:29:48 +01:00
|
|
|
|
2022-05-24 19:51:38 +01:00
|
|
|
provider "acme" {
|
|
|
|
server_url = "https://acme-v02.api.letsencrypt.org/directory"
|
|
|
|
}
|
|
|
|
|
2022-03-10 14:26:22 +00:00
|
|
|
provider "aws" {
|
|
|
|
access_key = "{{ aws_access_key }}"
|
|
|
|
secret_key = "{{ aws_secret_key }}"
|
2022-04-22 15:12:31 +01:00
|
|
|
region = "us-east-2"
|
2022-03-10 14:26:22 +00:00
|
|
|
}
|
2022-05-16 13:29:48 +01:00
|
|
|
|
2022-05-24 19:51:38 +01:00
|
|
|
locals {
|
|
|
|
smart_zone = "{{ smart_zone }}"
|
|
|
|
}
|
|
|
|
|
2022-03-10 14:26:22 +00:00
|
|
|
{% for group in groups %}
|
|
|
|
module "label_{{ group.id }}" {
|
|
|
|
source = "cloudposse/label/null"
|
|
|
|
version = "0.25.0"
|
|
|
|
namespace = "{{ global_namespace }}"
|
|
|
|
tenant = "{{ group.group_name }}"
|
|
|
|
label_order = ["namespace", "tenant", "name", "attributes"]
|
|
|
|
}
|
2022-05-16 13:29:48 +01:00
|
|
|
|
2022-03-10 14:26:22 +00:00
|
|
|
module "log_bucket_{{ group.id }}" {
|
|
|
|
source = "cloudposse/s3-log-storage/aws"
|
|
|
|
version = "0.28.0"
|
|
|
|
context = module.label_{{ group.id }}.context
|
|
|
|
name = "logs"
|
|
|
|
attributes = ["cloudfront"]
|
|
|
|
acl = "log-delivery-write"
|
|
|
|
standard_transition_days = 30
|
|
|
|
glacier_transition_days = 60
|
|
|
|
expiration_days = 90
|
|
|
|
}
|
2022-05-16 13:29:48 +01:00
|
|
|
|
2022-03-10 14:26:22 +00:00
|
|
|
resource "aws_sns_topic" "alarms_{{ group.id }}" {
|
|
|
|
name = "${module.label_{{ group.id }}.id}-cloudfront-alarms"
|
|
|
|
}
|
2022-05-24 19:51:38 +01:00
|
|
|
|
|
|
|
{% for origin in group.origins | selectattr("destroyed", "none") | selectattr("smart") %}
|
|
|
|
{% if loop.first %}
|
|
|
|
module "smart_proxy_{{ group.id }}" {
|
2022-08-09 14:39:46 +01:00
|
|
|
source = "{{ terraform_modules_path }}/terraform-aws-bc-smart-proxy-instance"
|
2022-05-24 19:51:38 +01:00
|
|
|
context = module.label_{{ group.id }}.context
|
|
|
|
name = "smart-proxy"
|
2023-03-26 14:45:55 +01:00
|
|
|
config_filename = "smart_proxy.{{ group.id }}.conf"
|
2022-05-24 19:51:38 +01:00
|
|
|
disable_api_termination = false
|
2023-03-26 15:02:59 +01:00
|
|
|
dns_zone = "{{ smart_zone }}"
|
2023-03-30 13:32:49 +01:00
|
|
|
letsencrypt_email_address = "{{ admin_email }}"
|
2023-03-26 15:02:59 +01:00
|
|
|
max_transfer_per_hour = "13000000000"
|
2022-05-24 19:51:38 +01:00
|
|
|
}
|
2023-03-26 15:26:08 +01:00
|
|
|
{% endif %}
|
2022-05-24 19:51:38 +01:00
|
|
|
{% endfor %}
|
2022-03-10 14:26:22 +00:00
|
|
|
{% endfor %}
|
2022-05-16 13:29:48 +01:00
|
|
|
|
2022-03-10 14:26:22 +00:00
|
|
|
{% for proxy in proxies %}
|
|
|
|
module "cloudfront_{{ proxy.id }}" {
|
2022-08-09 14:39:46 +01:00
|
|
|
source = "{{ terraform_modules_path }}/terraform-aws-bc-proxy"
|
2022-05-24 19:51:38 +01:00
|
|
|
{% if proxy.origin.smart %}
|
2023-04-26 15:41:43 +01:00
|
|
|
origin_domain = "origin-{{ proxy.origin.id }}.{{ proxy.origin.group.group_name }}.smart.{{ smart_zone[:-1] }}"
|
2022-05-24 19:51:38 +01:00
|
|
|
{% else %}
|
2022-03-10 14:26:22 +00:00
|
|
|
origin_domain = "{{ proxy.origin.domain_name }}"
|
2022-05-24 19:51:38 +01:00
|
|
|
{% endif %}
|
2022-03-10 14:26:22 +00:00
|
|
|
logging_bucket = module.log_bucket_{{ proxy.origin.group.id }}.bucket_domain_name
|
|
|
|
sns_topic_arn = aws_sns_topic.alarms_{{ proxy.origin.group.id }}.arn
|
|
|
|
low_bandwidth_alarm = false
|
|
|
|
context = module.label_{{ proxy.origin.group.id }}.context
|
|
|
|
name = "proxy"
|
|
|
|
attributes = ["{{ proxy.origin.domain_name }}"]
|
2022-04-19 14:32:04 +01:00
|
|
|
bypass_token = "{{ bypass_token }}"
|
2022-03-10 14:26:22 +00:00
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
"""
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
def import_state(self, state: Any) -> None:
|
2022-05-16 13:36:20 +01:00
|
|
|
if not isinstance(state, dict):
|
2022-05-16 12:47:40 +01:00
|
|
|
raise RuntimeError("The Terraform state object returned was not a dict.")
|
2022-05-16 11:44:03 +01:00
|
|
|
if "child_modules" not in state['values']['root_module']:
|
|
|
|
# There are no CloudFront proxies deployed to import state for
|
|
|
|
return
|
2022-06-23 12:34:29 +01:00
|
|
|
# CloudFront distributions (proxies)
|
2022-05-08 13:01:15 +01:00
|
|
|
for mod in state['values']['root_module']['child_modules']:
|
|
|
|
if mod['address'].startswith('module.cloudfront_'):
|
|
|
|
for res in mod['resources']:
|
|
|
|
if res['address'].endswith('aws_cloudfront_distribution.this'):
|
|
|
|
proxy = Proxy.query.filter(Proxy.id == mod['address'][len('module.cloudfront_'):]).first()
|
|
|
|
proxy.url = "https://" + res['values']['domain_name']
|
|
|
|
proxy.slug = res['values']['id']
|
2024-12-06 16:08:48 +00:00
|
|
|
proxy.terraform_updated = datetime.now(tz=timezone.utc)
|
2022-05-08 13:01:15 +01:00
|
|
|
break
|
2022-06-23 12:34:29 +01:00
|
|
|
# EC2 instances (smart proxies)
|
2022-05-24 19:51:38 +01:00
|
|
|
for g in state["values"]["root_module"]["child_modules"]:
|
|
|
|
if g["address"].startswith("module.smart_proxy_"):
|
|
|
|
group_id = int(g["address"][len("module.smart_proxy_"):])
|
|
|
|
for s in g["child_modules"]:
|
|
|
|
if s["address"].endswith(".module.instance"):
|
|
|
|
for x in s["resources"]:
|
|
|
|
if x["address"].endswith(".module.instance.aws_instance.default[0]"):
|
2023-05-03 14:48:11 +01:00
|
|
|
update_smart_proxy_instance(group_id, self.provider, "us-east-2a", x['values']['id'])
|
2022-05-08 13:01:15 +01:00
|
|
|
db.session.commit()
|