-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from accurics/terrascan-v1.0-add-webhook-noti…
…fications Terrascan v1.0 add webhook notifications
- Loading branch information
Showing
28 changed files
with
809 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# terrascan configuration file | ||
|
||
# notifications configuration | ||
[notifications] | ||
[notifications.webhook] | ||
url = "https://httpbin.org/post" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package notifications | ||
|
||
// Notifier defines the interface which every type of notification provider | ||
// needs to implement to claim support in terrascan | ||
type Notifier interface { | ||
Init(interface{}) error | ||
SendNotification(interface{}) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
Copyright (C) 2020 Accurics, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package notifications | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/accurics/terrascan/pkg/utils" | ||
"github.com/pelletier/go-toml" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
notificationsConfigKey = "notifications" | ||
) | ||
|
||
var ( | ||
errNotPresent = fmt.Errorf("config file not present") | ||
errNotifierNotSupported = fmt.Errorf("notifier not supported") | ||
errTomlLoadConfig = fmt.Errorf("failed to load toml config") | ||
errTomlKeyNotPresent = fmt.Errorf("key not present in toml config") | ||
) | ||
|
||
// NewNotifier returns a new notifier | ||
func NewNotifier(notifierType string) (notifier Notifier, err error) { | ||
|
||
// get notifier from supportedNotifierss | ||
notifierObject, supported := supportedNotifiers[supportedNotifierType(notifierType)] | ||
if !supported { | ||
zap.S().Errorf("notifier type '%s' not supported", notifierType) | ||
return notifier, errNotifierNotSupported | ||
} | ||
|
||
// successful | ||
return reflect.New(notifierObject).Interface().(Notifier), nil | ||
} | ||
|
||
// NewNotifiers returns a list of notifiers configured in the config file | ||
func NewNotifiers(configFile string) ([]Notifier, error) { | ||
|
||
var notifiers []Notifier | ||
|
||
// empty config file path | ||
if configFile == "" { | ||
zap.S().Infof("no config file specified") | ||
return notifiers, nil | ||
} | ||
|
||
// check if file exists | ||
_, err := os.Stat(configFile) | ||
if err != nil { | ||
zap.S().Errorf("config file '%s' not present", configFile) | ||
return notifiers, errNotPresent | ||
} | ||
|
||
// parse toml config file | ||
config, err := toml.LoadFile(configFile) | ||
if err != nil { | ||
zap.S().Errorf("failed to load toml config file '%s'. error: '%v'", err) | ||
return notifiers, errTomlLoadConfig | ||
} | ||
|
||
// get config for 'notifications' | ||
keyConfig := config.Get(notificationsConfigKey) | ||
if keyConfig == nil { | ||
zap.S().Infof("key '%s' not present in toml config", notificationsConfigKey) | ||
return notifiers, errTomlKeyNotPresent | ||
} | ||
|
||
// get all the notifier types configured in TOML config | ||
keyTomlConfig := keyConfig.(*toml.Tree) | ||
notifierTypes := keyTomlConfig.Keys() | ||
|
||
// create notifiers | ||
var allErrs error | ||
for _, nType := range notifierTypes { | ||
|
||
if !IsNotifierSupported(nType) { | ||
zap.S().Errorf("notifier type '%s' not supported", nType) | ||
allErrs = utils.WrapError(errNotifierNotSupported, allErrs) | ||
continue | ||
} | ||
|
||
// check if toml config present for notifier type | ||
nTypeConfig := keyTomlConfig.Get(nType) | ||
if nTypeConfig.(*toml.Tree).String() == "" { | ||
zap.S().Errorf("notifier '%v' config not present", nType) | ||
allErrs = utils.WrapError(errTomlKeyNotPresent, allErrs) | ||
continue | ||
} | ||
|
||
// create a new notifier | ||
n, err := NewNotifier(nType) | ||
if err != nil { | ||
allErrs = utils.WrapError(err, allErrs) | ||
continue | ||
} | ||
|
||
// populate data | ||
err = n.Init(nTypeConfig) | ||
if err != nil { | ||
allErrs = utils.WrapError(err, allErrs) | ||
continue | ||
} | ||
|
||
// add to the list of notifiers | ||
notifiers = append(notifiers, n) | ||
} | ||
|
||
// return list of notifiers | ||
return notifiers, allErrs | ||
} | ||
|
||
// IsNotifierSupported returns true/false depending on whether the notifier | ||
// is supported in terrascan or not | ||
func IsNotifierSupported(notifierType string) bool { | ||
if _, supported := supportedNotifiers[supportedNotifierType(notifierType)]; !supported { | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package notifications | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/accurics/terrascan/pkg/notifications/webhook" | ||
) | ||
|
||
func TestNewNotifier(t *testing.T) { | ||
|
||
table := []struct { | ||
name string | ||
nType string | ||
wantType Notifier | ||
wantErr error | ||
}{ | ||
{ | ||
name: "valid notifier", | ||
nType: "webhook", | ||
wantType: &webhook.Webhook{}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "invalid notifier", | ||
nType: "notthere", | ||
wantErr: errNotifierNotSupported, | ||
}, | ||
} | ||
|
||
for _, tt := range table { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotType, gotErr := NewNotifier(tt.nType) | ||
if !reflect.DeepEqual(gotType, tt.wantType) { | ||
t.Errorf("got: '%v', want: '%v'", gotType, tt.wantType) | ||
} | ||
if !reflect.DeepEqual(gotErr, tt.wantErr) { | ||
t.Errorf("incorrect error; got: '%v', want: '%v'", gotErr, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewNotifiers(t *testing.T) { | ||
|
||
table := []struct { | ||
name string | ||
configFile string | ||
wantErr error | ||
}{ | ||
{ | ||
name: "config not present", | ||
configFile: "notthere", | ||
wantErr: errNotPresent, | ||
}, | ||
{ | ||
name: "invalid toml", | ||
configFile: "testdata/invalid.toml", | ||
wantErr: errTomlLoadConfig, | ||
}, | ||
{ | ||
name: "key not present", | ||
configFile: "testdata/nokey.toml", | ||
wantErr: errTomlKeyNotPresent, | ||
}, | ||
{ | ||
name: "invalid notifier", | ||
configFile: "testdata/invalid-notifier-type.toml", | ||
wantErr: errNotifierNotSupported, | ||
}, | ||
{ | ||
name: "empty notifier config", | ||
configFile: "testdata/empty-notifier-config.toml", | ||
wantErr: errTomlKeyNotPresent, | ||
}, | ||
{ | ||
name: "invalid notifier config", | ||
configFile: "testdata/invalid-notifier-config.toml", | ||
wantErr: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range table { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, gotErr := NewNotifiers(tt.configFile) | ||
if !reflect.DeepEqual(gotErr, tt.wantErr) { | ||
t.Errorf("incorrect error; got: '%v', want: '%v'", gotErr, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright (C) 2020 Accurics, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package notifications | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// map of supported notifier types | ||
var supportedNotifiers = make(map[supportedNotifierType]reflect.Type) | ||
|
||
// RegisterNotifier registers an notifier provider for terrascan | ||
func RegisterNotifier(notifierType supportedNotifierType, notifierProvider reflect.Type) { | ||
supportedNotifiers[notifierType] = notifierProvider | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[notifications] | ||
[notifications.webhook] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[notifications] | ||
[notifications.webhook] | ||
key1 = "val1" | ||
key2 = "val2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# terrascan configuration file | ||
|
||
# notifications configuration | ||
[notifications] | ||
[notifications.invalid] | ||
url = "https://httpbin.org/post" |
Oops, something went wrong.