mirror of
https://github.com/rancher/steve.git
synced 2025-09-04 08:55:55 +00:00
Refactor
This commit is contained in:
67
pkg/auth/cli/webhookcli.go
Normal file
67
pkg/auth/cli/webhookcli.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/rancher/steve/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.BoolFlag{
|
||||
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,
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user