Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config): protect against nil pointer for misconfigured account #86

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"