Skip to content

Commit 5b810ea

Browse files
authored
Merge pull request #54 from jungaretti/priority
Deploy MX and SRV records with priority
2 parents c279dbb + c6f68f9 commit 5b810ea

File tree

6 files changed

+80
-9
lines changed

6 files changed

+80
-9
lines changed

config.example.yml

+10
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ records:
88
type: CNAME
99
ttl: 600
1010
content: pixie.porkbun.com
11+
- type: MX
12+
host: bacontest42.com
13+
content: in1-smtp.messagingengine.com
14+
ttl: 600
15+
priority: 10
16+
- type: MX
17+
host: bacontest42.com
18+
content: in2-smtp.messagingengine.com
19+
ttl: 600
20+
priority: 20

pkg/config/config_test.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ records:
1616
type: CNAME
1717
ttl: 600
1818
content: pixie.porkbun.com
19+
- type: MX
20+
host: bacontest42.com
21+
content: in1-smtp.messagingengine.com
22+
ttl: 600
23+
priority: 10
1924
`)
2025
if err != nil {
2126
t.Fatal("could not seed config to temp file", err)
@@ -26,8 +31,8 @@ records:
2631
t.Fatal("could not read config file", err)
2732
}
2833

29-
if len(config.Records) != 2 {
30-
t.Fatal("expected 2 records after deployment, got", len(config.Records))
34+
if len(config.Records) != 3 {
35+
t.Fatal("expected 3 records after deployment, got", len(config.Records))
3136
}
3237
}
3338

@@ -140,3 +145,23 @@ records:
140145
t.Fatal("expected error when a record is missing content field", err)
141146
}
142147
}
148+
149+
func TestInvalidConfigInvalidPriority(t *testing.T) {
150+
configFile, err := SeedConfigToTempFile(`
151+
domain: bacontest42.com
152+
records:
153+
- host: bacontest42.com
154+
type: ALIAS
155+
ttl: 600
156+
content: pixie.porkbun.com
157+
priority: 20
158+
`)
159+
if err != nil {
160+
t.Fatal("could not seed config to temp file", err)
161+
}
162+
163+
_, err = ReadFile(configFile)
164+
if err == nil {
165+
t.Fatal("priority is not allowed on ALIAS records", err)
166+
}
167+
}

pkg/config/record.go

+32-7
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ import (
66
"strings"
77
)
88

9-
type Record struct {
10-
Name string `yaml:"host"`
11-
Type string `yaml:"type"`
12-
Ttl int `yaml:"ttl"`
13-
Data string `yaml:"content"`
14-
}
15-
169
const (
10+
// Record types that are allowed. Found in Porkbun's API documentation.
1711
TYPE_ALLOWLIST = "A, MX, CNAME, ALIAS, TXT, NS, AAAA, SRV, TLSA, CAA, HTTPS, SVCB"
12+
// Record types that are allowed to have a priority. Found in Porkbun's web app.
13+
PRIORITY_ALLOWLIST = "MX, SRV"
1814
)
1915

16+
type Record struct {
17+
Name string `yaml:"host"`
18+
Type string `yaml:"type"`
19+
Ttl int `yaml:"ttl"`
20+
Data string `yaml:"content"`
21+
Priority int `yaml:"priority"`
22+
}
23+
2024
func (r Record) GetName() string {
2125
return r.Name
2226
}
@@ -33,6 +37,10 @@ func (r Record) GetData() string {
3337
return r.Data
3438
}
3539

40+
func (r Record) GetPriority() string {
41+
return fmt.Sprint(r.Priority)
42+
}
43+
3644
func (r Record) Validate() error {
3745
if r.Name == "" {
3846
return fmt.Errorf("host is required")
@@ -53,6 +61,10 @@ func (r Record) Validate() error {
5361
return fmt.Errorf("content is required")
5462
}
5563

64+
if r.Priority != 0 && !isPriorityAllowed(r) {
65+
return fmt.Errorf("priority must be one of %v", PRIORITY_ALLOWLIST)
66+
}
67+
5668
return nil
5769
}
5870

@@ -69,4 +81,17 @@ func isTypeAllowed(r Record) bool {
6981
return true
7082
}
7183

84+
func isPriorityAllowed(r Record) bool {
85+
allowedPriorities := make(map[string]bool)
86+
for _, t := range strings.Split(PRIORITY_ALLOWLIST, ", ") {
87+
allowedPriorities[t] = true
88+
}
89+
90+
if _, ok := allowedPriorities[r.Type]; !ok {
91+
return false
92+
}
93+
94+
return true
95+
}
96+
7297
var _ dns.Record = Record{}

pkg/dns/record.go

+3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ type Record interface {
1010
GetType() string
1111
GetTtl() string
1212
GetData() string
13+
GetPriority() string
1314
}
1415

1516
func RecordEquals(l Record, r Record) bool {
1617
equal := l.GetName() == r.GetName()
1718
equal = equal && l.GetType() == r.GetType()
1819
equal = equal && l.GetTtl() == r.GetTtl()
1920
equal = equal && l.GetData() == r.GetData()
21+
equal = equal && l.GetPriority() == r.GetPriority()
2022
return equal
2123
}
2224

@@ -26,5 +28,6 @@ func RecordHash(r Record) string {
2628
r.GetType(),
2729
r.GetTtl(),
2830
r.GetData(),
31+
r.GetPriority(),
2932
}, "-")
3033
}

pkg/providers/porkbun/porkbun.go

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func (p PorkProvider) CreateRecord(domain string, newRecord dns.Record) error {
4949
Content: newRecord.GetData(),
5050
}
5151

52+
if newRecord.GetPriority() != "0" {
53+
porkRecord.Priority = newRecord.GetPriority()
54+
}
55+
5256
_, err := p.Api.CreateRecord(domain, porkRecord)
5357
return err
5458
}

pkg/providers/porkbun/record/record.go

+4
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@ func (r Record) GetData() string {
2828
return r.Content
2929
}
3030

31+
func (r Record) GetPriority() string {
32+
return r.Priority
33+
}
34+
3135
var _ dns.Record = Record{}

0 commit comments

Comments
 (0)