Skip to content

Commit a9d21cb

Browse files
author
Seungwon Lee
committed
Add TLS option for webhook
1 parent 6cd8115 commit a9d21cb

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

cmd/webhook.go

+28
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"strconv"
21+
2022
"github.com/bitnami-labs/kubewatch/config"
2123
"github.com/sirupsen/logrus"
2224
"github.com/spf13/cobra"
@@ -42,6 +44,30 @@ var webhookConfigCmd = &cobra.Command{
4244
logrus.Fatal(err)
4345
}
4446

47+
cert, err := cmd.Flags().GetString("cert")
48+
if err == nil {
49+
if len(cert) > 0 {
50+
conf.Handler.Webhook.Cert = cert
51+
}
52+
} else {
53+
logrus.Fatal(err)
54+
}
55+
56+
tlsSkip, err := cmd.Flags().GetString("tlsskip")
57+
if err == nil {
58+
if len(tlsSkip) > 0 {
59+
skip, err := strconv.ParseBool(tlsSkip)
60+
if err != nil {
61+
logrus.Fatal(err)
62+
}
63+
conf.Handler.Webhook.TlsSkip = skip
64+
} else {
65+
conf.Handler.Webhook.TlsSkip = false
66+
}
67+
} else {
68+
logrus.Fatal(err)
69+
}
70+
4571
if err = conf.Write(); err != nil {
4672
logrus.Fatal(err)
4773
}
@@ -50,4 +76,6 @@ var webhookConfigCmd = &cobra.Command{
5076

5177
func init() {
5278
webhookConfigCmd.Flags().StringP("url", "u", "", "Specify Webhook url")
79+
webhookConfigCmd.Flags().StringP("cert", "", "", "Specify Webhook cert path")
80+
webhookConfigCmd.Flags().StringP("tlsskip", "", "", "Specify whether Webhook skips tls verify; TRUE or FALSE")
5381
}

config/config.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ type Flock struct {
119119
// Webhook contains webhook configuration
120120
type Webhook struct {
121121
// Webhook URL.
122-
Url string `json:"url"`
122+
Url string `json:"url"`
123+
Cert string `json:"cert"`
124+
TlsSkip bool `json:"tlsskip"`
123125
}
124126

125127
// CloudEvent contains CloudEvent configuration

config/sample.go

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ handler:
2626
webhook:
2727
# Webhook URL.
2828
url: ""
29+
# Whether skip tls or not.
30+
tlsskip: ""
31+
# Path of webhook cert. Default value is false.
32+
cert: ""
2933
cloudevent:
3034
# CloudEvent webhook URL.
3135
url: ""

examples/conf/kubewatch.conf.webhook.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ handler:
1313
flock:
1414
url: ""
1515
webhook:
16-
url: "http://localhost:8080"
16+
url: "https://localhost:443"
17+
tlsskip: false
18+
cert: "/root/tls/ca.crt"
1719
resource:
1820
deployment: false
1921
replicationcontroller: false

pkg/handlers/webhook/webhook.go

+26
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ limitations under the License.
1717
package webhook
1818

1919
import (
20+
"crypto/tls"
21+
"crypto/x509"
2022
"fmt"
23+
"io/ioutil"
2124
"log"
2225
"os"
2326

@@ -66,13 +69,36 @@ type EventMeta struct {
6669
// Init prepares Webhook configuration
6770
func (m *Webhook) Init(c *config.Config) error {
6871
url := c.Handler.Webhook.Url
72+
cert := c.Handler.Webhook.Cert
73+
tlsSkip := c.Handler.Webhook.TlsSkip
6974

7075
if url == "" {
7176
url = os.Getenv("KW_WEBHOOK_URL")
7277
}
78+
if cert == "" {
79+
cert = os.Getenv("KW_WEBHOOK_CERT")
80+
}
7381

7482
m.Url = url
7583

84+
if tlsSkip {
85+
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
86+
} else {
87+
if cert == "" {
88+
log.Printf("No webhook cert is given")
89+
} else {
90+
caCert, err := ioutil.ReadFile(cert)
91+
if err != nil {
92+
log.Printf("%s\n", err)
93+
return err
94+
}
95+
caCertPool := x509.NewCertPool()
96+
caCertPool.AppendCertsFromPEM(caCert)
97+
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{RootCAs: caCertPool}
98+
}
99+
100+
}
101+
76102
return checkMissingWebhookVars(m)
77103
}
78104

0 commit comments

Comments
 (0)