-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproxy.py
94 lines (78 loc) · 2.89 KB
/
proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import pulumi
import pulumi_cloudflare
from dataclasses import dataclass
from typing import Optional
@dataclass
class ProxyAccessPolicyArgs:
account_id: str
whitelist_ipv6_cidr: str
whitelist_ipv4: Optional[str] = None
@dataclass
class ProxyArgs:
access_policies: list[int]
account_id: str
hostname: str
type: str # Either "registry" or "transparent".
proxy_to: str
zone_id: str
class ProxyAccessPolicy(pulumi.ComponentResource):
def __init__(self, name, args: ProxyAccessPolicyArgs, opts: pulumi.ResourceOptions = None):
super().__init__("openttd:cfw:ProxyAccessPolicy", name, None, opts)
self.access_policy = pulumi_cloudflare.AccessPolicy(
f"{name}-app-policy",
account_id=args.account_id,
decision="bypass",
includes=[
pulumi_cloudflare.AccessPolicyIncludeArgs(
ips=[
args.whitelist_ipv6_cidr,
]
+ ([args.whitelist_ipv4] if args.whitelist_ipv4 else []),
),
],
name=f"IPv6 Whitelist ({name})",
opts=pulumi.ResourceOptions(parent=self),
)
self.register_outputs({})
class Proxy(pulumi.ComponentResource):
"""
Currently, most of GitHub is only available on IPv4. This of course is
a bit weird in 2023, but here we are.
As we deploy our infrastructure as IPv6-only, we need a way to reach
GitHub's services over IPv6.
For this we use Cloudflare Workers, which relay the requests to GitHub.
To make sure that these proxies aren't abused, we use Cloudflare Access
to only allow requests from our IPv6 ranges.
"""
def __init__(self, name, args: ProxyArgs, opts: pulumi.ResourceOptions = None):
super().__init__("openttd:cfw:Proxy", name, None, opts)
worker = pulumi_cloudflare.WorkerScript(
f"{name}-worker",
account_id=args.account_id,
content=args.proxy_to.apply(
lambda proxy_to: open(f"files/proxy_{args.type}.js").read().replace("[[ hostname ]]", proxy_to)
),
logpush=True,
name=name,
module=True,
opts=pulumi.ResourceOptions(parent=self),
)
pulumi_cloudflare.WorkerDomain(
f"{name}-domain",
account_id=args.account_id,
hostname=args.hostname,
service=name,
zone_id=args.zone_id,
opts=pulumi.ResourceOptions(parent=worker),
)
pulumi_cloudflare.AccessApplication(
f"{name}-app",
account_id=args.account_id,
app_launcher_visible=False,
domain=args.hostname,
name=name,
policies=args.access_policies,
type="self_hosted",
opts=pulumi.ResourceOptions(parent=self),
)
self.register_outputs({})