Skip to content

Commit

Permalink
Merge pull request #86 from ekristen/fix-nil-pointer-2
Browse files Browse the repository at this point in the history
fix(config): protect against nil pointer for misconfigured account
  • Loading branch information
ekristen authored Nov 13, 2024
2 parents 4a2e370 + 487a6e2 commit 585b852
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ func (c *Config) Filters(accountID string) (filter.Filters, error) {
}

account := c.Accounts[accountID]

if account == nil {
return nil, errors.ErrAccountNotConfigured
}

filters := account.Filters

if filters == nil {
Expand Down
33 changes: 33 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

"github.com/ekristen/libnuke/pkg/errors"
)

func init() {
Expand Down Expand Up @@ -242,3 +244,34 @@ func TestIncomplete(t *testing.T) {
assert.NoError(t, err)
_ = c
}

// TestBroken tests a broken configuration file with an incomplete account that is not properly
// configured. This should return an error when trying to get the filters for the account.
func TestBroken(t *testing.T) {
opts := Options{
Path: "testdata/broken.yaml",
}
c, err := New(opts)
assert.NoError(t, err)
_ = c

_, err = c.Filters("000000000000")
assert.ErrorIs(t, err, errors.ErrAccountNotConfigured)
}

// TestBrokenFixed tests a fixed broken configuration where there is at minimum an empty object provided for the
// account. This should return an empty set of filters and no errors.
func TestBrokenFixed(t *testing.T) {
opts := Options{
Path: "testdata/broken-fixed.yaml",
}
c, err := New(opts)
assert.NoError(t, err)
_ = c

_, err = c.Filters("000000000000")
assert.NoError(t, err)

_, err = c.Filters("111111111111")
assert.NoError(t, err)
}
17 changes: 17 additions & 0 deletions pkg/config/testdata/broken-fixed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
regions:
- global
- us-east-1
- ap-southeast-2

blocklist:
- "000000000000"

accounts:
"000000000000": {}
"111111111111":
filters:
__global__:
- property: tag:aws-nuke
value: "Disable"
- property: tag:aws-nuke
value: "disable"
16 changes: 16 additions & 0 deletions pkg/config/testdata/broken.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
regions:
- global
- us-east-1
- ap-southeast-2

blocklist:
- "000000000000"

accounts:
"000000000000":
filters:
__global__:
- property: tag:aws-nuke
value: "Disable"
- property: tag:aws-nuke
value: "disable"

0 comments on commit 585b852

Please sign in to comment.