Skip to content

Commit dc272c9

Browse files
author
James Mishra
committed
Initial commit for the domain name parser
This is the bare minimum of what we need.
0 parents  commit dc272c9

8 files changed

+13413
-0
lines changed

Diff for: LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Neocrym Records Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Terraform Domain Parser
2+
3+
This is a Terraform module that breaks up a DNS name (like `www.google.co.uk`)
4+
into parts. Currently we return:
5+
6+
- the domain (e.g. `google`)
7+
- the suffix (e.g. `co.uk`)
8+
9+
## Usage
10+
11+
```terraform
12+
# In Terraform, the module name can be whatever you want.
13+
# Here, we go with "parser_1".
14+
15+
module "parser_1" {
16+
source = "github.com/provose/domain-name-parser?ref=1.0.0"
17+
dns_name = "a.bunch.of.com.subdomain.google.co.uk"
18+
}
19+
20+
# You can access the domain with
21+
# module.parser_1.domain
22+
23+
# and the suffix with
24+
# module.parser_1.suffix
25+
```

Diff for: domain_regex.txt

+1
Large diffs are not rendered by default.

Diff for: main.tf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
locals {
2+
regex_output = regex(
3+
file("${path.module}/domain_regex.txt"),
4+
var.dns_name
5+
)
6+
}

Diff for: make_suffix_regex.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Parse a Public Suffix List (https://publicsuffix.org) file and outputs a regular expression.
4+
5+
The Public Suffix List offers a list of top-level domains and hosting sites that
6+
offer subdomains that are in effect like top-level domainsp.
7+
8+
We take only the ICANN TLDs from the Public Suffix List and turn them into a
9+
regular expression that this Terraform module can use.
10+
11+
Usage:
12+
python3 make_regex.py public_suffix_list.dat > regex.txt
13+
14+
"""
15+
import fileinput
16+
17+
BEGIN_LINE = "BEGIN ICANN DOMAINS"
18+
19+
END_LINE = "END ICANN DOMAINS"
20+
21+
def get_domains(input_iterator):
22+
start_parsing = False
23+
for line in input_iterator:
24+
if BEGIN_LINE in line:
25+
start_parsing = True
26+
continue
27+
if END_LINE in line:
28+
break
29+
if start_parsing:
30+
if "//" in line:
31+
continue
32+
splitted = line.split()
33+
if splitted:
34+
first_part = splitted[0]
35+
if first_part:
36+
if first_part[:2] == "*.":
37+
continue
38+
yield first_part[:2]
39+
else:
40+
yield first_part
41+
42+
REGEX_STR = "".join([
43+
r"(?P<domain>[^.:/@]+)\.",
44+
r"(?P<suffix>",
45+
r"|".join(
46+
domain.replace(".", r"\.")
47+
for domain in get_domains(fileinput.input())
48+
),
49+
r")$",
50+
])
51+
52+
print(REGEX_STR, end="")

Diff for: outputs.tf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "domain" {
2+
value = local.regex_output["domain"]
3+
}
4+
5+
output "suffix" {
6+
value = local.regex_output["suffix"]
7+
}

0 commit comments

Comments
 (0)