forked from rancher/norman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhookcli.go
67 lines (59 loc) · 1.46 KB
/
webhookcli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cli
import (
"os"
"time"
"github.com/iwilltry42/norman/v3/pkg/auth"
"github.com/urfave/cli"
)
type WebhookConfig struct {
WebhookAuthentication bool
WebhookKubeconfig string
WebhookURL string
CacheTTLSeconds int
}
func (w *WebhookConfig) MustWebhookMiddleware() auth.Middleware {
m, err := w.WebhookMiddleware()
if err != nil {
panic("failed to create webhook middleware: " + err.Error())
}
return m
}
func (w *WebhookConfig) WebhookMiddleware() (auth.Middleware, error) {
if !w.WebhookAuthentication {
return nil, nil
}
config := w.WebhookKubeconfig
if config == "" && w.WebhookURL != "" {
tempFile, err := auth.WebhookConfigForURL(w.WebhookURL)
if err != nil {
return nil, err
}
defer os.Remove(tempFile)
config = tempFile
}
return auth.NewWebhookMiddleware(time.Duration(w.CacheTTLSeconds)*time.Second, config)
}
func Flags(config *WebhookConfig) []cli.Flag {
return []cli.Flag{
cli.BoolTFlag{
Name: "webhook-auth",
EnvVar: "WEBHOOK_AUTH",
Destination: &config.WebhookAuthentication,
},
cli.StringFlag{
Name: "webhook-kubeconfig",
EnvVar: "WEBHOOK_KUBECONFIG",
Destination: &config.WebhookKubeconfig,
},
cli.StringFlag{
Name: "webhook-url",
EnvVar: "WEBHOOK_URL",
Destination: &config.WebhookURL,
},
cli.IntFlag{
Name: "webhook-cache-ttl",
EnvVar: "WEBHOOK_CACHE_TTL",
Destination: &config.CacheTTLSeconds,
},
}
}