forked from carlpett/tfz53
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
330 lines (283 loc) · 7.79 KB
/
main.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"sort"
"strings"
"text/template"
"github.com/miekg/dns"
"golang.org/x/net/idna"
)
// Build information. Populated at build-time.
var (
Version string
Revision string
Branch string
BuildUser string
BuildDate string
)
const (
zoneTemplateStr = `resource "google_dns_managed_zone" "{{ .ID }}" {
name = "{{ .Domain }}"
dns_name = "{{ .DnsName }}."
}
`
recordTemplateStr = `{{- range .Record.Comments }}
# {{ . }}{{ end }}
resource "google_dns_record_set" "{{ .ResourceID }}" {
managed_zone = {{ zoneReference .ZoneID }}
name = "{{ .Record.Name }}"
type = "{{ .Record.Type }}"
ttl = "{{ .Record.TTL }}"
rrdatas = [{{ range $idx, $elem := .Record.Data }}{{ if $idx }}, {{ end }}{{ ensureQuoted $elem }}{{ end }}]
}
`
)
type syntaxMode uint8
func (m syntaxMode) String() string {
switch m {
case Modern:
return "modern"
case Legacy:
return "legacy"
default:
panic("Unknown syntax")
}
}
const (
Modern syntaxMode = iota
Legacy
)
type configGenerator struct {
zoneTemplate *template.Template
recordTemplate *template.Template
syntax syntaxMode
}
func newConfigGenerator(syntax syntaxMode) *configGenerator {
g := &configGenerator{syntax: syntax}
g.zoneTemplate = template.Must(template.New("zone").Parse(zoneTemplateStr))
g.recordTemplate = template.Must(template.New("record").Funcs(template.FuncMap{
"ensureQuoted": ensureQuoted,
"zoneReference": g.zoneReference,
}).Parse(recordTemplateStr))
return g
}
type zoneTemplateData struct {
ID string
Domain string
DnsName string
}
type recordTemplateData struct {
ResourceID string
Record dnsRecord
ZoneID string
}
type dnsRecord struct {
Name string
Type string
TTL uint32
Data []string
Comments []string
}
type recordKey struct {
Name string
Type string
}
type recordKeySlice []recordKey
func (records recordKeySlice) Len() int {
return len(records)
}
func (records recordKeySlice) Less(i, j int) bool {
genKey := func(k recordKey) string {
return fmt.Sprintf("%s-%s", k.Name, k.Type)
}
return genKey(records[i]) < genKey(records[j])
}
func (records recordKeySlice) Swap(i, j int) {
tmp := records[i]
records[i] = records[j]
records[j] = tmp
}
var (
excludedTypesRaw = flag.String("exclude", "SOA,NS", "Comma-separated list of record types to ignore")
domain = flag.String("domain", "", "Name of domain")
zoneFile = flag.String("zone-file", "", "Path to zone file. Defaults to <domain>.zone in working dir")
showVersion = flag.Bool("version", false, "Show version")
legacySyntax = flag.Bool("legacy-syntax", false, "Generate legacy terraform syntax (versions older than 0.12)")
skipZoneCreation = flag.Bool("skip-zone-creation", false, "Skip creation of the zone")
)
func main() {
flag.Parse()
if *showVersion {
fmt.Printf("bind_zone_to_tf_gcp %s (%s/%s) (%s on %s)", Version, Branch, Revision, BuildUser, BuildDate)
os.Exit(0)
}
if *domain == "" {
log.Fatal("Domain is required")
}
if *zoneFile == "" {
*zoneFile = fmt.Sprintf("%s.zone", *domain)
}
excludedTypes := excludedTypesFromString(*excludedTypesRaw)
fileReader, err := os.Open(*zoneFile)
if err != nil {
log.Fatal(err)
}
var syntax syntaxMode
if !*legacySyntax {
syntax = Modern
} else {
syntax = Legacy
}
g := newConfigGenerator(syntax)
g.generateTerraformForZone(*domain, excludedTypes, fileReader, os.Stdout)
}
func (g *configGenerator) generateTerraformForZone(domain string, excludedTypes map[uint16]bool, zoneReader io.Reader, output io.Writer) {
records := readZoneRecords(zoneReader, excludedTypes)
zoneID, err := g.generateZoneResource(domain, output)
if err != nil {
log.Fatal(err)
}
recordKeys := make(recordKeySlice, 0, len(records))
for key := range records {
recordKeys = append(recordKeys, key)
}
sort.Sort(sort.Reverse(recordKeys))
for _, key := range recordKeys {
rec := records[key]
err := g.generateRecordResource(rec, zoneID, output)
if err != nil {
log.Printf("Error: %v\n", err)
continue
}
}
}
func readZoneRecords(zoneReader io.Reader, excludedTypes map[uint16]bool) map[recordKey]dnsRecord {
records := make(map[recordKey]dnsRecord)
for rr := range dns.ParseZone(zoneReader, *domain, *zoneFile) {
if rr.Error != nil {
log.Printf("Error: %v\n", rr.Error)
continue
}
recordType := rr.Header().Rrtype
isExcluded, ok := excludedTypes[recordType]
if ok && isExcluded {
continue
}
record := generateRecord(rr)
key := recordKey{record.Name, record.Type}
if _, ok := records[key]; ok {
record = mergeRecords(records[key], record)
}
records[key] = record
}
return records
}
func (g *configGenerator) generateZoneResource(domain string, w io.Writer) (string, error) {
zoneName := strings.TrimRight(domain, ".")
formatedZoneName := strings.Replace(zoneName, ".", "-", -1)
data := zoneTemplateData{
ID: formatedZoneName,
Domain: formatedZoneName,
DnsName: zoneName,
}
var err error
if ( ! *skipZoneCreation ){
err = g.zoneTemplate.Execute(w, data)
}
return data.ID, err
}
func (g *configGenerator) generateRecordResource(record dnsRecord, zoneID string, w io.Writer) error {
sanitizedName := sanitizeRecordName(record.Name)
id := fmt.Sprintf("%s-%s", sanitizedName, record.Type)
data := recordTemplateData{
ResourceID: id,
Record: record,
ZoneID: zoneID,
}
return g.recordTemplate.Execute(w, data)
}
func mergeRecords(a, b dnsRecord) dnsRecord {
a.Data = append(a.Data, b.Data...)
a.Comments = append(a.Comments, b.Comments...)
return a
}
func generateRecord(rr *dns.Token) dnsRecord {
header := rr.Header()
name := strings.ToLower(header.Name)
key := recordKey{
Name: name,
Type: dns.TypeToString[header.Rrtype],
}
data := strings.TrimPrefix(rr.String(), header.String())
if key.Type == "CNAME" {
data = strings.ToLower(data)
}
comments := make([]string, 0)
if rr.Comment != "" {
comments = append(comments, strings.TrimLeft(rr.Comment, ";"))
}
return dnsRecord{
Name: key.Name,
Type: key.Type,
TTL: header.Ttl,
Data: []string{data},
Comments: comments,
}
}
// sanitizeRecordName creates a normalized record name that Terraform accepts.
// Terraform only allows letters, numbers, dashes and underscores, while DNS
// records allow far more.
// 1. All dots are replaced with -
// 2. * is replaced by the string "wildcard"
// 3. IDN records are cleaned using punycode conversion
// 4. Any remaining non-allowed characters are replaced underscore
// 5. If the start of the record name is not a valid Terraform identifier,
// then prepend an underscore.
func sanitizeRecordName(name string) string {
withoutDots := strings.Replace(strings.TrimRight(name, "."), ".", "-", -1)
withoutAsterisk := strings.Replace(withoutDots, "*", "wildcard", -1)
punycoded, err := idna.Punycode.ToASCII(withoutAsterisk)
if err != nil {
log.Fatalf("Cannot create resource name from record %s: %v", name, err)
}
id := strings.Map(func(r rune) rune {
if (r >= 'a' && r <= 'z') ||
(r >= 'A' && r <= 'z') ||
(r >= '0' && r <= '9') ||
(r == '-' || r == '_') {
return r
}
return '_'
}, punycoded)
if (id[0] >= 'a' && id[0] <= 'z') ||
(id[0] >= 'A' && id[0] <= 'Z') ||
(id[0] == '_') {
return id
}
return fmt.Sprintf("_%s", id)
}
func excludedTypesFromString(s string) map[uint16]bool {
excludedTypes := make(map[uint16]bool)
for _, t := range strings.Split(s, ",") {
t = strings.ToUpper(t) // ensure upper case
rrType := dns.StringToType[t]
excludedTypes[rrType] = true
}
return excludedTypes
}
func ensureQuoted(s string) string {
if s[0] == '"' && s[len(s)-1] == '"' {
return s
}
return fmt.Sprintf("%q", s)
}
func (g *configGenerator) zoneReference(zone string) string {
if ( *skipZoneCreation ){
return ensureQuoted(zone)
}
return fmt.Sprintf("google_dns_managed_zone.%s.name", zone)
}