-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
main.go
140 lines (121 loc) · 3.5 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
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"log"
"net"
"strings"
"time"
"github.com/kelseyhightower/envconfig"
"google.golang.org/api/dns/v1"
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/serving/test/e2e/externaldomaintls/config"
)
var env config.EnvConfig
func main() {
if err := envconfig.Process("auto_tls_test", &env); err != nil {
log.Fatalf("Failed to process environment variable: %v.", err)
}
if err := setupDNSRecord(); err != nil {
log.Fatal("Failed to setup DNS record: ", err)
}
}
func setupDNSRecord() error {
dnsRecord := &config.DNSRecord{
Domain: env.FullHostName,
IP: env.IngressIP,
}
if err := createDNSRecord(dnsRecord); err != nil {
return err
}
if err := waitForDNSRecordVisible(dnsRecord); err != nil {
log.Printf("DNS record is not visible yet %v", err)
}
return nil
}
func createDNSRecord(dnsRecord *config.DNSRecord) error {
record := config.MakeRecordSet(dnsRecord)
svc, err := config.GetCloudDNSSvc()
if err != nil {
return err
}
// Look for existing records.
if list, err := svc.ResourceRecordSets.List(
env.CloudDNSProject, env.DNSZone).Name(record.Name).Type("A").Do(); err != nil {
return err
} else if len(list.Rrsets) > 0 {
return fmt.Errorf("record for domain %s already exists", record.Name)
}
addition := &dns.Change{
Additions: []*dns.ResourceRecordSet{record},
}
return config.ChangeDNSRecord(addition, svc, env.CloudDNSProject, env.DNSZone)
}
func waitForDNSRecordVisible(record *config.DNSRecord) error {
nameservers, err := net.LookupNS(env.DomainName)
if err != nil {
return err
}
var lastErr error
if err := wait.PollUntilContextTimeout(context.Background(), 10*time.Second, 300*time.Second, true, func(context.Context) (bool, error) {
for _, ns := range nameservers {
nsIP, err := net.LookupHost(ns.Host)
if err != nil {
log.Printf("Failed to look up host %s: %v", ns.Host, err)
lastErr = err
return false, nil
}
// This resolver bypasses the local resolver and instead queries the
// domain's authoritative servers.
r := &net.Resolver{
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{Timeout: 30 * time.Second}
return d.DialContext(ctx, "udp", nsIP[0]+":53")
},
}
valid, err := validateRecord(r, record)
if err != nil {
log.Printf("Failed to validate DNS record %v", err)
lastErr = err
return false, nil
}
if !valid {
return false, nil
}
}
return true, nil
}); err != nil {
return lastErr
}
return nil
}
func validateRecord(resolver *net.Resolver, record *config.DNSRecord) (bool, error) {
ips, err := resolver.LookupHost(context.Background(), replaceWildcard(record.Domain))
if err != nil {
return false, err
}
for _, ip := range ips {
if ip == record.IP {
return true, nil
}
}
return false, nil
}
func replaceWildcard(domain string) string {
if domain[0] != '*' {
return domain
}
return strings.Replace(domain, "*", "star", 1)
}