Skip to content

Commit 65341e1

Browse files
committed
fix panic with empty definitions in parent file
Updates #28
1 parent 6197dea commit 65341e1

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,13 @@ func addParentPathComments(parentDoc *ParsedDocument) error {
224224
default:
225225
pathComment(parentSection, parentDoc.Path)
226226
case *jwcc.Array:
227-
pathComment(parentSection.Value.(*jwcc.Array).Values[0], parentDoc.Path)
227+
if len(parentSection.Value.(*jwcc.Array).Values) != 0 {
228+
pathComment(parentSection.Value.(*jwcc.Array).Values[0], parentDoc.Path)
229+
}
228230
case *jwcc.Object:
229-
pathComment(parentSection.Value.(*jwcc.Object).Members[0], parentDoc.Path)
231+
if len(parentSection.Value.(*jwcc.Object).Members) != 0 {
232+
pathComment(parentSection.Value.(*jwcc.Object).Members[0], parentDoc.Path)
233+
}
230234
}
231235
}
232236
return nil

main_test.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ func TestHandleArray(t *testing.T) {
438438
handlerFn("acls", parentDoc.Path, parentDoc.Object, "CHILD", childSection)
439439

440440
mergedValues := parentDoc.Object.Find("acls").Value.(*jwcc.Array).Values
441-
442441
if len(mergedValues) != 2 {
443442
t.Fatalf("section [%v] should be [1], not [%v]", "acls", len(mergedValues))
444443
}
@@ -471,7 +470,6 @@ func TestHandleObject(t *testing.T) {
471470
handlerFn("groups", parentDoc.Path, parentDoc.Object, "CHILD", childSection)
472471

473472
mergedValues := parentDoc.Object.Find("groups").Value.(*jwcc.Object).Members
474-
475473
if len(mergedValues) != 3 {
476474
t.Fatalf("section [%v] should be [1], not [%v]", "groups", len(mergedValues))
477475
}
@@ -520,6 +518,76 @@ func TestHandleAutoApprovers(t *testing.T) {
520518
}
521519
}
522520

521+
func TestEmptyHosts(t *testing.T) {
522+
parent, err := jwcc.Parse(strings.NewReader(`{"hosts":{}}`))
523+
if err != nil {
524+
t.Fatalf("expected no error, got [%v]", err)
525+
}
526+
parentDoc := &ParsedDocument{
527+
Object: parent.Value.(*jwcc.Object),
528+
Path: "parent",
529+
}
530+
531+
child, err := jwcc.Parse(strings.NewReader(`{
532+
"hosts": {
533+
"host1": "100.99.98.97",
534+
}
535+
}`))
536+
if err != nil {
537+
t.Fatalf("expected no error, got [%v]", err)
538+
}
539+
540+
childDoc := &ParsedDocument{
541+
Object: child.Value.(*jwcc.Object),
542+
Path: "child",
543+
}
544+
545+
err = mergeDocs(preDefinedAclSections, parentDoc, []*ParsedDocument{childDoc})
546+
if err != nil {
547+
t.Fatalf("expected no error, got [%v]", err)
548+
}
549+
550+
mergedValues := parentDoc.Object.Find("hosts").Value.(*jwcc.Object).Members
551+
if len(mergedValues) != 1 {
552+
t.Fatalf("section [%v] should be [1], not [%v]", "hosts", len(mergedValues))
553+
}
554+
}
555+
556+
func TestEmptyAcls(t *testing.T) {
557+
parent, err := jwcc.Parse(strings.NewReader(`{"acls":[]}`))
558+
if err != nil {
559+
t.Fatalf("expected no error, got [%v]", err)
560+
}
561+
parentDoc := &ParsedDocument{
562+
Object: parent.Value.(*jwcc.Object),
563+
Path: "parent",
564+
}
565+
566+
child, err := jwcc.Parse(strings.NewReader(`{
567+
"acls": [
568+
{"action": "accept", "src": ["finance1"], "dst": ["tag:demo-infra:22"]},
569+
]
570+
}`))
571+
if err != nil {
572+
t.Fatalf("expected no error, got [%v]", err)
573+
}
574+
575+
childDoc := &ParsedDocument{
576+
Object: child.Value.(*jwcc.Object),
577+
Path: "child",
578+
}
579+
580+
err = mergeDocs(preDefinedAclSections, parentDoc, []*ParsedDocument{childDoc})
581+
if err != nil {
582+
t.Fatalf("expected no error, got [%v]", err)
583+
}
584+
585+
mergedValues := parentDoc.Object.Find("acls").Value.(*jwcc.Array).Values
586+
if len(mergedValues) != 1 {
587+
t.Fatalf("section [%v] should be [1], not [%v]", "acls", len(mergedValues))
588+
}
589+
}
590+
523591
func TestSort(t *testing.T) {
524592
parent, err := jwcc.Parse(strings.NewReader(ACL_PARENT))
525593
if err != nil {

0 commit comments

Comments
 (0)