|
| 1 | +import argparse |
| 2 | + |
| 3 | +from typing import List, Tuple, Iterable, Optional |
| 4 | + |
| 5 | +_DEFAULT_PORT = 1070 |
| 6 | +_GOOGLE_DEFAULT_PORT = 1080 |
| 7 | +_GOOGLEADMIN_DEFAULT_PORT = 1098 |
| 8 | +_OKTA_DEFAULT_PORT = 1090 |
| 9 | +_AWS_DEFAULT_PORT = 1091 |
| 10 | +_K8S_DEFAULT_PORT = 1092 |
| 11 | +_GITHUB_DEFAULT_PORT = 1093 |
| 12 | +_AZURE_DEFAULT_PORT = 1095 |
| 13 | +_SUMOLOGIC_DEFAULT_PORT = 1096 |
| 14 | +_DIGITALOCEAN_DEFAULT_PORT = 1097 |
| 15 | +_STACKQL_TEST_DEFAULT_PORT = 1099 |
| 16 | +_STACKQL_AUTH_TESTING_DEFAULT_PORT = 1170 |
| 17 | + |
| 18 | +_DEFAULT_AWS_GLOBAL_SERVICES: Tuple[str] = ( |
| 19 | + 'iam', |
| 20 | + 'route53', |
| 21 | +) |
| 22 | + |
| 23 | +_DEFAULT_AWS_REGIONAL_SERVICES: Tuple[str] = ( |
| 24 | + "cloudcontrolapi", # cloudcontrol |
| 25 | + 'cloudhsmv2', # cloudhsm |
| 26 | + "ec2", |
| 27 | + 'logs', # cloudwatch |
| 28 | + "s3", |
| 29 | + "transfer", |
| 30 | +) |
| 31 | + |
| 32 | +_DEFAULT_AWS_REGIONS: Tuple[str] = ( |
| 33 | + "us-east-1", |
| 34 | + "us-east-2", |
| 35 | + "us-west-1", |
| 36 | + "us-west-2", |
| 37 | + "ap-south-1", |
| 38 | + "ap-northeast-1", |
| 39 | + "ap-northeast-2", |
| 40 | + "ap-southeast-1", |
| 41 | + "ap-southeast-2", |
| 42 | + "ap-northeast-3", |
| 43 | + "ca-central-1", |
| 44 | + "eu-central-1", |
| 45 | + "eu-west-1", |
| 46 | + "eu-west-2", |
| 47 | + "eu-west-3", |
| 48 | + "eu-north-1", |
| 49 | + "sa-east-1", |
| 50 | +) |
| 51 | + |
| 52 | +_DEFAULT_GCP_SERVICES: Tuple[str] = ( |
| 53 | + 'compute', |
| 54 | + 'storage', |
| 55 | +) |
| 56 | + |
| 57 | +class _LB(object): |
| 58 | + |
| 59 | + def __init__(self, lb_host: str, lb_port, backend_host: str, backend_port: int) -> None: |
| 60 | + self._lb_host = lb_host |
| 61 | + self._lb_port = lb_port |
| 62 | + self._backend_host = backend_host |
| 63 | + self._backend_port = backend_port |
| 64 | + |
| 65 | + def get_lb_host(self) -> str: |
| 66 | + return self._lb_host |
| 67 | + |
| 68 | + def get_lb_port(self) -> int: |
| 69 | + return self._lb_port |
| 70 | + |
| 71 | + def get_backend_host(self) -> str: |
| 72 | + return self._backend_host |
| 73 | + |
| 74 | + def get_backend_port(self) -> int: |
| 75 | + return self._backend_port |
| 76 | + |
| 77 | +class _HostsGenerator(object): |
| 78 | + |
| 79 | + def __init__( |
| 80 | + self, |
| 81 | + aws_global_services: Optional[Iterable[str]] = None, |
| 82 | + aws_regional_services: Optional[Iterable[str]] = None, |
| 83 | + aws_regions: Optional[Iterable[str]] = None, |
| 84 | + google_services: Optional[Iterable[str]] = None |
| 85 | + ) -> None: |
| 86 | + self._aws_global_services = aws_global_services if aws_global_services is not None else _DEFAULT_AWS_GLOBAL_SERVICES |
| 87 | + self._aws_regional_services = aws_regional_services if aws_regional_services is not None else _DEFAULT_AWS_REGIONAL_SERVICES |
| 88 | + self._aws_regions = aws_regions if aws_regions is not None else _DEFAULT_AWS_REGIONS |
| 89 | + self._google_services = google_services if google_services is not None else _DEFAULT_GCP_SERVICES |
| 90 | + |
| 91 | + @staticmethod |
| 92 | + def _generate_aws_hosts(aws_regional_services: Iterable[str], aws_regions: Iterable[str], aws_global_services: Iterable[str]) -> Iterable[_LB]: |
| 93 | + for service in aws_regional_services: |
| 94 | + for region in aws_regions: |
| 95 | + yield _LB(f'{service}.{region}.amazonaws.com', 443, '127.0.0.1', _AWS_DEFAULT_PORT) |
| 96 | + for service in aws_global_services: |
| 97 | + yield _LB(f'{service}.amazonaws.com', 443, '127.0.0.1', _AWS_DEFAULT_PORT) |
| 98 | + |
| 99 | + @staticmethod |
| 100 | + def _generate_gcp_hosts(google_services: Iterable[str]) -> Iterable[_LB]: |
| 101 | + for service in google_services: |
| 102 | + yield _LB(f'{service}.googleapis.com', 443, '127.0.0.1', _GOOGLE_DEFAULT_PORT) |
| 103 | + |
| 104 | + def generate_all_load_balancers(self) -> Iterable[_LB]: |
| 105 | + yield from self._generate_aws_hosts(self._aws_regional_services, self._aws_regions, self._aws_global_services) |
| 106 | + yield from self._generate_gcp_hosts(self._google_services) |
| 107 | + |
| 108 | + |
| 109 | +class _NginxConfigGenerator(object): |
| 110 | + |
| 111 | + def __init__(self, n_indent: int = 2) -> None: |
| 112 | + self._n_indent = n_indent |
| 113 | + |
| 114 | + @staticmethod |
| 115 | + def _generate_backends(hosts: Iterable[_LB], indent: int) -> Iterable[str]: |
| 116 | + """ |
| 117 | + Generate the nginx config. |
| 118 | + """ |
| 119 | + return [ |
| 120 | + " " * indent + f'{host.get_lb_host()} {host.get_backend_host()}:{host.get_backend_port()};' |
| 121 | + for host in hosts |
| 122 | + ] |
| 123 | + |
| 124 | + def _generate_file_content(self, hosts: Iterable[_LB]) -> Tuple[str]: |
| 125 | + """ |
| 126 | + Generate the file content. |
| 127 | + """ |
| 128 | + lines : Tuple[str] = ( |
| 129 | + 'worker_processes 1;', |
| 130 | + '', |
| 131 | + '#error_log logs/error.log;', |
| 132 | + '#error_log logs/error.log notice;', |
| 133 | + '#error_log logs/error.log info;', |
| 134 | + '', |
| 135 | + '#pid logs/nginx.pid;', |
| 136 | + '', |
| 137 | + '', |
| 138 | + 'events {', |
| 139 | + f'{" " * self._n_indent}worker_connections 1024;', |
| 140 | + '}', |
| 141 | + '', |
| 142 | + 'stream {', |
| 143 | + '', |
| 144 | + f'{" " * self._n_indent}map $ssl_preread_server_name $targetBackend {{', |
| 145 | + *self._generate_backends(hosts, self._n_indent * 2), |
| 146 | + f'{" " * self._n_indent}}}', |
| 147 | + '', |
| 148 | + f'{" " * self._n_indent}server {{', |
| 149 | + f'{" " * self._n_indent}listen 443;', |
| 150 | + f'{" " * self._n_indent}', |
| 151 | + f'{" " * self._n_indent}proxy_connect_timeout 1s;', |
| 152 | + f'{" " * self._n_indent}proxy_timeout 3s;', |
| 153 | + f'{" " * self._n_indent}resolver 1.1.1.1;', |
| 154 | + f'{" " * self._n_indent}', |
| 155 | + f'{" " * self._n_indent}proxy_pass $targetBackend;', |
| 156 | + f'{" " * self._n_indent}ssl_preread on;', |
| 157 | + f'{" " * self._n_indent}}}', |
| 158 | + '}', |
| 159 | + ) |
| 160 | + return lines |
| 161 | + |
| 162 | + def generate_file_content(self, hosts: Iterable[_LB]) -> Tuple[str]: |
| 163 | + """ |
| 164 | + Generate the file content. |
| 165 | + """ |
| 166 | + return self._generate_file_content(hosts) |
| 167 | + |
| 168 | + |
| 169 | +class _HostsFileEntriesGenerator(object): |
| 170 | + |
| 171 | + def __init__(self, hosts: Iterable[_LB]) -> None: |
| 172 | + self._hosts = hosts |
| 173 | + |
| 174 | + def generate_entries(self) -> Iterable[str]: |
| 175 | + """ |
| 176 | + Generate the entries. |
| 177 | + """ |
| 178 | + return tuple( |
| 179 | + f'{host.get_backend_host()} {host.get_lb_host()}' |
| 180 | + for host in self._hosts |
| 181 | + ) |
| 182 | + |
| 183 | + |
| 184 | +def _parse_args() -> argparse.Namespace: |
| 185 | + """ |
| 186 | + Parse the arguments. |
| 187 | + """ |
| 188 | + parser = argparse.ArgumentParser(description='Create a token.') |
| 189 | + parser.add_argument('--generate-nginx-lb', help='Opt-in nginx config generation', action=argparse.BooleanOptionalAction) |
| 190 | + parser.add_argument('--generate-hosts-entries', help='Opt-in hosts files entries generation', action=argparse.BooleanOptionalAction) |
| 191 | + # parser.add_argument('--header', type=str, help='The header.') |
| 192 | + return parser.parse_args() |
| 193 | + |
| 194 | +def main(): |
| 195 | + args = _parse_args() |
| 196 | + host_gen = _HostsGenerator() |
| 197 | + all_hosts = [lb for lb in host_gen.generate_all_load_balancers()] |
| 198 | + nginx_cfg_gen = _NginxConfigGenerator() |
| 199 | + if args.generate_nginx_lb: |
| 200 | + for l in nginx_cfg_gen.generate_file_content(all_hosts): |
| 201 | + print(l) |
| 202 | + return |
| 203 | + if args.generate_hosts_entries: |
| 204 | + hosts_gen = _HostsFileEntriesGenerator(all_hosts) |
| 205 | + for l in hosts_gen.generate_entries(): |
| 206 | + print(l) |
| 207 | + return |
| 208 | + |
| 209 | +if __name__ == '__main__': |
| 210 | + main() |
0 commit comments