From d9c4d168acc8bb71f6f975295485d485486dc005 Mon Sep 17 00:00:00 2001 From: Nitishkumar Singh Date: Fri, 2 Sep 2022 21:11:49 +0530 Subject: [PATCH] fix cron connector for no auth Signed-off-by: Nitishkumar Singh --- config.go | 12 ++++++++++++ main.go | 22 ++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index c78e5b2..4eafa0d 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "strconv" "time" "github.com/openfaas/connector-sdk/types" @@ -40,6 +41,16 @@ func getControllerConfig() (*types.ControllerConfig, error) { rebuildInterval = d } + var basicAuth bool + if val, exists := os.LookupEnv("basic_auth"); exists { + a, err := strconv.ParseBool(val) + if err != nil { + return nil, err + } + + basicAuth = a + } + return &types.ControllerConfig{ RebuildInterval: rebuildInterval, GatewayURL: gURL, @@ -48,5 +59,6 @@ func getControllerConfig() (*types.ControllerConfig, error) { PrintResponse: true, PrintResponseBody: printResponseBody, PrintRequestBody: false, + BasicAuth: basicAuth, }, nil } diff --git a/main.go b/main.go index c0d1c92..f3a19d9 100644 --- a/main.go +++ b/main.go @@ -98,13 +98,27 @@ func (auth *BasicAuth) Set(req *http.Request) error { return nil } +// NoAuth auth to when basic auth is disabled +type NoAuth struct { +} + +// Set authorization header or request +func (auth *NoAuth) Set(req *http.Request) error { + return nil +} + func startFunctionProbe(interval time.Duration, probeTimeout time.Duration, topic string, c *types.ControllerConfig, cronScheduler *crontypes.Scheduler, invoker *types.Invoker) error { runningFuncs := make(crontypes.ScheduledFunctions, 0) - creds := types.GetCredentials() - auth := &BasicAuth{ - Username: creds.User, - Password: creds.Password, + var auth sdk.ClientAuth + if c.BasicAuth { + creds := types.GetCredentials() + auth = &BasicAuth{ + Username: creds.User, + Password: creds.Password, + } + } else { + auth = &NoAuth{} } sdkClient, err := sdk.NewClient(auth, c.GatewayURL, nil, &probeTimeout)