2017-01-04 02:44:32 +00:00
|
|
|
// Copyright 2017 clair authors
|
2015-12-15 16:36:06 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
// Package webhook implements a notification sender for HTTP JSON webhooks.
|
|
|
|
package webhook
|
2015-12-15 16:36:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-02-22 21:38:16 +00:00
|
|
|
"time"
|
2015-12-15 16:36:06 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
"github.com/coreos/clair/ext/notification"
|
2015-12-15 16:36:06 +00:00
|
|
|
)
|
|
|
|
|
2016-02-22 21:38:16 +00:00
|
|
|
const timeout = 5 * time.Second
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
type sender struct {
|
2015-12-15 16:36:06 +00:00
|
|
|
endpoint string
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
// Config represents the configuration of a Webhook Sender.
|
|
|
|
type Config struct {
|
2015-12-15 16:36:06 +00:00
|
|
|
Endpoint string
|
|
|
|
ServerName string
|
|
|
|
CertFile string
|
|
|
|
KeyFile string
|
|
|
|
CAFile string
|
2016-04-12 17:51:05 +00:00
|
|
|
Proxy string
|
2015-12-15 16:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-01-04 02:44:32 +00:00
|
|
|
notification.RegisterSender("webhook", &sender{})
|
2015-12-15 16:36:06 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 01:14:44 +00:00
|
|
|
func (s *sender) Configure(config *notification.Config) (bool, error) {
|
2015-12-15 16:36:06 +00:00
|
|
|
// Get configuration
|
2017-01-04 02:44:32 +00:00
|
|
|
var httpConfig Config
|
2015-12-15 16:36:06 +00:00
|
|
|
if config == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if _, ok := config.Params["http"]; !ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
yamlConfig, err := yaml.Marshal(config.Params["http"])
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("invalid configuration")
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(yamlConfig, &httpConfig)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("invalid configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate endpoint URL.
|
|
|
|
if httpConfig.Endpoint == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
2016-04-12 17:52:15 +00:00
|
|
|
if _, err := url.ParseRequestURI(httpConfig.Endpoint); err != nil {
|
2016-04-12 17:51:05 +00:00
|
|
|
return false, fmt.Errorf("could not parse endpoint URL: %s\n", err)
|
2015-12-15 16:36:06 +00:00
|
|
|
}
|
2017-01-04 02:44:32 +00:00
|
|
|
s.endpoint = httpConfig.Endpoint
|
2015-12-15 16:36:06 +00:00
|
|
|
|
2016-04-12 17:51:05 +00:00
|
|
|
// Setup HTTP client.
|
|
|
|
transport := &http.Transport{}
|
2017-01-04 02:44:32 +00:00
|
|
|
s.client = &http.Client{
|
2016-04-12 17:51:05 +00:00
|
|
|
Transport: transport,
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
|
|
|
|
2015-12-15 16:36:06 +00:00
|
|
|
// Initialize TLS.
|
2016-04-12 17:51:05 +00:00
|
|
|
transport.TLSClientConfig, err = loadTLSClientConfig(&httpConfig)
|
2015-12-15 16:36:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("could not initialize client cert auth: %s\n", err)
|
|
|
|
}
|
|
|
|
|
2016-04-12 17:51:05 +00:00
|
|
|
// Set proxy.
|
|
|
|
if httpConfig.Proxy != "" {
|
|
|
|
proxyURL, err := url.ParseRequestURI(httpConfig.Proxy)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("could not parse proxy URL: %s\n", err)
|
|
|
|
}
|
|
|
|
transport.Proxy = http.ProxyURL(proxyURL)
|
2015-12-15 16:36:06 +00:00
|
|
|
}
|
2016-04-12 17:51:05 +00:00
|
|
|
|
2015-12-15 16:36:06 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2016-02-24 20:05:39 +00:00
|
|
|
type notificationEnvelope struct {
|
|
|
|
Notification struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
func (s *sender) Send(notificationName string) error {
|
2015-12-15 16:36:06 +00:00
|
|
|
// Marshal notification.
|
2017-07-26 23:22:29 +00:00
|
|
|
jsonNotification, err := json.Marshal(notificationEnvelope{struct{ Name string }{notificationName}})
|
2015-12-15 16:36:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not marshal: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-12-17 18:37:03 +00:00
|
|
|
// Send notification via HTTP POST.
|
2017-01-04 02:44:32 +00:00
|
|
|
resp, err := s.client.Post(s.endpoint, "application/json", bytes.NewBuffer(jsonNotification))
|
2015-12-15 16:36:06 +00:00
|
|
|
if err != nil || resp == nil || (resp.StatusCode != 200 && resp.StatusCode != 201) {
|
|
|
|
if resp != nil {
|
2016-02-09 21:07:57 +00:00
|
|
|
return fmt.Errorf("got status %d, expected 200/201", resp.StatusCode)
|
2015-12-15 16:36:06 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
// loadTLSClientConfig initializes a *tls.Config using the given Config.
|
2015-12-15 16:36:06 +00:00
|
|
|
//
|
|
|
|
// If no certificates are given, (nil, nil) is returned.
|
|
|
|
// The CA certificate is optional and falls back to the system default.
|
2017-01-04 02:44:32 +00:00
|
|
|
func loadTLSClientConfig(cfg *Config) (*tls.Config, error) {
|
2015-12-15 16:36:06 +00:00
|
|
|
if cfg.CertFile == "" || cfg.KeyFile == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var caCertPool *x509.CertPool
|
|
|
|
if cfg.CAFile != "" {
|
|
|
|
caCert, err := ioutil.ReadFile(cfg.CAFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
caCertPool = x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: cfg.ServerName,
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
RootCAs: caCertPool,
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsConfig, nil
|
|
|
|
}
|