-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
150 lines (134 loc) · 3.55 KB
/
dns.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
package dns
import (
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
route53types "github.com/aws/aws-sdk-go-v2/service/route53/types"
log "github.com/cantara/bragi/sbragi"
)
type Cert struct {
Id string `json:"id"`
Domain string `json:"name"`
}
func GetHostedZoneId(domain string, r53 *route53.Client) (id string, err error) {
log.Trace("getting hosted zone", "domain", domain)
domain = domain + "."
result, err := r53.ListHostedZones(context.TODO(), &route53.ListHostedZonesInput{
//result, err := r53.ListHostedZonesByName(context.TODO(), &route53.ListHostedZonesByNameInput{
//DNSName: &domain,
})
if err != nil {
err = fmt.Errorf("Unable to list hosted zones, err: %v", err)
return
}
for _, zone := range result.HostedZones {
log.Trace("testing hosted zone", "name", *zone.Name)
if *zone.Name != domain {
continue //Should probably just return error here as it is already ordered
}
id := *zone.Id
idParts := strings.Split(id, "/")
id = idParts[len(idParts)-1]
log.Trace("found zone", "zone", zone, "id", id)
return id, nil
}
/*
arn := aws.ToString(result.Certificate.CertificateArn)
log.Trace("checking cert", "domain", domain, "found", arn)
dn := aws.ToString(result.Certificate.DomainName)
if dn != domain {
err = fmt.Errorf("certificate domain naim does not match %s != %s, error:%v", dn, domain, ErrNoCertFound)
return
}
log.Trace("found cert", "domain", domain, "id", arn)
cert = Cert{
Id: arn,
Domain: dn,
}
*/
err = ErrNoCertFound
return
}
func ToRecordType(t string) (route53types.RRType, error) {
t = strings.ToLower(t)
for _, rt := range route53types.RRTypeA.Values() {
if strings.ToLower(string(rt)) == t {
return rt, nil
}
}
return route53types.RRType(""), fmt.Errorf("invalid record type, %s", t)
}
func NewRecord(zone, key, val, t string, r53 *route53.Client) (err error) {
if key == "" {
return
}
rt, err := ToRecordType(t)
if err != nil {
return
}
/*
record, err := GetRecord(zone, key, r53)
if err != nil {
if !errors.Is(err, ErrNoCertFound) {
return err
}
} else {
log.Trace("record exists", "zone", zone, "key", key)
return nil
}
*/
log.Trace("creating new record", "zone", zone, "key", key)
_, err = r53.ChangeResourceRecordSets(context.TODO(), &route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53types.ChangeBatch{
Changes: []route53types.Change{
{
Action: route53types.ChangeActionCreate,
ResourceRecordSet: &route53types.ResourceRecordSet{
Name: aws.String(key),
ResourceRecords: []route53types.ResourceRecord{
{
Value: aws.String(val),
},
},
TTL: aws.Int64(60),
Type: rt,
},
},
},
},
HostedZoneId: aws.String(zone),
})
if err != nil {
return err
}
return
}
/*
_, err = e2.ModifyVpcAttribute(context.TODO(), &ec2.ModifyVpcAttributeInput{
VpcId: &id,
EnableDnsSupport: &ec2types.AttributeBooleanValue{Value: aws.Bool(true)},
})
_, err = e2.ModifyVpcAttribute(context.TODO(), &ec2.ModifyVpcAttributeInput{
VpcId: &id,
EnableDnsHostnames: &ec2types.AttributeBooleanValue{Value: aws.Bool(true)},
})
vpc = VPC{
Id: id,
Name: name,
CIDR: cidr,
CIDRv6: ipv6Cidr,
}
return
}
func Tag(tags []ec2types.Tag, key string) string {
for _, tag := range tags {
if strings.ToLower(aws.ToString(tag.Key)) != key {
continue
}
return aws.ToString(tag.Value)
}
return ""
*/
var ErrNoCertFound = fmt.Errorf("no cert found")