Skip to content

Commit

Permalink
Refactoring newChange
Browse files Browse the repository at this point in the history
Refactors newChange so it will add domain names not existing in the
current results.
  • Loading branch information
mys721tx committed Feb 10, 2024
1 parent db1767f commit 9df3004
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions cdh.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ type config struct {
type tlsa struct {
TrustAnchor string
EndEntity string
DNSNames map[string]bool
DNSNames []string
}

// NewTLSA creates a new instance of tlsa struct.
func NewTLSA() *tlsa {
var t tlsa
t.DNSNames = make(map[string]bool)
t.DNSNames = make([]string, 0)
return &t
}

Expand All @@ -62,9 +62,9 @@ func (t *tlsa) ReadCert(c *x509.Certificate) error {
for _, d := range c.DNSNames {
// Adds dot for DNS
if !strings.HasSuffix(d, ".") {
t.DNSNames[d+"."] = true
t.DNSNames = append(t.DNSNames, d+".")
} else {
t.DNSNames[d] = true
t.DNSNames = append(t.DNSNames, d)
}
}
}
Expand Down Expand Up @@ -147,22 +147,37 @@ func newDNSClient(f string) (*gcdns.Service, string, error) {
func newChange(rR []*gcdns.ResourceRecordSet, t *tlsa) *gcdns.Change {
cset := gcdns.Change{}

// Build a map of resource record sets
recordMap := make(map[string]*gcdns.ResourceRecordSet)
for _, r := range rR {
if r.Type == "TLSA" {
s := strings.SplitN(r.Name, ".", 3)
if _, ok := t.DNSNames[s[2]]; ok {
cset.Deletions = append(cset.Deletions, r)
cset.Additions = append(
cset.Additions,
&gcdns.ResourceRecordSet{
Kind: r.Kind,
Name: r.Name,
Ttl: r.Ttl,
Type: r.Type,
Rrdatas: t.MakeRRData(),
},
)
recordMap[r.Name] = r
}

for _, dnsName := range t.DNSNames {
// Check if the DNS name exists in the map
if r, ok := recordMap[dnsName]; ok {
// Append the original record to cset.Deletions
cset.Deletions = append(cset.Deletions, r)

// Create a new resource record set with updated Rrdatas
newRecord := &gcdns.ResourceRecordSet{
Kind: r.Kind,
Name: r.Name,
Ttl: r.Ttl,
Type: r.Type,
Rrdatas: t.MakeRRData(),
}
cset.Additions = append(cset.Additions, newRecord)
} else {
// Create a new resource record set with default values
newRecord := &gcdns.ResourceRecordSet{
Kind: "dns#resourceRecordSet",
Name: dnsName,
Ttl: 300,
Type: "TLSA",
Rrdatas: t.MakeRRData(),
}
cset.Additions = append(cset.Additions, newRecord)
}
}

Expand Down

0 comments on commit 9df3004

Please sign in to comment.