-
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.
add support for sending webhook notifications
- Loading branch information
Yusuf Kanchwala
committed
Aug 10, 2020
1 parent
600a6e6
commit 0ddf0a0
Showing
9 changed files
with
297 additions
and
2 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
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() error | ||
SendNotification() 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,57 @@ | ||
/* | ||
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" | ||
"reflect" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
errNotifierNotSupported = fmt.Errorf("notifier not supported") | ||
) | ||
|
||
// 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 | ||
} | ||
|
||
// notifier | ||
notifier = reflect.New(notifierObject).Interface().(Notifier) | ||
|
||
// initialize notifier | ||
notifier.Init() | ||
|
||
// successful | ||
return notifier, nil | ||
} | ||
|
||
// 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,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,20 @@ | ||
/* | ||
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 | ||
|
||
// SupportedNotifierType data type for supported IaC provider | ||
type supportedNotifierType string |
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,35 @@ | ||
/* | ||
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" | ||
|
||
webhookNotifier "github.com/accurics/terrascan/pkg/notifications/webhook" | ||
) | ||
|
||
// terraform specific constants | ||
const ( | ||
terraform supportedNotifierType = "webhook" | ||
) | ||
|
||
// register terraform as an IaC provider with terrascan | ||
func init() { | ||
|
||
// register iac provider | ||
RegisterNotifier(terraform, reflect.TypeOf(webhookNotifier.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,23 @@ | ||
/* | ||
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 webhook | ||
|
||
// Webhook implements the Notifier interface | ||
type Webhook struct { | ||
url string | ||
authToken string | ||
} |
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,51 @@ | ||
/* | ||
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 webhook | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Init initalizes the webhook notifier, reads config file and configures the | ||
// necessary parameters for webhook notifications to work | ||
func (w *Webhook) Init() error { | ||
|
||
// check if conf file exists | ||
|
||
// parse conf file | ||
|
||
// read webhook url and auth token | ||
|
||
// initalize Webhook struct with url and token | ||
|
||
// succesful | ||
zap.S().Debug("initialized webhook notifier") | ||
return nil | ||
} | ||
|
||
// SendNotification sends webhook notification i.e sends a http POST request | ||
// to the configured URL | ||
func (w *Webhook) SendNotification() error { | ||
|
||
// make http POST request | ||
|
||
// validate http response | ||
|
||
// successful | ||
zap.S().Debug("sent webhook notification") | ||
return nil | ||
} |
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,58 @@ | ||
package httputils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/hashicorp/go-retryablehttp" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
errNewRequest = fmt.Errorf("failed to create http request") | ||
errDoRequest = fmt.Errorf("failed to make http request") | ||
) | ||
|
||
// default global http client | ||
var client *http.Client = &http.Client{} | ||
|
||
// init creates a http client which retries on errors like connection timeouts, | ||
// server too slow respond etc. | ||
func init() { | ||
retryClient := retryablehttp.NewClient() | ||
retryClient.RetryMax = 10 | ||
client = retryClient.StandardClient() | ||
} | ||
|
||
// SendRequest sends a http request on the given url | ||
func SendRequest(method, url, token string, data []byte) (*http.Response, error) { | ||
|
||
var resp *http.Response | ||
|
||
// new http request | ||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) | ||
if err != nil { | ||
zap.S().Errorf("failed to create http request; method: '%v', url: '%v'") | ||
return resp, errNewRequest | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
if token != nil { | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer: '%s'", token)) | ||
} | ||
|
||
// make request | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
zap.S().Errorf("failed to make http request; method: '%v', url: '%v'") | ||
return resp, errDoRequest | ||
} | ||
|
||
return resp, err | ||
} | ||
|
||
// SendPOSTRequest sends a http POST request | ||
func SendPOSTRequest(url, token string) (*http.Response, error) { | ||
return SendRequest("POST", url, token) | ||
} |