1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-10 20:00:23 +00:00
Files
steve/pkg/auth/cli/webhookcli.go

75 lines
1.6 KiB
Go
Raw Normal View History

2020-01-30 22:37:59 -07:00
package cli
import (
"os"
"time"
"github.com/rancher/steve/pkg/auth"
2025-04-08 12:46:32 -07:00
"github.com/urfave/cli/v2"
"k8s.io/client-go/tools/clientcmd"
2020-01-30 22:37:59 -07:00
)
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
}
2022-05-25 18:49:28 -05:00
kubeConfig, err := clientcmd.BuildConfigFromFlags("", config)
if err != nil {
return nil, err
}
return auth.NewWebhookMiddleware(time.Duration(w.CacheTTLSeconds)*time.Second, kubeConfig)
2020-01-30 22:37:59 -07:00
}
func Flags(config *WebhookConfig) []cli.Flag {
return []cli.Flag{
2025-04-08 12:46:32 -07:00
&cli.BoolFlag{
2020-01-30 22:37:59 -07:00
Name: "webhook-auth",
2025-04-08 12:46:32 -07:00
EnvVars: []string{"WEBHOOK_AUTH"},
2020-01-30 22:37:59 -07:00
Destination: &config.WebhookAuthentication,
},
2025-04-08 12:46:32 -07:00
&cli.StringFlag{
2020-01-30 22:37:59 -07:00
Name: "webhook-kubeconfig",
2025-04-08 12:46:32 -07:00
EnvVars: []string{"WEBHOOK_KUBECONFIG"},
2020-01-30 22:37:59 -07:00
Destination: &config.WebhookKubeconfig,
},
2025-04-08 12:46:32 -07:00
&cli.StringFlag{
2020-01-30 22:37:59 -07:00
Name: "webhook-url",
2025-04-08 12:46:32 -07:00
EnvVars: []string{"WEBHOOK_URL"},
2020-01-30 22:37:59 -07:00
Destination: &config.WebhookURL,
},
2025-04-08 12:46:32 -07:00
&cli.IntFlag{
2020-01-30 22:37:59 -07:00
Name: "webhook-cache-ttl",
2025-04-08 12:46:32 -07:00
EnvVars: []string{"WEBHOOK_CACHE_TTL"},
2020-01-30 22:37:59 -07:00
Destination: &config.CacheTTLSeconds,
},
}
}