Skip to content

Commit e4cf0da

Browse files
committed
Fix panic: missing check if state is a cty null value
1 parent 1501a9a commit e4cf0da

2 files changed

Lines changed: 115 additions & 4 deletions

File tree

pkg/resource/select.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ func (f Filter) Apply(res []awsls.Resource) []awsls.Resource {
3838

3939
func GetTags(r *awsls.Resource) (map[string]string, error) {
4040
if r == nil || r.UpdatableResource == nil {
41-
return nil, fmt.Errorf("resource is nil")
41+
return nil, fmt.Errorf("resource is nil: %+v", r)
4242
}
4343

4444
state := r.State()
4545

46-
if state == nil {
47-
return nil, fmt.Errorf("state is nil")
46+
if state == nil || state.IsNull() {
47+
return nil, fmt.Errorf("state is nil: %+v", state)
4848
}
4949

5050
if !state.CanIterateElements() {
51-
return nil, fmt.Errorf("cannot iterate: %s", *state)
51+
return nil, fmt.Errorf("cannot iterate: %s", state.GoString())
5252
}
5353

5454
attrValue, ok := state.AsValueMap()["tags"]

pkg/resource/select_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package resource_test
22

33
import (
4+
"reflect"
45
"testing"
56
"time"
67

8+
"github.com/zclconf/go-cty/cty"
9+
710
"github.com/aws/aws-sdk-go/aws"
811
awsls "github.com/jckuester/awsls/aws"
912
"github.com/jckuester/awsweeper/pkg/resource"
13+
terradozerRes "github.com/jckuester/terradozer/pkg/resource"
1014
"github.com/stretchr/testify/assert"
1115
"github.com/stretchr/testify/require"
1216
)
@@ -409,3 +413,110 @@ func TestYamlFilter_Apply_NegatedStringFilter(t *testing.T) {
409413
require.Len(t, result, 1)
410414
assert.Equal(t, "select-this", result[0].ID)
411415
}
416+
417+
func TestGetTags(t *testing.T) {
418+
tests := []struct {
419+
name string
420+
arg *awsls.Resource
421+
want map[string]string
422+
wantErr string
423+
}{
424+
{
425+
name: "resource is nil",
426+
wantErr: "resource is nil: <nil>",
427+
},
428+
{
429+
name: "embedded updatable resource is nil",
430+
arg: &awsls.Resource{},
431+
wantErr: "resource is nil: &{Type: ID: Region: Tags:map[] CreatedAt:<nil> UpdatableResource:<nil>}",
432+
},
433+
{
434+
name: "state is nil",
435+
arg: &awsls.Resource{
436+
UpdatableResource: &terradozerRes.Resource{},
437+
},
438+
wantErr: "state is nil: <nil>",
439+
},
440+
{
441+
name: "state is nil value",
442+
arg: &awsls.Resource{
443+
UpdatableResource: terradozerRes.NewWithState("aws_foo", "1234", nil, &cty.NilVal),
444+
},
445+
wantErr: "state is nil: &{ty:{typeImpl:<nil>} v:<nil>}",
446+
},
447+
{
448+
name: "null map",
449+
arg: &awsls.Resource{
450+
UpdatableResource: terradozerRes.NewWithState("aws_foo", "1234",
451+
nil, ctyValuePtr(cty.NullVal(cty.Map(cty.String)))),
452+
},
453+
wantErr: "state is nil: &{ty:{typeImpl:{typeImplSigil:{} ElementTypeT:{typeImpl:{typeImplSigil:{} Kind:83}}}} v:<nil>}",
454+
},
455+
{
456+
name: "unhandled type",
457+
arg: &awsls.Resource{
458+
UpdatableResource: terradozerRes.NewWithState("aws_foo", "1234",
459+
nil, ctyValuePtr(cty.ObjectVal(map[string]cty.Value{
460+
"tags": cty.StringVal("foo"),
461+
}))),
462+
},
463+
wantErr: "currently unhandled type: string",
464+
},
465+
{
466+
name: "tags attribute not found",
467+
arg: &awsls.Resource{
468+
UpdatableResource: terradozerRes.NewWithState("aws_foo", "1234",
469+
nil, ctyValuePtr(cty.ObjectVal(map[string]cty.Value{
470+
"tag": cty.StringVal("foo"),
471+
}))),
472+
},
473+
wantErr: "attribute not found: tags",
474+
},
475+
{
476+
name: "cannot iterate element",
477+
arg: &awsls.Resource{
478+
UpdatableResource: terradozerRes.NewWithState("aws_foo", "1234",
479+
nil, ctyValuePtr(cty.StringVal("foo"))),
480+
},
481+
wantErr: "cannot iterate: cty.StringVal(\"foo\")",
482+
},
483+
{
484+
name: "empty map of tags",
485+
arg: &awsls.Resource{
486+
UpdatableResource: terradozerRes.NewWithState("aws_foo", "1234",
487+
nil, ctyValuePtr(cty.ObjectVal(map[string]cty.Value{
488+
"tags": cty.MapValEmpty(cty.String),
489+
}))),
490+
},
491+
want: map[string]string{},
492+
},
493+
{
494+
name: "some tags",
495+
arg: &awsls.Resource{
496+
UpdatableResource: terradozerRes.NewWithState("aws_foo", "1234",
497+
nil, ctyValuePtr(cty.ObjectVal(map[string]cty.Value{
498+
"tags": cty.MapVal(map[string]cty.Value{"foo": cty.StringVal("bar")}),
499+
}))),
500+
},
501+
want: map[string]string{"foo": "bar"},
502+
},
503+
}
504+
for _, tt := range tests {
505+
t.Run(tt.name, func(t *testing.T) {
506+
got, err := resource.GetTags(tt.arg)
507+
508+
if tt.wantErr != "" {
509+
assert.EqualError(t, err, tt.wantErr)
510+
} else {
511+
require.NoError(t, err)
512+
if !reflect.DeepEqual(got, tt.want) {
513+
t.Errorf("GetTags() got = %v, want %v", got, tt.want)
514+
}
515+
}
516+
})
517+
}
518+
}
519+
520+
func ctyValuePtr(v cty.Value) *cty.Value {
521+
return &v
522+
}

0 commit comments

Comments
 (0)