Skip to content

Commit 4c4037b

Browse files
authored
fix autoApprovers panic (#25)
Fixes #24
1 parent a9d8794 commit 4c4037b

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ testdata:
44
-f testdata/input-parent.hujson \
55
-d testdata/departments/ \
66
-o testdata/output-file-to-compare-to.hujson \
7-
-allow=acls,grants,groups,ipsets,ssh,tests
7+
-allow=acls,autoApprovers,grants,groups,ipsets,ssh,tests

main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ type SectionHandler func(sectionKey string, parentPath string, parent *jwcc.Obje
135135

136136
func handleArray() SectionHandler {
137137
return func(sectionKey string, parentPath string, parent *jwcc.Object, childPath string, childSection *jwcc.Member) {
138+
if childSection == nil {
139+
return
140+
}
138141
parentProps := parent.FindKey(ast.TextEqual(sectionKey))
139-
if parentProps != nil {
142+
if parentProps != nil && len(parentProps.Value.(*jwcc.Array).Values) > 0 {
140143
pathComment(parentProps.Value.(*jwcc.Array).Values[0], parentPath)
141144
}
142145

@@ -160,6 +163,9 @@ func handleArray() SectionHandler {
160163

161164
func handleObject() SectionHandler {
162165
return func(sectionKey string, parentPath string, parent *jwcc.Object, childPath string, childSection *jwcc.Member) {
166+
if childSection == nil {
167+
return
168+
}
163169
parentProps := parent.FindKey(ast.TextEqual(sectionKey))
164170
if parentProps != nil {
165171
pathComment(parentProps.Value.(*jwcc.Object).Members[0], parentPath)
@@ -185,6 +191,9 @@ func handleObject() SectionHandler {
185191
func handleAutoApprovers() SectionHandler {
186192
// https://tailscale.com/kb/1337/acl-syntax#auto-approvers-autoapprovers
187193
return func(sectionKey string, parentPath string, parent *jwcc.Object, childPath string, childSection *jwcc.Member) {
194+
if childSection == nil {
195+
return
196+
}
188197
newObj := existingOrNewObject(*parent, sectionKey)
189198

190199
childSectionObj := childSection.Value.(*jwcc.Object)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"autoApprovers": {
3+
"routes": {
4+
"10.0.0.0/32": [
5+
"tag:example"
6+
]
7+
},
8+
"exitNode": ["tag:example"],
9+
},
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"autoApprovers": {
3+
"routes": {
4+
"10.0.10.0/32": [
5+
"tag:example"
6+
]
7+
}
8+
}
9+
}

testdata/output-file-to-compare-to.hujson

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@
5252
},
5353
],
5454

55+
"autoApprovers": {
56+
"exitNode": [
57+
// from `testdata/departments/engineering/autoApprovers.hujson`
58+
"tag:example",
59+
],
60+
61+
"routes": {
62+
// from `testdata/input-parent.hujson`
63+
"10.0.0.0/32": ["tag:example"],
64+
65+
// from `testdata/departments/finance/autoApprovers.hujson`
66+
"10.0.10.0/32": ["tag:example"],
67+
},
68+
},
69+
5570
// from `testdata/input-parent.hujson`
5671
"extraDNSRecords": [
5772
{
@@ -87,7 +102,7 @@
87102
"ipset:parent": ["192.0.2.0"],
88103

89104
// from `testdata/departments/finance/ipsets.hujson`
90-
"ipset:parent": ["192.0.2.1"],
105+
"ipset:finance": ["192.0.2.1"],
91106
},
92107

93108
// from `testdata/input-parent.hujson`

0 commit comments

Comments
 (0)